public void TestByteVector()
        {
            const string templateXml = "<template name='bvt'>" +
                                       "  <byteVector name='data'>" +
                                       "    <length name='dataLength'/>" +
                                       "    <tail/>" +
                                       "  </byteVector>" +
                                       "</template>";
            IMessageTemplateLoader loader = new XmlMessageTemplateLoader();
            MessageTemplate bvt = loader.Load(new MemoryStream(Encoding.ASCII.GetBytes(templateXml)))[0];

            AssertScalarField(bvt, 1, FastType.ByteVector, "data", Operator.Tail);
        }
        public void TestDeltas()
        {
            var templateLoader = new XmlMessageTemplateLoader { LoadTemplateIdFromAuxId = true };

            using (var stream = File.OpenRead("CME/templates.xml"))
            {
                templateLoader.Load(stream);
            }

            using (var stream = File.OpenRead("CME/messages.fast"))
            {
                var mis = new MessageInputStream(stream, templateLoader.TemplateRegistry);
                Message md = mis.ReadMessage();
                Assert.AreEqual(-5025.0, md.GetSequence("MDEntries")[0].GetDouble("NetChgPrevDay"), .1);
            }
        }
        public void TestDynamicTemplateReference()
        {
            const string template1Xml = "<template name='t1'>" +
                                        "  <string name='string'/>" +
                                        "</template>";
            const string template2Xml = "<template name='t2'>" +
                                        "  <uInt32 name='quantity'/>" +
                                        "  <templateRef/>" +
                                        "  <decimal name='price'/>" +
                                        "</template>";

            IMessageTemplateLoader loader = new XmlMessageTemplateLoader();
            loader.Load(new MemoryStream(Encoding.ASCII.GetBytes(template1Xml)));
            MessageTemplate[] templates = loader.Load(new MemoryStream(Encoding.ASCII.GetBytes(template2Xml)));

            Assert.AreEqual(4, templates[0].FieldCount);
            AssertScalarField(templates[0], 1, FastType.U32, "quantity", Operator.None);
            AssertScalarField(templates[0], 3, FastType.Decimal, "price", Operator.None);
            Assert.True(templates[0].GetField(2) is DynamicTemplateReference);
        }
        public void RunAllTests()
        {
            Console.WriteLine("Initializing...");

            // throw args.Exception;
            ValidationEventHandler errorHandler =
                (sender, args) => Console.WriteLine("Error {0}\n{1}", args.Message, args.Exception);

            Type thisType = MethodBase.GetCurrentMethod().DeclaringType;
            Stream schema = thisType.Assembly.GetManifestResourceStream(thisType, "TestSchema.xsd");
            Assert.IsNotNull(schema, "Unable to load XSD from the resources");

            var settings = new XmlReaderSettings();
            settings.Schemas.Add(XmlSchema.Read(schema, errorHandler));

            //
            // BUG!  TODO! change to schema-validated input
            //
            settings.ValidationType = ValidationType.None; // ValidationType.Schema;
            settings.ValidationEventHandler += errorHandler;

            foreach (string xmlFile in Directory.GetFiles(XmlTestFilesDir, "*.xml"))
            {
                Console.WriteLine("Processing {0}...", xmlFile);

                XmlReader reader = XmlReader.Create(xmlFile, settings);
                try
                {
                    var xml = new XmlDocument();
                    xml.Load(reader);

                    foreach (XmlElement test in xml.GetElementsByTagName("test"))
                    {
                        XmlElement desc = test.GetElement("desc");

                        string actionStr = test.GetAttribute("action");
                        if (string.IsNullOrEmpty(actionStr)) actionStr = "all";
                        var action = (Actions) Enum.Parse(typeof (Actions), actionStr, true);

                        Console.WriteLine("  Test {0}{1}{2}", test.GetAttribute("name"),
                                          desc == null || string.IsNullOrEmpty(desc.Value) ? "" : " - " + desc.Value,
                                          action == Actions.All ? "" : " (" + action + " only)");

                        var tmpl = new XmlMessageTemplateLoader();

                        XmlElement templFile = test.GetElement("templatesfile");
                        tmpl.LoadTemplateIdFromAuxId = true;
                        if (templFile != null)
                        {
                            using (FileStream stream = File.OpenRead(templFile.GetAttribute("path")))
                                tmpl.Load(stream);
                        }
                        else
                        {
                            XmlElement templXml = test.GetElement("templates", FastConstants.TemplateDefinition11);
                            if (templXml == null)
                                throw new InvalidOperationException("Expected <templates> element not found");
                            tmpl.Load(templXml);
                        }

                        XmlElement binFile = test.GetElement("binfile");
                        MessageInputStream mis;
                        byte[] binData;
                        if (binFile != null)
                        {
                            using (FileStream stream = File.OpenRead(binFile.GetAttribute("path")))
                            {
                                mis = new MessageInputStream(stream, tmpl.TemplateRegistry);
                                binData = File.ReadAllBytes(binFile.GetAttribute("path"));
                            }
                        }
                        else
                        {
                            XmlElement binstr = test.GetElement("binstr");
                            if (binstr != null)
                            {
                                string binStream = binstr.InnerText.Trim();
                                binData = ByteUtil.ConvertBitStringToFastByteArray(binStream);
                            }
                            else
                            {
                                XmlElement bin64 = test.GetElement("bin64");
                                binData = Convert.FromBase64String(bin64.Value);
                            }

                            mis = new MessageInputStream(new MemoryStream(binData), tmpl.TemplateRegistry);

                        }

                        //
                        // TODO - read the messages and check them against the tests
                        //

                        //
                        // TODO: check against "action" variable -- if All or Encode/Decode
                        //

                        Message msg;
                        XmlElement target = test.GetElement("data");
                        XmlNode msgString = target.FirstChild;
                        var msgStream = new MemoryStream();
                        var mout = new MessageOutputStream(msgStream, tmpl.TemplateRegistry);

                        while ((msg = mis.ReadMessage()) != null)
                        {
                            //TODO: Introduce FIX decoding/encoding Scheme);
                            if (msgString != null)
                            {
                                mout.WriteMessage(msg);
                                Assert.AreEqual(msgString.InnerText.Trim(), msg.ToString());
                                msgString = msgString.NextSibling;

                            }
                            else
                            {
                                Console.WriteLine(msg);
                            }
                        }
                        if (action == Actions.All)
                        {
                            //Verifying Encoding
                            if (binData != null)
                            {
                                byte[] outStrem = msgStream.ToArray();
                                if (outStrem.Length > 0)
                                {
                                    for (int i = 0; i < binData.Length; i++)
                                    {
                                        Assert.AreEqual(binData[i], outStrem[i]);
                                    }
                                }
                                else
                                {
                                    Console.WriteLine("WARNING: No data emitted during encoding.");
                                }

                            }
                        }
                    }
                }
                finally
                {
                    reader.Close();
                }
            }
        }
        public void TestLoadMdIncrementalRefreshTemplate()
        {
            MessageTemplate messageTemplate;
            using (var stream = File.OpenRead("FPL/mdIncrementalRefreshTemplate.xml"))
                messageTemplate = new XmlMessageTemplateLoader().Load(stream)[0];

            Assert.AreEqual("MDIncrementalRefresh", messageTemplate.TypeReference.Name);
            Assert.AreEqual("MDRefreshSample", messageTemplate.Name);
            Assert.AreEqual(10, messageTemplate.FieldCount);

            /********************************** TEMPLATE FIELDS **********************************/
            int index = 0;
            AssertScalarField(messageTemplate, index++, FastType.U32, "templateId", Operator.Copy);
            AssertScalarField(messageTemplate, index++, FastType.Ascii, "8", Operator.Constant);
            AssertScalarField(messageTemplate, index++, FastType.U32, "9", Operator.Constant);
            AssertScalarField(messageTemplate, index++, FastType.Ascii, "35", Operator.Constant);
            AssertScalarField(messageTemplate, index++, FastType.Ascii, "49", Operator.Constant);
            AssertScalarField(messageTemplate, index++, FastType.U32, "34", Operator.Increment);
            AssertScalarField(messageTemplate, index++, FastType.Ascii, "52", Operator.Delta);
            AssertScalarField(messageTemplate, index++, FastType.U32, "75", Operator.Copy);

            /************************************* SEQUENCE **************************************/
            AssertSequence(messageTemplate, index, 17);

            var sequence = (Sequence) messageTemplate.GetField(index++);
            Assert.AreEqual("MDEntries", sequence.TypeReference.Name);
            AssertSequenceLengthField(sequence, "268", FastType.U32, Operator.None);

            /********************************** SEQUENCE FIELDS **********************************/
            int seqIndex = 0;
            AssertScalarField(sequence, seqIndex++, FastType.Decimal, "270", Operator.Delta);
            AssertScalarField(sequence, seqIndex++, FastType.I32, "271", Operator.Delta);
            AssertScalarField(sequence, seqIndex++, FastType.U32, "273", Operator.Delta);
            AssertOptionalScalarField(sequence, seqIndex++, FastType.U32, "346", Operator.None);
            AssertScalarField(sequence, seqIndex++, FastType.U32, "1023", Operator.Increment);
            AssertScalarField(sequence, seqIndex++, FastType.Ascii, "279", Operator.Copy);
            AssertScalarField(sequence, seqIndex++, FastType.Ascii, "269", Operator.Copy);
            AssertScalarField(sequence, seqIndex++, FastType.Ascii, "107", Operator.Copy);
            AssertScalarField(sequence, seqIndex++, FastType.Ascii, "48", Operator.Delta);
            AssertScalarField(sequence, seqIndex++, FastType.Ascii, "276", Operator.Copy);
            AssertScalarField(sequence, seqIndex++, FastType.Ascii, "274", Operator.Copy);
            AssertScalarField(sequence, seqIndex++, FastType.Decimal, "451", Operator.Copy);
            AssertScalarField(sequence, seqIndex++, FastType.Ascii, "277", Operator.Default);
            AssertOptionalScalarField(sequence, seqIndex++, FastType.U32, "1020", Operator.None);
            AssertScalarField(sequence, seqIndex++, FastType.I32, "537", Operator.Default);
            AssertScalarField(sequence, seqIndex++, FastType.Ascii, "1024", Operator.Default);
            AssertScalarField(sequence, seqIndex, FastType.Ascii, "336", Operator.Default);

            AssertScalarField(messageTemplate, index, FastType.Ascii, "10", Operator.None);
        }
 public void TestStaticTemplateReference()
 {
     const string templateXml = "<templates>" +
                                "  <template name='t1'>" +
                                "    <string name='string'/>" +
                                "  </template>" +
                                "  <template name='t2'>" +
                                "    <uInt32 name='quantity'/>" +
                                "    <templateRef name='t1'/>" +
                                "    <decimal name='price'/>" +
                                "  </template>" +
                                "</templates>";
     MessageTemplate[] templates =
         new XmlMessageTemplateLoader().Load(new MemoryStream(Encoding.ASCII.GetBytes(templateXml)));
     AssertEquals(4, templates[1].FieldCount);
     AssertScalarField(templates[1], 1, FastType.U32, "quantity", Operator.None);
     AssertScalarField(templates[1], 2, FastType.Ascii, "string", Operator.None);
     AssertScalarField(templates[1], 3, FastType.Decimal, "price", Operator.None);
 }
        public void TestReferencedTemplateInOtherLoader()
        {
            const string template1Xml = "<template name='t1'>" +
                                        "  <string name='string'/>" +
                                        "</template>";
            const string template2Xml = "<template name='t2'>" +
                                        "  <uInt32 name='quantity'/>" +
                                        "  <templateRef name='t1'/>" +
                                        "  <decimal name='price'/>" +
                                        "</template>";

            IMessageTemplateLoader loader1 = new XmlMessageTemplateLoader();
            IMessageTemplateLoader loader2 = new XmlMessageTemplateLoader {TemplateRegistry = loader1.TemplateRegistry};

            loader1.Load(new MemoryStream(Encoding.ASCII.GetBytes(template1Xml)));
            MessageTemplate[] templates = loader2.Load(new MemoryStream(Encoding.ASCII.GetBytes(template2Xml)));
            Assert.AreEqual(4, templates[0].FieldCount);
            AssertScalarField(templates[0], 1, FastType.U32, "quantity", Operator.None);
            AssertScalarField(templates[0], 2, FastType.Ascii, "string", Operator.None);
            AssertScalarField(templates[0], 3, FastType.Decimal, "price", Operator.None);
        }
 public void TestNullDocument()
 {
     var loader = new XmlMessageTemplateLoader {ErrorHandler = ErrorHandlerFields.Null};
     Assert.AreEqual(0, loader.Load((Stream) null).Length);
 }
        public void TestLoadTemplateWithUnicodeString()
        {
            const string templateXml = "<templates xmlns='http://www.fixprotocol.org/ns/template-definition'" +
                                       "	ns='http://www.fixprotocol.org/ns/templates/sample'>" +
                                       "	<template name='SampleTemplate'>" +
                                       "		<string name='name' charset='unicode' presence='mandatory'><copy/></string>" +
                                       "		<string name='id' charset='unicode' presence='optional'><copy/></string>" +
                                       "		<string name='location' charset='ascii' presence='mandatory'><copy/></string>" +
                                       "		<string name='id2' charset='ascii' presence='optional'><copy/></string>" +
                                       "	</template>" + "</templates>";

            IMessageTemplateLoader loader = new XmlMessageTemplateLoader();
            MessageTemplate[] templates = loader.Load(new MemoryStream(
                                                          Encoding.ASCII.GetBytes(templateXml)));
            MessageTemplate messageTemplate = templates[0];

            Scalar name = messageTemplate.GetScalar("name");
            Scalar id = messageTemplate.GetScalar("id");
            Scalar location = messageTemplate.GetScalar("location");
            Scalar id2 = messageTemplate.GetScalar("id2");

            Assert.False(name.IsOptional);
            Assert.True(id.IsOptional);
            Assert.False(location.IsOptional);
            Assert.True(id2.IsOptional);

            Assert.AreEqual(TypeCodec.Unicode, name.TypeCodec);
            Assert.AreEqual(TypeCodec.NullableUnicode, id.TypeCodec);
            Assert.AreEqual(TypeCodec.Ascii, location.TypeCodec);
            Assert.AreEqual(TypeCodec.NullableAscii, id2.TypeCodec);
        }
        public void TestLoadTemplateWithKey()
        {
            const string templateXml = "<templates xmlns='http://www.fixprotocol.org/ns/template-definition'" +
                                       "	ns='http://www.fixprotocol.org/ns/templates/sample'>" +
                                       "	<template name='SampleTemplate'>" +
                                       "		<uInt32 name='value'><copy key='integer'/></uInt32>" +
                                       "	</template>" +
                                       "</templates>";

            IMessageTemplateLoader loader = new XmlMessageTemplateLoader();
            MessageTemplate[] templates = loader.Load(new MemoryStream(
                                                          Encoding.ASCII.GetBytes(templateXml)));
            MessageTemplate messageTemplate = templates[0];

            Scalar scalar = messageTemplate.GetScalar("value");
            Assert.AreEqual(new QName("integer"), scalar.Key);
        }
        public void TestLoadTemplateThatContainsGroups()
        {
            const string templateXml = "<templates xmlns='http://www.fixprotocol.org/ns/template-definition'" +
                                       "	ns='http://www.fixprotocol.org/ns/templates/sample'>" +
                                       "	<template name='SampleTemplate'>" +
                                       "		<group name='guy'><string name='First Name'/><string name='Last Name'/></group>" +
                                       "	</template>" + "</templates>";

            IMessageTemplateLoader loader = new XmlMessageTemplateLoader();
            MessageTemplate[] templates = loader.Load(new MemoryStream(
                                                          Encoding.ASCII.GetBytes(templateXml)));
            MessageTemplate messageTemplate = templates[0];

            Assert.AreEqual("SampleTemplate", messageTemplate.Name);
            Assert.AreEqual(2, messageTemplate.FieldCount);

            AssertGroup(messageTemplate, 1, "guy");
        }
 public void TestLoadTemplateThatContainsDecimalWithTwinOperators()
 {
     const string templateXml = "<templates xmlns='http://www.fixprotocol.org/ns/template-definition'" +
                                "	ns='http://www.fixprotocol.org/ns/templates/sample'>" +
                                "	<template name='SampleTemplate'>" +
                                "		<decimal name='bid'><mantissa><delta/></mantissa><exponent><copy value='-2'/></exponent></decimal>" +
                                "		<decimal name='ask'><mantissa><delta/></mantissa></decimal>" +
                                "		<decimal name='high'><exponent><copy/></exponent></decimal>" +
                                "		<decimal name='low'><mantissa><delta value='10'/></mantissa><exponent><copy value='-2'/></exponent></decimal>" +
                                "		<decimal name='open'><copy/></decimal>" +
                                "		<decimal name='close'><copy/></decimal>" +
                                "	</template>" +
                                "</templates>";
     IMessageTemplateLoader loader = new XmlMessageTemplateLoader();
     MessageTemplate[] templates = loader.Load(new MemoryStream(Encoding.ASCII.GetBytes(templateXml)));
     MessageTemplate messageTemplate = templates[0];
     Assert.AreEqual("SampleTemplate", messageTemplate.Name);
     Assert.AreEqual(7, messageTemplate.FieldCount);
     AssertComposedScalarField(messageTemplate, 1, FastType.Decimal, "bid", Operator.Copy, Int(-2),
                               Operator.Delta, ScalarValue.Undefined);
     AssertComposedScalarField(messageTemplate, 2, FastType.Decimal, "ask", Operator.None, ScalarValue.Undefined,
                               Operator.Delta, ScalarValue.Undefined);
     AssertComposedScalarField(messageTemplate, 3, FastType.Decimal, "high", Operator.Copy, ScalarValue.Undefined,
                               Operator.None, ScalarValue.Undefined);
     AssertComposedScalarField(messageTemplate, 4, FastType.Decimal, "low", Operator.Copy, Int(-2),
                               Operator.Delta, Int(10));
     AssertScalarField(messageTemplate, 5, FastType.Decimal, "open", Operator.Copy);
     AssertScalarField(messageTemplate, 6, FastType.Decimal, "close", Operator.Copy);
 }
 protected static MessageTemplate Template(String templateXml)
 {
     MessageTemplate[] templates = new XmlMessageTemplateLoader().Load
         (new MemoryStream(Encoding.ASCII.GetBytes(templateXml)));
     return templates[0];
 }