示例#1
0
        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));
                }
            }
        }
示例#2
0
        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));
                }
            }
        }
示例#3
0
        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));
                }
            }
        }
示例#4
0
        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"));
                }
            }
        }
示例#5
0
 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.");
         }
     }
 }
示例#6
0
        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");
                }
            }
        }
示例#7
0
        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"));
                }
            }
        }
示例#8
0
        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));
                }
            }
        }
示例#9
0
        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));
                }
            }
        }
示例#10
0
        public void PluginStatusCollectionMerge()
        {
            Guid id1 = Guid.NewGuid();
            Guid id2 = Guid.NewGuid();
            Guid id3 = Guid.NewGuid();

            var ctx = MiniContext.CreateMiniContext("PluginStatusCollectionMerge");

            PluginStatusCollection collection = new PluginStatusCollection(ctx.ConfigManager.SystemConfiguration as SystemConfiguration);

            collection.SetStatus(id1, ConfigPluginStatus.Manual);

            string path = TestBase.GetTestFilePath("PluginStatusCollectionMerge");

            using (Stream s = new FileStream(path, FileMode.Create))
            {
                using (var w = SimpleStructuredWriter.CreateWriter(s, null))
                {
                    PluginStatusCollection collection2 = new PluginStatusCollection(ctx.ConfigManager.SystemConfiguration as SystemConfiguration);
                    collection2.SetStatus(id1, ConfigPluginStatus.Disabled);
                    collection2.SetStatus(id2, ConfigPluginStatus.Disabled);
                    collection2.SetStatus(id3, ConfigPluginStatus.AutomaticStart);
                    w.WriteInlineObjectStructuredElement("PC", collection2);
                }
            }

            int changingCount = 0, changedCount = 0;
            PluginStatusCollectionChangingEventArgs lastChanging = null;
            PluginStatusCollectionChangedEventArgs  lastChanged = null;

            collection.Changing += (o, e) => { lastChanging = e; changingCount++; };
            collection.Changed  += (o, e) =>
            {
                lastChanged = e; changedCount++;
            };

            using (Stream s = new FileStream(path, FileMode.Open))
            {
                using (var r = SimpleStructuredReader.CreateReader(s, null))
                {
                    r.ReadInlineObjectStructuredElement("PC", collection);
                }
            }

            // Check event count & args
            Assert.That(changingCount == 0 && changedCount == 1);
            Assert.That(lastChanging == null);
            Assert.That(lastChanged.Action == CK.Core.ChangeStatus.ContainerUpdate);
            Assert.That(lastChanged.Collection == collection);
            Assert.That(lastChanged.PluginID == Guid.Empty);
            Assert.That(lastChanged.Status == 0);

            // Check content
            Assert.That(collection.Count == 3);
            Assert.That(collection.GetStatus(id1, ConfigPluginStatus.Manual) == ConfigPluginStatus.Disabled);
            Assert.That(collection.GetStatus(id2, ConfigPluginStatus.Manual) == ConfigPluginStatus.Disabled);
            Assert.That(collection.GetStatus(id3, ConfigPluginStatus.Manual) == ConfigPluginStatus.AutomaticStart);
        }
示例#11
0
        protected virtual IStructuredWriter OpenWrite(Uri u)
        {
            if (u == null)
            {
                throw new ArgumentNullException("u");
            }

            if (!u.IsFile)
            {
                throw new ArgumentException("Only file:// protocol is currently supported.");
            }
            string path = u.LocalPath;

            return(SimpleStructuredWriter.CreateWriter(new FileStream(path, FileMode.Create), _ctx));
        }
示例#12
0
        public void WriteReadUserConfig()
        {
            string path = Path.Combine(TestFolder, "UserConfig.xml");
            Guid   id   = new Guid("{6AFBAE01-5CD1-4EDE-BB56-4590C5A253DF}");

            // Write ----------------------------------------------------------
            {
                ISharedDictionary      dic    = SharedDictionary.Create(null);
                IConfigManagerExtended config = ConfigurationManager.Create(dic);
                Assert.That(config, Is.Not.Null);
                Assert.That(config.HostUserConfig, Is.Not.Null);
                Assert.That(config.ConfigManager.UserConfiguration, Is.Not.Null);

                config.HostUserConfig["key1"] = "value1";
                config.HostUserConfig["key2"] = "value2";
                config.HostUserConfig["key3"] = "value3";
                config.ConfigManager.UserConfiguration.PluginsStatus.SetStatus(id, ConfigPluginStatus.AutomaticStart);

                Assert.That(config.IsUserConfigDirty);
                Assert.That(config.IsSystemConfigDirty, Is.False);

                using (Stream wrt = new FileStream(path, FileMode.Create))
                    using (IStructuredWriter sw = SimpleStructuredWriter.CreateWriter(wrt, null))
                    {
                        config.SaveUserConfig(sw);
                    }
            }

            // Read ------------------------------------------------------------
            {
                ISimpleServiceContainer container = new SimpleServiceContainer();
                ISharedDictionary       dic       = SharedDictionary.Create(container);
                IConfigManagerExtended  config    = ConfigurationManager.Create(dic);

                using (Stream str = new FileStream(path, FileMode.Open))
                    using (IStructuredReader sr = SimpleStructuredReader.CreateReader(str, container))
                    {
                        config.LoadUserConfig(sr);
                    }
                Assert.That(config.HostUserConfig["key1"], Is.EqualTo("value1"));
                Assert.That(config.HostUserConfig["key2"], Is.EqualTo("value2"));
                Assert.That(config.HostUserConfig["key3"], Is.EqualTo("value3"));
                Assert.That(config.ConfigManager.UserConfiguration.PluginsStatus.GetStatus(id, ConfigPluginStatus.Disabled) == ConfigPluginStatus.AutomaticStart);
            }
        }
示例#13
0
        public void WriteReadSystemConfig()
        {
            string path = Path.Combine(TestFolder, "SystemConfig.xml");

            // Write ----------------------------------------------------------
            {
                ISharedDictionary      dic    = SharedDictionary.Create(null);
                IConfigManagerExtended config = ConfigurationManager.Create(dic);

                Assert.That(config.ConfigManager.SystemConfiguration != null);

                config.HostSystemConfig["key1"] = "value1";
                config.HostSystemConfig["key2"] = "value2";
                config.HostSystemConfig["key3"] = "value3";
                config.HostSystemConfig["{12A9FCC0-ECDC-4049-8DBF-8961E49A9EDE}"] = true;

                Assert.That(config.IsSystemConfigDirty);
                Assert.That(config.IsUserConfigDirty, Is.False);

                using (Stream wrt = new FileStream(path, FileMode.Create))
                    using (IStructuredWriter sw = SimpleStructuredWriter.CreateWriter(wrt, null))
                    {
                        config.SaveSystemConfig(sw);
                    }
            }
            TestBase.DumpFileToConsole(path);
            // Read ------------------------------------------------------------
            {
                ISharedDictionary      dic    = SharedDictionary.Create(null);
                IConfigManagerExtended config = new ConfigManagerImpl(dic);

                using (Stream str = new FileStream(path, FileMode.Open))
                    using (IStructuredReader sr = SimpleStructuredReader.CreateReader(str, null))
                    {
                        config.LoadSystemConfig(sr);
                    }
                Assert.That(config.HostSystemConfig["key1"], Is.EqualTo("value1"));
                Assert.That(config.HostSystemConfig["key2"], Is.EqualTo("value2"));
                Assert.That(config.HostSystemConfig["key3"], Is.EqualTo("value3"));
                Assert.That(config.HostSystemConfig["{12A9FCC0-ECDC-4049-8DBF-8961E49A9EDE}"], Is.EqualTo(true));
            }
        }
示例#14
0
 public static void Write(string testName, string path, ISharedDictionary dic, object o, Action <XmlDocument> afterWrite)
 {
     using (Stream wrt = new FileStream(path, FileMode.Create))
     {
         using (IStructuredWriter sw = SimpleStructuredWriter.CreateWriter(wrt, ServiceProvider))
         {
             using (ISharedDictionaryWriter w = dic.RegisterWriter(sw))
             {
                 w.WritePluginsDataElement(testName, o);
             }
         }
     }
     if (afterWrite != null)
     {
         XmlDocument d = new XmlDocument();
         d.Load(path);
         afterWrite(d);
         d.Save(path);
     }
 }
示例#15
0
        public void EmptyFile()
        {
            string test = TestBase.GetTestFilePath("Storage", "EmptyFile");

            using (Stream wrt = new FileStream(test, FileMode.Create))
            {
                IStructuredWriter writer = SimpleStructuredWriter.CreateWriter(wrt, new SimpleServiceContainer());
                writer.Dispose();
            }

            Assert.That(File.Exists(test));

            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))
                {
                    Assert.That(reader.StorageVersion, Is.GreaterThanOrEqualTo(new Version(2, 5, 0)));
                }
            }
        }
示例#16
0
        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());
                }
            }
        }
示例#17
0
        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));
                }
            }
        }
示例#18
0
        public void SimpleStructuredSerializerTest()
        {
            string testPath = TestBase.GetTestFilePath("CKTests.Storage", "StructuredSerializer");
            IStructuredSerializer <Dog> serializer = new SimpleStructuredDogSerializer();
            Dog dog = CreateDog();

            using (Stream str = new FileStream(testPath, FileMode.Create))
            {
                using (IStructuredWriter writer = SimpleStructuredWriter.CreateWriter(str, new SimpleServiceContainer()))
                {
                    // This is an independant scope: we just created the writer...
                    writer.ServiceContainer.Add <UniqueService>(new UniqueService());
                    writer.ServiceContainer.Add <IStructuredSerializer <Dog> >(new SimpleStructuredDogSerializer());
                    writer.ServiceContainer.Add <IStructuredSerializer <Paw> >(new SimpleStructuredPawSerializer());

                    writer.WriteObjectElement("Dog", dog);
                }
            }

            Dog readDog;

            // 1 - Use ReadInlineObject
            using (Stream str = new FileStream(testPath, FileMode.Open))
                using (IStructuredReader reader = CreateConfiguredReader(str))
                {
                    // This is an independant scope: we just created the reader...
                    reader.ServiceContainer.Add <UniqueService>(new UniqueService());

                    StandardReadStatus status;
                    object             o = reader.ReadInlineObject(out status);
                    readDog = o as Dog;
                }
            CheckReadDog(readDog);

            // 2 - Use ReadInlineObjectStructured( Type )
            using (Stream str = new FileStream(testPath, FileMode.Open))
                using (IStructuredReader reader = CreateConfiguredReader(str))
                {
                    // This is an independant scope: we just created the reader...
                    reader.ServiceContainer.Add <UniqueService>(new UniqueService());

                    Assert.That(reader.Xml.IsStartElement("Dog"));
                    // We ignore attributes added by WriteObjectElement: we directly call ReadInlineObjectStructured for the Dog type.
                    object o = reader.ReadInlineObjectStructured(typeof(Dog), null);
                    readDog = o as Dog;
                }
            CheckReadDog(readDog);

            // 3 - Use ReadInlineObjectStructured( object )
            using (Stream str = new FileStream(testPath, FileMode.Open))
                using (IStructuredReader reader = CreateConfiguredReader(str))
                {
                    // This is an independant scope: we just created the reader...
                    reader.ServiceContainer.Add <UniqueService>(new UniqueService());

                    Assert.That(reader.Xml.IsStartElement("Dog"));
                    readDog = new Dog();
                    // We ignore attributes added by WriteObjectElement: we directly call ReadInlineObjectStructured for an empty Dog object.
                    object o = reader.ReadInlineObjectStructured(readDog);
                }
            CheckReadDog(readDog);
        }
示例#19
0
        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));
                }
            }
        }
示例#20
0
        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));
                }
            }
        }