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 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 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 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); }