public void FileSerialize_DeserializeTest() { var collection = new SerializerTestCollection(); var text = "asd"; var path = Path.GetTempPath(); var filename = "filename"; var s = new CollectionTestXmlSerializer(collection, text, path, filename); int val = 123; Assert.AreEqual(0, collection.Count); collection.Add(new SerializerTestItem() { Prop = val }); s.Dispose(); collection.Clear(); s = new CollectionTestXmlSerializer(collection, text, path, filename); s.Deserialize(); Assert.AreEqual(1, collection.Count); Assert.AreEqual(val, collection[0].Prop); s.ClearSerializedData(); s.Dispose(); }
public void FileTest() { var item = new SerializerTestCollection(); var text = "123sdf"; var path = Path.GetTempPath(); var filename = "filename"; var s = new CollectionTestXmlSerializer(item, text, path, filename); Assert.AreEqual($"{filename}.xml", s.FileName); Assert.AreEqual(path, s.StoragePath); Assert.IsTrue(s.StoreToFile); var x = File.Exists(s.StoragePath + s.FileName); Assert.IsFalse(x); Assert.AreEqual(false, s.CheckFile()); s.ClearSerializedData(); var stream = File.Create(s.StoragePath + s.FileName); stream.Dispose(); x = File.Exists(s.StoragePath + s.FileName); Assert.IsTrue(x); Assert.AreEqual(true, s.CheckFile()); s.ClearSerializedData(); x = File.Exists(s.StoragePath + s.FileName); Assert.IsFalse(x); s.Dispose(); path = path.Remove(path.Length - 1, 1); s = new CollectionTestXmlSerializer(item, text, path, filename); Assert.AreEqual(path + Path.DirectorySeparatorChar, s.StoragePath); s.Dispose(); }
public void Test() { var c = new SerializerTestCollection(); Assert.AreEqual(0, c.Serializers.Count); var f = new TestSerializationFactory(c); Assert.AreEqual(1, c.Serializers.Count); Assert.IsInstanceOfType(c.Serializers[0], typeof(TestCollectionSerializer)); var i = new SerializerTestItem(); Assert.AreEqual(0, i.Serializers.Count); c.Add(i); Assert.AreEqual(1, i.Serializers.Count); Assert.IsInstanceOfType(i.Serializers[0], typeof(TestItemSerializer)); TestItemSerializer s = (TestItemSerializer)i.Serializers[0]; c.Remove(i); Assert.IsTrue(s.IsDisposed); Assert.AreEqual(0, i.Serializers.Count); i = new SerializerTestItem(); c.Add(i); Assert.AreEqual(1, i.Serializers.Count); Assert.IsInstanceOfType(i.Serializers[0], typeof(TestItemSerializer)); s = (TestItemSerializer)i.Serializers[0]; f.Dispose(); Assert.IsTrue(s.IsDisposed); }
public void XmlTest() { var item = new SerializerTestCollection(); var text = "123sdf"; var s = new CollectionTestXmlSerializer(item, text); Assert.IsFalse(s.StoreToFile); Assert.AreEqual(string.Empty, s.FileName); Assert.AreEqual(string.Empty, s.StoragePath); Assert.IsNotNull(s.XmlDocument); Assert.IsNotNull(s.XmlRoot); Assert.AreEqual(text, s.XmlRoot.Name); var element = s.XmlDocument.CreateElement("element1"); s.XmlRoot.AppendChild(element); Assert.AreEqual(1, s.XmlRoot.ChildNodes.Count); element = s.XmlDocument.CreateElement("element2"); s.XmlRoot.AppendChild(element); Assert.AreEqual(2, s.XmlRoot.ChildNodes.Count); s.XmlRoot.RemoveChild(element); Assert.AreEqual(1, s.XmlRoot.ChildNodes.Count); element = s.XmlDocument.CreateElement("element3"); s.XmlRoot.AppendChild(element); Assert.AreEqual(2, s.XmlRoot.ChildNodes.Count); s.ClearSerializedData(); Assert.AreEqual(0, s.XmlRoot.ChildNodes.Count); s.Dispose(); }
public void SerializeOnCollectionChangedTest() { var collection = new SerializerTestCollection(); collection.PropertyChanged += Collection_PropertyChanged; collection.CollectionChanged += Collection_CollectionChanged; var serializer = new TestCollectionSerializer(collection); collection.Serializers.Add(serializer); //Test on Add serializer.SerializerCalled = false; serializer.DeserializerCalled = false; _notifyCollectionChangedEventArgs = null; _propertyChangedEventArgs = null; for (int i = 0; i < 10; i++) { serializer.SerializerCalled = false; serializer.DeserializerCalled = false; _notifyCollectionChangedEventArgs = null; _propertyChangedEventArgs = null; collection.Add(new SerializerTestItem { Prop = i }); Assert.IsTrue(serializer.SerializerCalled); Assert.IsFalse(serializer.DeserializerCalled); Assert.IsNotNull(_notifyCollectionChangedEventArgs); Assert.IsNull(_propertyChangedEventArgs); } //Test on RemoveAt for (int i = 0; i < 10; i++) { serializer.SerializerCalled = false; serializer.DeserializerCalled = false; _notifyCollectionChangedEventArgs = null; _propertyChangedEventArgs = null; collection.RemoveAt(0); Assert.IsTrue(serializer.SerializerCalled); Assert.IsFalse(serializer.DeserializerCalled); Assert.IsNotNull(_notifyCollectionChangedEventArgs); Assert.IsNull(_propertyChangedEventArgs); } //Test on Remove for (int i = 0; i < 10; i++) { serializer.SerializerCalled = false; serializer.DeserializerCalled = false; _notifyCollectionChangedEventArgs = null; _propertyChangedEventArgs = null; collection.Add(new SerializerTestItem { Prop = i }); Assert.IsTrue(serializer.SerializerCalled); Assert.IsFalse(serializer.DeserializerCalled); Assert.IsNotNull(_notifyCollectionChangedEventArgs); Assert.IsNull(_propertyChangedEventArgs); } for (int i = 0; i < 10; i++) { serializer.SerializerCalled = false; serializer.DeserializerCalled = false; _notifyCollectionChangedEventArgs = null; _propertyChangedEventArgs = null; collection.Remove(collection[0]); Assert.IsTrue(serializer.SerializerCalled); Assert.IsFalse(serializer.DeserializerCalled); Assert.IsNotNull(_notifyCollectionChangedEventArgs); Assert.IsNull(_propertyChangedEventArgs); } //Test on Clear for (int i = 0; i < 10; i++) { serializer.SerializerCalled = false; serializer.DeserializerCalled = false; _notifyCollectionChangedEventArgs = null; _propertyChangedEventArgs = null; collection.Add(new SerializerTestItem { Prop = i }); Assert.IsTrue(serializer.SerializerCalled); Assert.IsFalse(serializer.DeserializerCalled); Assert.IsNotNull(_notifyCollectionChangedEventArgs); Assert.IsNull(_propertyChangedEventArgs); } serializer.SerializerCalled = false; serializer.DeserializerCalled = false; _notifyCollectionChangedEventArgs = null; _propertyChangedEventArgs = null; collection.Clear(); Assert.IsTrue(serializer.SerializerCalled); Assert.IsFalse(serializer.DeserializerCalled); Assert.IsNotNull(_notifyCollectionChangedEventArgs); Assert.IsNull(_propertyChangedEventArgs); //Test on Dispose for (int i = 0; i < 10; i++) { serializer.SerializerCalled = false; serializer.DeserializerCalled = false; _notifyCollectionChangedEventArgs = null; _propertyChangedEventArgs = null; collection.Add(new SerializerTestItem { Prop = i }); Assert.IsTrue(serializer.SerializerCalled); Assert.IsFalse(serializer.DeserializerCalled); Assert.IsNotNull(_notifyCollectionChangedEventArgs); Assert.IsNull(_propertyChangedEventArgs); } serializer.SerializerCalled = false; serializer.DeserializerCalled = false; _notifyCollectionChangedEventArgs = null; _propertyChangedEventArgs = null; collection.Dispose(); Assert.IsFalse(serializer.SerializerCalled); Assert.IsFalse(serializer.DeserializerCalled); Assert.IsNull(_notifyCollectionChangedEventArgs); Assert.IsNull(_propertyChangedEventArgs); }
public void SerializeOnPropertyChangedTest() { var collection = new SerializerTestCollection(); collection.PropertyChanged += Collection_PropertyChanged; collection.CollectionChanged += Collection_CollectionChanged; var serializer = new TestCollectionSerializer(collection); collection.Serializers.Add(serializer); //Test on Add serializer.SerializerCalled = false; serializer.DeserializerCalled = false; _notifyCollectionChangedEventArgs = null; _propertyChangedEventArgs = null; serializer.SerializerCalled = false; serializer.DeserializerCalled = false; _notifyCollectionChangedEventArgs = null; _propertyChangedEventArgs = null; var item = new SerializerTestItem { Prop = 1 }; collection.Add(item); Assert.IsTrue(serializer.SerializerCalled); Assert.IsFalse(serializer.DeserializerCalled); Assert.IsNotNull(_notifyCollectionChangedEventArgs); Assert.IsNull(_propertyChangedEventArgs); //Collection Property change test serializer.SerializerCalled = false; serializer.DeserializerCalled = false; _notifyCollectionChangedEventArgs = null; _propertyChangedEventArgs = null; collection.Prop++; Assert.IsTrue(serializer.SerializerCalled); Assert.IsFalse(serializer.DeserializerCalled); Assert.IsNull(_notifyCollectionChangedEventArgs); Assert.IsNotNull(_propertyChangedEventArgs); //Item Property change test - must not be serialized serializer.DefaultCheckPropertyChanged = true; serializer.SerializerCalled = false; serializer.DeserializerCalled = false; _notifyCollectionChangedEventArgs = null; _propertyChangedEventArgs = null; item.Prop++; Assert.IsFalse(serializer.SerializerCalled); Assert.IsFalse(serializer.DeserializerCalled); Assert.IsNull(_notifyCollectionChangedEventArgs); Assert.IsNotNull(_propertyChangedEventArgs); serializer.DefaultCheckPropertyChanged = false; serializer.SerializerCalled = false; serializer.DeserializerCalled = false; _notifyCollectionChangedEventArgs = null; _propertyChangedEventArgs = null; item.Prop++; Assert.IsTrue(serializer.SerializerCalled); Assert.IsFalse(serializer.DeserializerCalled); Assert.IsNull(_notifyCollectionChangedEventArgs); Assert.IsNotNull(_propertyChangedEventArgs); }
public void SerializeDeserializeTest() { var collection = new SerializerTestCollection(); collection.PropertyChanged += Collection_PropertyChanged; collection.CollectionChanged += Collection_CollectionChanged; for (int i = 0; i < 10; i++) { collection.Add(new SerializerTestItem { Prop = i }); } var serializer = new TestCollectionSerializer(collection); var collectionLen = collection.Count; serializer.SerializerCalled = false; serializer.DeserializerCalled = false; _notifyCollectionChangedEventArgs = null; _propertyChangedEventArgs = null; collection.Serializers.Add(serializer); //Test Empty deserialize Assert.IsFalse(serializer.SerializerCalled); Assert.IsFalse(serializer.DeserializerCalled); Assert.IsNull(_notifyCollectionChangedEventArgs); Assert.IsNull(_propertyChangedEventArgs); collection.Serializers[0].Deserialize(); Assert.IsFalse(serializer.SerializerCalled); Assert.IsTrue(serializer.DeserializerCalled); Assert.AreEqual(collectionLen, collection.Count); Assert.IsNull(_notifyCollectionChangedEventArgs); Assert.IsNull(_propertyChangedEventArgs); //TestSerialize serializer.SerializerCalled = false; serializer.DeserializerCalled = false; _notifyCollectionChangedEventArgs = null; _propertyChangedEventArgs = null; collection.Serializers[0].Serialize(); Assert.IsTrue(serializer.SerializerCalled); Assert.IsFalse(serializer.DeserializerCalled); Assert.AreEqual(collectionLen, collection.Count); Assert.IsNull(_notifyCollectionChangedEventArgs); Assert.IsNull(_propertyChangedEventArgs); collection.Clear(); Assert.AreEqual(0, collection.Count); //TestDeserialize serializer.SerializerCalled = false; serializer.DeserializerCalled = false; _notifyCollectionChangedEventArgs = null; _propertyChangedEventArgs = null; collection.Serializers[0].Deserialize(); Assert.IsFalse(serializer.SerializerCalled); Assert.IsTrue(serializer.DeserializerCalled); Assert.AreEqual(collectionLen, collection.Count); Assert.IsNotNull(_notifyCollectionChangedEventArgs); Assert.IsNull(_propertyChangedEventArgs); Assert.AreEqual(10, collection.Count); for (int i = 0; i < 10; i++) { collection[i].Prop = i; } serializer.Dispose(); }