2 // XmlObjectSerializerTest.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
6 // Ankit Jain <JAnkit@novell.com>
8 // Copyright (C) 2005 Novell, Inc. http://www.novell.com
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
33 using System.Collections;
34 using System.Collections.Generic;
35 using System.Collections.ObjectModel;
39 using System.Runtime.Serialization;
42 using System.Xml.Schema;
43 using System.Xml.Serialization;
44 using NUnit.Framework;
46 namespace MonoTests.System.Runtime.Serialization
49 public class DataContractResolverTest
52 public void UseCase1 ()
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 ());
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 ('\'', '"');
63 Assert.AreEqual (xml, sw.ToString (), "#1");
64 } catch (AssertionException) {
65 Assert.AreEqual (xml2, sw.ToString (), "#2");
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");
74 public class MyResolver : DataContractResolver
76 public override bool TryResolveType (Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
78 //Console.WriteLine ("TryResolveType: {0} {1}", type, declaredType);
79 if (knownTypeResolver.TryResolveType (type, declaredType, null, out typeName, out typeNamespace))
81 return SafeResolveType (type, out typeName, out typeNamespace);
84 XmlDictionary dic = new XmlDictionary ();
86 bool SafeResolveType (Type type, out XmlDictionaryString name, out XmlDictionaryString ns)
88 // Console.WriteLine ("SafeResolveType: {0}", type);
89 name = dic.Add (type.Name);
90 ns = dic.Add (type.Namespace ?? "urn:dummy");
94 public override Type ResolveName (string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
96 //Console.WriteLine ("ResolveName: {0} {1} {2}", typeName, typeNamespace, declaredType);
97 return knownTypeResolver.ResolveName (typeName, typeNamespace, declaredType, null) ?? RecoveringResolveName (typeName, typeNamespace);
100 Type RecoveringResolveName (string typeName, string typeNamespace)
102 if (typeNamespace == "urn:dummy")
103 return Type.GetType (typeName);
105 return Type.GetType (typeNamespace + '.' + typeName);
111 public class ResolvedClass
114 public Guid Baz = Guid.Parse ("c74376f0-5517-4cb7-8a07-35026423f565");