3 // Atsushi Enomoto <atsushi@ximian.com>
5 // Copyright (C) 2011 Novell, Inc. http://www.novell.com
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:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
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.
30 using System.Collections.Generic;
32 using System.Reflection;
33 using System.Runtime.Serialization;
35 using NUnit.Framework;
37 namespace MonoTests.System.Runtime.Serialization
40 public class KnownTypeAttributeTest
42 void Serialize (object instance)
44 var ds = new DataContractSerializer (instance.GetType ());
45 using (var xw = XmlWriter.Create (TextWriter.Null))
46 ds.WriteObject (xw, instance);
50 public void MethodName ()
52 Serialize (new Data () { X = new Bar () });
56 public void MethodName2 ()
58 Serialize (new Data2 () { X = new Bar () });
62 [ExpectedException (typeof (InvalidDataContractException))]
63 public void MethodName3 ()
65 Serialize (new Data3 () { X = new Bar () });
69 [ExpectedException (typeof (InvalidDataContractException))]
70 public void MethodName4 ()
72 Serialize (new Data4 () { X = new Bar () });
76 public void GenericTypeNameArgument ()
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");
90 [ExpectedException (typeof (InvalidDataContractException))]
91 public void DataContractOnCollection ()
93 Serialize (new MyListWrong<Foo> ());
98 public string S { get; set; }
101 public class Bar : Foo
103 public string T { get; set; }
107 [KnownType ("GetTypes")]
111 public Foo X { get; set; }
113 public static IEnumerable<Type> GetTypes ()
115 yield return typeof (Bar);
120 [KnownType ("GetTypes")]
124 public Foo X { get; set; }
126 static IEnumerable<Type> GetTypes () // non-public
128 yield return typeof (Bar);
133 [KnownType ("GetTypes")]
137 public Foo X { get; set; }
139 public IEnumerable<Type> GetTypes () // non-static
141 yield return typeof (Bar);
146 [KnownType ("GetTypes")]
150 public Foo X { get; set; }
152 public static IEnumerable<Type> GetTypes (ICustomAttributeProvider provider) // wrong args
154 yield return typeof (Bar);
158 [CollectionDataContract (Name = "Foo_{0}", Namespace = "urn:foo")]
159 public class MyList<T> : List<T>
163 [DataContract (Name = "Foo_{0}", Namespace = "urn:foo")]
164 public class MyListWrong<T> : List<T>