Index: DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/Translations.xml =================================================================== diff -u -r3019 -r3044 --- DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/Translations.xml (.../Translations.xml) (revision 3019) +++ DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/Translations.xml (.../Translations.xml) (revision 3044) @@ -578,7 +578,8 @@ - + + Index: DamClients/DamUI/trunk/src/Dam/Tests/StiImporter/SoilProfile2DImporterTest.cs =================================================================== diff -u -r3036 -r3044 --- DamClients/DamUI/trunk/src/Dam/Tests/StiImporter/SoilProfile2DImporterTest.cs (.../SoilProfile2DImporterTest.cs) (revision 3036) +++ DamClients/DamUI/trunk/src/Dam/Tests/StiImporter/SoilProfile2DImporterTest.cs (.../SoilProfile2DImporterTest.cs) (revision 3044) @@ -43,11 +43,8 @@ [TestCase("")] public void Import_InvalidSoilProfileDirectory_ThrowsArgumentException(string invalidSoilProfileDirectory) { - // Setup - var importer = new SoilProfile2DImporter(); - // Call - TestDelegate call = () => importer.Import(invalidSoilProfileDirectory, new Segment(), new SoilList()); + TestDelegate call = () => SoilProfile2DImporter.Import(invalidSoilProfileDirectory, new Segment(), new SoilList()); // Assert Assert.That(call, Throws.ArgumentException @@ -57,11 +54,8 @@ [Test] public void Import_SegmentNull_ThrowsArgumentNullException() { - // Setup - var importer = new SoilProfile2DImporter(); - // Call - TestDelegate call = () => importer.Import(string.Empty, null, new SoilList()); + TestDelegate call = () => SoilProfile2DImporter.Import(string.Empty, null, new SoilList()); // Assert Assert.That(call, Throws.TypeOf() @@ -72,11 +66,8 @@ [Test] public void Import_AvailableSoilsNull_ThrowsArgumentNullException() { - // Setup - var importer = new SoilProfile2DImporter(); - // Call - TestDelegate call = () => importer.Import(string.Empty, new Segment(), null); + TestDelegate call = () => SoilProfile2DImporter.Import(string.Empty, new Segment(), null); // Assert Assert.That(call, Throws.TypeOf() @@ -102,11 +93,9 @@ "Peat", "Muck" }); - - var importer = new SoilProfile2DImporter(); // Call - IEnumerable soilProfiles = importer.Import(TestDataFolder, segment, availableSoils); + IEnumerable soilProfiles = SoilProfile2DImporter.Import(TestDataFolder, segment, availableSoils); // Assert Assert.That(soilProfiles, Has.Count.EqualTo(2)); @@ -131,7 +120,7 @@ } [Test] - public void Import_WithIncompleteSoilList_ReturnsOnlyCompleteSoilProfilesAndLogsMessage() + public void Import_WithIncompleteSoilList_ThrowsSoilProfileImporterException() { // Setup LogManager.Messages.Clear(); // Clear all messages as it is a singleton @@ -149,75 +138,41 @@ "Muck" }); - var importer = new SoilProfile2DImporter(); - // Call - IEnumerable soilProfiles = importer.Import(TestDataFolder, segment, availableSoils); + TestDelegate call = () => SoilProfile2DImporter.Import(TestDataFolder, segment, availableSoils); // Assert - Assert.That(soilProfiles, Has.Count.EqualTo(1)); - - SoilProfile2D soilProfileOne = soilProfiles.ElementAt(0); - Assert.That(soilProfileOne.Name, Is.EqualTo(profileName)); - CollectionAssert.AreEqual(new[] - { - "Soft Clay", - "Muck" - }, soilProfileOne.Surfaces.Select(s => s.Name)); - - string expectedMessage = $"'{invalidSoilProfile}' contains undefined soils."; - CollectionAssert.AreEqual(new[] - { - expectedMessage - }, LogManager.Messages.Select(m => m.Message)); - CollectionAssert.AreEqual(new[] - { - LogMessageType.Error - }, LogManager.Messages.Select(m => m.MessageType)); - CollectionAssert.AreEqual(new[] - { - importer - }, LogManager.Messages.Select(m => m.Subject)); + Assert.That(call, Throws.Exception.TypeOf() + .With.Message.EqualTo($"'{invalidSoilProfile}' contains undefined soils.")); + } [Test] - public void Import_WithSoilProfileCausingReadException_ReturnsOnlyCompleteSoilProfilesAndLogsMessage() + public void Import_WithSoilProfileCausingReadException_ThrowsSoilProfileImporterException() { // Setup LogManager.Messages.Clear(); // Clear all messages as it is a singleton - const string profileName = "SimpleProfile"; + const string invalidSoilProfileName = "NonExistentSoilProfile"; Segment segment = CreateSegmentWithProfiles(new[] { - $"{profileName}.sti", - "NonExistentSoilProfile" + "SimpleProfile.sti", + invalidSoilProfileName }); SoilList availableSoils = CreateSoilList(new[] { "Soft Clay", "Muck" }); - var importer = new SoilProfile2DImporter(); - // Call - IEnumerable soilProfiles = importer.Import(TestDataFolder, segment, availableSoils); + TestDelegate call = () => SoilProfile2DImporter.Import(TestDataFolder, segment, availableSoils); // Assert - Assert.That(soilProfiles, Has.Count.EqualTo(1)); - - SoilProfile2D soilProfileOne = soilProfiles.ElementAt(0); - Assert.That(soilProfileOne.Name, Is.EqualTo(profileName)); - CollectionAssert.AreEqual(new[] - { - "Soft Clay", - "Muck" - }, soilProfileOne.Surfaces.Select(s => s.Name)); - - LogMessage logMessage = LogManager.Messages.Single(); - Assert.That(logMessage.Message, Is.Not.Empty); - Assert.That(logMessage.MessageType, Is.EqualTo(LogMessageType.Error)); - Assert.That(logMessage.Subject, Is.SameAs(importer)); + var exception = Assert.Throws(call); + Exception innerException = exception.InnerException; + Assert.That(innerException, Is.Not.Null); + Assert.That(exception.Message, Is.EqualTo($"Could not import soil profile '{invalidSoilProfileName}': {innerException.Message}.")); } private static Segment CreateSegmentWithProfiles(IEnumerable soilProfileNames) Index: DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/StiImporter/SoilProfileImporterException.cs =================================================================== diff -u --- DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/StiImporter/SoilProfileImporterException.cs (revision 0) +++ DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/StiImporter/SoilProfileImporterException.cs (revision 3044) @@ -0,0 +1,55 @@ +using System; +using System.Runtime.Serialization; + +namespace Deltares.Dam.Data.StiImporter +{ + /// + /// Exception thrown when the soil profiles could not be successfully imported. + /// + [Serializable] + public class SoilProfileImporterException : Exception + { + /// + /// Initializes a new instance of the class. + /// + public SoilProfileImporterException() + { + } + + /// + /// Initializes a new instance of the class + /// with a specified error message. + /// + /// The message that describes the error. + public SoilProfileImporterException(string message) + : base(message) + { + } + + /// + /// Initializes a new instance of the class with a specified error message + /// and a reference to the inner exception that is the cause of this exception. + /// + /// The error message that explains the reason for the exception. + /// The exception that is the cause of the current exception, + /// or null if no inner exception is specified. + public SoilProfileImporterException(string message, Exception innerException) : base(message, innerException) + { + } + + /// + /// Initializes a new instance of with + /// serialized data. + /// The that holds the serialized + /// object data about the exception being thrown. + /// The that contains contextual + /// information about the source or destination. + /// The parameter is + /// null. + /// The class name is null or + /// is zero (0). + protected SoilProfileImporterException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } + } +} \ No newline at end of file Index: DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/Deltares.Dam.Data.csproj =================================================================== diff -u -r3034 -r3044 --- DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/Deltares.Dam.Data.csproj (.../Deltares.Dam.Data.csproj) (revision 3034) +++ DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/Deltares.Dam.Data.csproj (.../Deltares.Dam.Data.csproj) (revision 3044) @@ -202,6 +202,7 @@ + Index: DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/StiImporter/SoilProfile2DImporter.cs =================================================================== diff -u -r3036 -r3044 --- DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/StiImporter/SoilProfile2DImporter.cs (.../SoilProfile2DImporter.cs) (revision 3036) +++ DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/StiImporter/SoilProfile2DImporter.cs (.../SoilProfile2DImporter.cs) (revision 3044) @@ -25,14 +25,13 @@ using System.Linq; using Deltares.Geotechnics.Soils; using Deltares.Standard.Language; -using Deltares.Standard.Logging; namespace Deltares.Dam.Data.StiImporter { /// /// Importer to import . /// - public class SoilProfile2DImporter + public static class SoilProfile2DImporter { /// /// Imports the that are contained within . @@ -41,15 +40,15 @@ /// The to import the soil profiles for. /// The that contains all the valid soil materials. /// A collection of . - /// - /// Thrown when or is null. + /// Thrown when or is null. /// /// Thrown when /// is null, empty or consist of only whitespaces. /// - /// Thrown when no soil profiles could be read from . + /// Thrown when no soil profiles could be read from . /// - public IEnumerable Import(string soilProfileDirectory, Segment segment, SoilList availableSoils) + /// Thrown when the soil profiles could not be successfully imported. + public static IEnumerable Import(string soilProfileDirectory, Segment segment, SoilList availableSoils) { if (segment == null) { @@ -75,25 +74,19 @@ { string soilProfileFile = profile.SoilGeometry2DName; SoilProfile2D readSoilProfile = ReadSoilProfile(soilProfileDirectory, soilProfileFile); - if (readSoilProfile != null) + if (!IsValidSoilProfile(availableSoils, readSoilProfile)) { - if (IsValidSoilProfile(availableSoils, readSoilProfile)) - { - importedSoilProfiles.Add(readSoilProfile); - } - else - { - string messageFormat = LocalizationManager.GetTranslatedText(this, "ImportSoilProfileError"); - string message = string.Format(messageFormat, soilProfileFile); - LogManager.Messages.Add(new LogMessage(LogMessageType.Error, this, message)); - } + string messageFormat = LocalizationManager.GetTranslatedText(typeof(SoilProfile2DImporter), "ImportSoilProfileErrorUndefinedMaterials"); + throw new SoilProfileImporterException(string.Format(messageFormat, soilProfileFile)); } + + importedSoilProfiles.Add(readSoilProfile); } return importedSoilProfiles; } - private SoilProfile2D ReadSoilProfile(string soilProfileDirectory, string soilProfileFile) + private static SoilProfile2D ReadSoilProfile(string soilProfileDirectory, string soilProfileFile) { string filePath = Path.Combine(soilProfileDirectory, soilProfileFile); @@ -105,10 +98,10 @@ } catch (StiFileReadException e) { - LogManager.Messages.Add(new LogMessage(LogMessageType.Error, this, e.Message)); + string soilProfileName = Path.GetFileNameWithoutExtension(soilProfileFile); + string messageFormat = LocalizationManager.GetTranslatedText(typeof(SoilProfile2DImporter), "ImportSoilProfileErrorReadFailed"); + throw new SoilProfileImporterException(string.Format(messageFormat, soilProfileName, e.Message), e); } - - return null; } private static bool IsValidSoilProfile(SoilList availableSoils, SoilProfile2D profile)