Merge pull request #1196 from tritao/master
[mono.git] / mcs / class / System.XML / Test / System.Xml.Serialization / XmlSerializerTests.cs
index f4f85543dd453005c3a265328ab771fc7a95aac8..e9a161c6e3fd422f3153cc489f51483e564a02be 100644 (file)
@@ -31,6 +31,7 @@ using System.Globalization;
 using System.IO;
 using System.Text;
 using System.Xml;
+using System.Data;
 using System.Xml.Schema;
 using System.Xml.Serialization;
 #if NET_2_0
@@ -1934,6 +1935,15 @@ namespace MonoTests.System.XmlSerialization
                        Assert.AreEqual (Infoset ("<ReadOnlyProperties xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
                }
 
+               [Test]
+               public void TestSerializeReadOnlyListProp ()
+               {
+                       ReadOnlyListProperty ts = new ReadOnlyListProperty ();
+                       Serialize (ts);
+                       Assert.AreEqual (Infoset ("<ReadOnlyListProperty xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><StrList><string>listString1</string><string>listString2</string></StrList></ReadOnlyListProperty>"), WriterText);
+               }
+
+
                [Test]
                public void TestSerializeIList ()
                {
@@ -2186,8 +2196,7 @@ namespace MonoTests.System.XmlSerialization
                        ser.Deserialize (new XmlTextReader (xml, XmlNodeType.Document, null));
                }
 
-#if NET_2_0
-#if !TARGET_JVM
+#if !TARGET_JVM && !MOBILE
                [Test]
                public void GenerateSerializerGenerics ()
                {
@@ -2265,7 +2274,6 @@ namespace MonoTests.System.XmlSerialization
                        Assert.AreEqual (TestEnumWithNulls.bb, w.nenum.Value);
                        Assert.AreEqual (t, w.ndate.Value);
                }
-#endif
 
                [Test]
                public void SerializeBase64Binary()
@@ -2412,6 +2420,35 @@ namespace MonoTests.System.XmlSerialization
                        new XmlSerializer (typeof (XmlSerializableImplicitConvertible), attrOverrides).Serialize (TextWriter.Null, x);
                }
 
+               [Test] // bug #566370
+               public void SerializeEnumWithCSharpKeyword ()
+               {
+                       var ser = new XmlSerializer (typeof (DoxCompoundKind));
+                       for (int i = 0; i < 100; i++) // test serialization code generator
+                               ser.Serialize (Console.Out, DoxCompoundKind.@class);
+               }
+
+               public enum DoxCompoundKind
+               {
+                       [XmlEnum("class")]
+                       @class,
+                       [XmlEnum("struct")]
+                       @struct,
+                       union,
+                       [XmlEnum("interface")]
+                       @interface,
+                       protocol,
+                       category,
+                       exception,
+                       file,
+                       [XmlEnum("namespace")]
+                       @namespace,
+                       group,
+                       page,
+                       example,
+                       dir
+               }
+
                #region GenericsSeralizationTests
 
 #if NET_2_0
@@ -2802,6 +2839,128 @@ namespace MonoTests.System.XmlSerialization
                        Assert.AreEqual (Infoset (xml), WriterText);
                        xs.Deserialize (new StringReader (xml));
                }
+
+               [Test]
+               public void XmlAnyElementForObjects () // bug #553032
+               {
+                       new XmlSerializer (typeof (XmlAnyElementForObjectsType));
+               }
+
+               [Test]
+               [ExpectedException (typeof (InvalidOperationException))]
+               public void XmlAnyElementForObjects2 () // bug #553032-2
+               {
+                       new XmlSerializer (typeof (XmlAnyElementForObjectsType)).Serialize (TextWriter.Null, new XmlAnyElementForObjectsType ());
+               }
+
+
+               public class Bug2893 {
+                       public Bug2893 ()
+                       {                       
+                               Contents = new XmlDataDocument();
+                       }
+                       
+                       [XmlAnyElement("Contents")]
+                       public XmlNode Contents;
+               }
+
+               // Bug Xamarin #2893
+               [Test]
+               public void XmlAnyElementForXmlNode ()
+               {
+                       var obj = new Bug2893 ();
+                       XmlSerializer mySerializer = new XmlSerializer(typeof(Bug2893));
+                       XmlWriterSettings settings = new XmlWriterSettings();
+
+                       var xsn = new XmlSerializerNamespaces();
+                       xsn.Add(string.Empty, string.Empty);
+
+                       byte[] buffer = new byte[2048];
+                       var ms = new MemoryStream(buffer);
+                       using (var xw = XmlWriter.Create(ms, settings))
+                       {
+                               mySerializer.Serialize(xw, obj, xsn);
+                               xw.Flush();
+                       }
+
+                       mySerializer.Serialize(ms, obj);
+               }
+
+               [Test]
+               public void XmlRootOverridesSchemaProviderQName ()
+               {
+                       var obj = new XmlRootOverridesSchemaProviderQNameType ();
+
+                       XmlSerializer xs = new XmlSerializer (obj.GetType ());
+
+                       var sw = new StringWriter ();
+                       using (XmlWriter xw = XmlWriter.Create (sw))
+                               xs.Serialize (xw, obj);
+                       Assert.IsTrue (sw.ToString ().IndexOf ("foo") > 0, "#1");
+               }
+
+               public class AnotherArrayListType
+               {
+                       [XmlAttribute]
+                       public string one = "aaa";
+                       [XmlAttribute]
+                       public string another = "bbb";
+               }
+
+               public class DerivedArrayListType : AnotherArrayListType
+               {
+
+               }
+
+               public class ClassWithArrayList
+               {
+                       [XmlElement (Type = typeof(int), ElementName = "int_elem")]
+                       [XmlElement (Type = typeof(string), ElementName = "string_elem")]
+                       [XmlElement (Type = typeof(AnotherArrayListType), ElementName = "another_elem")]
+                       [XmlElement (Type = typeof(DerivedArrayListType), ElementName = "derived_elem")]
+                       public ArrayList list;
+               }
+
+               public class ClassWithArray
+               {
+                       [XmlElement (Type = typeof(int), ElementName = "int_elem")]
+                       [XmlElement (Type = typeof(string), ElementName = "string_elem")]
+                       [XmlElement (Type = typeof(AnotherArrayListType), ElementName = "another_elem")]
+                       [XmlElement (Type = typeof(DerivedArrayListType), ElementName = "derived_elem")]
+                       public object[] list;
+
+               }
+
+               [Test]
+               public void MultipleXmlElementAttributesOnArrayList()
+               {
+                       var test = new ClassWithArrayList();
+
+                       test.list = new ArrayList();
+                       test.list.Add(3);
+                       test.list.Add("apepe");
+                       test.list.Add(new AnotherArrayListType());
+                       test.list.Add(new DerivedArrayListType());
+
+                       Serialize(test);
+                       var expected_text = "<:ClassWithArrayList http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:int_elem>3</><:string_elem>apepe</><:another_elem :another='bbb' :one='aaa'></><:derived_elem :another='bbb' :one='aaa'></></>";
+
+                       Assert.AreEqual(WriterText, expected_text, WriterText);
+               }
+
+               [Test]
+               public void MultipleXmlElementAttributesOnArray()
+               {
+                       var test = new ClassWithArray();
+
+                       test.list = new object[] { 3, "apepe", new AnotherArrayListType(), new DerivedArrayListType() };
+
+                       Serialize(test);
+                       var expected_text = "<:ClassWithArray http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:int_elem>3</><:string_elem>apepe</><:another_elem :another='bbb' :one='aaa'></><:derived_elem :another='bbb' :one='aaa'></></>";
+
+                       Assert.AreEqual(WriterText, expected_text, WriterText);
+               }
+
 #endif
 
                #endregion //GenericsSeralizationTests
@@ -2954,6 +3113,39 @@ namespace MonoTests.System.XmlSerialization
                        [XmlArrayItem (typeof (XmlSchemaProviderQNameBecomesRootNameType))]
                        public object [] Foo = new object [] {new XmlSchemaProviderQNameBecomesRootNameType ()};
                }
+
+               public class XmlAnyElementForObjectsType
+               {
+                       [XmlAnyElement]
+                       public object [] arr = new object [] {3,4,5};
+               }
+
+               [XmlRoot ("foo")]
+               [XmlSchemaProvider ("GetSchema")]
+               public class XmlRootOverridesSchemaProviderQNameType : IXmlSerializable
+               {
+                       public static XmlQualifiedName GetSchema (XmlSchemaSet xss)
+                       {
+                               var xs = new XmlSchema ();
+                               var xse = new XmlSchemaComplexType () { Name = "bar" };
+                               xs.Items.Add (xse);
+                               xss.Add (xs);
+                               return new XmlQualifiedName ("bar");
+                       }
+
+                       XmlSchema IXmlSerializable.GetSchema ()
+                       {
+                               return null;
+                       }
+
+                       void IXmlSerializable.ReadXml (XmlReader reader)
+                       {
+                       }
+                       void IXmlSerializable.WriteXml (XmlWriter writer)
+                       {
+                       }
+               }
+
 #endif
 
                void CDataTextNodes_BadNode (object s, XmlNodeEventArgs e)