Example #1
0
        public void TestIsValidNode()
        {
            EcellXmlReader reader = new EcellXmlReader();
            Assert.IsNotNull(reader, "Constructor of type, EcellXmlReader failed to create instance.");
            Type type = reader.GetType();
            MethodInfo methodInfo = type.GetMethod("IsValidNode", BindingFlags.NonPublic | BindingFlags.Instance);

            bool expected = true;
            XmlDocument eml = new XmlDocument();
            eml.Load(TestConstant.Model_RBC);
            XmlNode node = eml.ChildNodes[0];
            bool result = (bool)methodInfo.Invoke(reader, new object[] { node });

            Assert.AreEqual(expected, result, "IsValidNode method returned unexpected value.");

            node = eml.CreateComment("");
            expected = false;
            result = (bool)methodInfo.Invoke(reader, new object[] { node });
            Assert.AreEqual(expected, result, "IsValidNode method returned unexpected value.");

            result = (bool)methodInfo.Invoke(reader, new object[] { null });
            Assert.AreEqual(expected, result, "IsValidNode method returned unexpected value.");
        }
Example #2
0
        public void TestParseEcellValue()
        {
            EcellXmlReader reader = new EcellXmlReader();
            Assert.IsNotNull(reader, "Constructor of type, EcellXmlReader failed to create instance.");
            Type type = reader.GetType();
            MethodInfo readerMethod = type.GetMethod("ParseEcellValue", BindingFlags.NonPublic | BindingFlags.Instance);

            XmlDocument eml = new XmlDocument();

            eml.LoadXml("<property name=\"Value\"><value>1E-15</value></property>");
            XmlNode node = eml.DocumentElement;
            EcellValue value = (EcellValue)readerMethod.Invoke(reader, new object[] { node });
            double expected = 1e-15;
            Assert.IsTrue(value.IsDouble, "IsValidNode method returned unexpected value.");
            Assert.AreEqual(expected, (double)value.Value, "IsValidNode method returned unexpected value.");

            eml.LoadXml("<property name=\"Value\"><value>+∞</value></property>");
            node = eml.DocumentElement;
            value = (EcellValue)readerMethod.Invoke(reader, new object[] { node });
            expected = double.PositiveInfinity;
            Assert.IsTrue(value.IsDouble, "IsValidNode method returned unexpected value.");
            Assert.AreEqual(expected, (double)value.Value, "IsValidNode method returned unexpected value.");

            eml.LoadXml("<property name=\"Value\"><value>-∞</value></property>");
            node = eml.DocumentElement;
            value = (EcellValue)readerMethod.Invoke(reader, new object[] { node });
            expected = double.NegativeInfinity;
            Assert.IsTrue(value.IsDouble, "IsValidNode method returned unexpected value.");
            Assert.AreEqual(expected, (double)value.Value, "IsValidNode method returned unexpected value.");

            eml.LoadXml("<property name=\"Value\"><value>-11</value></property>");
            node = eml.DocumentElement;
            value = (EcellValue)readerMethod.Invoke(reader, new object[] { node });
            Assert.IsTrue(value.IsInt, "IsValidNode method returned unexpected value.");
            Assert.AreEqual(-11, (int)value.Value, "IsValidNode method returned unexpected value.");

            eml.LoadXml("<property name=\"Value\"><value>Test</value></property>");
            node = eml.DocumentElement;
            value = (EcellValue)readerMethod.Invoke(reader, new object[] { node });
            Assert.IsTrue(value.IsString, "IsValidNode method returned unexpected value.");
            Assert.AreEqual("Test", (string)value.Value, "IsValidNode method returned unexpected value.");

            eml.LoadXml("<property name=\"VariableReferenceList\"><value><value>S0</value><value>Variable:/cell:LacI</value><value>-1</value><value>0</value></value></property>");
            node = eml.DocumentElement;
            value = (EcellValue)readerMethod.Invoke(reader, new object[] { node });
            Assert.IsTrue(value.IsList, "IsValidNode method returned unexpected value.");
            List<object> list = (List<object>)value.Value;
            Assert.IsNotEmpty(list, "IsValidNode method returned unexpected value.");

            List<EcellReference> erList = EcellReference.ConvertFromEcellValue(value);
            EcellReference er = erList[0];
            Assert.AreEqual("S0", er.Name, "Name is unexpected value.");
            Assert.AreEqual("Variable:/cell:LacI", er.FullID, "FullID is unexpected value.");
            Assert.AreEqual(-1, er.Coefficient, "Coefficient is unexpected value.");
            Assert.AreEqual(0, er.IsAccessor, "IsAccessor is unexpected value.");

            try
            {
                eml.LoadXml("<property name=\"Value\" />");
                node = eml.DocumentElement;
                value = (EcellValue)readerMethod.Invoke(reader, new object[] { node });

                Assert.Fail("IsValidNode method failed to throw exception.");
            }
            catch (Exception)
            {
            }

            try
            {
                eml.LoadXml("<property name=\"Value\"><Value/></property>");
                node = eml.DocumentElement;
                value = (EcellValue)readerMethod.Invoke(reader, new object[] { node });

                Assert.Fail("IsValidNode method failed to throw exception.");
            }
            catch (Exception)
            {
            }

            try
            {
                eml.LoadXml("<property name=\"VariableReferenceList\"><hoge><value>S0</value><value>Variable:/cell:LacI</value><value>-1</value><value>0</value></hoge></property>");
                node = eml.DocumentElement;
                value = (EcellValue)readerMethod.Invoke(reader, new object[] { node });

                Assert.Fail("IsValidNode method failed to throw exception.");
            }
            catch (Exception)
            {
            }
        }
Example #3
0
        public void TestWriteValueElements()
        {
            EcellXmlReader reader = new EcellXmlReader();
            Assert.IsNotNull(reader, "Constructor of type, EcellXmlReader failed to create instance.");
            Type type = reader.GetType();
            MethodInfo readerMethod = type.GetMethod("ParseEcellValue", BindingFlags.NonPublic | BindingFlags.Instance);

            XmlDocument eml = new XmlDocument();
            eml.LoadXml("<property name=\"VariableReferenceList\"><value><value>S0</value><value>Variable:/cell:LacI</value><value>-1</value><value>0</value></value></property>");
            XmlNode node = eml.DocumentElement;
            EcellValue value = (EcellValue)readerMethod.Invoke(reader, new object[] { node });

            string filename = TestConstant.TestDirectory + "test.xml";
            XmlTextWriter tx = new XmlTextWriter(filename, Encoding.UTF8);
            EcellXmlWriter writer = new EcellXmlWriter(tx);
            Assert.IsNotNull(writer, "Constructor of type, EcellXmlWriter failed to create instance.");
            type = writer.GetType();
            MethodInfo writerMethod = type.GetMethod("WriteValueElements", BindingFlags.NonPublic | BindingFlags.Instance);

            // int
            tx.WriteStartElement("Value");
            writerMethod.Invoke(writer, new object[] { new EcellValue(10) });

            // double
            writerMethod.Invoke(writer, new object[] { new EcellValue(0.01) });

            // double max
            writerMethod.Invoke(writer, new object[] { new EcellValue(double.MaxValue) });

            // double infinity
            writerMethod.Invoke(writer, new object[] { new EcellValue(double.PositiveInfinity) });

            // string
            writerMethod.Invoke(writer, new object[] { new EcellValue("Text") });
            tx.WriteEndElement();
            tx.Close();

            eml.LoadXml("<value><value>10</value><value>0.01</value><value>1.7976931348623157E+308</value><value>INF</value><value>Text</value></value>");
            node = eml.DocumentElement;

            eml.Load(filename);
            XmlNode nodeCopy = eml.DocumentElement;
            Assert.AreEqual(node.InnerText, nodeCopy.InnerText, "EcellXmlWriter method returned unexpected value.");

            // List
            tx = new XmlTextWriter(filename, Encoding.UTF8);
            writer = new EcellXmlWriter(tx);
            writerMethod.Invoke(writer, new object[] { value });
            tx.Close();
            writer = null;

            eml.LoadXml("<value><value>S0</value><value>Variable:/cell:LacI</value><value>-1</value><value>0</value></value>");
            node = eml.DocumentElement;

            eml.Load(filename);
            nodeCopy = eml.DocumentElement;
            Assert.AreEqual(node.InnerText, nodeCopy.InnerText, "EcellXmlWriter method returned unexpected value.");

            File.Delete(filename);
        }