public void ObjectExtenderContainerConverterConvertFromTypeCompliantWithXmlSerializer() { MockServiceProvider mockServiceProvider = new MockServiceProvider(); Type[] types = { typeof(TestSerializableObject) }; MockServiceProviderService mockService = new MockServiceProviderService(); mockService.ObjectExtenderTypes.Add(types[0]); mockServiceProvider.AddService(typeof(IExtensionProviderService), mockService); ObjectExtenderContainerConverter converter = new ObjectExtenderContainerConverter(mockServiceProvider); TestSerializableObject testObject1 = new TestSerializableObject(); testObject1.ValueOne = "TestDataOne"; testObject1.ValueTwo = 33; string stringRepresentation; ObjectExtenderContainer container1 = new ObjectExtenderContainer(); container1.ObjectExtenders.Add(testObject1); stringRepresentation = GenericSerializer.Serialize<ObjectExtenderContainer>(container1, types); ObjectExtenderContainer container2 = converter.ConvertFrom(stringRepresentation) as ObjectExtenderContainer; Assert.IsNotNull(container2, "container is null"); Assert.IsNotNull(container2.ObjectExtenders, "ObjectExtenders is null"); Assert.AreEqual(1, container2.ObjectExtenders.Count, "container.ObjectExtenders.Count != 1"); TestSerializableObject testObject2 = container2.ObjectExtenders[0] as TestSerializableObject; Assert.AreEqual(testObject1.ValueOne, testObject2.ValueOne, "Not equal"); Assert.AreEqual(testObject1.ValueTwo, testObject2.ValueTwo, "Not equal"); }
public CachingSerializerTest() { TestSerializableObject obj = new TestSerializableObject(); Type[] supportedTypes = new Type[1]; supportedTypes[0] = obj.GetType(); this.SupportedTypes = supportedTypes; }
public void DeserializeFailsWhenDataFileDoesntExist() { TestSerializableObject obj = new TestSerializableObject(); string originalValue = "original value 123"; obj.Test = originalValue; TestCacheSerializer.Serialize(obj); //2. Delete the cachePath AppContext.StreamManager.CloseStream(TmpCachePath); //disposes and removes cache stream File.Delete(TmpDataPath); // After deleting the data file, attempting to deserialize the object should return // a null object. obj = TestCacheSerializer.Deserialize <TestSerializableObject>(); Assert.IsNull(obj); }
public void GetByteRepresentationTest() { TestSerializableObject obj = new TestSerializableObject(); string originalValue = "original value 123"; obj.Test = originalValue; TestCacheSerializer.Serialize(obj); // Attempting to deserialize the invalidated data file should delete the file var bytes = TestCacheSerializer.GetByteRepresentation(); //attempt to deserialize from bytes back to original object var binaryFormatter = new BinaryFormatter(); TestSerializableObject obj2 = binaryFormatter.Deserialize(new MemoryStream(bytes)) as TestSerializableObject; Assert.AreEqual(obj, obj2); }
public void TestDeserializeCheckIfCacheHasBeenUpdated() { //------ TestSerializableObject obj = new TestSerializableObject(); obj.Test = "original value 123"; TestCacheSerializer.Serialize(obj); //1. should change timestampt of the file - Data should be newer than Cache string newTestValue = "modified value 987!"; ModifyXMLAndSave(TmpDataPath, newTestValue); //2. assure that Data is newer than cache - update lastwritetime for newer moment FileInfo pathFileInfo = new FileInfo(TmpDataPath); FileInfo cachedFileInfo = new FileInfo(TmpCachePath); //3. just make Data LastWriteTime 5 minutes newer than LastWriteTime of Cache pathFileInfo.LastWriteTime = new DateTime(cachedFileInfo.LastWriteTime.Ticks).Add(new System.TimeSpan(0, 0, 5, 0)); //double check that it is newer Assert.IsTrue(pathFileInfo.LastWriteTime > cachedFileInfo.LastWriteTime); //4. Deserialize object - it should use data from xml... cache is old! We expect to get new value TestSerializableObject deseralizedObj = TestCacheSerializer.Deserialize <TestSerializableObject>(); Assert.AreEqual(newTestValue, deseralizedObj.Test); //------- //5. but cache should be updated as well //so change Cache LastWriteTime to be newer than Data LastWriteTime cachedFileInfo.LastWriteTime = new DateTime(pathFileInfo.LastWriteTime.Ticks).Add(new System.TimeSpan(0, 0, 5, 0)); //double check that Data is older than Cache, just in case Assert.IsTrue(pathFileInfo.LastWriteTime < cachedFileInfo.LastWriteTime); //6. Deserialize object this time it should use cache - and if cache it updated then it will return newTestValue // otherwise it would be old original value, which would be wrong. TestSerializableObject deseralizedObj2 = TestCacheSerializer.Deserialize <TestSerializableObject>(); Assert.AreEqual(newTestValue, deseralizedObj2.Test); }
public void TestDeserializeCreateCacheWhenCacheDidNotExist() { TestSerializableObject obj = new TestSerializableObject(); string originalValue = "original value 123"; obj.Test = originalValue; TestCacheSerializer.Serialize(obj); //2. Delete the cachePath AppContext.StreamManager.CloseStream(TmpCachePath); //disposes and removes cache stream (new FileInfo(TmpCachePath)).Delete(); //3. Currently there is only Data file //4. Deserialize object - it should use data from xml... cache does not exists... it should still work TestSerializableObject deseralizedObj = TestCacheSerializer.Deserialize <TestSerializableObject>(); //5. cache should be have been created as well //so lets change Data string newTestValue = "modified value 987!"; ModifyXMLAndSave(TmpDataPath, newTestValue); //and change Cache LastWriteTime to be newer than Data LastWriteTime FileInfo cachedFileInfo = new FileInfo(TmpCachePath); FileInfo pathFileInfo = new FileInfo(TmpDataPath); cachedFileInfo.LastWriteTime = new DateTime(pathFileInfo.LastWriteTime.Ticks).Add(new System.TimeSpan(0, 0, 5, 0)); //double check that Data is older than Cache, just in case Assert.IsTrue(pathFileInfo.LastWriteTime < cachedFileInfo.LastWriteTime); //6. Deserialize object this time it should use cache... so it should have original value TestSerializableObject deseralizedObj2 = TestCacheSerializer.Deserialize <TestSerializableObject>(); Assert.AreEqual(originalValue, deseralizedObj2.Test); }
public void TestDeserializeUseCacheIfTimestampsAreEqual() { TestSerializableObject obj = new TestSerializableObject(); obj.Test = "original value 123"; TestCacheSerializer.Serialize(obj); //changes timestampt of the file - Data is newer than Cache - we don't want it in this test string newTestValue = "modified value 987!"; ModifyXMLAndSave(TmpDataPath, newTestValue); //so change the timestamp of the xml file to old FileInfo pathFileInfo = new FileInfo(TmpDataPath); FileInfo cachedFileInfo = new FileInfo(TmpCachePath); pathFileInfo.LastWriteTime = cachedFileInfo.LastWriteTime; //cache = data //it should use cache... cache is newer! TestSerializableObject deseralizedObj = TestCacheSerializer.Deserialize <TestSerializableObject>(); Assert.AreEqual("original value 123", deseralizedObj.Test); }
public void TestDeserializeUseCache() { TestSerializableObject obj = new TestSerializableObject(); obj.Test = "original value 123"; TestCacheSerializer.Serialize(obj); //changes timestampt of the file - Data is newer than Cache - we don't want it in this test string newTestValue = "modified value 987!"; ModifyXMLAndSave(TmpDataPath, newTestValue); //so change the timestamp of the xml file to old FileInfo pathFileInfo = new FileInfo(TmpDataPath); FileInfo cachedFileInfo = new FileInfo(TmpCachePath); pathFileInfo.LastWriteTime = new DateTime(cachedFileInfo.LastWriteTime.Ticks).Subtract(new System.TimeSpan(0, 0, 5, 0)); //just make it 5 minutes older //double check that it is older Assert.IsTrue(pathFileInfo.LastWriteTime < cachedFileInfo.LastWriteTime); //it should use cache... cache is newer! TestSerializableObject deseralizedObj = TestCacheSerializer.Deserialize<TestSerializableObject>(); Assert.AreEqual("original value 123", deseralizedObj.Test); }
public void TestDeserializeCheckIfCacheHasBeenUpdated() { //------ TestSerializableObject obj = new TestSerializableObject(); obj.Test = "original value 123"; TestCacheSerializer.Serialize(obj); //1. should change timestampt of the file - Data should be newer than Cache string newTestValue = "modified value 987!"; ModifyXMLAndSave(TmpDataPath, newTestValue); //2. assure that Data is newer than cache - update lastwritetime for newer moment FileInfo pathFileInfo = new FileInfo(TmpDataPath); FileInfo cachedFileInfo = new FileInfo(TmpCachePath); //3. just make Data LastWriteTime 5 minutes newer than LastWriteTime of Cache pathFileInfo.LastWriteTime = new DateTime(cachedFileInfo.LastWriteTime.Ticks).Add(new System.TimeSpan(0, 0, 5, 0)); //double check that it is newer Assert.IsTrue(pathFileInfo.LastWriteTime > cachedFileInfo.LastWriteTime); //4. Deserialize object - it should use data from xml... cache is old! We expect to get new value TestSerializableObject deseralizedObj = TestCacheSerializer.Deserialize<TestSerializableObject>(); Assert.AreEqual(newTestValue, deseralizedObj.Test); //------- //5. but cache should be updated as well //so change Cache LastWriteTime to be newer than Data LastWriteTime cachedFileInfo.LastWriteTime = new DateTime(pathFileInfo.LastWriteTime.Ticks).Add(new System.TimeSpan(0, 0, 5, 0)); //double check that Data is older than Cache, just in case Assert.IsTrue(pathFileInfo.LastWriteTime < cachedFileInfo.LastWriteTime); //6. Deserialize object this time it should use cache - and if cache it updated then it will return newTestValue // otherwise it would be old original value, which would be wrong. TestSerializableObject deseralizedObj2 = TestCacheSerializer.Deserialize<TestSerializableObject>(); Assert.AreEqual(newTestValue, deseralizedObj2.Test); }
public void TestDeserializeUseData() { TestSerializableObject obj = new TestSerializableObject(); obj.Test = "original value 123"; TestCacheSerializer.Serialize(obj); //1. should change timestampt of the file - Data should be newer than Cache string newTestValue = "modified value 987!"; ModifyXMLAndSave(TmpDataPath, newTestValue); //2. assure that Data is newer than cache - update lastwritetime for newer moment FileInfo pathFileInfo = new FileInfo(TmpDataPath); FileInfo cachedFileInfo = new FileInfo(TmpCachePath); //3. just make Data LastWriteTime 5 minutes newer than LastWriteTime of Cache pathFileInfo.LastWriteTime = new DateTime(cachedFileInfo.LastWriteTime.Ticks).Add(new System.TimeSpan(0, 0, 5, 0)); //double check that it is newer Assert.IsTrue(pathFileInfo.LastWriteTime > cachedFileInfo.LastWriteTime); //4. Deserialize object - it should use data from xml... cache is old! We expect to get new value TestSerializableObject deseralizedObj = TestCacheSerializer.Deserialize<TestSerializableObject>(); Assert.AreEqual(newTestValue, deseralizedObj.Test); }
/// <summary> /// Equalses the specified other. /// </summary> /// <param name="other">The other.</param> /// <returns></returns> /// <remarks></remarks> public bool Equals(TestSerializableObject other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; return Equals(other.String1, String1) && Equals(other.String2, String2); }
public void DeserializeBadDataFails() { TestSerializableObject obj = new TestSerializableObject(); string originalValue = "original value 123"; obj.Test = originalValue; TestCacheSerializer.Serialize(obj); //2. Delete the cachePath AppContext.StreamManager.CloseStream(TmpCachePath); //disposes and removes cache stream // Sleep, just to cause the file write times to be different. System.Threading.Thread.Sleep(15); using (XmlWriter writer = XmlWriter.Create(TmpDataPath)) { writer.WriteStartDocument(); writer.WriteStartElement("Foo"); writer.WriteEndElement(); writer.WriteEndDocument(); } // Attempting to deserialize the invalidated data file should delete the file obj = TestCacheSerializer.Deserialize<TestSerializableObject>(); Assert.IsNull(obj); Assert.IsFalse(File.Exists(TmpDataPath)); }
public void DeserializeGracefullyFailsOnDataError() { TestSerializableObject obj = new TestSerializableObject(); string originalValue = "original value 123"; obj.Test = originalValue; TestCacheSerializer.Serialize(obj); //2. Delete the cachePath AppContext.StreamManager.CloseStream(TmpCachePath); //disposes and removes cache stream using (BinaryWriter writer = new BinaryWriter(new FileStream(TmpCachePath, FileMode.Open))) { string foo = "foo"; writer.Write(foo); writer.Flush(); } // Now that it is closed, we should be able to repeat the process and successfully delete the bad cache file. obj = TestCacheSerializer.Deserialize<TestSerializableObject>(); Assert.IsNotNull(obj); Assert.AreEqual(originalValue, obj.Test); Assert.IsTrue(File.Exists(TmpCachePath)); // Deserializing it should have forced the cache file back to the correct data. System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); using (Stream stream = new FileStream(TmpCachePath, FileMode.Open)) { obj = (TestSerializableObject)formatter.Deserialize(stream); } Assert.AreEqual(originalValue, obj.Test); }
public void TestDeserializeUseCacheIfTimestampsAreEqual() { TestSerializableObject obj = new TestSerializableObject(); obj.Test = "original value 123"; TestCacheSerializer.Serialize(obj); //changes timestampt of the file - Data is newer than Cache - we don't want it in this test string newTestValue = "modified value 987!"; ModifyXMLAndSave(TmpDataPath, newTestValue); //so change the timestamp of the xml file to old FileInfo pathFileInfo = new FileInfo(TmpDataPath); FileInfo cachedFileInfo = new FileInfo(TmpCachePath); pathFileInfo.LastWriteTime = cachedFileInfo.LastWriteTime; //cache = data //it should use cache... cache is newer! TestSerializableObject deseralizedObj = TestCacheSerializer.Deserialize<TestSerializableObject>(); Assert.AreEqual("original value 123", deseralizedObj.Test); }
public void ObjectExtenderContainerConverterConvertToTypeCompliantWithXmlSerializer() { MockServiceProvider mockServiceProvider = new MockServiceProvider(); MockServiceProviderService mockService = new MockServiceProviderService(); mockServiceProvider.AddService(typeof(IExtensionProviderService), mockService); ObjectExtenderContainerConverter converter = new ObjectExtenderContainerConverter(mockServiceProvider); TestSerializableObject testObject1 = new TestSerializableObject(); testObject1.ValueOne = "TestDataOne"; testObject1.ValueTwo = 33; string stringRepresentation; Type[] types = { typeof(TestSerializableObject) }; ObjectExtenderContainer container1 = new ObjectExtenderContainer(); container1.ObjectExtenders.Add(testObject1); stringRepresentation = GenericSerializer.Serialize<ObjectExtenderContainer>(container1, types); string stringRepresentation2 = converter.ConvertTo(stringRepresentation, typeof(string)) as string; Assert.AreEqual(stringRepresentation, stringRepresentation2, "Not equal"); }
public void TestDeserializeCreateCacheWhenCacheDidNotExist() { TestSerializableObject obj = new TestSerializableObject(); string originalValue = "original value 123"; obj.Test = originalValue; TestCacheSerializer.Serialize(obj); //2. Delete the cachePath AppContext.StreamManager.CloseStream(TmpCachePath); //disposes and removes cache stream (new FileInfo(TmpCachePath)).Delete(); //3. Currently there is only Data file //4. Deserialize object - it should use data from xml... cache does not exists... it should still work TestSerializableObject deseralizedObj = TestCacheSerializer.Deserialize<TestSerializableObject>(); //5. cache should be have been created as well //so lets change Data string newTestValue = "modified value 987!"; ModifyXMLAndSave(TmpDataPath, newTestValue); //and change Cache LastWriteTime to be newer than Data LastWriteTime FileInfo cachedFileInfo = new FileInfo(TmpCachePath); FileInfo pathFileInfo = new FileInfo(TmpDataPath); cachedFileInfo.LastWriteTime = new DateTime(pathFileInfo.LastWriteTime.Ticks).Add(new System.TimeSpan(0, 0, 5, 0)); //double check that Data is older than Cache, just in case Assert.IsTrue(pathFileInfo.LastWriteTime < cachedFileInfo.LastWriteTime); //6. Deserialize object this time it should use cache... so it should have original value TestSerializableObject deseralizedObj2 = TestCacheSerializer.Deserialize<TestSerializableObject>(); Assert.AreEqual(originalValue, deseralizedObj2.Test); }
public void TestSerializeNullObj() { TestSerializableObject obj = new TestSerializableObject(); obj.Test = "test"; TestCacheSerializer.Serialize(null); //should throw null exception }
public void DeserializeFailsWhenDataFileDoesntExist() { TestSerializableObject obj = new TestSerializableObject(); string originalValue = "original value 123"; obj.Test = originalValue; TestCacheSerializer.Serialize(obj); //2. Delete the cachePath AppContext.StreamManager.CloseStream(TmpCachePath); //disposes and removes cache stream File.Delete(TmpDataPath); // After deleting the data file, attempting to deserialize the object should return // a null object. obj = TestCacheSerializer.Deserialize<TestSerializableObject>(); Assert.IsNull(obj); }
public void TestSerialize() { TestSerializableObject obj = new TestSerializableObject(); obj.Test = "test"; TestCacheSerializer.Serialize(obj); //should pass XmlDocument myXmlDocument = new XmlDocument(); myXmlDocument.Load(TmpDataPath); XmlNode node = myXmlDocument.DocumentElement; //find Test node bool nodeFound = false; foreach (XmlNode node1 in node.ChildNodes) { if (node1.Name.Equals("Test")) { nodeFound = true; Assert.AreEqual("test", node1.InnerText); } } Assert.IsTrue(nodeFound); }
public void TestDeserialize() { TestSerializableObject obj = new TestSerializableObject(); obj.Test = "test123"; TestCacheSerializer.Serialize(obj); TestSerializableObject desObj = TestCacheSerializer.Deserialize<TestSerializableObject>(); Assert.IsTrue(desObj.Equals(obj)); }
public async Task ExecuteReader_WithNestedTupleParameter_ExecutesSuccessfully() { SqlProgram <IEnumerable <Tuple <int, string, bool, bool, decimal, decimal, double, Tuple <string, short, TestSerializableObject, byte, DateTime, DateTime, XElement, Tuple <int, long, int, int> > > > > tupleTableTypeTest = await SqlProgram <IEnumerable <Tuple <int, string, bool, bool, decimal, decimal, double, Tuple <string, short, TestSerializableObject, byte, DateTime, DateTime, XElement, Tuple <int, long, int, int> > > > > .Create((Connection)DifferentLocalDatabaseConnectionString, "spTakesMultiTupleTable"); var rows = new List < Tuple <int, string, bool, bool, decimal, decimal, double, Tuple <string, short, TestSerializableObject, byte, DateTime, DateTime, XElement, Tuple <int, long, int, int> > > >(); for (int i = 0; i < Random.Next(3, 10); i++) { rows.Add(ExtendedTuple.Create( Random.RandomInt32(), Random.RandomString(50, false), false, true, RandomSqlSafeDecimal(), RandomSqlSafeDecimal(), Random.RandomDouble(), Random.RandomString(), Random.RandomInt16(), new TestSerializableObject { String1 = Random.RandomString(), String2 = Random.RandomString() }, Random.RandomByte(), RandomSqlSafeDateTime(), RandomSqlSafeDateTime(), new XElement("Test", new XAttribute("attribute", Random.Next())), Random.RandomInt32(), Random.RandomInt64(), Random.RandomInt32(), Random.RandomInt32())); } var indexer = typeof( Tuple <int, string, bool, bool, decimal, decimal, double, Tuple <string, short, TestSerializableObject, byte, DateTime, DateTime, XElement, Tuple <int, long, int, int> > > ).GetTupleIndexer(); tupleTableTypeTest.ExecuteReader( reader => { int r = 0; while (reader.Read()) { var tuple = rows[r++]; for (int c = 0; c < 18; c++) { // Get the tuple value that was passed into the sproc. object cell = indexer(tuple, c); // Check collections (e.g. byte[]) ICollection cellCollection = cell as ICollection; if (cellCollection != null) { ICollection resultCollection = reader.GetValue(c) as ICollection; Assert.IsNotNull( resultCollection, "The db did not return a collection"); CollectionAssert.AreEqual(cellCollection, resultCollection); continue; } // Check serialized object TestSerializableObject serializedObject = cell as TestSerializableObject; if (serializedObject != null) { // Deserialize object TestSerializableObject deserializedObject = (TestSerializableObject)reader.GetObjectByName(reader.GetName(c)); // Check we don't have same object instance. Assert.IsFalse(ReferenceEquals(serializedObject, deserializedObject)); // Check equality of object instances using equality method. Assert.AreEqual(serializedObject, deserializedObject); continue; } // Check XELement XElement xelement = cell as XElement; if (xelement != null) { XElement result = XElement.Parse(reader.GetString(c)); Assert.AreEqual(xelement.ToString(), result.ToString()); continue; } Assert.AreEqual(cell, reader.GetValue(c)); } } }, rows); }
public async Task ExecuteReader_WithSerializableObjectParameter_ReturnsByteArray() { SqlProgram<TestSerializableObject> serializeObjectTest = await SqlProgram<TestSerializableObject>.Create((Connection)DifferentLocalDatabaseConnectionString, "spTakeByteArray"); TestSerializableObject objecToSerialize = new TestSerializableObject { String1 = Random.RandomString(), String2 = Random.RandomString() }; serializeObjectTest.ExecuteReader( reader => { Assert.IsTrue(reader.Read()); Assert.IsInstanceOfType(reader.GetValue(0), typeof (byte[])); // Deserialize object TestSerializableObject deserializedObject = (TestSerializableObject) reader.GetObjectByName(reader.GetName(0)); // Check we don't have same object instance. Assert.IsFalse(ReferenceEquals(objecToSerialize, deserializedObject)); // Check equality of object instances using equality method. Assert.AreEqual(objecToSerialize, deserializedObject); }, objecToSerialize); }