From 09c6369115ea6994f08b5f5317fe883649a6aed0 Mon Sep 17 00:00:00 2001 From: Markus Quaritsch <markus.quaritsch@tugraz.at> Date: Fri, 5 May 2017 13:26:04 +0200 Subject: [PATCH] adding more testcases --- VectoCommon/VectoHashing/VectoHash.cs | 16 ++++-- .../VectoHashingTest/Utils/AssertHelper.cs | 25 +++++++++ VectoCommon/VectoHashingTest/VectoHashTest.cs | 51 ++++++++++++++++++- 3 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 VectoCommon/VectoHashingTest/Utils/AssertHelper.cs diff --git a/VectoCommon/VectoHashing/VectoHash.cs b/VectoCommon/VectoHashing/VectoHash.cs index dbfbd75d17..1998953566 100644 --- a/VectoCommon/VectoHashing/VectoHash.cs +++ b/VectoCommon/VectoHashing/VectoHash.cs @@ -80,17 +80,23 @@ namespace TUGraz.VectoHashing public XDocument AddHash() { var components = GetContainigComponents(); - if (components.Count > 1) { - throw new Exception("can only add hash for a single component!"); - } - if (components[0] == VectoComponents.Vehicle) { + if (components.Contains(VectoComponents.Vehicle)) { throw new Exception("adding hash for Vehicle is not supported"); } + if (components.Count > 1) { + throw new Exception("input must not contain multiple components!"); + } var query = string.Format("//*[local-name()='{0}']/*[local-name()='Data']", components[0]); var node = document.SelectSingleNode(query); if (node == null) { - throw new Exception(string.Format("'Data' element for component {0} not found!", components[0])); + throw new Exception(string.Format("'Data' element for component '{0}' not found!", components[0])); + } + query = string.Format("//*[local-name()='{0}']/*[local-name()='Signature']", components[0]); + var sigNodes = document.SelectNodes(query); + if (sigNodes != null && sigNodes.Count > 0) { + throw new Exception("input data already contains a signature element"); } + var attributes = node.Attributes; var id = components[0].HashIdPrefix() + Guid.NewGuid().ToString("n").Substring(0, 20); var idSet = false; diff --git a/VectoCommon/VectoHashingTest/Utils/AssertHelper.cs b/VectoCommon/VectoHashingTest/Utils/AssertHelper.cs new file mode 100644 index 0000000000..72ef0f1256 --- /dev/null +++ b/VectoCommon/VectoHashingTest/Utils/AssertHelper.cs @@ -0,0 +1,25 @@ +using System; +using System.Diagnostics; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace VectoHashingTest.Utils +{ + public static class AssertHelper + { + /// <summary> + /// Assert an expected Exception. + /// </summary> + [DebuggerHidden] + public static void Exception<T>(this Action func, string message = null) where T : Exception + { + try { + func(); + Assert.Fail("Expected Exception {0}, but no exception occured.", typeof(T)); + } catch (T ex) { + if (message != null) { + Assert.AreEqual(message, ex.Message); + } + } + } + } +} \ No newline at end of file diff --git a/VectoCommon/VectoHashingTest/VectoHashTest.cs b/VectoCommon/VectoHashingTest/VectoHashTest.cs index cc5f83c12d..895c70441b 100644 --- a/VectoCommon/VectoHashingTest/VectoHashTest.cs +++ b/VectoCommon/VectoHashingTest/VectoHashTest.cs @@ -1,9 +1,11 @@ -using System.IO; +using System; +using System.IO; using System.Linq; using System.Text; using System.Xml; using NUnit.Framework; using TUGraz.VectoHashing; +using VectoHashingTest.Utils; using Assert = NUnit.Framework.Assert; namespace VectoHashingTest @@ -94,7 +96,41 @@ namespace VectoHashingTest } + [TestCase(@"Testdata\XML\Validation\vecto_engine_valid.xml"), + TestCase(@"Testdata\XML\Validation\vecto_gearbox_valid.xml")] + public void TestValidation(string file) + { + var h = VectoHash.Load(file); + Assert.IsTrue(h.ValidateHash()); + } + + [TestCase(@"Testdata\XML\Validation\vecto_engine_invalid.xml"), + TestCase(@"Testdata\XML\Validation\vecto_gearbox_invalid.xml")] + public void TestValidationInvalid(string file) + { + var h = VectoHash.Load(file); + Assert.IsFalse(h.ValidateHash()); + } + + [TestCase(VectoComponents.Engine), + TestCase(VectoComponents.Gearbox), + TestCase(VectoComponents.Axlegear), + TestCase(VectoComponents.Angledrive), + TestCase(VectoComponents.Retarder), + TestCase(VectoComponents.TorqueConverter), + TestCase(VectoComponents.Tyre), + TestCase(VectoComponents.Airdrag), + ] + public void TestValidationComponentInvalid(VectoComponents component) + { + var file = @"Testdata\XML\Validation\vecto_vehicle_components_invalid.xml"; + var h = VectoHash.Load(file); + + Assert.IsFalse(h.ValidateHash(component)); + } + [TestCase(@"Testdata\XML\ToHash\vecto_engine-input.xml"), + TestCase(@"Testdata\XML\ToHash\vecto_engine_withid-input.xml"), TestCase(@"Testdata\XML\ToHash\vecto_gearbox-input.xml")] public void TestAddHash(string file) { @@ -111,5 +147,18 @@ namespace VectoHashingTest var h2 = VectoHash.Load(destination); 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_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\multiple_components.xml", "input must not contain multiple components!"), + ] + public void TestAddHashException(string file, string expectedExceptionMsg) + { + var destination = Path.GetFileNameWithoutExtension(file) + "_hashed.xml"; + + var h = VectoHash.Load(file); + AssertHelper.Exception<Exception>(() => { var r = h.AddHash(); }, expectedExceptionMsg); + } } } \ No newline at end of file -- GitLab