Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit f378cf30 authored by Markus Quaritsch's avatar Markus Quaritsch
Browse files

extend assertion in testcase for # gearshifts

parent d7b431da
No related branches found
No related tags found
No related merge requests found
...@@ -29,430 +29,430 @@ ...@@ -29,430 +29,430 @@
* Martin Rexeis, rexeis@ivt.tugraz.at, IVT, Graz University of Technology * Martin Rexeis, rexeis@ivt.tugraz.at, IVT, Graz University of Technology
*/ */
using System; using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Xml; using System.Xml;
using System.Xml.Linq; using System.Xml.Linq;
using System.Xml.Schema; using System.Xml.Schema;
using NUnit.Framework; using NUnit.Framework;
using TUGraz.VectoCore.Utils; using TUGraz.VectoCore.Utils;
using TUGraz.VectoHashing; using TUGraz.VectoHashing;
using VectoHashingTest.Utils; using VectoHashingTest.Utils;
using Assert = NUnit.Framework.Assert; using Assert = NUnit.Framework.Assert;
namespace VectoHashingTest namespace VectoHashingTest
{ {
[TestFixture] [TestFixture]
public class VectoHashTest public class VectoHashTest
{ {
public const string ReferenceXMLEngine = @"Testdata\XML\Reference\vecto_engine-sample.xml"; public const string ReferenceXMLEngine = @"Testdata\XML\Reference\vecto_engine-sample.xml";
public const string ReferenceXMLVehicle = @"Testdata\XML\Reference\vecto_vehicle-sample_FULL.xml"; public const string ReferenceXMLVehicle = @"Testdata\XML\Reference\vecto_vehicle-sample_FULL.xml";
[TestCase] [TestCase]
public void TestComponentsEngineFile() public void TestComponentsEngineFile()
{ {
var h = VectoHash.Load(ReferenceXMLEngine); var h = VectoHash.Load(ReferenceXMLEngine);
var components = h.GetContainigComponents().ToList(); var components = h.GetContainigComponents().ToList();
Assert.AreEqual(1, components.Count); Assert.AreEqual(1, components.Count);
Assert.AreEqual(VectoComponents.Engine, components[0]); Assert.AreEqual(VectoComponents.Engine, components[0]);
} }
[TestCase] [TestCase]
public void TestComponentsVehicleFile() public void TestComponentsVehicleFile()
{ {
var h = VectoHash.Load(ReferenceXMLVehicle); var h = VectoHash.Load(ReferenceXMLVehicle);
var components = h.GetContainigComponents().ToList(); var components = h.GetContainigComponents().ToList();
Assert.AreEqual(10, components.Count); Assert.AreEqual(10, components.Count);
} }
[TestCase(ReferenceXMLEngine, VectoComponents.Engine, BasicHasingTests.HashEngineXML)] [TestCase(ReferenceXMLEngine, VectoComponents.Engine, BasicHasingTests.HashEngineXML)]
public void TestHashComputationSelected(string file, VectoComponents component, string expectedHash) public void TestHashComputationSelected(string file, VectoComponents component, string expectedHash)
{ {
var h = VectoHash.Load(file); var h = VectoHash.Load(file);
var hash = h.ComputeHash(component); var hash = h.ComputeHash(component);
Assert.AreEqual(expectedHash, hash); Assert.AreEqual(expectedHash, hash);
} }
[TestCase(ReferenceXMLVehicle, BasicHasingTests.HashVehicleXML), [TestCase(ReferenceXMLVehicle, BasicHasingTests.HashVehicleXML),
TestCase(ReferenceXMLEngine, BasicHasingTests.HashEngineXML)] TestCase(ReferenceXMLEngine, BasicHasingTests.HashEngineXML)]
public void TestHashComputation(string file, string expectedHash) public void TestHashComputation(string file, string expectedHash)
{ {
var h = VectoHash.Load(file); var h = VectoHash.Load(file);
var hash = h.ComputeHash(); var hash = h.ComputeHash();
Assert.AreEqual(expectedHash, hash); Assert.AreEqual(expectedHash, hash);
} }
[TestCase(ReferenceXMLEngine)] [TestCase(ReferenceXMLEngine)]
public void TestHashComputationInvalidComponent(string file) public void TestHashComputationInvalidComponent(string file)
{ {
var h = VectoHash.Load(file); var h = VectoHash.Load(file);
AssertHelper.Exception<Exception>(() => h.ComputeHash(VectoComponents.Gearbox), "Component Gearbox not found"); AssertHelper.Exception<Exception>(() => h.ComputeHash(VectoComponents.Gearbox), "Component Gearbox not found");
} }
[TestCase(ReferenceXMLEngine)] [TestCase(ReferenceXMLEngine)]
public void TestReadHashInvalidComponent(string file) public void TestReadHashInvalidComponent(string file)
{ {
var h = VectoHash.Load(file); var h = VectoHash.Load(file);
AssertHelper.Exception<Exception>(() => h.ReadHash(VectoComponents.Gearbox), "Component Gearbox not found"); AssertHelper.Exception<Exception>(() => h.ReadHash(VectoComponents.Gearbox), "Component Gearbox not found");
} }
[TestCase(ReferenceXMLVehicle, VectoComponents.Engine, "e0c253b643f7f8f09b963aca4a264d06fbfa599f"), [TestCase(ReferenceXMLVehicle, VectoComponents.Engine, "e0c253b643f7f8f09b963aca4a264d06fbfa599f"),
TestCase(ReferenceXMLVehicle, VectoComponents.Gearbox, "d14189366134120e08fa3f2c6e3328dd13c08a23")] TestCase(ReferenceXMLVehicle, VectoComponents.Gearbox, "d14189366134120e08fa3f2c6e3328dd13c08a23")]
public void TestReadHash(string file, VectoComponents component, string expectedHash) public void TestReadHash(string file, VectoComponents component, string expectedHash)
{ {
var h = VectoHash.Load(file); var h = VectoHash.Load(file);
var existingHash = h.ReadHash(component); var existingHash = h.ReadHash(component);
Assert.AreEqual(expectedHash, existingHash); Assert.AreEqual(expectedHash, existingHash);
} }
[TestCase(ReferenceXMLVehicle, VectoComponents.Tyre, 0, "5074334bb2c090c5e258e9a664f5d19689a3f13d"), [TestCase(ReferenceXMLVehicle, VectoComponents.Tyre, 0, "5074334bb2c090c5e258e9a664f5d19689a3f13d"),
TestCase(ReferenceXMLVehicle, VectoComponents.Tyre, 1, "6074334bb2c090c5e258e9a664f5d19689a3f13d")] TestCase(ReferenceXMLVehicle, VectoComponents.Tyre, 1, "6074334bb2c090c5e258e9a664f5d19689a3f13d")]
public void TestReadHashIdx(string file, VectoComponents component, int index, string expectedHash) public void TestReadHashIdx(string file, VectoComponents component, int index, string expectedHash)
{ {
var h = VectoHash.Load(file); var h = VectoHash.Load(file);
var existingHash = h.ReadHash(component, index); var existingHash = h.ReadHash(component, index);
Assert.AreEqual(expectedHash, existingHash); Assert.AreEqual(expectedHash, existingHash);
} }
[TestCase] [TestCase]
public void TestReadTyres1Index() public void TestReadTyres1Index()
{ {
var file = @"Testdata\XML\ToHash\vecto_vehicle-sample_3axle1.xml"; var file = @"Testdata\XML\ToHash\vecto_vehicle-sample_3axle1.xml";
var h = VectoHash.Load(file); var h = VectoHash.Load(file);
var expectedHash = new[] { var expectedHash = new[] {
"5074334bb2c090c5e258e9a664f5d19689a3f13d", "5074334bb2c090c5e258e9a664f5d19689a3f13d",
"6074334bb2c090c5e258e9a664f5d19689a3f13d", "6074334bb2c090c5e258e9a664f5d19689a3f13d",
"6074334bb2c090c5e258e9a664f5d19689a3f13d" "6074334bb2c090c5e258e9a664f5d19689a3f13d"
}; };
for (int i = 0; i < expectedHash.Length; i++) { for (int i = 0; i < expectedHash.Length; i++) {
var existingHash = h.ReadHash(VectoComponents.Tyre, i); var existingHash = h.ReadHash(VectoComponents.Tyre, i);
Assert.AreEqual(expectedHash[i], existingHash); Assert.AreEqual(expectedHash[i], existingHash);
} }
} }
[TestCase] [TestCase]
public void TestReadTyres2Index() public void TestReadTyres2Index()
{ {
var file = @"Testdata\XML\ToHash\vecto_vehicle-sample_3axle2.xml"; var file = @"Testdata\XML\ToHash\vecto_vehicle-sample_3axle2.xml";
var h = VectoHash.Load(file); var h = VectoHash.Load(file);
var expectedHash = new[] { var expectedHash = new[] {
"5074334bb2c090c5e258e9a664f5d19689a3f13d", "5074334bb2c090c5e258e9a664f5d19689a3f13d",
"5074334bb2c090c5e258e9a664f5d19689a3f13d", "5074334bb2c090c5e258e9a664f5d19689a3f13d",
"6074334bb2c090c5e258e9a664f5d19689a3f13d" "6074334bb2c090c5e258e9a664f5d19689a3f13d"
}; };
for (int i = 0; i < expectedHash.Length; i++) { for (int i = 0; i < expectedHash.Length; i++) {
var existingHash = h.ReadHash(VectoComponents.Tyre, i); var existingHash = h.ReadHash(VectoComponents.Tyre, i);
Assert.AreEqual(expectedHash[i], existingHash); Assert.AreEqual(expectedHash[i], existingHash);
} }
AssertHelper.Exception<Exception>(() => h.ReadHash(VectoComponents.Tyre, 3), AssertHelper.Exception<Exception>(() => h.ReadHash(VectoComponents.Tyre, 3),
"index exceeds number of components found! index: 3, #components: 3"); "index exceeds number of components found! index: 3, #components: 3");
} }
[TestCase] [TestCase]
public void TestComputeTyres1Index() public void TestComputeTyres1Index()
{ {
var file = @"Testdata\XML\ToHash\vecto_vehicle-sample_3axle1.xml"; var file = @"Testdata\XML\ToHash\vecto_vehicle-sample_3axle1.xml";
var h = VectoHash.Load(file); var h = VectoHash.Load(file);
var hash1 = h.ComputeHash(VectoComponents.Tyre, 1); var hash1 = h.ComputeHash(VectoComponents.Tyre, 1);
var hash2 = h.ComputeHash(VectoComponents.Tyre, 2); var hash2 = h.ComputeHash(VectoComponents.Tyre, 2);
Assert.AreEqual(hash1, hash2); Assert.AreEqual(hash1, hash2);
AssertHelper.Exception<Exception>(() => h.ComputeHash(VectoComponents.Tyre, 3), AssertHelper.Exception<Exception>(() => h.ComputeHash(VectoComponents.Tyre, 3),
"index exceeds number of components found! index: 3, #components: 3"); "index exceeds number of components found! index: 3, #components: 3");
} }
[TestCase] [TestCase]
public void TestComputeTyres2Index() public void TestComputeTyres2Index()
{ {
var file = @"Testdata\XML\ToHash\vecto_vehicle-sample_3axle2.xml"; var file = @"Testdata\XML\ToHash\vecto_vehicle-sample_3axle2.xml";
var h = VectoHash.Load(file); var h = VectoHash.Load(file);
var hash1 = h.ComputeHash(VectoComponents.Tyre, 0); var hash1 = h.ComputeHash(VectoComponents.Tyre, 0);
var hash2 = h.ComputeHash(VectoComponents.Tyre, 1); var hash2 = h.ComputeHash(VectoComponents.Tyre, 1);
Assert.AreEqual(hash1, hash2); Assert.AreEqual(hash1, hash2);
AssertHelper.Exception<Exception>(() => h.ComputeHash(VectoComponents.Tyre, 3), AssertHelper.Exception<Exception>(() => h.ComputeHash(VectoComponents.Tyre, 3),
"index exceeds number of components found! index: 3, #components: 3"); "index exceeds number of components found! index: 3, #components: 3");
} }
[TestCase("vecto_vehicle-sample_FULL_Comments.xml", BasicHasingTests.HashVehicleXML), [TestCase("vecto_vehicle-sample_FULL_Comments.xml", BasicHasingTests.HashVehicleXML),
TestCase("vecto_vehicle-sample_FULL_Entry_Order.xml", BasicHasingTests.HashVehicleXML), TestCase("vecto_vehicle-sample_FULL_Entry_Order.xml", BasicHasingTests.HashVehicleXML),
TestCase("vecto_vehicle-sample_FULL_Newlines_Linux_LF.xml", BasicHasingTests.HashVehicleXML), TestCase("vecto_vehicle-sample_FULL_Newlines_Linux_LF.xml", BasicHasingTests.HashVehicleXML),
TestCase("vecto_vehicle-sample_FULL_Newlines_Mac_CR.xml", BasicHasingTests.HashVehicleXML), TestCase("vecto_vehicle-sample_FULL_Newlines_Mac_CR.xml", BasicHasingTests.HashVehicleXML),
TestCase("vecto_vehicle-sample_FULL_Newlines_Windows_CRLF.xml", BasicHasingTests.HashVehicleXML), TestCase("vecto_vehicle-sample_FULL_Newlines_Windows_CRLF.xml", BasicHasingTests.HashVehicleXML),
TestCase("vecto_engine-sample Encoding ISO 8859-15.xml", BasicHasingTests.HashEngineXML), TestCase("vecto_engine-sample Encoding ISO 8859-15.xml", BasicHasingTests.HashEngineXML),
TestCase("vecto_engine-sample Encoding UTF-8 BOM.xml", BasicHasingTests.HashEngineXML), TestCase("vecto_engine-sample Encoding UTF-8 BOM.xml", BasicHasingTests.HashEngineXML),
TestCase("vecto_engine-sample Encoding UTF-8.xml", BasicHasingTests.HashEngineXML), TestCase("vecto_engine-sample Encoding UTF-8.xml", BasicHasingTests.HashEngineXML),
TestCase("vecto_engine-sample Encoding UTF-16 BE BOM.xml", BasicHasingTests.HashEngineXML), TestCase("vecto_engine-sample Encoding UTF-16 BE BOM.xml", BasicHasingTests.HashEngineXML),
TestCase("vecto_engine-sample Encoding UTF-16 LE.xml", BasicHasingTests.HashEngineXML), TestCase("vecto_engine-sample Encoding UTF-16 LE.xml", BasicHasingTests.HashEngineXML),
TestCase("vecto_engine-sample Encoding windows-1292.xml", BasicHasingTests.HashEngineXML), TestCase("vecto_engine-sample Encoding windows-1292.xml", BasicHasingTests.HashEngineXML),
TestCase("vecto_engine-sample_Whitespaces.xml", BasicHasingTests.HashEngineXML), TestCase("vecto_engine-sample_Whitespaces.xml", BasicHasingTests.HashEngineXML),
] ]
public void TestHashComputationVariations(string file, string expectedHash) public void TestHashComputationVariations(string file, string expectedHash)
{ {
var h = VectoHash.Load(@"Testdata\XML\Variations\" + file); var h = VectoHash.Load(@"Testdata\XML\Variations\" + file);
var hash = h.ComputeHash(); var hash = h.ComputeHash();
Assert.AreEqual(expectedHash, hash); Assert.AreEqual(expectedHash, hash);
} }
[TestCase(@"Testdata\XML\Validation\vecto_engine_valid.xml"), [TestCase(@"Testdata\XML\Validation\vecto_engine_valid.xml"),
TestCase(@"Testdata\XML\Validation\vecto_gearbox_valid.xml")] TestCase(@"Testdata\XML\Validation\vecto_gearbox_valid.xml")]
public void TestValidation(string file) public void TestValidation(string file)
{ {
var h = VectoHash.Load(file); var h = VectoHash.Load(file);
Assert.IsTrue(h.ValidateHash()); Assert.IsTrue(h.ValidateHash());
} }
[TestCase(@"Testdata\XML\Validation\vecto_engine_invalid.xml"), [TestCase(@"Testdata\XML\Validation\vecto_engine_invalid.xml"),
TestCase(@"Testdata\XML\Validation\vecto_gearbox_invalid.xml")] TestCase(@"Testdata\XML\Validation\vecto_gearbox_invalid.xml")]
public void TestValidationInvalid(string file) public void TestValidationInvalid(string file)
{ {
var h = VectoHash.Load(file); var h = VectoHash.Load(file);
Assert.IsFalse(h.ValidateHash()); Assert.IsFalse(h.ValidateHash());
} }
[TestCase(VectoComponents.Engine), [TestCase(VectoComponents.Engine),
TestCase(VectoComponents.Gearbox), TestCase(VectoComponents.Gearbox),
] ]
public void TestValidationComponentValid(VectoComponents component) public void TestValidationComponentValid(VectoComponents component)
{ {
var file = @"Testdata\XML\Validation\vecto_vehicle_components_valid-engine_gbx.xml"; var file = @"Testdata\XML\Validation\vecto_vehicle_components_valid-engine_gbx.xml";
var h = VectoHash.Load(file); var h = VectoHash.Load(file);
Assert.IsTrue(h.ValidateHash(component)); Assert.IsTrue(h.ValidateHash(component));
} }
[TestCase(VectoComponents.Engine), [TestCase(VectoComponents.Engine),
TestCase(VectoComponents.Gearbox), TestCase(VectoComponents.Gearbox),
TestCase(VectoComponents.Axlegear), TestCase(VectoComponents.Axlegear),
TestCase(VectoComponents.Angledrive), TestCase(VectoComponents.Angledrive),
TestCase(VectoComponents.Retarder), TestCase(VectoComponents.Retarder),
TestCase(VectoComponents.TorqueConverter), TestCase(VectoComponents.TorqueConverter),
TestCase(VectoComponents.Tyre), TestCase(VectoComponents.Tyre),
TestCase(VectoComponents.Airdrag), TestCase(VectoComponents.Airdrag),
] ]
public void TestValidationComponentInvalid(VectoComponents component) public void TestValidationComponentInvalid(VectoComponents component)
{ {
var file = @"Testdata\XML\Validation\vecto_vehicle_components_invalid.xml"; var file = @"Testdata\XML\Validation\vecto_vehicle_components_invalid.xml";
var h = VectoHash.Load(file); var h = VectoHash.Load(file);
Assert.IsFalse(h.ValidateHash(component)); Assert.IsFalse(h.ValidateHash(component));
} }
[TestCase(@"Testdata\XML\ToHash\vecto_engine-input.xml"), [TestCase(@"Testdata\XML\ToHash\vecto_engine-input.xml"),
TestCase(@"Testdata\XML\ToHash\vecto_engine_withid-input.xml"), TestCase(@"Testdata\XML\ToHash\vecto_engine_withid-input.xml"),
TestCase(@"Testdata\XML\ToHash\vecto_gearbox-input.xml")] TestCase(@"Testdata\XML\ToHash\vecto_gearbox-input.xml")]
public void TestAddHash(string file) public void TestAddHash(string file)
{ {
var destination = Path.GetFileNameWithoutExtension(file) + "_hashed.xml"; var destination = Path.GetFileNameWithoutExtension(file) + "_hashed.xml";
var h = VectoHash.Load(file); var h = VectoHash.Load(file);
var r = h.AddHash(); var r = h.AddHash();
var writer = new XmlTextWriter(destination, Encoding.UTF8); var writer = new XmlTextWriter(destination, Encoding.UTF8);
r.WriteTo(writer); r.WriteTo(writer);
writer.Flush(); writer.Flush();
writer.Close(); writer.Close();
var h2 = VectoHash.Load(destination); var h2 = VectoHash.Load(destination);
Assert.IsTrue(h2.ValidateHash()); Assert.IsTrue(h2.ValidateHash());
} }
[TestCase(@"Testdata\XML\ToHash\vecto_engine_withhash-input.xml", "input data already contains a signature element"), [TestCase(@"Testdata\XML\ToHash\vecto_engine_withhash-input.xml", "input data already contains a signature element"),
TestCase(@"Testdata\XML\ToHash\vecto_vehicle-sample.xml", "adding hash for Vehicle is not supported"), TestCase(@"Testdata\XML\ToHash\vecto_vehicle-sample.xml", "adding hash for Vehicle is not supported"),
TestCase(@"Testdata\XML\ToHash\vecto_gearbox-input_nodata.xml", "'Data' element for component 'Gearbox' not found!"), TestCase(@"Testdata\XML\ToHash\vecto_gearbox-input_nodata.xml", "'Data' element for component 'Gearbox' not found!"),
TestCase(@"Testdata\XML\ToHash\multiple_components.xml", "input must not contain multiple components!"), TestCase(@"Testdata\XML\ToHash\multiple_components.xml", "input must not contain multiple components!"),
] ]
public void TestAddHashException(string file, string expectedExceptionMsg) public void TestAddHashException(string file, string expectedExceptionMsg)
{ {
var destination = Path.GetFileNameWithoutExtension(file) + "_hashed.xml"; var destination = Path.GetFileNameWithoutExtension(file) + "_hashed.xml";
var h = VectoHash.Load(file); var h = VectoHash.Load(file);
AssertHelper.Exception<Exception>(() => { var r = h.AddHash(); }, expectedExceptionMsg); AssertHelper.Exception<Exception>(() => { var r = h.AddHash(); }, expectedExceptionMsg);
} }
[TestCase] [TestCase]
public void TestDuplicateSigElement() public void TestDuplicateSigElement()
{ {
var filename = @"Testdata\XML\Invalid\duplicate-sig.xml"; var filename = @"Testdata\XML\Invalid\duplicate-sig.xml";
var h = VectoHash.Load(filename); var h = VectoHash.Load(filename);
AssertHelper.Exception<Exception>(() => { var r = h.ReadHash(); }, "Multiple DigestValue elements found!"); AssertHelper.Exception<Exception>(() => { var r = h.ReadHash(); }, "Multiple DigestValue elements found!");
} }
[TestCase()] [TestCase()]
public void TestLoadFromStream() public void TestLoadFromStream()
{ {
var fs = new FileStream(BasicHasingTests.ReferenceXMLVehicle, FileMode.Open); var fs = new FileStream(BasicHasingTests.ReferenceXMLVehicle, FileMode.Open);
var h = VectoHash.Load(fs); var h = VectoHash.Load(fs);
var hash = h.ComputeHash(); var hash = h.ComputeHash();
Assert.AreEqual(BasicHasingTests.HashVehicleXML, hash); Assert.AreEqual(BasicHasingTests.HashVehicleXML, hash);
fs.Close(); fs.Close();
} }
[TestCase(WhitespaceHandling.All), [TestCase(WhitespaceHandling.All),
TestCase(WhitespaceHandling.None), TestCase(WhitespaceHandling.None),
TestCase(WhitespaceHandling.Significant)] TestCase(WhitespaceHandling.Significant)]
public void TestLoadXmlDocument(WhitespaceHandling whitespace) public void TestLoadXmlDocument(WhitespaceHandling whitespace)
{ {
var xml = new XmlDocument(); var xml = new XmlDocument();
var reader = new XmlTextReader(BasicHasingTests.ReferenceXMLVehicle); var reader = new XmlTextReader(BasicHasingTests.ReferenceXMLVehicle);
reader.WhitespaceHandling = whitespace; reader.WhitespaceHandling = whitespace;
xml.Load(reader); xml.Load(reader);
var h = VectoHash.Load(xml); var h = VectoHash.Load(xml);
var hash = h.ComputeHash(); var hash = h.ComputeHash();
Assert.AreEqual(BasicHasingTests.HashVehicleXML, hash); Assert.AreEqual(BasicHasingTests.HashVehicleXML, hash);
} }
[TestCase(@"Testdata\XML\ToHash\vecto_engine-input.xml"), [TestCase(@"Testdata\XML\ToHash\vecto_engine-input.xml"),
TestCase(@"Testdata\XML\ToHash\vecto_engine_withid-input.xml"), TestCase(@"Testdata\XML\ToHash\vecto_engine_withid-input.xml"),
TestCase(@"Testdata\XML\ToHash\vecto_gearbox-input.xml")] TestCase(@"Testdata\XML\ToHash\vecto_gearbox-input.xml")]
public void TestHashedComponentIsValid(string file) public void TestHashedComponentIsValid(string file)
{ {
var destination = Path.GetFileNameWithoutExtension(file) + "_hashed.xml"; var destination = Path.GetFileNameWithoutExtension(file) + "_hashed.xml";
var h = VectoHash.Load(file); var h = VectoHash.Load(file);
var r = h.AddHash(); var r = h.AddHash();
var writer = new XmlTextWriter(destination, Encoding.UTF8); var writer = new XmlTextWriter(destination, Encoding.UTF8);
r.WriteTo(writer); r.WriteTo(writer);
writer.Flush(); writer.Flush();
writer.Close(); writer.Close();
var h2 = VectoHash.Load(destination); var h2 = VectoHash.Load(destination);
Assert.IsTrue(h2.ValidateHash()); Assert.IsTrue(h2.ValidateHash());
// re-load generated XML and perform XSD validation // re-load generated XML and perform XSD validation
var settings = new XmlReaderSettings() { var settings = new XmlReaderSettings() {
ValidationType = ValidationType.Schema, ValidationType = ValidationType.Schema,
ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema | ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema |
//XmlSchemaValidationFlags.ProcessSchemaLocation | //XmlSchemaValidationFlags.ProcessSchemaLocation |
XmlSchemaValidationFlags.ReportValidationWarnings XmlSchemaValidationFlags.ReportValidationWarnings
}; };
settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack); settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
settings.Schemas.Add(GetXMLSchema(false)); settings.Schemas.Add(GetXMLSchema(false));
var xmlValidator = XmlReader.Create(destination, settings); var xmlValidator = XmlReader.Create(destination, settings);
var xmlDoc = XDocument.Load(xmlValidator); var xmlDoc = XDocument.Load(xmlValidator);
} }
[TestCase("vecto_vehicle-namespace_prefix.xml", BasicHasingTests.HashVehicleXML)] [TestCase("vecto_vehicle-namespace_prefix.xml", BasicHasingTests.HashVehicleXML)]
public void TestNamespacePrefixVariations(string file, string expectedHash) public void TestNamespacePrefixVariations(string file, string expectedHash)
{ {
var h = VectoHash.Load(@"Testdata\XML\Variations\" + file); var h = VectoHash.Load(@"Testdata\XML\Variations\" + file);
var hash = h.ComputeHash(); var hash = h.ComputeHash();
Assert.AreEqual(expectedHash, hash); Assert.AreEqual(expectedHash, hash);
} }
[TestCase()] [TestCase()]
public void TestInvalidXMLAsFile() public void TestInvalidXMLAsFile()
{ {
var file = @"Testdata\XML\Invalid\invalid-comp.xml"; var file = @"Testdata\XML\Invalid\invalid-comp.xml";
AssertHelper.Exception<Exception>(() => VectoHash.Load(file), "failed to read XML document"); AssertHelper.Exception<Exception>(() => VectoHash.Load(file), "failed to read XML document");
} }
[TestCase()] [TestCase()]
public void TestInvalidXMLAsStream() public void TestInvalidXMLAsStream()
{ {
var file = @"Testdata\XML\Invalid\invalid-comp.xml"; var file = @"Testdata\XML\Invalid\invalid-comp.xml";
var stream = File.Open(file, FileMode.Open); var stream = File.Open(file, FileMode.Open);
AssertHelper.Exception<Exception>(() => VectoHash.Load(stream), "failed to read XML document"); AssertHelper.Exception<Exception>(() => VectoHash.Load(stream), "failed to read XML document");
} }
[TestCase()] [TestCase()]
public void TestComputeHashNoComponentInXML() public void TestComputeHashNoComponentInXML()
{ {
var xml = @"<VectoInputDeclaration/>"; var xml = @"<VectoInputDeclaration/>";
var stream = new MemoryStream(); var stream = new MemoryStream();
var writer = new StreamWriter(stream); var writer = new StreamWriter(stream);
writer.Write(xml); writer.Write(xml);
writer.Flush(); writer.Flush();
stream.Seek(0, SeekOrigin.Begin); stream.Seek(0, SeekOrigin.Begin);
var h = VectoHash.Load(stream); var h = VectoHash.Load(stream);
AssertHelper.Exception<Exception>(() => h.ComputeHash(), "No component found"); AssertHelper.Exception<Exception>(() => h.ComputeHash(), "No component found");
} }
[TestCase()] [TestCase()]
public void TestReadHashNoComponentInXML() public void TestReadHashNoComponentInXML()
{ {
var xml = @"<VectoInputDeclaration/>"; var xml = @"<VectoInputDeclaration/>";
var stream = new MemoryStream(); var stream = new MemoryStream();
var writer = new StreamWriter(stream); var writer = new StreamWriter(stream);
writer.Write(xml); writer.Write(xml);
writer.Flush(); writer.Flush();
stream.Seek(0, SeekOrigin.Begin); stream.Seek(0, SeekOrigin.Begin);
var h = VectoHash.Load(stream); var h = VectoHash.Load(stream);
AssertHelper.Exception<Exception>(() => h.ReadHash(), "No component found"); AssertHelper.Exception<Exception>(() => h.ReadHash(), "No component found");
} }
[TestCase(VectoComponents.Engine, "ENG-"), [TestCase(VectoComponents.Engine, "ENG-"),
TestCase(VectoComponents.Gearbox, "GBX-"), TestCase(VectoComponents.Gearbox, "GBX-"),
TestCase(VectoComponents.Axlegear, "AXL-"), TestCase(VectoComponents.Axlegear, "AXL-"),
TestCase(VectoComponents.Retarder, "RET-"), TestCase(VectoComponents.Retarder, "RET-"),
TestCase(VectoComponents.TorqueConverter, "TC-"), TestCase(VectoComponents.TorqueConverter, "TC-"),
TestCase(VectoComponents.Angledrive, "ANGL-"), TestCase(VectoComponents.Angledrive, "ANGL-"),
TestCase(VectoComponents.Airdrag, "AD-"), TestCase(VectoComponents.Airdrag, "AD-"),
TestCase(VectoComponents.Tyre, "TYRE-"), TestCase(VectoComponents.Tyre, "TYRE-"),
] ]
public void TestIdPrefix(VectoComponents component, string expectedPrefix) public void TestIdPrefix(VectoComponents component, string expectedPrefix)
{ {
Assert.AreEqual(expectedPrefix, component.HashIdPrefix()); Assert.AreEqual(expectedPrefix, component.HashIdPrefix());
} }
[TestCase()] [TestCase()]
public void TestInvalidComponentXMLName() public void TestInvalidComponentXMLName()
{ {
AssertHelper.Exception<ArgumentOutOfRangeException>(() => ((VectoComponents)9999).XMLElementName()); AssertHelper.Exception<ArgumentOutOfRangeException>(() => ((VectoComponents)9999).XMLElementName());
} }
[TestCase()] [TestCase()]
public void TestInvalidComponentPrefix() public void TestInvalidComponentPrefix()
{ {
AssertHelper.Exception<ArgumentOutOfRangeException>(() => ((VectoComponents)9999).HashIdPrefix()); AssertHelper.Exception<ArgumentOutOfRangeException>(() => ((VectoComponents)9999).HashIdPrefix());
} }
private static XmlSchemaSet GetXMLSchema(bool job) private static XmlSchemaSet GetXMLSchema(bool job)
{ {
var resource = RessourceHelper.LoadResourceAsStream(RessourceHelper.ResourceType.XMLSchema, var resource = RessourceHelper.LoadResourceAsStream(RessourceHelper.ResourceType.XMLSchema,
job ? "VectoInput.xsd" : "VectoComponent.xsd"); job ? "VectoInput.xsd" : "VectoComponent.xsd");
var xset = new XmlSchemaSet() { XmlResolver = new XmlResourceResolver() }; var xset = new XmlSchemaSet() { XmlResolver = new XmlResourceResolver() };
var reader = XmlReader.Create(resource, new XmlReaderSettings(), "schema://"); var reader = XmlReader.Create(resource, new XmlReaderSettings(), "schema://");
xset.Add(XmlSchema.Read(reader, null)); xset.Add(XmlSchema.Read(reader, null));
xset.Compile(); xset.Compile();
return xset; return xset;
} }
private static void ValidationCallBack(object sender, ValidationEventArgs args) private static void ValidationCallBack(object sender, ValidationEventArgs args)
{ {
if (args.Severity == XmlSeverityType.Error) { if (args.Severity == XmlSeverityType.Error) {
throw new Exception(string.Format("Validation error: {0}" + Environment.NewLine + throw new Exception(string.Format("Validation error: {0}" + Environment.NewLine +
"Line: {1}", args.Message, args.Exception.LineNumber)); "Line: {1}", args.Message, args.Exception.LineNumber));
} }
} }
} }
} }
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment