From 4fb22323486885a5ddec35d3a1a2e227e584713b Mon Sep 17 00:00:00 2001
From: Markus Quaritsch <markus.quaritsch@tugraz.at>
Date: Tue, 16 May 2017 13:19:01 +0200
Subject: [PATCH] increase test coverage, close files after reading

---
 VectoCommon/VectoHashing/VectoHash.cs         | 10 +++-
 VectoCommon/VectoHashingTest/VectoHashTest.cs | 55 +++++++++++++++++++
 2 files changed, 63 insertions(+), 2 deletions(-)

diff --git a/VectoCommon/VectoHashing/VectoHash.cs b/VectoCommon/VectoHashing/VectoHash.cs
index af2706f955..3602a8bdbf 100644
--- a/VectoCommon/VectoHashing/VectoHash.cs
+++ b/VectoCommon/VectoHashing/VectoHash.cs
@@ -19,10 +19,16 @@ namespace TUGraz.VectoHashing
 		public static VectoHash Load(string filename)
 		{
 			var doc = new XmlDocument();
+			XmlTextReader reader = null;
 			try {
-				doc.Load(new XmlTextReader(filename));
+				reader = new XmlTextReader(filename);
+				doc.Load(reader);
 			} catch (Exception e) {
 				throw new Exception("failed to read XML document", e);
+			} finally {
+				if (reader != null) {
+					reader.Close();
+				}
 			}
 			return new VectoHash(doc);
 		}
@@ -156,7 +162,7 @@ namespace TUGraz.VectoHashing
 		{
 			var nodes = Document.SelectNodes(GetComponentQueryString());
 			if (nodes == null || nodes.Count == 0) {
-				throw new Exception(string.Format("Component {0} not found", nodes.Count));
+				throw new Exception("No component found");
 			}
 			return ReadHashValue(nodes[0]);
 		}
diff --git a/VectoCommon/VectoHashingTest/VectoHashTest.cs b/VectoCommon/VectoHashingTest/VectoHashTest.cs
index beddb40d17..ddad6df508 100644
--- a/VectoCommon/VectoHashingTest/VectoHashTest.cs
+++ b/VectoCommon/VectoHashingTest/VectoHashTest.cs
@@ -354,6 +354,61 @@ namespace VectoHashingTest
 			AssertHelper.Exception<Exception>(() => VectoHash.Load(stream), "failed to read XML document");
 		}
 
+		[TestCase()]
+		public void TestComputeHashNoComponentInXML()
+		{
+			var xml = @"<VectoInputDeclaration/>";
+			var stream = new MemoryStream();
+			var writer = new StreamWriter(stream);
+			writer.Write(xml);
+			writer.Flush();
+			stream.Seek(0, SeekOrigin.Begin);
+
+			var h = VectoHash.Load(stream);
+			AssertHelper.Exception<Exception>(() => h.ComputeHash(), "No component found");
+		}
+
+		[TestCase()]
+		public void TestReadHashNoComponentInXML()
+		{
+			var xml = @"<VectoInputDeclaration/>";
+			var stream = new MemoryStream();
+			var writer = new StreamWriter(stream);
+			writer.Write(xml);
+			writer.Flush();
+			stream.Seek(0, SeekOrigin.Begin);
+
+			var h = VectoHash.Load(stream);
+			AssertHelper.Exception<Exception>(() => h.ReadHash(), "No component found");
+		}
+
+		[TestCase(VectoComponents.Engine, "ENG-"),
+		TestCase(VectoComponents.Gearbox, "GBX-"),
+		TestCase(VectoComponents.Axlegear, "AXL-"),
+		TestCase(VectoComponents.Retarder, "RET-"),
+		TestCase(VectoComponents.TorqueConverter, "TC-"),
+		TestCase(VectoComponents.Angledrive, "ANGL-"),
+		TestCase(VectoComponents.Airdrag, "AD-"),
+		TestCase(VectoComponents.Tyre, "TYRE-"),
+		
+		]
+		public void TestIdPrefix(VectoComponents component, string expectedPrefix)
+		{
+			Assert.AreEqual(expectedPrefix, component.HashIdPrefix());
+		}
+
+		[TestCase()]
+		public void TestInvalidComponentXMLName()
+		{
+			AssertHelper.Exception<ArgumentOutOfRangeException>(() => ((VectoComponents)9999).XMLElementName());
+		}
+
+		[TestCase()]
+		public void TestInvalidComponentPrefix()
+		{
+			AssertHelper.Exception<ArgumentOutOfRangeException>(() => ((VectoComponents)9999).HashIdPrefix());
+		}
+
 		private static XmlSchemaSet GetXMLSchema(bool job)
 		{
 			var resource = RessourceHelper.LoadResourceAsStream(RessourceHelper.ResourceType.XMLSchema,
-- 
GitLab