Merge pull request #1410 from alesliehughes/master
[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 #if NET_2_0
29
30 using System;
31 using System.Collections.Generic;
32 using System.IO;
33 using System.Reflection;
34 using System.Runtime.Serialization;
35 using System.Xml;
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Runtime.Serialization
39 {
40         [TestFixture]
41         public class KnownTypeAttributeTest
42         {
43                 void Serialize (object instance)
44                 {
45                         var ds = new DataContractSerializer (instance.GetType ());
46                         using (var xw = XmlWriter.Create (TextWriter.Null))
47                                 ds.WriteObject (xw, instance);
48                 }
49
50                 [Test]
51                 public void MethodName ()
52                 {
53                         Serialize (new Data () { X = new Bar () });
54                 }
55
56                 [Test]
57                 public void MethodName2 ()
58                 {
59                         Serialize (new Data2 () { X = new Bar () });
60                 }
61
62                 [Test]
63                 [ExpectedException (typeof (InvalidDataContractException))]
64                 public void MethodName3 ()
65                 {
66                         Serialize (new Data3 () { X = new Bar () });
67                 }
68
69                 [Test]
70                 [ExpectedException (typeof (InvalidDataContractException))]
71                 public void MethodName4 ()
72                 {
73                         Serialize (new Data4 () { X = new Bar () });
74                 }
75
76                 [Test]
77                 public void GenericTypeNameArgument ()
78                 {
79                         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 ('\'', '"');
80                         var ds = new DataContractSerializer (typeof (MyList<Foo>));
81                         var l = new MyList<Foo> ();
82                         l.Add (new Foo () { S = "z" });
83                         var sw = new StringWriter ();
84                         var settings = new XmlWriterSettings () { OmitXmlDeclaration = true };
85                         using (var xw = XmlWriter.Create (sw, settings))
86                                 ds.WriteObject (xw, l);
87                         Assert.AreEqual (expected, sw.ToString (), "#1");
88                 }
89
90                 [Test]
91                 [ExpectedException (typeof (InvalidDataContractException))]
92                 public void DataContractOnCollection ()
93                 {
94                         Serialize (new MyListWrong<Foo> ());
95                 }
96
97                 public class Foo
98                 {
99                         public string S { get; set; }
100                 }
101
102                 public class Bar : Foo
103                 {
104                         public string T { get; set; }
105                 }
106
107                 [DataContract]
108                 [KnownType ("GetTypes")]
109                 public class Data
110                 {
111                         [DataMember]
112                         public Foo X { get; set; }
113
114                         public static IEnumerable<Type> GetTypes ()
115                         {
116                                 yield return typeof (Bar);
117                         }
118                 }
119
120                 [DataContract]
121                 [KnownType ("GetTypes")]
122                 public class Data2
123                 {
124                         [DataMember]
125                         public Foo X { get; set; }
126
127                         static IEnumerable<Type> GetTypes () // non-public
128                         {
129                                 yield return typeof (Bar);
130                         }
131                 }
132
133                 [DataContract]
134                 [KnownType ("GetTypes")]
135                 public class Data3
136                 {
137                         [DataMember]
138                         public Foo X { get; set; }
139
140                         public IEnumerable<Type> GetTypes () // non-static
141                         {
142                                 yield return typeof (Bar);
143                         }
144                 }
145
146                 [DataContract]
147                 [KnownType ("GetTypes")]
148                 public class Data4
149                 {
150                         [DataMember]
151                         public Foo X { get; set; }
152
153                         public static IEnumerable<Type> GetTypes (ICustomAttributeProvider provider) // wrong args
154                         {
155                                 yield return typeof (Bar);
156                         }
157                 }
158                 
159                 [CollectionDataContract (Name = "Foo_{0}", Namespace = "urn:foo")]
160                 public class MyList<T> : List<T>
161                 {
162                 }
163                 
164                 [DataContract (Name = "Foo_{0}", Namespace = "urn:foo")]
165                 public class MyListWrong<T> : List<T>
166                 {
167                 }
168         }
169 }
170 #endif