Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/PipingSoilProfileReaderTest.cs =================================================================== diff -u -rd73739bed38c7aaf880d1c57c8904f9667ee2329 -rd514ce187a1ce571355fd92ca1edf822d943ba39 --- Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/PipingSoilProfileReaderTest.cs (.../PipingSoilProfileReaderTest.cs) (revision d73739bed38c7aaf880d1c57c8904f9667ee2329) +++ Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/PipingSoilProfileReaderTest.cs (.../PipingSoilProfileReaderTest.cs) (revision d514ce187a1ce571355fd92ca1edf822d943ba39) @@ -17,25 +17,53 @@ private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Piping.IO, "PipingSoilProfilesReader"); [Test] - public void ReadProfile_NextNotCalled_ThrowsInvalidOperationException() + public void Constructor_NonExistingPath_ThrowsFileNotFoundException() { // Setup - var testFile = "1dprofile.soil"; - var dbFile = Path.Combine(testDataPath, testFile); + var testFile = Path.Combine(testDataPath, "none.soil"); - using (var pipingSoilProfileReader = new PipingSoilProfileReader(dbFile)) - { - // Call - TestDelegate test = () => - { - pipingSoilProfileReader.ReadProfile(); - }; - // Assert - Assert.Throws(test); - } + // Call + TestDelegate test = () => new PipingSoilProfileReader(testFile); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual(String.Format(Resources.Error_File_0_does_not_exist, testFile), exception.Message); } [Test] + [TestCase(null)] + [TestCase("")] + public void Constructor_FileNullOrEmpty_ThrowsArgumentException(string fileName) + { + // Call + TestDelegate test = () => new PipingSoilProfileReader(fileName); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual(Resources.Error_PathMustBeSpecified, exception.Message); + } + + [Test] + [TestCase("text.txt")] + [TestCase("empty.soil")] + public void Constructor_IncorrectFormatFileOrInvalidSchema_ThrowsPipingSoilProfileReadException(string dbName) + { + // Setup + var dbFile = Path.Combine(testDataPath, dbName); + + // Precondition + Assert.IsTrue(FileHelper.CanOpenFileForWrite(dbFile), "Precondition: file can be opened for edits."); + + // Call + TestDelegate test = () => new PipingSoilProfileReader(dbFile); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual(String.Format(Resources.Error_SoilProfileReadFromDatabase, dbName), exception.Message); + Assert.IsTrue(FileHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] public void ReadProfile_AfterDatabaseHasBeenRead_ThrowsInvalidOperationException() { // Setup @@ -44,41 +72,19 @@ using (var pipingSoilProfileReader = new PipingSoilProfileReader(dbFile)) { - while (pipingSoilProfileReader.Next()) + while (pipingSoilProfileReader.HasNext) { pipingSoilProfileReader.ReadProfile(); } // Call - TestDelegate test = () => - { - pipingSoilProfileReader.ReadProfile(); - }; + TestDelegate test = () => { pipingSoilProfileReader.ReadProfile(); }; // Assert Assert.Throws(test); } } [Test] - public void Next_InvalidSchema_ThrowsPipingSoilProfileReadException() - { - // Setup - var testFile = "empty.soil"; - var dbFile = Path.Combine(testDataPath, testFile); - - using (var pipingSoilProfileReader = new PipingSoilProfileReader(dbFile)) - { - // Call - TestDelegate test = () => - { - pipingSoilProfileReader.Next(); - }; - // Assert - Assert.Throws(test); - } - } - - [Test] public void Dispose_AfterConstruction_CorrectlyReleasesFile() { // Setup @@ -117,74 +123,20 @@ } [Test] - public void Constructor_NonExistingPath_ThrowsFileNotFoundException() - { - // Setup - var testFile = Path.Combine(testDataPath, "none.soil"); - - // Call - TestDelegate test = () => new PipingSoilProfileReader(testFile); - - // Assert - var exception = Assert.Throws(test); - Assert.AreEqual(String.Format(Resources.Error_File_0_does_not_exist, testFile), exception.Message); - } - - [Test] - [TestCase(null)] - [TestCase("")] - public void Constructor_FileNullOrEmpty_ThrowsArgumentException(string fileName) - { - // Call - TestDelegate test = () => new PipingSoilProfileReader(fileName); - - // Assert - var exception = Assert.Throws(test); - Assert.AreEqual(Resources.Error_PathMustBeSpecified, exception.Message); - } - - [Test] - public void Next_IncorrectFormatFile_ThrowsPipingSoilProfileReadException() - { - // Setup - var dbName = "text"; - var dbFile = Path.Combine(testDataPath, dbName + ".txt"); - - using (var pipingSoilProfileReader = new PipingSoilProfileReader(dbFile)) - { - // Call - TestDelegate test = () => pipingSoilProfileReader.Next(); - - // Assert - var exception = Assert.Throws(test); - Assert.AreEqual(String.Format(Resources.Error_SoilProfileReadFromDatabase, dbName), exception.Message); - } - } - - [Test] public void ReadProfile_DatabaseWith1DProfile3Layers_ReturnsProfile() { // Setup var testFile = "1dprofile.soil"; var dbFile = Path.Combine(testDataPath, testFile); var reader = new PipingSoilProfileReader(dbFile); + PipingSoilProfile profile; - var skipped = 0; + // Call - while (reader.Next()) - { - try - { - profile = reader.ReadProfile(); - } - catch (PipingSoilProfileReadException) - { - skipped++; - } - } + profile = reader.ReadProfile(); // Assert - Assert.AreEqual(0, skipped); + Assert.AreEqual(3,profile.Layers.Count()); } [Test] @@ -199,7 +151,7 @@ var result = new Collection(); // Call - while (pipingSoilProfilesReader.Next()) + while (pipingSoilProfilesReader.HasNext) { result.Add(pipingSoilProfilesReader.ReadProfile()); } @@ -211,21 +163,25 @@ } [Test] - public void Read_DatabaseWithNullValueForBottom_ThrowsPipingSoilProfileReadException() + public void Read_DatabaseProfileWithInvalid2dLayerGeometry_SkipsTheProfile() { // Setup - var testFile = "invalidbottom.soil"; + var testFile = "invalid2dGeometry.soil"; using (var pipingSoilProfilesReader = new PipingSoilProfileReader(Path.Combine(testDataPath, testFile))) { - pipingSoilProfilesReader.Next(); - // Call - TestDelegate test = () => pipingSoilProfilesReader.ReadProfile(); + TestDelegate profile = () => pipingSoilProfilesReader.ReadProfile(); + Func profile2 = () => pipingSoilProfilesReader.ReadProfile(); // Assert - var exception = Assert.Throws(test); - var message = String.Format(Resources.PipingSoilProfileReader_InvalidValueOnColumn, "Bottom"); + var exception = Assert.Throws(profile); + var message = String.Format(Resources.PipingSoilProfileReader_CouldNotParseGeometryOfLayer_0_InProfile_1_, 1, "Profile"); Assert.AreEqual(message, exception.Message); + + var pipingSoilProfile = profile2(); + Assert.AreEqual("Profile2", pipingSoilProfile.Name); + Assert.AreEqual(3, pipingSoilProfile.Layers.Count()); + Assert.IsTrue(FileHelper.CanOpenFileForWrite(testFile)); } } @@ -266,21 +222,22 @@ { // Call var result = new Collection(); - var skippedProfiles = 0; + var skipped = 0; - while (pipingSoilProfilesReader.Next()) + while (pipingSoilProfilesReader.HasNext) { try { result.Add(pipingSoilProfilesReader.ReadProfile()); } catch { - skippedProfiles++; + skipped++; } } // Assert + Assert.AreEqual(0, skipped); Assert.AreEqual(1, result.Count); Assert.AreEqual(-2.1, result[0].Bottom); CollectionAssert.AreEqual(new[] @@ -302,7 +259,7 @@ ICollection result = new List(); int skipped = 0; - while (pipingSoilProfilesReader.Next()) + while (pipingSoilProfilesReader.HasNext) { try { @@ -379,7 +336,32 @@ CollectionAssert.AreEquivalent(new[] { - 9,8,8,6,6,5,5,6,4,4,3,3,7,7,7,5,5,5,6,6,4,5,4,4,2,3 + 9, + 8, + 8, + 6, + 6, + 5, + 5, + 6, + 4, + 4, + 3, + 3, + 7, + 7, + 7, + 5, + 5, + 5, + 6, + 6, + 4, + 5, + 4, + 4, + 2, + 3 }, result.Select(p => p.Layers.Count())); var firstProfile = result.FirstOrDefault(l => l.Name == "AD640M00_Segment_36005_1D1");