Index: Ringtoets/Common/src/Ringtoets.Common.IO/Readers/XElementExtensions.cs
===================================================================
diff -u -re786dd8b8c7f833c217fdcddd909a616d590fe15 -r52d5e1c2609a6f84238d88e20aa135d28de24cb0
--- Ringtoets/Common/src/Ringtoets.Common.IO/Readers/XElementExtensions.cs (.../XElementExtensions.cs) (revision e786dd8b8c7f833c217fdcddd909a616d590fe15)
+++ Ringtoets/Common/src/Ringtoets.Common.IO/Readers/XElementExtensions.cs (.../XElementExtensions.cs) (revision 52d5e1c2609a6f84238d88e20aa135d28de24cb0)
@@ -94,9 +94,12 @@
/// The name of the descendant element.
/// The converted value, or null when the
/// does not have descendant elements of .
+ /// Thrown when calling .
+ /// results in an exception being thrown.
/// Thrown when any parameter is null.
/// Thrown when the conversion cannot be performed.
- public static object GetConvertedValueFromDescendantStringElement(this XElement parentElement, string descendantElementName) where TConverter : TypeConverter, new()
+ public static object GetConvertedValueFromDescendantStringElement(this XElement parentElement, string descendantElementName)
+ where TConverter : TypeConverter, new()
{
string stringValue = parentElement.GetStringValueFromDescendantElement(descendantElementName);
if (stringValue == null)
@@ -114,9 +117,16 @@
/// The name of the descendant element.
/// The converted value, or null when the
/// does not have descendant elements of .
+ /// Thrown when calling .
+ /// results in an exception being thrown.
/// Thrown when any parameter is null.
+ /// Thrown when the value from a descendant element is
+ /// not in the correct format.
+ /// Thrown when the value from a descendant element
+ /// represents a number less than or greater than .
/// Thrown when the conversion cannot be performed.
- public static object GetConvertedValueFromDescendantDoubleElement(this XElement parentElement, string descendantElementName) where TConverter : TypeConverter, new()
+ public static object GetConvertedValueFromDescendantDoubleElement(this XElement parentElement, string descendantElementName)
+ where TConverter : TypeConverter, new()
{
double? doubleValue = parentElement.GetDoubleValueFromDescendantElement(descendantElementName);
if (doubleValue == null)
Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/Readers/XElementExtensionsTest.cs
===================================================================
diff -u -r92dcf3ff33f93a75b92ac8fcd05441ce35332edb -r52d5e1c2609a6f84238d88e20aa135d28de24cb0
--- Ringtoets/Common/test/Ringtoets.Common.IO.Test/Readers/XElementExtensionsTest.cs (.../XElementExtensionsTest.cs) (revision 92dcf3ff33f93a75b92ac8fcd05441ce35332edb)
+++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/Readers/XElementExtensionsTest.cs (.../XElementExtensionsTest.cs) (revision 52d5e1c2609a6f84238d88e20aa135d28de24cb0)
@@ -294,6 +294,39 @@
}
[Test]
+ public void GetConvertedValueFromDescendantStringElement_DescendantElementInvalidToConvert_ThrowException()
+ {
+ // Setup
+ const string descendantElementName = "value";
+ const string elementValue = "three";
+
+ var element = new XElement("Root", new XElement(descendantElementName, elementValue));
+
+ // Call
+ TestDelegate call = () => element.GetConvertedValueFromDescendantStringElement(descendantElementName);
+
+ // Assert
+ Exception exception = Assert.Throws(call);
+ Assert.AreEqual($"{elementValue} is not a valid value for Double.", exception.Message);
+ }
+
+ [Test]
+ public void GetConvertedValueFromDescendantStringElement_DescendantElementNotSupported_ThrowNotSupportedException()
+ {
+ // Setup
+ const string descendantElementName = "value";
+ const string elementValue = "3";
+
+ var element = new XElement("Root", new XElement(descendantElementName, elementValue));
+
+ // Call
+ TestDelegate call = () => element.GetConvertedValueFromDescendantStringElement(descendantElementName);
+
+ // Assert
+ Assert.Throws(call);
+ }
+
+ [Test]
[TestCase(true)]
[TestCase(false)]
public void GetConvertedValueFromDescendantStringElement_ValidDescendantElement_ReturnValue(bool value)
@@ -327,6 +360,20 @@
}
[Test]
+ public void GetConvertedValueFromDescendantDoubleElement_ParentElementNull_ThrowArgumentNullException()
+ {
+ // Setup
+ XElement element = null;
+
+ // Call
+ TestDelegate test = () => element.GetConvertedValueFromDescendantDoubleElement("0");
+
+ // Assert
+ var exception = Assert.Throws(test);
+ Assert.AreEqual("parentElement", exception.ParamName);
+ }
+
+ [Test]
public void GetConvertedValueFromDescendantDoubleElement_DescendantElementNameNull_ThrowArgumentNullException()
{
// Setup
@@ -341,6 +388,72 @@
}
[Test]
+ public void GetConvertedValueFromDescendantDoubleElement_ConvertFromThrowsException_ThrowException()
+ {
+ // Setup
+ const string descendantElementName = "value";
+ const double elementValue = 1;
+
+ var element = new XElement("Root", new XElement(descendantElementName, elementValue));
+
+ // Call
+ TestDelegate call = () => element.GetConvertedValueFromDescendantDoubleElement(
+ descendantElementName);
+
+ // Assert
+ Exception exception = Assert.Throws(call);
+ Assert.AreEqual($"{elementValue} is not a valid value for the target type.", exception.Message);
+ }
+
+ [Test]
+ public void GetConvertedValueFromDescendantDoubleElement_DescendantElementInvalidFormat_ThrowFormatException()
+ {
+ // Setup
+ const string descendantElementName = "number";
+ const string descendantElementValue = "drie";
+
+ var element = new XElement("Root", new XElement(descendantElementName, descendantElementValue));
+
+ // Call
+ TestDelegate test = () => element.GetConvertedValueFromDescendantDoubleElement(descendantElementName);
+
+ // Assert
+ Assert.Throws(test);
+ }
+
+ [Test]
+ public void GetConvertedValueFromDescendantDoubleElement_DescendantElementOverflows_ThrowOverflowException()
+ {
+ // Setup
+ const string descendantElementName = "number";
+ string descendantElementValue = string.Format(CultureInfo.InvariantCulture, "1{0}", double.MaxValue);
+
+ var element = new XElement("Root", new XElement(descendantElementName, descendantElementValue));
+
+ // Call
+ TestDelegate test = () => element.GetConvertedValueFromDescendantDoubleElement(descendantElementName);
+
+ // Assert
+ Assert.Throws(test);
+ }
+
+ [Test]
+ public void GetConvertedValueFromDescendantDoubleElement_DescendantElementNotSupportedByConverter_ThrowNotSupportedException()
+ {
+ // Setup
+ const string descendantElementName = "number";
+ const string descendantElementValue = "4";
+
+ var element = new XElement("Root", new XElement(descendantElementName, descendantElementValue));
+
+ // Call
+ TestDelegate test = () => element.GetConvertedValueFromDescendantDoubleElement(descendantElementName);
+
+ // Assert
+ Assert.Throws(test);
+ }
+
+ [Test]
[TestCase(0, false)]
[TestCase(-1, true)]
[TestCase(1, true)]
@@ -546,5 +659,13 @@
return base.ConvertFrom(context, culture, value);
}
}
+
+ private class ConverterThrowsExceptionOnConvertFrom : TypeConverter
+ {
+ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
+ {
+ throw new Exception($"{value} is not a valid value for the target type.");
+ }
+ }
}
}
\ No newline at end of file