Merge pull request #2799 from BrzVlad/fix-conc-card-clean
[mono.git] / mcs / class / System.Runtime.Serialization / Test / System.Runtime.Serialization / DataContractSerializerTest.cs
1 //
2 // DataContractSerializerTest.cs
3 //
4 // Author:
5 //      Brendan Zagaeski 
6 //      Miguel de Icaza
7 //
8 // Copyright (C) 2013-2014 Xamarin Inc http://www.xamarin.com
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 //
30 // This test code contains tests for attributes in System.Runtime.Serialization
31 //
32
33 using System;
34 using System.Collections.Generic;
35 using System.Linq;
36 using System.Text;
37 using NUnit.Framework;
38 using System.Reflection;
39 using System.Globalization;
40 using System.Runtime.Serialization;
41 using System.IO;
42 using System.Xml;
43
44 namespace MonoTests.System.Runtime.Serialization
45 {
46         [KnownType (typeof(MyObject[]))]
47         [DataContract]
48         public class MyObject2
49         {
50                 [DataMember]
51                 public object NameList;
52         }
53
54         [KnownType(typeof(string[]))]
55         [DataContract]
56         public class MyObject
57         {
58                 [DataMember]
59                 public object NameList;
60         }
61         
62         [TestFixture]
63         public class DataContractSerializerTestBugs {
64
65
66                 // Bug #15574
67                 [Test]
68                 public void SerializeMyObject ()
69                 {
70                         var attrs = (KnownTypeAttribute[])typeof(MyObject).
71                                 GetCustomAttributes (typeof(KnownTypeAttribute), true);
72
73                         for (int i = 0; i < attrs.Length; i ++) {
74                                 Console.WriteLine (attrs [i].Type);
75                         }
76
77                         var ser = new DataContractSerializer (typeof(MyObject));
78                 }
79
80                 [Test]
81                 public void Bug ()
82                 {
83                         var s = "<MyObject xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
84                                 "  <NameList i:type=\"a:ArrayOfstring\" xmlns:a=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">\n" +
85                                 "    <a:string>Name1</a:string>\n" +
86                                 "    <a:string>Name2</a:string>\n" + 
87                                 "  </NameList>\n" +
88                                 "</MyObject>";
89
90                         //<MyObject xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/ServiceTest">
91
92                         var ser = new DataContractSerializer(typeof(MyObject));
93                         ser.ReadObject (XmlReader.Create(new StringReader(s)));
94                 }
95
96                 [Flags ()]
97                 [Serializable]
98                 public enum FlagsEnum
99                 {
100                         None = 0,
101                         Flag1 = 0x10,
102                         Flag2 = 0x20,
103                         All = 0xffff,
104                 };
105
106                 [Serializable]
107                 public class ClassWithFlagsEnum
108                 {
109                         public FlagsEnum Flags = FlagsEnum.All;
110                 }
111
112                 // Bug #21072
113                 [Test]
114                 public void FlagsEnumTest ()
115                 {
116                         var ser = new DataContractSerializer (typeof (ClassWithFlagsEnum));
117
118                         using (var m = new MemoryStream ()) {
119                                 ser.WriteObject (m, new ClassWithFlagsEnum ());
120                                 var data = m.ToArray ();
121                                 var s = Encoding.UTF8.GetString (data, 0, (int) data.Length);
122                                 Assert.IsTrue (s.Contains ("<Flags>All</Flags>"));
123                         }
124                 }
125
126                 // Bug #37116
127                 [Test]
128                 public void KeyPairOfAny ()
129                 {
130                         var dict = new Dictionary<string, object> ();
131                         dict.Add ("test", new List<string> () { "test entry" });
132
133                         var dcs = new DataContractSerializer (typeof(Dictionary<string, object>));
134                         dcs.WriteObject (new MemoryStream (), dict);
135                         // Should not throw exception.
136                 }
137         }
138 }