Merge pull request #1896 from meum/patch-1
[mono.git] / mcs / class / System.Runtime.Serialization / Test / System.Runtime.Serialization / DataContractSerializerTest_ISerializable.cs
1 //
2 // DataContractSerializerTest_ISerializable.cs
3 //
4 // Author:
5 //   Aaron Bockover <abock@xamarin.com>
6 //
7 // Copyright 2015 Xamarin Inc. All rights reserved.
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 using System;
29 using System.IO;
30 using System.Runtime.Serialization;
31 using System.Xml;
32
33 using NUnit.Framework;
34
35 namespace MonoTests.System.Runtime.Serialization
36 {
37         [TestFixture]
38         public class DataContractSerializerTest_ISerializable
39         {
40                 [Serializable]
41                 sealed class TestClassISerializable : ISerializable
42                 {
43                         public string Foo { get; }
44
45                         public TestClassISerializable (string foo)
46                         {
47                                 Foo = foo;
48                         }
49
50                         TestClassISerializable (SerializationInfo info, StreamingContext context)
51                         {
52                                 Foo = info.GetString ("foo");
53                         }
54
55                         void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
56                         {
57                                 info.AddValue ("foo", Foo);
58                         }
59                 }
60
61                 // Tests that the ISerializable constructor is properly invoked, which
62                 // regressed when integrating MS Reference Source DCS, et. al.:
63                 //   https://bugzilla.xamarin.com/show_bug.cgi?id=37171
64                 [Test]
65                 public void TestISerializableCtor ()
66                 {
67                         var serializer = new DataContractSerializer (
68                                 typeof(TestClassISerializable),
69                                 new DataContractSerializerSettings {
70                                         DataContractResolver = new Resolver ()
71                                 }
72                         );
73
74                         var stream = new MemoryStream ();
75
76                         var expected = new TestClassISerializable ("hello world");
77                         serializer.WriteObject (stream, expected);
78
79                         stream.Flush ();
80                         stream.Position = 0;
81
82                         var actual = (TestClassISerializable)serializer.ReadObject (stream);
83
84                         Assert.AreEqual (expected.Foo, actual.Foo, "#DCS_ISer_Ctor");
85                 }
86
87                 // Resolver to force DCS to serialize any type, ensuring the ISerializable
88                 // path will be taken for objects implementing that interface
89                 class Resolver : DataContractResolver
90                 {
91                         public override Type ResolveName (string typeName, string typeNamespace,
92                                 Type declaredType, DataContractResolver knownTypeResolver)
93                         {
94                                 return Type.GetType (typeNamespace == null
95                                         ? typeName
96                                         : typeNamespace + "." + typeName
97                                 );
98                         }
99
100                         public override bool TryResolveType (Type type, Type declaredType,
101                                 DataContractResolver knownTypeResolver,
102                                 out XmlDictionaryString typeName,
103                                 out XmlDictionaryString typeNamespace)
104                         {
105                                 var name = type.FullName;
106                                 var namesp = type.Namespace;
107                                 name = name.Substring (type.Namespace.Length + 1);
108                                 typeName = new XmlDictionaryString (XmlDictionary.Empty, name, 0);
109                                 typeNamespace = new XmlDictionaryString (XmlDictionary.Empty, namesp, 0);
110                                 return true;
111                         }
112                 }
113         }
114 }