// // ComplexDataStructure.cs // // Author: // Lluis Sanchez Gual (lluis@ximian.com) // // (C) 2004 Novell, Inc. // // using System; using System.IO; using System.Xml; using System.Xml.Schema; using System.Xml.Serialization; using System.Collections; using System.ComponentModel; using NUnit.Framework; namespace MonoTests.System.XmlSerialization { [TestFixture] public class ComplexDataStructure: Assertion { [Test] public void TestWriteLiteral () { Test data = BuildTestObject (); XmlSerializer ss = new XmlSerializer (GetLiteralTypeMapping ()); XmlSerializerNamespaces nams = new XmlSerializerNamespaces (); StringWriter sw = new StringWriter(); ss.Serialize (sw,data,nams); string serialized = sw.ToString (); serialized = XmlSerializerTests.Infoset (serialized); StreamReader sr = new StreamReader ("Test/XmlFiles/literal-data.xml"); string expected = sr.ReadToEnd (); sr.Close (); expected = XmlSerializerTests.Infoset (expected); AssertEquals (expected, serialized); } public void TestReadLiteral () { XmlSerializer ss = new XmlSerializer (GetLiteralTypeMapping ()); XmlSerializerNamespaces nams = new XmlSerializerNamespaces (); StreamReader sr = new StreamReader ("Test/XmlFiles/literal-data.xml"); Test data = (Test) ss.Deserialize (sr); sr.Close (); CheckObjectContent (BuildTestObject(), data); } XmlTypeMapping GetLiteralTypeMapping () { XmlRootAttribute root = new XmlRootAttribute("rootroot"); Type[] types = new Type[] {typeof(UknTestPart), typeof(AnotherTestPart), typeof(DblStringContainer) }; XmlReflectionImporter ri = new XmlReflectionImporter (); foreach (Type t in types) ri.IncludeType (t); return ri.ImportTypeMapping (typeof(Test), root); } XmlTypeMapping GetEncodedTypeMapping () { SoapReflectionImporter sri = new SoapReflectionImporter (); sri.IncludeType (typeof(UknTestPart)); sri.IncludeType (typeof(AnotherTestPart)); sri.IncludeType (typeof(DblStringContainer)); return sri.ImportTypeMapping (typeof(Test)); } Test BuildTestObject () { XmlDocument doc = new XmlDocument(); Test t = new UknTestPart(); t.a = 1; t.b = "hola"; t.bbis = t.b; t.c = 44; t.parts = new TestPart[3]; t.parts[0] = new TestPart(); t.parts[0].name = "un"; t.parts[0].bval = true; t.parts[1] = new TestPart(); t.parts[1].name = "dos"; t.parts[1].bval = false; t.parts[2] = t.parts[0]; t.part = t.parts[1]; t.strings = new string[] { "un", "dos", null, "tres" }; t.ushorts = new ushort[] { 1,2,3 }; t.ta = new TB(); t.ta.extraTextNodes = new XmlNode[] { doc.CreateTextNode ("AA"), doc.CreateTextNode ("BB") }; t.tam2 = new TA[][][] { new TA[][] { new TA[] {new TA(), new TA()}, new TA[] {new TA(), new TA()}}, new TA[][] { new TA[] {new TB(), new TA()}, new TA[] {new TB(), new TA()}}, new TA[][] { new TA[] {new TA(), new TB()}, new TA[] {new TA(), new TA()}} }; t.tam3 = t.tam2; t.flatParts = t.parts; t.flatParts2 = new TA[] {new TA(), new TB(), null, new TB()}; t.anot = new AnotherTestPart (); ((AnotherTestPart)t.anot).lo = 1234567890; t.ob = t.parts[1]; t.ob2 = t.parts[1]; XmlElement e1 = doc.CreateElement ("explicitElement"); XmlElement e2 = doc.CreateElement ("subElement"); e2.SetAttribute ("unAtrib","val"); doc.AppendChild (e1); e1.AppendChild (e2); t.oneElem = e1; t.oneElem2 = e1; t.someElems = new XmlNode[3]; t.someElems[0] = e1; t.someElems[1] = null; t.someElems[2] = e2; t.extraElems = new XmlElement[1]; t.extraElems[0] = doc.CreateElement ("extra1"); t.extraElems[0].SetAttribute ("val","1"); t.extraElems23 = new XmlElement[2]; t.extraElems23[0] = doc.CreateElement ("extra2"); t.extraElems23[0].SetAttribute ("val","2"); t.extraElems23[1] = doc.CreateElement ("extra3"); t.extraElems23[1].SetAttribute ("val","3"); t.extraElemsRest = doc.CreateElement ("extra4"); t.extraElemsRest.SetAttribute ("val","4"); t.uktester = new UnknownAttributeTester(); t.uktester.aa = "hihi"; t.uktester.extraAtts = new XmlAttribute[3]; t.uktester.extraAtts[0] = doc.CreateAttribute ("extraAtt1"); t.uktester.extraAtts[0].Value = "val1"; t.uktester.extraAtts[1] = doc.CreateAttribute ("extraAtt2"); t.uktester.extraAtts[1].Value = "val2"; t.uktester.extraAtts[2] = doc.CreateAttribute ("extraAtt3"); t.uktester.extraAtts[2].Value = "val3"; t.ob3 = 12345; t.ob4 = (float)54321.12; t.op1 = option.AA; t.opArray = new option[] { option.CC, option.BB, option.AA }; t.ukOpt = option.DD; t.opAtt = option.BB; t.byteArray = new byte[] { 11,33,55,222 }; t.byteByteArray = new byte[][] { t.byteArray, t.byteArray }; t.ttList = new ArrayList(); t.ttList.Add ("two"); t.ttList.Add ("strings"); // t.extraText = "Additional text"; t.RoList = new ArrayList (); t.RoList.Add (t.parts[0]); t.RoList.Add (t.parts[1]); /* t.struc = new OneStruct(); t.struc.aa = 776655; t.struc.cc = "this is a struct"; */ t.multiList = new ArrayList[2]; t.multiList[0] = new ArrayList (); t.multiList[0].Add (22); t.multiList[0].Add (33); t.multiList[1] = new ArrayList (); t.multiList[1].Add (888); t.multiList[1].Add (999); t.defElem = "theDefValue"; t.defAttr = "theDefValue"; t.special = new CustomHashtable (); t.special.Add ("one","1"); t.special.Add ("two","2"); t.special.Add ("three","3"); t.attqname = new XmlQualifiedName ("thename","thenamespace"); DblStringContainer dbc = new DblStringContainer (); dbc.doublestring = new string [][] { null, new string[] {"hello"} }; AnotherTestPart at = new AnotherTestPart (); at.lo = 567; dbc.at = at; DblStringContainerAnm dbca = new DblStringContainerAnm (); dbca.at = dbc; t.dbscontainer = dbca; return t; } void CheckObjectContent (Test exp, Test t) { AssertEquals ("t.a", exp.a, t.a); AssertEquals ("t.b", exp.b, t.b); AssertEquals ("t.bbis", exp.bbis, t.bbis); AssertEquals ("t.c", exp.c, t.c); AssertNotNull ("t.parts", t.parts); CheckParts ("t.parts", exp.parts, t.parts); TestPart.AssertEquals ("t.part", exp.part, t.part); AssertionHelper.AssertEqualsArray ("t.strings", exp.strings, t.strings); AssertionHelper.AssertEqualsArray ("t.ushorts", exp.ushorts, t.ushorts); TA.AssertEquals ("t.ta", exp.ta, t.ta); AssertNotNull ("t.tam2", t.tam2); CheckTaArray ("t.tam2", exp.tam2, t.tam2); AssertNotNull ("t.tam3", t.tam3); CheckTaArray ("t.tam3", exp.tam3, t.tam3); AssertNotNull ("t.flatParts", t.flatParts); CheckParts ("t.flatParts", exp.flatParts, t.flatParts); // Null element is ignored AssertNotNull ("t.flatParts2", t.flatParts2); AssertEquals ("t.flatParts2.Length", 3, t.flatParts2.Length); TA.AssertEquals ("t.flatParts2 0", exp.flatParts2[0], t.flatParts2[0]); TA.AssertEquals ("t.flatParts2 1", exp.flatParts2[1], t.flatParts2[1]); TA.AssertEquals ("t.flatParts2 2", exp.flatParts2[3], t.flatParts2[2]); AssertNotNull ("t.anot", t.anot); AssertEquals ("t.anot.lo", ((AnotherTestPart)exp.anot).lo, ((AnotherTestPart)t.anot).lo); TestPart.AssertEquals ("t.ob", exp.ob as TestPart, t.ob as TestPart); TestPart.AssertEquals ("t.ob2", exp.ob2 as TestPart, t.ob2 as TestPart); AssertionHelper.AssertEqualsXml ("t.oneElem", exp.oneElem, t.oneElem); AssertionHelper.AssertEqualsXml ("t.oneElem2", exp.oneElem2, t.oneElem2); // One of the elements was null and it is ignored AssertNotNull ("t.someElems", t.someElems); AssertEquals ("t.someElems.Length", 2, t.someElems.Length); AssertionHelper.AssertEqualsXml ("t.someElems[0]", exp.someElems[0], t.someElems[0]); AssertionHelper.AssertEqualsXml ("t.someElems[1]", exp.someElems[2], t.someElems[1]); AssertNotNull ("t.extraElems", t.extraElems); AssertEquals ("t.extraElems.Length", exp.extraElems.Length, t.extraElems.Length); for (int n=0; n