[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / System.Runtime.Serialization / Test / System.Runtime.Serialization / KnownTypeAttributeTest.cs
1 //
2 // Author:
3 //      Atsushi Enomoto <atsushi@ximian.com>
4 //
5 // Copyright (C) 2011 Novell, Inc.  http://www.novell.com
6
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
15 // 
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 // 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 //
27
28
29 using System;
30 using System.Collections.Generic;
31 using System.IO;
32 using System.Reflection;
33 using System.Runtime.Serialization;
34 using System.Xml;
35 using NUnit.Framework;
36
37 namespace MonoTests.System.Runtime.Serialization
38 {
39         [TestFixture]
40         public class KnownTypeAttributeTest
41         {
42                 void Serialize (object instance)
43                 {
44                         var ds = new DataContractSerializer (instance.GetType ());
45                         using (var xw = XmlWriter.Create (TextWriter.Null))
46                                 ds.WriteObject (xw, instance);
47                 }
48
49                 [Test]
50                 public void MethodName ()
51                 {
52                         Serialize (new Data () { X = new Bar () });
53                 }
54
55                 [Test]
56                 public void MethodName2 ()
57                 {
58                         Serialize (new Data2 () { X = new Bar () });
59                 }
60
61                 [Test]
62                 [ExpectedException (typeof (InvalidDataContractException))]
63                 public void MethodName3 ()
64                 {
65                         Serialize (new Data3 () { X = new Bar () });
66                 }
67
68                 [Test]
69                 [ExpectedException (typeof (InvalidDataContractException))]
70                 public void MethodName4 ()
71                 {
72                         Serialize (new Data4 () { X = new Bar () });
73                 }
74
75                 [Test]
76                 public void GenericTypeNameArgument ()
77                 {
78                         string expected = "<Foo_KnownTypeAttributeTest.Foo xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns:d1p1='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization' xmlns='urn:foo'><KnownTypeAttributeTest.Foo><d1p1:S>z</d1p1:S></KnownTypeAttributeTest.Foo></Foo_KnownTypeAttributeTest.Foo>".Replace ('\'', '"');
79                         var ds = new DataContractSerializer (typeof (MyList<Foo>));
80                         var l = new MyList<Foo> ();
81                         l.Add (new Foo () { S = "z" });
82                         var sw = new StringWriter ();
83                         var settings = new XmlWriterSettings () { OmitXmlDeclaration = true };
84                         using (var xw = XmlWriter.Create (sw, settings))
85                                 ds.WriteObject (xw, l);
86                         Assert.AreEqual (expected, sw.ToString (), "#1");
87                 }
88
89                 [Test]
90                 [ExpectedException (typeof (InvalidDataContractException))]
91                 public void DataContractOnCollection ()
92                 {
93                         Serialize (new MyListWrong<Foo> ());
94                 }
95
96                 public class Foo
97                 {
98                         public string S { get; set; }
99                 }
100
101                 public class Bar : Foo
102                 {
103                         public string T { get; set; }
104                 }
105
106                 [DataContract]
107                 [KnownType ("GetTypes")]
108                 public class Data
109                 {
110                         [DataMember]
111                         public Foo X { get; set; }
112
113                         public static IEnumerable<Type> GetTypes ()
114                         {
115                                 yield return typeof (Bar);
116                         }
117                 }
118
119                 [DataContract]
120                 [KnownType ("GetTypes")]
121                 public class Data2
122                 {
123                         [DataMember]
124                         public Foo X { get; set; }
125
126                         static IEnumerable<Type> GetTypes () // non-public
127                         {
128                                 yield return typeof (Bar);
129                         }
130                 }
131
132                 [DataContract]
133                 [KnownType ("GetTypes")]
134                 public class Data3
135                 {
136                         [DataMember]
137                         public Foo X { get; set; }
138
139                         public IEnumerable<Type> GetTypes () // non-static
140                         {
141                                 yield return typeof (Bar);
142                         }
143                 }
144
145                 [DataContract]
146                 [KnownType ("GetTypes")]
147                 public class Data4
148                 {
149                         [DataMember]
150                         public Foo X { get; set; }
151
152                         public static IEnumerable<Type> GetTypes (ICustomAttributeProvider provider) // wrong args
153                         {
154                                 yield return typeof (Bar);
155                         }
156                 }
157                 
158                 [CollectionDataContract (Name = "Foo_{0}", Namespace = "urn:foo")]
159                 public class MyList<T> : List<T>
160                 {
161                 }
162                 
163                 [DataContract (Name = "Foo_{0}", Namespace = "urn:foo")]
164                 public class MyListWrong<T> : List<T>
165                 {
166                 }
167         }
168 }