Example #1
0
        public void TestObjectMethods()
        {
            //Equals
            SimpleElement se1 = new SimpleElement("name");
            SimpleElement se2 = new SimpleElement("name", "value");
            SimpleValue   sv  = new SimpleValue("value");

            Assert.IsFalse(se1.Equals(sv));
            Assert.IsFalse(se1.Equals(se2));
            se2.SetString(se1.GetString());
            Assert.IsTrue(se1.Equals(se2));
            se1.AddElement("child1");
            se1.AddElement("child2");
            Assert.IsFalse(se1.Equals(se2));
            se2.AddElement("child1");
            se2.AddElement("child2");
            Assert.IsTrue(se1.Equals(se2));
            se1.AddAttribute("attr1");
            Assert.IsFalse(se1.Equals(se2));
            se2.AddAttribute("attr1");
            Assert.IsTrue(se1.Equals(se2));

            //GetHashCode
            Assert.AreEqual(XmlHelper.HashElement(se1), se1.GetHashCode());

            //Clone
            SimpleElement se1c = (SimpleElement)se1.Clone();

            Assert.AreEqual(se1c, se1);
            Assert.AreNotSame(se1, se1c);
            se1.Comment = "comment";
            Assert.AreNotEqual(se1c, se1);
        }
Example #2
0
        public void TestElementMethods()
        {
            //GetElement
            SimpleElement se = new SimpleElement();

            Assert.IsNull(se.GetElement("child1"));
            IXmlElement child1 = se.AddElement("child1");

            Assert.IsNotNull(se.GetElement("child1"));

            //FindElement
            Assert.IsNull(se.FindElement("/child2"));
            Assert.AreEqual(child1, se.FindElement("/child1"));

            //EnsureElement
            Assert.AreEqual(se.ElementList.Count, 1);
            Assert.IsNotNull(se.EnsureElement("/child2"));
            IXmlElement child2 = se.GetElement("child2");

            Assert.IsTrue(child2.IsMutable);
            Assert.AreEqual(se.ElementList.Count, 2); //element is added

            //GetSafeElement
            Assert.AreEqual(se.ElementList.Count, 2);
            Assert.AreEqual(se, se.GetSafeElement(""));
            IXmlElement child3 = se.GetSafeElement("/child3");

            Assert.IsNotNull(child3);
            Assert.IsFalse(child3.IsMutable);
            Assert.AreEqual(se.ElementList.Count, 2); //element is not added
            IXmlElement child4 = se.GetSafeElement("/child3/../child4");

            Assert.IsNotNull(child4);
            Exception e = null;

            try
            {
                se.GetSafeElement(null);
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(ArgumentNullException), e);
            e = null;
            try
            {
                se.GetSafeElement("..");
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(ArgumentException), e);
        }
Example #3
0
        /// <summary>
        /// Parse and configure network filter information.
        /// </summary>
        private void ParseFilterConfig()
        {
            IDictionary filterMap = new HashDictionary();

            var config = Config.FindElement("cluster-config/filters");

            if (config != null)
            {
                for (var filters = config.GetElements("filter"); filters.MoveNext();)
                {
                    var xmlFilter  = (IXmlElement)filters.Current;
                    var name       = xmlFilter.GetSafeElement("filter-name").GetString();
                    var className  = xmlFilter.GetSafeElement("filter-class").GetString();
                    var initParams = xmlFilter.GetElement("init-params");

                    var xmlFilterClass = new SimpleElement("instance");
                    xmlFilterClass.AddElement("class-name").SetString(className);
                    if (initParams != null)
                    {
                        var xmlParams = xmlFilterClass.AddElement("init-params");
                        for (var init = initParams.GetElements("init-param"); init.MoveNext();)
                        {
                            xmlParams.ElementList.Add(init.Current);
                        }
                    }
                    var factory = (IWrapperStreamFactory)XmlHelper.CreateInstance(
                        xmlFilterClass, null, typeof(IWrapperStreamFactory));

                    filterMap.Add(name, factory);
                }
            }

            // add well known "gzip" filter
            if (filterMap["gzip"] == null)
            {
                filterMap["gzip"] = new CompressionFilter();
            }

            FilterMap = filterMap;
        }
Example #4
0
        public void TestElementsEnumerator()
        {
            SimpleElement root       = new SimpleElement();
            IEnumerator   child1Enum = root.GetElements("child1");
            IXmlElement   el1        = null;

            Exception e = null;

            try
            {
                el1 = (IXmlElement)child1Enum.Current;  //without MoveNext()
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNull(el1);
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(InvalidOperationException), e);

            el1 = root.AddElement("child1");
            root.AddElement("child2");
            root.AddElement("child3");
            IXmlElement el2 = root.AddElement("child1");

            root.AddElement("child2");

            child1Enum = root.GetElements("child1");

            Assert.IsTrue(child1Enum.MoveNext());
            Assert.AreEqual(child1Enum.Current, el1);
            Assert.IsTrue(child1Enum.MoveNext());
            Assert.IsTrue(child1Enum.MoveNext()); //will do nothing
            Assert.AreEqual(child1Enum.Current, el2);
            Assert.IsFalse(child1Enum.MoveNext());
            child1Enum.Reset();
            Assert.IsTrue(child1Enum.MoveNext());
        }
Example #5
0
        public void TestWriteMethods()
        {
            //WriteValue
            TextWriter    writer = new StringWriter();
            SimpleElement se     = new SimpleElement("root", "some\ntext");

            se.WriteValue(writer, true);
            Assert.AreEqual(se.GetString(), writer.ToString());

            writer = new StringWriter();
            IndentingWriter indentedWriter = new IndentingWriter(writer, 2);

            se.WriteValue(indentedWriter, true);
            Assert.AreEqual(se.GetString(), writer.ToString());

            //WriteXml
            se     = new SimpleElement("root");
            writer = new StringWriter();
            se.WriteXml(writer, true);
            Assert.AreEqual("<root/>", writer.ToString());

            se.AddAttribute("attr1").SetString("value1");
            writer = new StringWriter();
            se.WriteXml(writer, true);
            Assert.AreEqual("<root attr1='value1'/>", writer.ToString());

            se.Comment = "comment text";
            se.SetString("content");
            writer = new StringWriter();
            se.WriteXml(writer, true);
            Assert.AreEqual("<root attr1='value1'><!--" + writer.NewLine + "comment text" + writer.NewLine + "-->content</root>", writer.ToString());
            writer = new StringWriter();
            se.WriteXml(writer, false);
            Assert.AreEqual("<root attr1='value1'><!--comment text-->content</root>", writer.ToString());

            se.AddElement("child").SetString("child content");
            writer = new StringWriter();
            se.WriteXml(writer, true);
            string result = "<root attr1='value1'>" + writer.NewLine + "  <!--" + writer.NewLine + "  comment text" +
                            writer.NewLine + "  -->" + writer.NewLine + "content" + writer.NewLine + "  <child>child content</child>" +
                            writer.NewLine + "</root>";

            Assert.AreEqual(result, writer.ToString());
            Assert.AreEqual(result, se.ToString());

            writer = new StringWriter();
            se.WriteXml(writer, false);
            result = "<root attr1='value1'><!--comment text-->content<child>child content</child></root>";
            Assert.AreEqual(result, writer.ToString());
        }
Example #6
0
        public void TestSerialization()
        {
            ConfigurablePofContext ctx = new ConfigurablePofContext("assembly://Coherence.Tests/Tangosol.Resources/s4hc-test-config.xml");

            Assert.IsNotNull(ctx);

            SimpleElement se = new SimpleElement("name", 5);

            se.Comment = "comment";
            se.AddElement("child1");
            se.AddElement("child2");
            se.AddAttribute("attr1");
            se.AddAttribute("attr2");
            se.AddAttribute("attr3");

            Stream stream = new MemoryStream();

            ctx.Serialize(new DataWriter(stream), se);

            stream.Position = 0;
            SimpleElement sed = (SimpleElement)ctx.Deserialize(new DataReader(stream));

            Assert.AreEqual(se, sed);
        }
        public void TestObjectHelpers()
        {
            Exception e = null;

            //EqualsValue
            SimpleValue sv1 = new SimpleValue("");
            SimpleValue sv2 = new SimpleValue(Binary.NO_BINARY);

            Assert.IsTrue(XmlHelper.EqualsValue(sv1, sv2)); //both are empty
            sv1.SetString("True");
            Assert.IsFalse(XmlHelper.EqualsValue(sv1, sv2));
            sv2.SetBoolean(true);
            Assert.IsTrue(XmlHelper.EqualsValue(sv1, sv2));
            try
            {
                XmlHelper.EqualsValue(null, sv2);
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(ArgumentException), e);
            sv2.SetString(null);
            try
            {
                XmlHelper.EqualsValue(sv1, sv2);
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(ArgumentException), e);
            sv2.SetBoolean(true);

            //EqualsElement
            SimpleElement se1 = new SimpleElement();
            SimpleElement se2 = new SimpleElement("name", "value");

            se2.Comment = "comment";
            Assert.IsFalse(XmlHelper.EqualsElement(se1, se2));
            se1.Name = se2.Name;
            Assert.IsFalse(XmlHelper.EqualsElement(se1, se2));
            se1.SetString(se2.GetString());
            Assert.IsFalse(XmlHelper.EqualsElement(se1, se2));
            se1.Comment = se2.Comment;
            Assert.IsTrue(XmlHelper.EqualsElement(se1, se2));
            se1.AddElement("el1");
            Assert.IsFalse(XmlHelper.EqualsElement(se1, se2));
            se2.AddElement("el1");
            Assert.IsTrue(XmlHelper.EqualsElement(se1, se2));
            se1.AddAttribute("attr1");
            Assert.IsFalse(XmlHelper.EqualsElement(se1, se2));
            se2.AddAttribute("attr1");
            Assert.IsTrue(XmlHelper.EqualsElement(se1, se2));
            try
            {
                XmlHelper.EqualsElement(null, se2);
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(ArgumentException), e);

            //HashValue
            SimpleValue sv = new SimpleValue();

            Assert.IsNull(sv.Value);
            Assert.AreEqual(0, XmlHelper.HashValue(sv));
            sv.SetString("test");
            Assert.AreEqual("test".GetHashCode(), XmlHelper.HashValue(sv));
            sv.SetDecimal(decimal.MinusOne);
            Assert.AreEqual(XmlHelper.Convert(decimal.MinusOne, XmlValueType.String).GetHashCode(),
                            XmlHelper.HashValue(sv));
            Assert.AreEqual(XmlHelper.HashValue(sv1), XmlHelper.HashValue(sv2));

            //HashElement
            Assert.AreEqual(XmlHelper.HashElement(se1), XmlHelper.HashElement(se2));
            IXmlElement el1 = se1.GetElement("el1");

            el1.SetString("new value");
            Assert.AreNotEqual(XmlHelper.HashElement(se1), XmlHelper.HashElement(se2));
        }
        public void TestCreateInstance()
        {
            // no params
            IXmlElement xmlClass = new SimpleElement();

            xmlClass.AddElement("class-name").SetString("Tangosol.Run.Xml.XmlHelperTests+Dummy, Coherence.Tests");
            Assert.IsFalse(XmlHelper.IsInstanceConfigEmpty(xmlClass));
            object o = XmlHelper.CreateInstance(xmlClass, null);

            Assert.IsNotNull(o);
            Assert.IsInstanceOf(typeof(Dummy), o);
            Dummy d = o as Dummy;

            Assert.IsNotNull(d);
            Assert.AreEqual(d.FloatValue, 0.0);

            IXmlElement xmlClassFactory = new SimpleElement();

            xmlClassFactory.AddElement("class-factory-name").SetString("Tangosol.Run.Xml.XmlHelperTests+Dummy, Coherence.Tests");
            xmlClassFactory.AddElement("method-name").SetString("CreateDummyInstance");
            o = XmlHelper.CreateInstance(xmlClassFactory, null);
            Assert.IsNotNull(o);
            Assert.IsInstanceOf(typeof(Dummy), o);
            d = o as Dummy;
            Assert.IsNotNull(d);
            Assert.AreEqual(d.FloatValue, 0.0);

            xmlClassFactory.GetElement("class-factory-name").SetString(null);
            Exception e = null;

            try
            {
                XmlHelper.CreateInstance(xmlClassFactory, null);
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(ArgumentException), e);

            //with params
            IXmlElement initParams = new SimpleElement("init-params");
            IXmlElement initParam  = initParams.AddElement("init-param");

            initParam.AddElement("param-type").SetString("long");
            initParam.AddElement("param-value").SetString("101");
            initParam = initParams.AddElement("init-param");
            initParam.AddElement("param-type").SetString("date");
            initParam.AddElement("param-value").SetString("2/16/1992 12:15:12");
            initParam = initParams.AddElement("init-param");
            initParam.AddElement("param-type").SetString("float");
            initParam.AddElement("param-value").SetString("0.00000001");
            initParam = initParams.AddElement("init-param");
            initParam.AddElement("param-type").SetString("string");
            initParam.AddElement("param-value").SetString("test");

            xmlClass.ElementList.Add(initParams);
            o = XmlHelper.CreateInstance(xmlClass, null);
            Assert.IsNotNull(o);
            Assert.IsInstanceOf(typeof(Dummy), o);
            d = o as Dummy;
            Assert.IsNotNull(d);
            Assert.AreEqual(d.StringValue, "test");

            xmlClassFactory.GetElement("class-factory-name").SetString("Tangosol.Run.Xml.XmlHelperTests+Dummy, Coherence.Tests");
            xmlClassFactory.GetElement("method-name").SetString("CreateDummyInstanceWithParams");
            xmlClassFactory.ElementList.Add(initParams);
            o = XmlHelper.CreateInstance(xmlClassFactory, null);
            Assert.IsNotNull(o);
            Assert.IsInstanceOf(typeof(Dummy), o);
            d = o as Dummy;
            Assert.IsNotNull(d);
            Assert.AreEqual(d.StringValue, "test");

            //configurable
            IXmlElement config = new SimpleElement("config");

            XmlHelper.TransformInitParams(config, initParams);

            xmlClass.GetElement("class-name").SetString("Tangosol.Run.Xml.XmlHelperTests+ConfigurableDummy, Coherence.Tests");
            o = XmlHelper.CreateInstance(xmlClass, null);
            Assert.IsNotNull(o);
            Assert.IsInstanceOf(typeof(ConfigurableDummy), o);
            ConfigurableDummy cd = o as ConfigurableDummy;

            Assert.IsNotNull(cd);
            Assert.IsNotNull(cd.StringValue);
            Assert.IsNotNull(cd.Config);
            Assert.IsTrue(cd.Config.Equals(config));

            xmlClassFactory.GetElement("class-factory-name").SetString("Tangosol.Run.Xml.XmlHelperTests+ConfigurableDummy, Coherence.Tests");
            xmlClassFactory.GetElement("method-name").SetString("CreateConfigurableDummyInstance");
            xmlClassFactory.ElementList.Add(initParams);
            o = XmlHelper.CreateInstance(xmlClassFactory, null);
            Assert.IsNotNull(o);
            Assert.IsInstanceOf(typeof(ConfigurableDummy), o);
            cd = o as ConfigurableDummy;
            Assert.IsNotNull(cd);
            Assert.IsNotNull(cd.StringValue);
            Assert.IsNotNull(cd.Config);
            Assert.IsTrue(cd.Config.Equals(config));

            e = null;
            xmlClassFactory.GetElement("class-factory-name").SetString("Tangosol.Run.Xml.XmlHelperTests+ConfigurableDummy1, Coherence.Tests");
            try
            {
                XmlHelper.CreateInstance(xmlClassFactory, null);
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);

            IXmlDocument xml = XmlHelper.LoadXml("assembly://Coherence.Tests/Tangosol.Resources/s4hc-test-util.xml");
            IXmlElement  el  = xml.FindElement("user-type-list/user-type/serializer");

            o = XmlHelper.CreateInstance(el, null);
            Assert.IsNotNull(o);
            Assert.IsInstanceOf(typeof(Dummy), o);
            d = o as Dummy;
            Assert.IsNotNull(d);
            Assert.AreEqual(d.FloatValue, (float)0.00000001);
            Assert.AreEqual(d.DateTimeValue, new DateTime(1992, 2, 2, 12, 15, 12));
            Assert.AreEqual(d.LongValue, 101L);
            Assert.AreEqual(d.StringValue, "{test}");
        }
        public void TestParseInitParams()
        {
            IXmlElement root = new SimpleElement("init-params");

            IXmlElement paramEl = root.AddElement("init-param");

            paramEl.AddElement("param-type").SetString("string");
            paramEl.AddElement("param-value").SetString("test");

            paramEl = root.AddElement("init-param");
            paramEl.AddElement("param-type").SetString("bool");
            paramEl.AddElement("param-value").SetString("true");

            paramEl = root.AddElement("init-param");
            paramEl.AddElement("param-type").SetString("int");
            paramEl.AddElement("param-value").SetString("152");

            paramEl = root.AddElement("init-param");
            paramEl.AddElement("param-type").SetString("long");
            paramEl.AddElement("param-value").SetString("90089");

            paramEl = root.AddElement("init-param");
            paramEl.AddElement("param-type").SetString("float");
            paramEl.AddElement("param-value").SetString("3.5");

            paramEl = root.AddElement("init-param");
            paramEl.AddElement("param-type").SetString("double");
            paramEl.AddElement("param-value").SetString("3.521");

            paramEl = root.AddElement("init-param");
            paramEl.AddElement("param-type").SetString("decimal");
            paramEl.AddElement("param-value").SetString("0");

            paramEl = root.AddElement("init-param");
            paramEl.AddElement("param-type").SetString("date");
            paramEl.AddElement("param-value").SetString("2/16/1992 12:15:12");

            paramEl = root.AddElement("init-param");
            paramEl.AddElement("param-type").SetString("xml");
            IXmlElement value = paramEl.AddElement("param-value");

            value.AddElement("child").AddAttribute("attr").SetString("attrValue");

            object[] result = XmlHelper.ParseInitParams(root);
            Assert.IsNotNull(result);
            Assert.AreEqual(result.Length, 9);
            Assert.IsTrue(result[0] is string);
            Assert.IsTrue(result[1] is bool);
            Assert.IsTrue(result[2] is int);
            Assert.IsTrue(result[3] is long);
            Assert.IsTrue(result[4] is float);
            Assert.IsTrue(result[5] is double);
            Assert.IsTrue(result[6] is decimal);
            Assert.IsTrue(result[7] is DateTime);
            Assert.IsTrue(result[8] is IXmlElement);

            root    = new SimpleElement("init-params");
            paramEl = root.AddElement("init-param");
            paramEl.AddElement("param-type").SetString("System.String");
            paramEl.AddElement("param-value").SetString("test");

            paramEl = root.AddElement("init-param");
            paramEl.AddElement("param-type").SetString("System.Boolean");
            paramEl.AddElement("param-value").SetString("true");

            paramEl = root.AddElement("init-param");
            paramEl.AddElement("param-type").SetString("System.Int32");
            paramEl.AddElement("param-value").SetString("152");

            paramEl = root.AddElement("init-param");
            paramEl.AddElement("param-type").SetString("System.Int64");
            paramEl.AddElement("param-value").SetString("90089");

            paramEl = root.AddElement("init-param");
            paramEl.AddElement("param-type").SetString("System.Single");
            paramEl.AddElement("param-value").SetString("3.5");

            paramEl = root.AddElement("init-param");
            paramEl.AddElement("param-type").SetString("System.Double");
            paramEl.AddElement("param-value").SetString("3.521");

            paramEl = root.AddElement("init-param");
            paramEl.AddElement("param-type").SetString("System.Decimal");
            paramEl.AddElement("param-value").SetString("0");

            paramEl = root.AddElement("init-param");
            paramEl.AddElement("param-type").SetString("System.DateTime");
            paramEl.AddElement("param-value").SetString("2/16/1992 12:15:12");

            paramEl = root.AddElement("init-param");
            paramEl.AddElement("param-type").SetString("Tangosol.Run.Xml.IXmlElement");
            value = paramEl.AddElement("param-value");
            value.AddElement("child").AddAttribute("attr").SetString("attrValue");

            object[] result2 = XmlHelper.ParseInitParams(root);
            Assert.IsNotNull(result2);
            Assert.AreEqual(result2.Length, 9);
            Assert.IsTrue(result2[0] is string);
            Assert.IsTrue(result2[1] is bool);
            Assert.IsTrue(result2[2] is int);
            Assert.IsTrue(result2[3] is long);
            Assert.IsTrue(result2[4] is float);
            Assert.IsTrue(result2[5] is double);
            Assert.IsTrue(result2[6] is decimal);
            Assert.IsTrue(result2[7] is DateTime);
            Assert.IsTrue(result2[8] is IXmlElement);

            root    = new SimpleElement("init-params");
            paramEl = root.AddElement("init-param");
            paramEl.AddElement("param-type").SetString("Tangosol.Temperature");
            paramEl.AddElement("param-value").SetString("test");
            result2 = XmlHelper.ParseInitParams(root);
            Assert.AreEqual(result2.Length, 1);
            Assert.AreEqual(result2[0], paramEl);

            root    = new SimpleElement("init-params");
            paramEl = root.AddElement("init-param");
            paramEl.AddElement("param-type");
            paramEl.AddElement("param-value").SetString("test");
            Exception e = null;

            try
            {
                XmlHelper.ParseInitParams(root);
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);

            IXmlDocument xml = XmlHelper.LoadXml("assembly://Coherence.Tests/Tangosol.Resources/s4hc-test-util.xml");

            IXmlElement el = xml.FindElement("user-type-list/user-type/serializer/init-params");

            object[] o = XmlHelper.ParseInitParams(el);
            Assert.AreEqual(101, o[0]);
            Assert.IsInstanceOf(typeof(DateTime), o[1]);
            Assert.AreEqual(new DateTime(1992, 2, 2, 12, 15, 12), o[1]);
            Assert.AreEqual((float)0.00000001, o[2]);

            object[] o1 = XmlHelper.ParseInitParams(el, new SimpleResolver());
            Assert.AreEqual(o1.Length, 4);
            Assert.AreEqual("test", o1[3]);

            object[] o2 = XmlHelper.ParseInitParams(el, new UnresolvedResolver());
            Assert.AreEqual(o2.Length, 4);
            Assert.AreEqual("{test}", o2[3]);
        }
        public void TestElementHelpers()
        {
            Exception e = null;

            //GetAbsolutePath
            SimpleElement root = new SimpleElement();

            Assert.AreEqual(XmlHelper.GetAbsolutePath(root), "/");
            root.Name = "root";
            Assert.AreEqual(XmlHelper.GetAbsolutePath(root), "/root");
            SimpleElement se = new SimpleElement("child");

            se.Parent = root;
            Assert.AreEqual(XmlHelper.GetAbsolutePath(se), "/root/child");
            try
            {
                XmlHelper.GetAbsolutePath(null);
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(ArgumentNullException), e);

            //IsEmpty
            se = new SimpleElement();
            Assert.IsTrue(XmlHelper.IsEmpty(se));
            se.SetString("value");
            Assert.IsFalse(XmlHelper.IsEmpty(se));
            se.SetString(null);
            Assert.IsTrue(XmlHelper.IsEmpty(se));
            se.AddElement("name");
            Assert.IsFalse(XmlHelper.IsEmpty(se));
            se.ElementList.Clear();
            Assert.IsTrue(XmlHelper.IsEmpty(se));
            se.AddAttribute("attr");
            Assert.IsFalse(XmlHelper.IsEmpty(se));

            //GetElement
            se = new SimpleElement();
            Assert.IsNull(XmlHelper.GetElement(se, "child1"));
            se.AddElement("child1");
            se.AddElement("child2");
            Assert.IsNotNull(XmlHelper.GetElement(se, "child1"));
            e = null;
            try
            {
                XmlHelper.GetElement(se, "&invalid");
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(ArgumentException), e);

            //FindElement
            root = new SimpleElement("root");
            IXmlElement child1 = root.AddElement("child1");
            IXmlElement child2 = child1.AddElement("child2");
            string      path   = "/child1";

            Assert.AreEqual(child1, XmlHelper.FindElement(root, path));
            Assert.AreEqual(child1, XmlHelper.FindElement(child1, path));
            path = "child2";
            Assert.IsNull(XmlHelper.FindElement(root, path));
            Assert.AreEqual(child2, XmlHelper.FindElement(child1, path));
            path = "../child1";
            Assert.IsNull(XmlHelper.FindElement(child2, path));
            Assert.AreEqual(child1, XmlHelper.FindElement(child1, path));
            e = null;
            try
            {
                XmlHelper.FindElement(root, null);
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(ArgumentException), e);
            e = null;
            try
            {
                XmlHelper.FindElement(root, path);
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(ArgumentException), e);

            //FindElement with value
            root   = new SimpleElement();
            child1 = root.AddElement("child1");
            child1.SetString("value1");
            root.AddElement("child1").SetString("value2");
            child2 = child1.AddElement("child2");
            path   = "/child1";
            Assert.AreEqual(child1, XmlHelper.FindElement(root, path, "value1"));
            Assert.IsNull(XmlHelper.FindElement(root, path, "valueX"));
            path = "child2";
            Assert.IsNull(XmlHelper.FindElement(root, path, null));
            Assert.AreEqual(child2, XmlHelper.FindElement(child1, path, null));
            path = "../child1";
            Assert.IsNull(XmlHelper.FindElement(child2, path, null));
            Assert.AreNotEqual(child1, XmlHelper.FindElement(child1, path, "value2"));
            path = "child2/../child2";
            Assert.IsNull(XmlHelper.FindElement(child1, path, 5));
            Assert.AreEqual(child2, XmlHelper.FindElement(child1, path, null));
            e = null;
            try
            {
                XmlHelper.FindElement(null, path, null);
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(ArgumentException), e);

            //EnsureElement
            root   = new SimpleElement("root");
            child1 = root.AddElement("child1");
            child1.AddElement("child2");
            path = "/child1";
            Assert.AreEqual(root.ElementList.Count, 1);
            Assert.AreEqual(child1, XmlHelper.EnsureElement(root, path));
            path = "/child3";
            Assert.IsNotNull(XmlHelper.EnsureElement(root, path));
            Assert.AreEqual(root.ElementList.Count, 2);
            path = "/child3/../child4";
            Assert.IsNotNull(XmlHelper.EnsureElement(root, path));
            Assert.AreEqual(root.ElementList.Count, 3);
            e = null;
            try
            {
                XmlHelper.EnsureElement(null, path);
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(ArgumentException), e);
            e = null;
            try
            {
                XmlHelper.EnsureElement(root, "../child1");
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(ArgumentException), e);

            //AddElements
            root = new SimpleElement();
            Assert.AreEqual(root.ElementList.Count, 0);
            IList elements = new ArrayList();

            elements.Add(new SimpleElement("el1"));
            elements.Add(new SimpleElement("el2"));
            elements.Add(new SimpleElement("el3"));
            XmlHelper.AddElements(root, elements.GetEnumerator());
            Assert.AreEqual(root.ElementList.Count, 3);

            //RemoveElement
            root = new SimpleElement();
            Assert.AreEqual(root.ElementList.Count, 0);
            int result = XmlHelper.RemoveElement(root, "child");

            Assert.AreEqual(result, 0);
            root.AddElement("child");
            root.AddElement("child");
            root.AddElement("child2");
            Assert.AreEqual(root.ElementList.Count, 3);
            result = XmlHelper.RemoveElement(root, "child");
            Assert.AreEqual(result, 2);
            Assert.AreEqual(root.ElementList.Count, 1);

            //ReplaceElement
            root = new SimpleElement("root");
            root.AddElement("child1");
            root.AddElement("child2");
            Assert.AreEqual(root.ElementList.Count, 2);
            Assert.IsNull(root.GetElement("child1").Value);
            IXmlElement replaceEl = new SimpleElement("child1", "value");
            bool        replaced  = XmlHelper.ReplaceElement(root, replaceEl);

            Assert.IsTrue(replaced);
            Assert.AreEqual(root.GetElement("child1").GetString(), replaceEl.GetString());
            Assert.AreEqual(root.ElementList.Count, 2);

            replaceEl = new SimpleElement("child3");
            replaced  = XmlHelper.ReplaceElement(root, replaceEl);
            Assert.IsFalse(replaced);
            Assert.AreEqual(root.ElementList.Count, 3);
        }
Example #11
0
        public void TestElementList()
        {
            SimpleElement parent = new SimpleElement("parent", "value");

            Assert.IsTrue(parent.IsMutable); //so, element list is mutable as well

            IList elements = parent.ElementList;

            Assert.IsNotNull(elements);
            Assert.AreEqual(elements.Count, 0);

            //adding something that is not IXmlElement will fail
            Exception e = null;

            try
            {
                elements.Add("something");
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(InvalidCastException), e);
            Assert.AreEqual(elements.Count, 0);

            IXmlElement child0 = parent.AddElement("child0");
            IXmlElement child1 = new SimpleElement("child1");
            IXmlElement child2 = new SimpleElement("child2");
            IXmlElement child3 = new SimpleElement("child3");
            IXmlElement child4 = new SimpleElement("child5");
            IXmlElement child5 = new SimpleElement("child6");

            Assert.IsNotNull(child0);
            Assert.AreEqual(child0.Parent, parent);
            Assert.AreEqual(elements.Count, 1);
            Assert.AreEqual(elements[0], child0);

            child1.Comment = "comment";
            child1.AddAttribute("attr").SetString("attrValue");
            child1.Parent = child0;
            elements.Add(child1);
            Assert.AreEqual(elements.Count, 2);
            Assert.AreNotEqual(child1.Parent, ((SimpleElement)elements[1]).Parent);
            Assert.AreEqual(parent, ((SimpleElement)elements[1]).Parent);
            Assert.AreEqual(elements[1], child1);
            Assert.AreNotSame(elements[1], child1);

            Assert.IsInstanceOf(typeof(ArrayList), elements);
            ArrayList elementList = (ArrayList)elements;

            IList l = new ArrayList();

            l.Add(child2);
            elementList.AddRange(l);
            Assert.AreEqual(elementList.Count, 3);
            Assert.AreEqual(child2.Parent, parent);
            Assert.AreEqual(elementList[2], child2);
            elementList.Insert(3, child3);
            Assert.AreEqual(elementList.Count, 4);
            Assert.AreEqual(child3.Parent, parent);
            Assert.AreEqual(elementList[3], child3);

            l = new ArrayList();
            l.Add(child4);
            l.Add(child5);
            elementList.InsertRange(4, l);
            Assert.AreEqual(elementList.Count, 6);
            Assert.AreEqual(elementList[4], child4);
            Assert.AreEqual(elementList[5], child5);

            Assert.IsTrue(elementList.Contains(child5));
            elementList.Remove(child5);
            Assert.AreEqual(elementList.Count, 5);
            elementList.RemoveRange(3, 2);
            Assert.AreEqual(elementList.Count, 3);

            Assert.IsFalse(elementList.Contains(child4));
            Assert.IsFalse(elementList.Contains(child5));
            elementList.SetRange(1, l);
            Assert.AreEqual(elementList.Count, 3);
            Assert.AreEqual(elementList[0], child0);
            Assert.AreEqual(elementList[1], child4);
            Assert.AreEqual(elementList[2], child5);
            elementList[0] = child3;

            Assert.AreEqual(elementList[0], child3); //3,4,5
            elementList.Reverse();
            Assert.AreEqual(elementList[2], child3); //5,4,3
            Assert.AreEqual(elementList[0], child5); //5,4,3
            elementList.Reverse(0, 2);
            Assert.AreEqual(elementList[2], child3); //4,5,3
            Assert.AreEqual(elementList[0], child4); //4,5,3

            elementList.Sort(new SimpleElementComparer());
            Assert.AreEqual(elementList[0], child3); //3,4,5
            Assert.AreEqual(elementList[2], child5); //3,4,5
            elementList.Reverse();
            elementList.Sort(0, 2, new SimpleElementComparer());
            Assert.AreEqual(elementList[0], child4); //4,5,3
            Assert.AreEqual(elementList[2], child3); //4,5,3


            elementList.Clear();
            Assert.AreEqual(elementList.Count, 0);

            e = null;
            try
            {
                IXmlElement immutable = parent.GetSafeElement("immutable");
                immutable.AddElement("child");
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(InvalidOperationException), e);

            SimpleElement parent2 = new SimpleElement();

            parent.AddElement("child1");
            Assert.IsFalse(parent.ElementList.Equals(parent2.ElementList));
            Assert.IsFalse(parent.ElementList.Equals("test"));
            parent2.AddElement("child1");
            Assert.IsTrue(parent.ElementList.Equals(parent2.ElementList));
            parent.AddElement("child2");
            parent2.AddElement("child2");
            Assert.AreEqual(parent.ElementList, parent2.ElementList);
            parent2.GetElement("child2").AddAttribute("attr");
            Assert.AreNotEqual(parent.ElementList, parent2.ElementList);
        }