Index: DamEngine/trunk/src/Deltares.DamEngine.Io/IdValidator.cs
===================================================================
diff -u
--- DamEngine/trunk/src/Deltares.DamEngine.Io/IdValidator.cs (revision 0)
+++ DamEngine/trunk/src/Deltares.DamEngine.Io/IdValidator.cs (revision 2101)
@@ -0,0 +1,47 @@
+// Copyright (C) Stichting Deltares 2019. All rights reserved.
+//
+// This file is part of the Dam Engine.
+//
+// The Dam Engine is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see .
+//
+// All names, logos, and references to "Deltares" are registered trademarks of
+// Stichting Deltares and remain full property of Stichting Deltares at all times.
+// All rights reserved.
+
+using System;
+using System.Text.RegularExpressions;
+
+namespace Deltares.DamEngine.Io
+{
+ /// Validator for Id names
+ public static class IdValidator
+ {
+ /// Checks if Id contains only valid characters
+ /// Valid characters are (between quotes):
+ /// "ABCDEFGHIJKLMNOPQRSTUVW" // Specified as "A-Z"
+ /// "abcdefghijklmnopqrstuvw" // Specified as "a-z"
+ /// "01234567879" // Specified as "0-9"
+ /// "!#$%&()*+,-./" // Specified as "!#-/"
+ /// ":;<=>?@" // Specified as ":-@"
+ /// "[\]^_`" // Specified as "[-`"
+ /// "{|}~ " // Specified as "{-~ "
+ /// " " // Specified as "\s"
+ /// Name to check
+ static public bool IsCorrectName(string name)
+ {
+ Regex regex = new Regex(@"[^A-Za-z0-9!#-/:-@[-`{-~\s]");
+ return !regex.IsMatch(name);
+ }
+ }
+}
Index: DamEngine/trunk/src/Deltares.DamEngine.Io/Deltares.DamEngine.Io.csproj
===================================================================
diff -u -r1497 -r2101
--- DamEngine/trunk/src/Deltares.DamEngine.Io/Deltares.DamEngine.Io.csproj (.../Deltares.DamEngine.Io.csproj) (revision 1497)
+++ DamEngine/trunk/src/Deltares.DamEngine.Io/Deltares.DamEngine.Io.csproj (.../Deltares.DamEngine.Io.csproj) (revision 2101)
@@ -39,6 +39,7 @@
+
Index: DamEngine/trunk/src/Deltares.DamEngine.Io.Tests/IdValidatorTests.cs
===================================================================
diff -u
--- DamEngine/trunk/src/Deltares.DamEngine.Io.Tests/IdValidatorTests.cs (revision 0)
+++ DamEngine/trunk/src/Deltares.DamEngine.Io.Tests/IdValidatorTests.cs (revision 2101)
@@ -0,0 +1,71 @@
+// Copyright (C) Stichting Deltares 2019. All rights reserved.
+//
+// This file is part of the Dam Engine.
+//
+// The Dam Engine is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see .
+//
+// All names, logos, and references to "Deltares" are registered trademarks of
+// Stichting Deltares and remain full property of Stichting Deltares at all times.
+// All rights reserved.
+
+using NUnit.Framework;
+
+namespace Deltares.DamEngine.Io.Tests
+{
+ [TestFixture]
+ public class IdValidatorTests
+ {
+ [TestCase("ABCDEFGHIJLMNOPQRSTUVWXYZ")]
+ [TestCase("A")]
+ [TestCase("Z")]
+ [TestCase("K")]
+ [TestCase("JUSTATEST")]
+ [TestCase("abcdefghijklmnopqrstuvwxyz")]
+ [TestCase("a")]
+ [TestCase("z")]
+ [TestCase("k")]
+ [TestCase("justatest")]
+ [TestCase("01234567879")]
+ [TestCase("0")]
+ [TestCase("9")]
+ [TestCase("5")]
+ [TestCase("!#$%&()*+,-./")]
+ [TestCase(":;<=>?@")]
+ [TestCase("[]^_`")]
+ [TestCase("{|}~")]
+ [TestCase("!")]
+ [TestCase("Een locatie 1!")]
+ public void GivenCorrectIdWhenValidatingThenSucceeds(string id)
+ {
+ // Given Correct Id When Validating
+ bool isOk = IdValidator.IsCorrectName(id);
+
+ // Then Succeeds
+ Assert.IsTrue(isOk);
+ }
+
+ [TestCase("segment_12_2_1D1")] // Between "segment_12" and "_2_1D1" there are 2 illegal characters (1F hex)
+ [TestCase("De laatste letter is foutÀ")]
+ [TestCase("Een letter ¡ middenin is fout")]
+ [TestCase("ÀDe eerste letter is fout")]
+ public void GivenInCorrectIdWhenValidatingThenFails(string id)
+ {
+ // Given Correct Id When Validating
+ bool isOk = IdValidator.IsCorrectName(id);
+
+ // Then Succeeds
+ Assert.IsFalse(isOk);
+ }
+ }
+}
Index: DamEngine/trunk/src/Deltares.DamEngine.Interface.Tests/FillDamFromXmlInputTests.cs
===================================================================
diff -u -r2094 -r2101
--- DamEngine/trunk/src/Deltares.DamEngine.Interface.Tests/FillDamFromXmlInputTests.cs (.../FillDamFromXmlInputTests.cs) (revision 2094)
+++ DamEngine/trunk/src/Deltares.DamEngine.Interface.Tests/FillDamFromXmlInputTests.cs (.../FillDamFromXmlInputTests.cs) (revision 2101)
@@ -82,14 +82,15 @@
}
[Test]
+ [TestCase("segment_12_2_1D1")] // Between "segment_12" and "_2_1D1" there are 2 illegal characters (1F hex)
// This should be a better exception type and exception message
[ExpectedException(typeof(System.InvalidOperationException))]
- public void GivenDataSetContainingIllegalCharactersWhenWritingXmlThenRaiseExceptionWithClearMessage()
+ public void GivenDataSetContainingIllegalCharactersWhenWritingXmlThenRaiseExceptionWithClearMessage(string id)
{
// Given DataSet Containing Illegal Characters
DamProjectData expectedDamProjectData = FactoryForDamProjectData.CreateExampleDamProjectData();
var location = expectedDamProjectData.Dike.Locations[0];
- location.Name = "segment_12_2_1D1"; // Between "segment_12" and "_2_1D1" there are 2 illegal characters (1F hex)
+ location.Name = id;
// When Writing Xml
string xmlString;
@@ -102,6 +103,45 @@
CompareDamProjectData(actualDamProjectData, expectedDamProjectData);
}
+
+ [Test]
+ [TestCase("ABCDEFGHIJLMNOPQRSTUVWXYZ")]
+ [TestCase("A")]
+ [TestCase("Z")]
+ [TestCase("K")]
+ [TestCase("JUSTATEST")]
+ [TestCase("abcdefghijklmnopqrstuvwxyz")]
+ [TestCase("a")]
+ [TestCase("z")]
+ [TestCase("k")]
+ [TestCase("justatest")]
+ [TestCase("01234567879")]
+ [TestCase("0")]
+ [TestCase("9")]
+ [TestCase("5")]
+ [TestCase("!#$%&()*+,-./")]
+ [TestCase(":;<=>?@")]
+ [TestCase(@"[\]^_`")]
+ [TestCase("{|}~")]
+ [TestCase("!")]
+ public void GivenDataSetContainingIdWithLegalCharactersWhenWritingXmlThenSucceeds(string id)
+ {
+ // Given DataSet Containing Illegal Characters
+ DamProjectData expectedDamProjectData = FactoryForDamProjectData.CreateExampleDamProjectData();
+ var location = expectedDamProjectData.Dike.Locations[0];
+ location.Name = id;
+
+ // When Writing Xml
+ string xmlString;
+ Input input = FillXmlInputFromDam.CreateInput(expectedDamProjectData);
+ xmlString = DamXmlSerialization.SaveInputAsXmlString(input);
+
+ // Then Raise Exception With Clear Message()
+ input = DamXmlSerialization.LoadInputFromXmlString(xmlString);
+ DamProjectData actualDamProjectData = FillDamFromXmlInput.CreateDamProjectData(input);
+ CompareDamProjectData(actualDamProjectData, expectedDamProjectData);
+
+ }
private void CompareDamProjectData(DamProjectData actual, DamProjectData expected)
{
Assert.AreEqual(FactoryForDamProjectData.ExpectedAnalysisType, DamProjectCalculationSpecification.SelectedAnalysisType);
Index: DamEngine/trunk/src/Deltares.DamEngine.Io.Tests/Deltares.DamEngine.Io.Tests.csproj
===================================================================
diff -u -r1497 -r2101
--- DamEngine/trunk/src/Deltares.DamEngine.Io.Tests/Deltares.DamEngine.Io.Tests.csproj (.../Deltares.DamEngine.Io.Tests.csproj) (revision 1497)
+++ DamEngine/trunk/src/Deltares.DamEngine.Io.Tests/Deltares.DamEngine.Io.Tests.csproj (.../Deltares.DamEngine.Io.Tests.csproj) (revision 2101)
@@ -45,6 +45,7 @@
+