Merge pull request #1909 from esdrubal/reflection
[mono.git] / mcs / class / System.Runtime.Serialization / Test / System.Runtime.Serialization / DataContractResolverTest.cs
1 //
2 // XmlObjectSerializerTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //      Ankit Jain <JAnkit@novell.com>
7 //
8 // Copyright (C) 2005 Novell, Inc.  http://www.novell.com
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31
32 using System;
33 using System.Collections;
34 using System.Collections.Generic;
35 using System.Collections.ObjectModel;
36 using System.Data;
37 using System.IO;
38 using System.Net;
39 using System.Runtime.Serialization;
40 using System.Text;
41 using System.Xml;
42 using System.Xml.Schema;
43 using System.Xml.Serialization;
44 using NUnit.Framework;
45
46 namespace MonoTests.System.Runtime.Serialization
47 {
48         [TestFixture]
49         public class DataContractResolverTest
50         {
51                 [Test]
52                 public void UseCase1 ()
53                 {
54                         var ds = new DataContractSerializer (typeof (Colors), null, 10000, false, false, null, new MyResolver ());
55                         var sw = new StringWriter ();
56                         using (var xw = XmlWriter.Create (sw))
57                                 ds.WriteObject (xw, new ResolvedClass ());
58
59                         // xml and xml2 are equivalent in infoset, except for prefixes and position of namespace nodes. So the difference should not matter.
60                         string xml = @"<?xml version='1.0' encoding='utf-16'?><Colors xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns:d1p1='urn:dummy' i:type='d1p1:ResolvedClass' xmlns='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization'><Baz xmlns='http://schemas.datacontract.org/2004/07/'>c74376f0-5517-4cb7-8a07-35026423f565</Baz></Colors>".Replace ('\'', '"');
61                         string xml2 = @"<?xml version='1.0' encoding='utf-16'?><Colors xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns:d1p1='urn:dummy' xmlns:d1p2='http://schemas.datacontract.org/2004/07/' i:type='d1p2:ResolvedClass' xmlns='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization'><d1p2:Baz>c74376f0-5517-4cb7-8a07-35026423f565</d1p2:Baz></Colors>".Replace ('\'', '"');
62                         try {
63                                 Assert.AreEqual (xml, sw.ToString (), "#1");
64                         } catch (AssertionException) {
65                                 Assert.AreEqual (xml2, sw.ToString (), "#2");
66                         }
67                         using (var xr = XmlReader.Create (new StringReader (xml)))
68                                 Assert.AreEqual (typeof (ResolvedClass), ds.ReadObject (xr).GetType (), "#3");
69                         using (var xr = XmlReader.Create (new StringReader (xml)))
70                                 Assert.AreEqual (typeof (ResolvedClass), ds.ReadObject (xr).GetType (), "#4");
71                 }
72         }
73
74         public class MyResolver : DataContractResolver
75         {
76                 public override bool TryResolveType (Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
77                 {
78                         //Console.WriteLine ("TryResolveType: {0} {1}", type, declaredType);
79                         if (knownTypeResolver.TryResolveType (type, declaredType, null, out typeName, out typeNamespace))
80                                 return true;
81                         return SafeResolveType (type, out typeName, out typeNamespace);
82                 }
83
84                 XmlDictionary dic = new XmlDictionary ();
85
86                 bool SafeResolveType (Type type, out XmlDictionaryString name, out XmlDictionaryString ns)
87                 {
88                         // Console.WriteLine ("SafeResolveType: {0}", type);
89                         name = dic.Add (type.Name);
90                         ns = dic.Add (type.Namespace ?? "urn:dummy");
91                         return true;
92                 }
93
94                 public override Type ResolveName (string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
95                 {
96                         //Console.WriteLine ("ResolveName: {0} {1} {2}", typeName, typeNamespace, declaredType);
97                         return knownTypeResolver.ResolveName (typeName, typeNamespace, declaredType, null) ?? RecoveringResolveName (typeName, typeNamespace);
98                 }
99                 
100                 Type RecoveringResolveName (string typeName, string typeNamespace)
101                 {
102                         if (typeNamespace == "urn:dummy")
103                                 return Type.GetType (typeName);
104                         else
105                                 return Type.GetType (typeNamespace + '.' + typeName);
106                 }
107         }
108 }
109
110 [DataContract]
111 public class ResolvedClass
112 {
113         [DataMember]
114         public Guid Baz = Guid.Parse ("c74376f0-5517-4cb7-8a07-35026423f565");
115 }
116