Merge pull request #1196 from tritao/master
[mono.git] / mcs / class / System.XML / Test / System.Xml.Serialization / XmlSerializerTests.cs
index 2fd5597998de9faa6e106f600a169edf83fca4db..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()
@@ -2844,6 +2852,115 @@ namespace MonoTests.System.XmlSerialization
                {
                        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
@@ -3002,6 +3119,33 @@ namespace MonoTests.System.XmlSerialization
                        [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)