Index: Ringtoets/Common/src/Ringtoets.Common.IO/Readers/ConfigurationReader.cs
===================================================================
diff -u -r4136a0156ea9e20e7e1aca569c5e89cbc713fed2 -r9adb7863b832fbef7249803d20750688061c846e
--- Ringtoets/Common/src/Ringtoets.Common.IO/Readers/ConfigurationReader.cs (.../ConfigurationReader.cs) (revision 4136a0156ea9e20e7e1aca569c5e89cbc713fed2)
+++ Ringtoets/Common/src/Ringtoets.Common.IO/Readers/ConfigurationReader.cs (.../ConfigurationReader.cs) (revision 9adb7863b832fbef7249803d20750688061c846e)
@@ -50,7 +50,13 @@
///
/// The file path to the XML file.
/// A string representing an XML Schema Definition (XSD).
- /// Thrown when is invalid.
+ /// Thrown when:
+ ///
+ /// - is invalid.
+ /// - is null or empty.
+ /// - contains an invalid schema definition.
+ ///
+ ///
/// Thrown when:
///
/// - points to a file that does not exist.
@@ -62,6 +68,11 @@
{
IOUtils.ValidateFilePath(xmlFilePath);
+ if (string.IsNullOrWhiteSpace(schemaString))
+ {
+ throw new ArgumentException(nameof(schemaString));
+ }
+
ValidateFileExists(xmlFilePath);
xmlDocument = LoadDocument(xmlFilePath);
@@ -136,10 +147,19 @@
private static void ValidateToSchema(XDocument document, string schemaString, string xmlFilePath)
{
var xmlSchemaSet = new XmlSchemaSet();
- xmlSchemaSet.Add(XmlSchema.Read(new StringReader(schemaString), null));
try
{
+ xmlSchemaSet.Add(XmlSchema.Read(new StringReader(schemaString), null));
+ }
+ catch (Exception exception) when (exception is XmlException
+ || exception is XmlSchemaException)
+ {
+ throw new ArgumentException($"Invalid 'schemaString': {exception.Message}", exception);
+ }
+
+ try
+ {
document.Validate(xmlSchemaSet, null);
}
catch (XmlSchemaValidationException exception)
Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/Readers/ConfigurationReaderTest.cs
===================================================================
diff -u -r24f0b189764d40cc8ee7e9efd62ee6c6ea8c7b5f -r9adb7863b832fbef7249803d20750688061c846e
--- Ringtoets/Common/test/Ringtoets.Common.IO.Test/Readers/ConfigurationReaderTest.cs (.../ConfigurationReaderTest.cs) (revision 24f0b189764d40cc8ee7e9efd62ee6c6ea8c7b5f)
+++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/Readers/ConfigurationReaderTest.cs (.../ConfigurationReaderTest.cs) (revision 9adb7863b832fbef7249803d20750688061c846e)
@@ -23,6 +23,7 @@
using System.IO;
using System.Xml;
using System.Xml.Linq;
+using System.Xml.Schema;
using Core.Common.IO.Exceptions;
using Core.Common.TestUtil;
using NUnit.Framework;
@@ -33,6 +34,8 @@
[TestFixture]
public class ConfigurationReaderTest
{
+ private readonly string schemaString;
+
private readonly string testDirectoryPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO,
"ConfigurationReader");
@@ -43,7 +46,7 @@
public void Constructor_NoFilePath_ThrowArgumentException(string invalidFilePath)
{
// Call
- TestDelegate call = () => new TestConfigurationReader(invalidFilePath, "");
+ TestDelegate call = () => new TestConfigurationReader(invalidFilePath, schemaString);
// Assert
string expectedMessage = $"Fout bij het lezen van bestand '{invalidFilePath}': bestandspad mag niet leeg of ongedefinieerd zijn.";
@@ -60,7 +63,7 @@
string invalidFilePath = validFilePath.Replace("Config", invalidPathChars[3].ToString());
// Call
- TestDelegate call = () => new TestConfigurationReader(invalidFilePath, "");
+ TestDelegate call = () => new TestConfigurationReader(invalidFilePath, schemaString);
// Assert
string expectedMessage = $"Fout bij het lezen van bestand '{invalidFilePath}': "
@@ -75,7 +78,7 @@
string invalidFilePath = Path.Combine(testDirectoryPath, Path.DirectorySeparatorChar.ToString());
// Call
- TestDelegate call = () => new TestConfigurationReader(invalidFilePath, "");
+ TestDelegate call = () => new TestConfigurationReader(invalidFilePath, schemaString);
// Assert
string expectedMessage = $"Fout bij het lezen van bestand '{invalidFilePath}': bestandspad mag niet verwijzen naar een lege bestandsnaam.";
@@ -89,7 +92,7 @@
string invalidFilePath = Path.Combine(testDirectoryPath, "notExisting.xml");
// Call
- TestDelegate call = () => new TestConfigurationReader(invalidFilePath, "");
+ TestDelegate call = () => new TestConfigurationReader(invalidFilePath, schemaString);
// Assert
string expectedMessage = $"Fout bij het lezen van bestand '{invalidFilePath}': het bestand bestaat niet.";
@@ -98,6 +101,45 @@
}
[Test]
+ [TestCase("")]
+ [TestCase(" ")]
+ [TestCase(null)]
+ public void Constructor_NoSchemaString_ThrowArgumentException(string invalidSchemaString)
+ {
+ // Setup
+ string filePath = Path.Combine(testDirectoryPath, "validConfiguration.xml");
+
+ // Call
+ TestDelegate call = () => new TestConfigurationReader(filePath, invalidSchemaString);
+
+ // Assert
+ var exception = Assert.Throws(call);
+ Assert.AreEqual("schemaString", exception.Message);
+ }
+
+ [Test]
+ [TestCase("textContent.xsd",
+ "Invalid 'schemaString': Data at the root level is invalid. Line 1, position 1.",
+ typeof(XmlException))]
+ [TestCase("invalidXsdContent.xsd",
+ "Invalid 'schemaString': The root element of a W3C XML Schema should be and its namespace should be 'http://www.w3.org/2001/XMLSchema'.",
+ typeof(XmlSchemaException))]
+ public void Constructor_InvalidSchemaString_ThrowArgumentException(string fileName, string expectedMessage, Type expectedExceptionType)
+ {
+ // Setup
+ string filePath = Path.Combine(testDirectoryPath, "validConfiguration.xml");
+ string xsdPath = Path.Combine(testDirectoryPath, fileName);
+
+ // Call
+ TestDelegate call = () => new TestConfigurationReader(filePath, File.ReadAllText(xsdPath));
+
+ // Assert
+ var exception = Assert.Throws(call);
+ Assert.AreEqual(expectedMessage, exception.Message);
+ Assert.IsInstanceOf(expectedExceptionType, exception.InnerException);
+ }
+
+ [Test]
[TestCase("empty.xml")]
[TestCase("textContent.xml")]
[TestCase("invalidXmlContent.xml")]
@@ -107,7 +149,7 @@
string filePath = Path.Combine(testDirectoryPath, fileName);
// Call
- TestDelegate call = () => new TestConfigurationReader(filePath, "");
+ TestDelegate call = () => new TestConfigurationReader(filePath, schemaString);
// Assert
string expectedMessage = $"Fout bij het lezen van bestand '{filePath}': het bestand kon niet worden geopend. Mogelijk is het bestand corrupt of in gebruik door een andere applicatie.";
@@ -127,7 +169,7 @@
fileDisposeHelper.LockFiles();
// Call
- TestDelegate call = () => new TestConfigurationReader(path, "");
+ TestDelegate call = () => new TestConfigurationReader(path, schemaString);
// Assert
string expectedMessage = $"Fout bij het lezen van bestand '{path}': het bestand kon niet worden geopend. Mogelijk is het bestand corrupt of in gebruik door een andere applicatie.";
@@ -137,6 +179,11 @@
}
}
+ public ConfigurationReaderTest()
+ {
+ schemaString = File.ReadAllText(Path.Combine(testDirectoryPath, "ConfiguratieSchema.xsd"));
+ }
+
private class TestConfigurationReader : ConfigurationReader
{
public TestConfigurationReader(string xmlFilePath, string schemaString)
Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/ConfigurationReader/ConfiguratieSchema.xsd
===================================================================
diff -u
--- Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/ConfigurationReader/ConfiguratieSchema.xsd (revision 0)
+++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/ConfigurationReader/ConfiguratieSchema.xsd (revision 9adb7863b832fbef7249803d20750688061c846e)
@@ -0,0 +1,72 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/ConfigurationReader/invalidXsdContent.xsd
===================================================================
diff -u
--- Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/ConfigurationReader/invalidXsdContent.xsd (revision 0)
+++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/ConfigurationReader/invalidXsdContent.xsd (revision 9adb7863b832fbef7249803d20750688061c846e)
@@ -0,0 +1,37 @@
+
+
+
\ No newline at end of file
Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/ConfigurationReader/textContent.xsd
===================================================================
diff -u
--- Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/ConfigurationReader/textContent.xsd (revision 0)
+++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/ConfigurationReader/textContent.xsd (revision 9adb7863b832fbef7249803d20750688061c846e)
@@ -0,0 +1 @@
+Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur faucibus finibus magna, sed pellentesque lorem imperdiet nec. Mauris porttitor nisl blandit placerat aliquet. Aliquam nisl arcu, pretium ut erat quis, congue sagittis odio. Maecenas felis ante, dictum aliquet laoreet ac, consectetur ac purus. Donec non tristique libero, ut vulputate est. Fusce hendrerit justo eu vehicula dignissim. Duis auctor velit vitae vestibulum bibendum. Proin nec pharetra est, id ultricies sem. Aliquam dignissim blandit accumsan. Phasellus bibendum magna sed vestibulum dictum. Nullam vel ipsum quis turpis pulvinar convallis a et lectus. Maecenas eros orci, suscipit id elementum et, fringilla eget ligula. Nulla elit tellus, varius a nisl eget, ornare tempor libero.
\ No newline at end of file