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

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

adding more testcases

parent 02ab9821
No related branches found
No related tags found
No related merge requests found
......@@ -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;
......
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
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
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