public void TestClassEdits() { var testXmlNoRoot = @"<test> </test>"; var xmlObj = new XmlObject(testXml); // test to make sure that InvalidOperationException is thrown. Assert.Throws <InvalidOperationException>(() => xmlObj.AddAttribute("test4", "test", "test")); Assert.Throws <InvalidOperationException>(() => xmlObj.Write("test", "test")); Assert.Throws <InvalidOperationException>(() => xmlObj.Write("test2", "test", "test")); Assert.Throws <InvalidOperationException>(() => xmlObj.Write("test3", "test31", new string[] { "test1", "test2", "test3" })); xmlObj.TryRead("test"); xmlObj.TryRead("test2", "test"); xmlObj.TryRead("test3", "test31", null); Assert.Throws <InvalidOperationException>(() => xmlObj.Delete("test")); Assert.Throws <InvalidOperationException>(() => xmlObj.Delete("test2", "test")); Assert.Throws <InvalidOperationException>(() => xmlObj.ReopenFile()); xmlObj = new XmlObject(testXmlNoRoot); // reopen data from a file. var fstrm = File.Create( $"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}test.xml"); fstrm.Write(Encoding.UTF8.GetBytes(testXml), 0, testXml.Length); fstrm.Dispose(); xmlObj = new XmlObject( $"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}test.xml", testXml); NoThrows(() => xmlObj.AddAttribute("test4", "test", "test")); NoThrows(() => xmlObj.Write("test", "test")); NoThrows(() => xmlObj.Write("test2", "test", "test")); NoThrows(() => xmlObj.Write("test3", "test", new string[] { "test1", "test2", "test3" })); xmlObj.TryRead("test"); xmlObj.TryRead("test2", "test"); xmlObj.TryRead("test3", "test", null); xmlObj.TryRead("test4"); NoThrows(() => xmlObj.ReopenFile()); NoThrows(() => xmlObj.Write("test", "testnew")); xmlObj.TryRead("test"); xmlObj.TryRead("test2", "test"); xmlObj.TryRead("test3", "test", null); NoThrows(() => xmlObj.Delete("test")); NoThrows(() => xmlObj.Delete("test2", "test")); NoThrows(() => xmlObj.Save()); File.Delete( $"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}test.xml"); xmlObj = new XmlObject($"{Path.DirectorySeparatorChar}test.xml", testXml, true); xmlObj = new XmlObject($"{Path.DirectorySeparatorChar}test.xml", testXml, true); xmlObj = new XmlObject($"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}test.xml", testXml, true); Assert.Throws <ArgumentNullException>(() => xmlObj = new XmlObject(null, testXml, true)); }
public void Test_delete_file_element_Pass() { var testXmlFile = @"testDelElement.xml"; if (File.Exists($"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}" + testXmlFile)) { File.Delete($"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}" + testXmlFile); } Assert.False(File.Exists(testXmlFile)); var element = "TestElement"; var elementValue = "element value"; var xmlObj = new XmlObject(testXmlFile, testXml, true); xmlObj.Write(element, elementValue); xmlObj.Save(); xmlObj.ReopenFile(); xmlObj.Delete(element); xmlObj.Save(); xmlObj.ReopenFile(); var result = xmlObj.TryRead(element); Assert.NotEqual(result, element); File.Delete($"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}" + testXmlFile); }
public void Test_add_update_attribute_Pass() { var testXmlFile = @"testAddAttribute.xml"; if (File.Exists($"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}" + testXmlFile)) { File.Delete($"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}" + testXmlFile); } Assert.False(File.Exists(testXmlFile)); var element = "TestElement"; var attribute = "TestAttribute"; var attributeValue = "my cool value"; var newAttributeValue = "my new cool value"; var xmlObj = new XmlObject(testXmlFile, testXml, true); xmlObj.AddAttribute(element, attribute, attributeValue); xmlObj.AddAttribute(element, attribute, newAttributeValue); xmlObj.Save(); xmlObj.ReopenFile(); var result = xmlObj.TryRead(element, attribute); Assert.Equal(result, newAttributeValue); File.Delete($"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}" + testXmlFile); }
public void Test_Subelements() { var testXmlFile = @"testCreate.xml"; if (File.Exists($"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}" + testXmlFile)) { File.Delete($"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}" + testXmlFile); } var xmlObj = new XmlObject(testXmlFile, testXml, true); xmlObj.Save(); var values = new string[] { "test subelement value 1", "test subelement value 2" }; xmlObj.Write("testsubelements", "subelement", values); Assert.NotEqual(Array.Empty <string>(), xmlObj.TryRead("testsubelements", "subelement", null)); xmlObj.Save(); Assert.Equal(values, xmlObj.TryRead("testsubelements", "subelement", null)); File.Delete($"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}" + testXmlFile); }