public void BugUnexisting() { string xmlPath = TestBase.GetTestFilePath("Storage", "BugUnexistingEnum"); using (Stream wrt = new FileStream(xmlPath, FileMode.Create)) { using (IStructuredWriter writer = SimpleStructuredWriter.CreateWriter(wrt, new SimpleServiceContainer())) { writer.WriteObjectElement("data", UnexistingTestEnumValues.First); writer.WriteObjectElement("After", 3712 * 2); writer.WriteObjectElement("data", new MayBeUnexistingButValidXmlObject()); writer.WriteObjectElement("After2", 3712 * 3); } } TestBase.DumpFileToConsole(xmlPath); using (Stream str = new FileStream(xmlPath, FileMode.Open)) { SimpleServiceContainer s = new SimpleServiceContainer(); s.Add <ISimpleTypeFinder>(new UnexistingTypeFinder()); using (IStructuredReader reader = SimpleStructuredReader.CreateReader(str, s)) { Assert.Throws <TypeLoadException>(() => reader.ReadObjectElement("data")); // An exception does not break the reader. Assert.That(reader.ReadObjectElement("After"), Is.EqualTo(3712 * 2)); Assert.Throws <TypeLoadException>(() => reader.ReadObjectElement("data")); // An exception does not break the reader. Assert.That(reader.ReadObjectElement("After2"), Is.EqualTo(3712 * 3)); } } }
private void DoTestEnum(Action <XDocument> docModifier) { string test = TestBase.GetTestFilePath("Storage", "TestEnum"); using (Stream wrt = new FileStream(test, FileMode.Create)) { using (IStructuredWriter writer = SimpleStructuredWriter.CreateWriter(wrt, new SimpleServiceContainer())) { writer.WriteObjectElement("data", TestEnumValues.First); writer.WriteObjectElement("data", TestEnumValues.Second); writer.WriteObjectElement("After", 3712 * 2); } } LoadAndModifyXml(test, docModifier); using (Stream str = new FileStream(test, FileMode.Open)) { SimpleServiceContainer s = new SimpleServiceContainer(); s.Add(typeof(ISimpleTypeFinder), SimpleTypeFinder.WeakDefault, null); using (IStructuredReader reader = SimpleStructuredReader.CreateReader(str, s)) { TestEnumValues value1 = (TestEnumValues)reader.ReadObjectElement("data"); TestEnumValues value2 = (TestEnumValues)reader.ReadObjectElement("data"); Assert.That(value1, Is.EqualTo(TestEnumValues.First)); Assert.That(value2, Is.EqualTo(TestEnumValues.Second)); Assert.That(reader.ReadObjectElement("After"), Is.EqualTo(3712 * 2)); } } }
public void ColorStruct() { string xmlPath = TestBase.GetTestFilePath("Storage", "TestColor"); using (Stream wrt = new FileStream(xmlPath, FileMode.Create)) { using (IStructuredWriter writer = SimpleStructuredWriter.CreateWriter(wrt, new SimpleServiceContainer())) { writer.WriteObjectElement("data", Color.Red); writer.WriteObjectElement("data", Color.Blue); writer.WriteObjectElement("After", 3712 * 2); } } using (Stream str = new FileStream(xmlPath, FileMode.Open)) { SimpleServiceContainer s = new SimpleServiceContainer(); //s.Add( typeof( ISimpleTypeFinder ), SimpleTypeFinder.WeakDefault, null ); using (IStructuredReader reader = SimpleStructuredReader.CreateReader(str, s)) { CheckExactTypeAndValue(typeof(Color), Color.Red, reader.ReadObjectElement("data")); CheckExactTypeAndValue(typeof(Color), Color.Blue, reader.ReadObjectElement("data")); Assert.That(reader.ReadObjectElement("After"), Is.EqualTo(3712 * 2)); } } }
public void GenericListOfString() { string xmlPath = TestBase.GetTestFilePath("Storage", "TestGenericListOfString"); List <string> list = new List <string>(); list.Add("content1"); list.Add("content2"); list.Add("content3"); using (Stream wrt = new FileStream(xmlPath, FileMode.Create)) { using (IStructuredWriter writer = SimpleStructuredWriter.CreateWriter(wrt, new SimpleServiceContainer())) { writer.WriteObjectElement("Before", 3712); writer.WriteObjectElement("data", list); writer.WriteObjectElement("After", 3712 * 2); } } using (Stream str = new FileStream(xmlPath, FileMode.Open)) { SimpleServiceContainer s = new SimpleServiceContainer(); s.Add <ISimpleTypeFinder>(SimpleTypeFinder.WeakDefault); using (IStructuredReader reader = SimpleStructuredReader.CreateReader(str, s)) { Assert.That(reader.ReadObjectElement("Before"), Is.EqualTo(3712)); CheckExactTypeAndValue(typeof(List <string>), list, reader.ReadObjectElement("data")); Assert.That(reader.ReadObjectElement("After"), Is.EqualTo(3712 * 2)); } } }
public void BinarySerializableObject() { string xmlPath = TestBase.GetTestFilePath("Storage", "TestBinarySerializableObject"); SerializableObject o = new SerializableObject(); o.Name = "TestName"; o.Power = 20; using (Stream wrt = new FileStream(xmlPath, FileMode.Create)) { using (IStructuredWriter writer = SimpleStructuredWriter.CreateWriter(wrt, new SimpleServiceContainer())) { writer.WriteObjectElement("Before", 3712); writer.WriteObjectElement("data", o); writer.WriteObjectElement("After", 3712 * 2); } } using (Stream str = new FileStream(xmlPath, FileMode.Open)) { SimpleServiceContainer s = new SimpleServiceContainer(); s.Add(typeof(ISimpleTypeFinder), SimpleTypeFinder.WeakDefault, null); using (IStructuredReader reader = SimpleStructuredReader.CreateReader(str, s)) { Assert.That(reader.ReadObjectElement("Before"), Is.EqualTo(3712)); SerializableObject o2 = (SerializableObject)reader.ReadObjectElement("data"); Assert.AreEqual(o.Name, o2.Name); Assert.AreEqual(o.Power, o2.Power); Assert.That(reader.ReadObjectElement("After"), Is.EqualTo(3712 * 2)); } } }
private void TestXmlSerializableObject(string path, XmlRawObjectBase original) { using (Stream wrt = new FileStream(path, FileMode.Create)) { using (IStructuredWriter writer = SimpleStructuredWriter.CreateWriter(wrt, new SimpleServiceContainer())) { writer.WriteObjectElement("Before", 3712); writer.WriteObjectElement("data", original); writer.WriteObjectElement("After", 3712 * 2); } } using (Stream str = new FileStream(path, FileMode.Open)) { using (IStructuredReader reader = SimpleStructuredReader.CreateReader(str, null)) { Assert.That(reader.ReadObjectElement("Before"), Is.EqualTo(3712)); if (original.BugWhileReading == BugRead.ThrowApplicationException) { Assert.Throws <ApplicationException>(() => reader.ReadObjectElement("data")); // Even if an exception is thrown, we can continue to read the data. } else if (original.BugWhileReading == BugRead.None) { CheckExactTypeAndValue(original.GetType(), original, reader.ReadObjectElement("data")); } else { XmlRawObjectBase read = (XmlRawObjectBase)reader.ReadObjectElement("data"); Assert.That(read.BugWhileReading == original.BugWhileReading); } Assert.That(reader.ReadObjectElement("After"), Is.EqualTo(3712 * 2), "Whatever happens above, one can continue to read."); } } }
public void TestIntegers() { string test = TestBase.GetTestFilePath("Storage", "TestIntegers"); using (Stream wrt = new FileStream(test, FileMode.Create)) { using (IStructuredWriter writer = SimpleStructuredWriter.CreateWriter(wrt, new SimpleServiceContainer())) { writer.WriteObjectElement("data", (int)10); writer.WriteObjectElement("data", (SByte)(-8)); writer.WriteObjectElement("data", (Int16)(-16)); writer.WriteObjectElement("data", (Int32)(-32)); writer.WriteObjectElement("data", (Int64)(-64)); writer.WriteObjectElement("data", (Byte)8); writer.WriteObjectElement("data", (UInt16)16); writer.WriteObjectElement("data", (UInt32)32); writer.WriteObjectElement("data", (UInt64)64); } } using (Stream str = new FileStream(test, FileMode.Open)) { using (IStructuredReader reader = SimpleStructuredReader.CreateReader(str, null)) { CheckExactTypeAndValue(typeof(int), 10, reader.ReadObjectElement("data")); CheckExactTypeAndValue(typeof(SByte), -8, reader.ReadObjectElement("data")); CheckExactTypeAndValue(typeof(Int16), -16, reader.ReadObjectElement("data")); CheckExactTypeAndValue(typeof(Int32), -32, reader.ReadObjectElement("data")); CheckExactTypeAndValue(typeof(Int64), -64, reader.ReadObjectElement("data")); CheckExactTypeAndValue(typeof(Byte), 8, reader.ReadObjectElement("data")); CheckExactTypeAndValue(typeof(UInt16), 16, reader.ReadObjectElement("data")); CheckExactTypeAndValue(typeof(UInt32), 32, reader.ReadObjectElement("data")); CheckExactTypeAndValue(typeof(UInt64), 64, reader.ReadObjectElement("data")); } } }
public void BugBinarySizeDiffer() { string xmlPath = TestBase.GetTestFilePath("Storage", "BugBinarySizeDiffer"); SerializableObject original = new SerializableObject() { Name = "coucou", Power = 20 }; using (Stream wrt = new FileStream(xmlPath, FileMode.Create)) { using (IStructuredWriter writer = SimpleStructuredWriter.CreateWriter(wrt, new SimpleServiceContainer())) { writer.WriteObjectElement("data", original); } } LoadAndModifyXml(xmlPath, d => d.Root.Element("data").Attribute("size").SetValue("1")); using (Stream str = new FileStream(xmlPath, FileMode.Open)) { SimpleServiceContainer s = new SimpleServiceContainer(); s.Add(typeof(ISimpleTypeFinder), SimpleTypeFinder.WeakDefault, null); using (IStructuredReader reader = SimpleStructuredReader.CreateReader(str, s)) { Assert.Throws <CKException>(() => reader.ReadObjectElement("data")); } } }
public void BugBinaryTooBigContent() { string xmlPath = TestBase.GetTestFilePath("Storage", "BugBinaryTooBigContent"); SerializableObject original = new SerializableObject() { Name = "coucou", Power = 20 }; using (Stream wrt = new FileStream(xmlPath, FileMode.Create)) { using (IStructuredWriter writer = SimpleStructuredWriter.CreateWriter(wrt, new SimpleServiceContainer())) { writer.WriteObjectElement("data", original); } } LoadAndModifyXml(xmlPath, d => { var e = d.Root.Element("data"); e.SetValue(e.Value.Insert(e.Value.Length / 2, "00FF00FF")); }); using (Stream str = new FileStream(xmlPath, FileMode.Open)) { SimpleServiceContainer s = new SimpleServiceContainer(); s.Add(typeof(ISimpleTypeFinder), SimpleTypeFinder.WeakDefault, null); using (IStructuredReader reader = SimpleStructuredReader.CreateReader(str, s)) { object obj = reader.ReadObjectElement("data"); } } }
public void ArrayListWithSerializableObjects() { string xmlPath = TestBase.GetTestFilePath("Storage", "TestGenericListOfString"); ArrayList list = new ArrayList(); SerializableObject firstObject = new SerializableObject() { Name = "Albert", Power = 34 }; list.Add(firstObject); list.Add(new DateTime(2009, 01, 11)); list.Add("Franchement, les mecs, vous trouvez que c'est normal que ce soit Spi qui se cogne tous les tests unitaires ?"); using (Stream wrt = new FileStream(xmlPath, FileMode.Create)) { using (IStructuredWriter writer = SimpleStructuredWriter.CreateWriter(wrt, new SimpleServiceContainer())) { writer.WriteObjectElement("data", list); writer.WriteObjectElement("After", 3712 * 2); } } using (Stream str = new FileStream(xmlPath, FileMode.Open)) { SimpleServiceContainer s = new SimpleServiceContainer(); s.Add(typeof(ISimpleTypeFinder), SimpleTypeFinder.WeakDefault, null); using (IStructuredReader reader = SimpleStructuredReader.CreateReader(str, s)) { ArrayList list2 = (ArrayList)reader.ReadObjectElement("data"); Assert.AreEqual(((SerializableObject)list2[0]).Name, ((SerializableObject)list[0]).Name); Assert.AreEqual(((SerializableObject)list2[0]).Power, ((SerializableObject)list[0]).Power); CheckExactTypeAndValue(typeof(DateTime), list[1], list2[1]); CheckExactTypeAndValue(typeof(string), list[2], list2[2]); Assert.That(reader.ReadObjectElement("After"), Is.EqualTo(3712 * 2)); } } }
public void FirstTest() { string test = TestBase.GetTestFilePath("Storage", "FirstTest"); using (Stream wrt = new FileStream(test, FileMode.Create)) { using (IStructuredWriter writer = SimpleStructuredWriter.CreateWriter(wrt, new SimpleServiceContainer())) { writer.WriteObjectElement("data", (int)10); writer.Xml.WriteStartElement("test.done"); writer.Xml.WriteEndElement(); } } using (Stream str = new FileStream(test, FileMode.Open)) { using (IStructuredReader reader = SimpleStructuredReader.CreateReader(str, null)) { CheckExactTypeAndValue(typeof(int), 10, reader.ReadObjectElement("data")); Assert.That(reader.Xml.IsStartElement("test.done")); Assert.That(reader.Xml.Read()); } } }
public void StructuredSerializedObjectTest() { string xmlPath = TestBase.GetTestFilePath("Storage", "FakeStructuredSerializedObject"); StructuredSerializableObject original = new StructuredSerializableObject() { OneInteger = 43, OneString = "Let's go..." }; using (Stream wrt = new FileStream(xmlPath, FileMode.Create)) { using (IStructuredWriter writer = SimpleStructuredWriter.CreateWriter(wrt, new SimpleServiceContainer())) { writer.ObjectWriteExData += (s, e) => { if (e.Obj == original) { e.Writer.Xml.WriteStartElement("ExtraData"); e.Writer.Xml.WriteAttributeString("OneAtrribute", "23"); e.Writer.Xml.WriteElementString("SubValue", "string in element..."); e.Writer.Xml.WriteEndElement(); } }; writer.WriteObjectElement("data", original); writer.WriteObjectElement("After", 3712 * 2); } } TestBase.DumpFileToConsole(xmlPath); // Reads without reading ExtraData element. using (Stream str = new FileStream(xmlPath, FileMode.Open)) { SimpleServiceContainer s = new SimpleServiceContainer(); s.Add <ISimpleTypeFinder>(SimpleTypeFinder.WeakDefault); using (IStructuredReader reader = SimpleStructuredReader.CreateReader(str, s)) { object read = reader.ReadObjectElement("data"); Assert.That(read, Is.TypeOf(typeof(StructuredSerializableObject))); StructuredSerializableObject newOne = read as StructuredSerializableObject; Assert.That(newOne.OneString, Is.EqualTo(original.OneString)); Assert.That(newOne.OneInteger, Is.EqualTo(original.OneInteger)); Assert.That(reader.ReadObjectElement("After"), Is.EqualTo(3712 * 2)); } } // Reads ExtraData element. using (Stream str = new FileStream(xmlPath, FileMode.Open)) { SimpleServiceContainer s = new SimpleServiceContainer(); s.Add <ISimpleTypeFinder>(SimpleTypeFinder.WeakDefault); using (IStructuredReader reader = SimpleStructuredReader.CreateReader(str, s)) { reader.ObjectReadExData += (source, e) => { Assert.That(e.Reader.Xml.IsStartElement("ExtraData")); Assert.That(e.Reader.Xml.GetAttributeInt("OneAtrribute", -12), Is.EqualTo(23)); e.Reader.Xml.Read(); Assert.That(e.Reader.Xml.ReadElementContentAsString(), Is.EqualTo("string in element...")); // Forget to read the end element. Assert.That(e.Reader.Xml.NodeType == XmlNodeType.EndElement); }; object read = reader.ReadObjectElement("data"); Assert.That(read, Is.TypeOf(typeof(StructuredSerializableObject))); StructuredSerializableObject newOne = read as StructuredSerializableObject; Assert.That(newOne.OneString, Is.EqualTo(original.OneString)); Assert.That(newOne.OneInteger, Is.EqualTo(original.OneInteger)); Assert.That(reader.ReadObjectElement("After"), Is.EqualTo(3712 * 2)); } } }
public void TestXmlSerializer() { XmlSerializer serIXml = new XmlSerializer(typeof(XmlObjectViaIXmlSerializable)); XmlSerializer serAttr = new XmlSerializer(typeof(XmlObjectViaAttributes)); object oIxml = new XmlObjectViaIXmlSerializable() { Name = "York", Power = 126 }; object oAttr = new XmlObjectViaAttributes() { Name = "York n°2", Power = 47 }; string xmlPath = TestBase.GetTestFilePath("Storage", "TestXmlSerializer"); using (Stream wrt = new FileStream(xmlPath, FileMode.Create)) { using (IStructuredWriter writer = SimpleStructuredWriter.CreateWriter(wrt, new SimpleServiceContainer())) { writer.Xml.WriteStartElement("TestIXml"); serIXml.Serialize(writer.Xml, oIxml); writer.Xml.WriteEndElement(); // TestIXml writer.Xml.WriteStartElement("TestAttr"); serAttr.Serialize(writer.Xml, oAttr); writer.Xml.WriteEndElement(); // TestAttr writer.WriteObjectElement("Before", 3712 * 2); writer.WriteObjectElement("data", oIxml); writer.WriteObjectElement("After", 3712 * 3); } } TestBase.DumpFileToConsole(xmlPath); using (Stream str = new FileStream(xmlPath, FileMode.Open)) { SimpleServiceContainer s = new SimpleServiceContainer(); s.Add <ISimpleTypeFinder>(SimpleTypeFinder.Default); using (IStructuredReader reader = SimpleStructuredReader.CreateReader(str, s)) { reader.Xml.ReadStartElement("TestIXml"); object oIXml2 = serIXml.Deserialize(reader.Xml); Assert.That(oIXml2, Is.EqualTo(oIxml)); reader.Xml.ReadEndElement(); // TestIXml reader.Xml.ReadStartElement("TestAttr"); object oAttr2 = serAttr.Deserialize(reader.Xml); Assert.That(oAttr2, Is.EqualTo(oAttr)); reader.Xml.ReadEndElement(); // TestAttr Assert.That(reader.ReadObjectElement("Before"), Is.EqualTo(3712 * 2)); object oIXml2bis = reader.ReadObjectElement("data"); Assert.That(oIXml2bis, Is.EqualTo(oIxml)); // Since we can not (yet) inject XmlSerializer, the XmlObjectViaAttributes // can not be serialized as object. // This must be done for the moment with an external Serializer. Assert.That(reader.ReadObjectElement("After"), Is.EqualTo(3712 * 3)); } } }