Ejemplo n.º 1
0
        public void Load()
        {
            UndoMgr undomgr = new UndoMgr(5);
            ObjectStore <TestObject> objstore = new ObjectStore <TestObject>(undomgr);

            string xmlText =
                @"<?xml version=""1.0"" encoding=""utf-16""?>
<testobjects>
  <testobject id=""2"" x=""8"" f=""9.4"">goodbye</testobject>
  <testobject id=""3"" x=""9"" f=""9.9"">bat</testobject>
  <testobject id=""4"" x=""11"" f=""1.4"">foo</testobject>
</testobjects>";

            XmlInput xmlinput = new XmlInput(new StringReader(xmlText), "testfile");

            xmlinput.CheckElement("testobjects");
            xmlinput.Read();

            objstore.Load(xmlinput);
            xmlinput.Dispose();

            TestUtil.TestEnumerableAnyOrder(objstore.AllPairs,
                                            new KeyValuePair <Id <TestObject>, TestObject>[] {
                new KeyValuePair <Id <TestObject>, TestObject>(new Id <TestObject>(3), new TestObject(9, "bat", 9.9F)),
                new KeyValuePair <Id <TestObject>, TestObject>(new Id <TestObject>(4), new TestObject(11, "foo", 1.4F)),
                new KeyValuePair <Id <TestObject>, TestObject>(new Id <TestObject>(2), new TestObject(8, "goodbye", 9.4F))
            });
        }
Ejemplo n.º 2
0
 public void CloseFile()
 {
     // Make sure that Dispose really closes the underlying file.
     File.Copy(TestUtil.GetTestFile("xmlinput.xml"), TestUtil.GetTestFile("temp.xml"));
     XmlInput xmlinput = new XmlInput(TestUtil.GetTestFile("temp.xml"));
     xmlinput.CheckElement("rootsym");
     xmlinput.Dispose();
     File.Delete(TestUtil.GetTestFile("temp.xml"));
 }
Ejemplo n.º 3
0
        public void LoadModifySave()
        {
            Id <TestObject>          id;
            UndoMgr                  undomgr  = new UndoMgr(5);
            ObjectStore <TestObject> objstore = new ObjectStore <TestObject>(undomgr);

            string xmlText =
                @"<?xml version=""1.0"" encoding=""utf-16""?>
<testobjects>
  <testobject id=""2"" x=""8"" f=""9.4"">goodbye</testobject>
  <testobject id=""4"" x=""11"" f=""1.4"">foo</testobject>
</testobjects>";

            XmlInput xmlinput = new XmlInput(new StringReader(xmlText), "testfile");

            xmlinput.CheckElement("testobjects");
            xmlinput.Read();

            objstore.Load(xmlinput);
            xmlinput.Dispose();

            undomgr.BeginCommand(57, "Command1");
            id = objstore.Add(new TestObject(5, "hello", 5.4F));
            Assert.AreEqual(5, id.id);
            id = objstore.Add(new TestObject(9, "bat", 9.9F));
            Assert.AreEqual(6, id.id);
            undomgr.EndCommand(57);

            undomgr.BeginCommand(58, "Command2");
            objstore.Replace(new Id <TestObject>(2), new TestObject(-9, "elvis", 9.1F));
            objstore.Remove(new Id <TestObject>(4));
            undomgr.EndCommand(58);

            TextWriter    writer    = new StringWriter();
            XmlTextWriter xmloutput = new XmlTextWriter(writer);

            xmloutput.Formatting = Formatting.Indented;
            xmloutput.Namespaces = false;

            xmloutput.WriteStartDocument();
            xmloutput.WriteStartElement("testobjects");

            objstore.Save(xmloutput);

            xmloutput.WriteEndElement();
            xmloutput.WriteEndDocument();
            xmloutput.Close();

            Assert.AreEqual(
                @"<?xml version=""1.0"" encoding=""utf-16""?>
<testobjects>
  <testobject id=""2"" x=""-9"" f=""9.1"">elvis</testobject>
  <testobject id=""5"" x=""5"" f=""5.4"">hello</testobject>
  <testobject id=""6"" x=""9"" f=""9.9"">bat</testobject>
</testobjects>",
                writer.ToString());
        }
Ejemplo n.º 4
0
        public void CloseFile()
        {
            // Make sure that Dispose really closes the underlying file.
            File.Copy(TestUtil.GetTestFile("xmlinput.xml"), TestUtil.GetTestFile("temp.xml"));
            XmlInput xmlinput = new XmlInput(TestUtil.GetTestFile("temp.xml"));

            xmlinput.CheckElement("rootsym");
            xmlinput.Dispose();
            File.Delete(TestUtil.GetTestFile("temp.xml"));
        }
Ejemplo n.º 5
0
        public void TextReader()
        {
            string input =
                @"<?xml version=""1.0"" encoding=""utf-8""?>
<rootsym>
	<!-- This is a comment -->
	<fiddle x=""foo"" y=""1"" z=""false""></fiddle>
</rootsym>";

            XmlInput xmlinput = new XmlInput(new StringReader(input), "teststring");

            xmlinput.CheckElement("rootsym");
            xmlinput.Read();
            xmlinput.CheckElement("fiddle");
            Assert.AreEqual(1, xmlinput.GetAttributeInt("y"));
            Assert.AreEqual(false, xmlinput.GetAttributeBool("z"));
            Assert.AreEqual("foo", xmlinput.GetAttributeString("x"));
            xmlinput.Skip();
        }
Ejemplo n.º 6
0
        public void Read1()
        {
            XmlInput xmlinput = new XmlInput(TestUtil.GetTestFile("xmlinput.xml"));
            xmlinput.CheckElement("rootsym");

            try {
                xmlinput.CheckElement("fiddle");
                Assert.Fail("expect exception");
            }
            catch (Exception e) {
                Assert.IsTrue(e is XmlFileFormatException);
            }

            xmlinput.Read();
            xmlinput.CheckElement("fiddle");
            Assert.AreEqual(1, xmlinput.GetAttributeInt("y"));
            Assert.AreEqual(false, xmlinput.GetAttributeBool("z"));
            Assert.AreEqual("foo", xmlinput.GetAttributeString("x"));

            xmlinput.Skip();

            xmlinput.CheckElement("faddle");
            Assert.AreEqual(3.4F, xmlinput.GetAttributeFloat("x"));
            try {
                Assert.AreEqual("", xmlinput.GetAttributeString("q"));
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is XmlFileFormatException);
            }

            int i = 0;
            bool first = true;
            while (xmlinput.FindSubElement(first, "hello", "zung", "zang", "zing")) {
                ++i;
                first = false;

                switch (xmlinput.Name) {
                    case "zing":
                        Assert.AreEqual(5, xmlinput.GetAttributeInt("why", 5));
                        Assert.AreEqual(true, xmlinput.GetAttributeBool("why", true));
                        Assert.AreEqual(5.7F, xmlinput.GetAttributeFloat("why", 5.7F));
                        Assert.AreEqual("Whazzzle", xmlinput.GetAttributeString("why", "Whazzzle"));
                        Assert.AreEqual("This is the zing", xmlinput.GetContentString());
                        break;

                    case "zang":
                        Assert.AreEqual(true, xmlinput.GetAttributeBool("r"));
                        try {
                            Assert.AreEqual(true, xmlinput.GetAttributeBool("z"));
                            Assert.Fail("should throw");
                        }
                        catch (Exception) {
                        }

                        xmlinput.Skip();
                        break;

                    case "zung":
                        xmlinput.Read();
                        xmlinput.CheckElement("diddle");
                        xmlinput.Skip();
                        xmlinput.Skip();
                        break;
                    default:
                        Assert.Fail("shouldn't get here");
                        break;
                }
            }

            Assert.AreEqual(4, i);
        }
Ejemplo n.º 7
0
        public void TextReader()
        {
            string input =
            @"<?xml version=""1.0"" encoding=""utf-8""?>
            <rootsym>
            <!-- This is a comment -->
            <fiddle x=""foo"" y=""1"" z=""false""></fiddle>
            </rootsym>";

            XmlInput xmlinput = new XmlInput(new StringReader(input), "teststring");

            xmlinput.CheckElement("rootsym");
            xmlinput.Read();
            xmlinput.CheckElement("fiddle");
            Assert.AreEqual(1, xmlinput.GetAttributeInt("y"));
            Assert.AreEqual(false, xmlinput.GetAttributeBool("z"));
            Assert.AreEqual("foo", xmlinput.GetAttributeString("x"));
            xmlinput.Skip();
        }
Ejemplo n.º 8
0
        public void LoadModifySave()
        {
            Id<TestObject> id;
            UndoMgr undomgr = new UndoMgr(5);
            ObjectStore<TestObject> objstore = new ObjectStore<TestObject>(undomgr);

            string xmlText =
            @"<?xml version=""1.0"" encoding=""utf-16""?>
            <testobjects>
              <testobject id=""2"" x=""8"" f=""9.4"">goodbye</testobject>
              <testobject id=""4"" x=""11"" f=""1.4"">foo</testobject>
            </testobjects>";

            XmlInput xmlinput = new XmlInput(new StringReader(xmlText), "testfile");

            xmlinput.CheckElement("testobjects");
            xmlinput.Read();

            objstore.Load(xmlinput);
            xmlinput.Dispose();

            undomgr.BeginCommand(57, "Command1");
            id = objstore.Add(new TestObject(5, "hello", 5.4F));
            Assert.AreEqual(5, id.id);
            id = objstore.Add(new TestObject(9, "bat", 9.9F));
            Assert.AreEqual(6, id.id);
            undomgr.EndCommand(57);

            undomgr.BeginCommand(58, "Command2");
            objstore.Replace(new Id<TestObject>(2), new TestObject(-9, "elvis", 9.1F));
            objstore.Remove(new Id<TestObject>(4));
            undomgr.EndCommand(58);

            TextWriter writer = new StringWriter();
            XmlTextWriter xmloutput = new XmlTextWriter(writer);
            xmloutput.Formatting = Formatting.Indented;
            xmloutput.Namespaces = false;

            xmloutput.WriteStartDocument();
            xmloutput.WriteStartElement("testobjects");

            objstore.Save(xmloutput);

            xmloutput.WriteEndElement();
            xmloutput.WriteEndDocument();
            xmloutput.Close();

            Assert.AreEqual(
            @"<?xml version=""1.0"" encoding=""utf-16""?>
            <testobjects>
              <testobject id=""2"" x=""-9"" f=""9.1"">elvis</testobject>
              <testobject id=""5"" x=""5"" f=""5.4"">hello</testobject>
              <testobject id=""6"" x=""9"" f=""9.9"">bat</testobject>
            </testobjects>",
            writer.ToString());
        }
Ejemplo n.º 9
0
        public void Load()
        {
            UndoMgr undomgr = new UndoMgr(5);
            ObjectStore<TestObject> objstore = new ObjectStore<TestObject>(undomgr);

            string xmlText =
            @"<?xml version=""1.0"" encoding=""utf-16""?>
            <testobjects>
              <testobject id=""2"" x=""8"" f=""9.4"">goodbye</testobject>
              <testobject id=""3"" x=""9"" f=""9.9"">bat</testobject>
              <testobject id=""4"" x=""11"" f=""1.4"">foo</testobject>
            </testobjects>";

            XmlInput xmlinput = new XmlInput(new StringReader(xmlText), "testfile");

            xmlinput.CheckElement("testobjects");
            xmlinput.Read();

            objstore.Load(xmlinput);
            xmlinput.Dispose();

            TestUtil.TestEnumerableAnyOrder(objstore.AllPairs,
                                        new KeyValuePair<Id<TestObject>, TestObject>[] {
                                        new KeyValuePair<Id<TestObject>, TestObject>(new Id<TestObject>(3), new TestObject(9, "bat", 9.9F)),
                                        new KeyValuePair<Id<TestObject>, TestObject>(new Id<TestObject>(4), new TestObject(11, "foo", 1.4F)),
                                        new KeyValuePair<Id<TestObject>, TestObject>(new Id<TestObject>(2), new TestObject(8, "goodbye", 9.4F)) });
        }
Ejemplo n.º 10
0
        public void Read1()
        {
            XmlInput xmlinput = new XmlInput(TestUtil.GetTestFile("xmlinput.xml"));

            xmlinput.CheckElement("rootsym");

            try {
                xmlinput.CheckElement("fiddle");
                Assert.Fail("expect exception");
            }
            catch (Exception e) {
                Assert.IsTrue(e is XmlFileFormatException);
            }

            xmlinput.Read();
            xmlinput.CheckElement("fiddle");
            Assert.AreEqual(1, xmlinput.GetAttributeInt("y"));
            Assert.AreEqual(false, xmlinput.GetAttributeBool("z"));
            Assert.AreEqual("foo", xmlinput.GetAttributeString("x"));

            xmlinput.Skip();

            xmlinput.CheckElement("faddle");
            Assert.AreEqual(3.4F, xmlinput.GetAttributeFloat("x"));
            try {
                Assert.AreEqual("", xmlinput.GetAttributeString("q"));
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is XmlFileFormatException);
            }

            int  i     = 0;
            bool first = true;

            while (xmlinput.FindSubElement(first, "hello", "zung", "zang", "zing"))
            {
                ++i;
                first = false;

                switch (xmlinput.Name)
                {
                case "zing":
                    Assert.AreEqual(5, xmlinput.GetAttributeInt("why", 5));
                    Assert.AreEqual(true, xmlinput.GetAttributeBool("why", true));
                    Assert.AreEqual(5.7F, xmlinput.GetAttributeFloat("why", 5.7F));
                    Assert.AreEqual("Whazzzle", xmlinput.GetAttributeString("why", "Whazzzle"));
                    Assert.AreEqual("This is the zing", xmlinput.GetContentString());
                    break;

                case "zang":
                    Assert.AreEqual(true, xmlinput.GetAttributeBool("r"));
                    try {
                        Assert.AreEqual(true, xmlinput.GetAttributeBool("z"));
                        Assert.Fail("should throw");
                    }
                    catch (Exception) {
                    }

                    xmlinput.Skip();
                    break;

                case "zung":
                    xmlinput.Read();
                    xmlinput.CheckElement("diddle");
                    xmlinput.Skip();
                    xmlinput.Skip();
                    break;

                default:
                    Assert.Fail("shouldn't get here");
                    break;
                }
            }

            Assert.AreEqual(4, i);
        }