Index: Ringtoets/Common/src/Ringtoets.Common.IO/ReferenceLinesMetaReader.cs =================================================================== diff -u -r239ea628125c5ec4004a843abce2d269af93c431 -r50b203a8d5c805fc548fb6b92c70a847190da6a5 --- Ringtoets/Common/src/Ringtoets.Common.IO/ReferenceLinesMetaReader.cs (.../ReferenceLinesMetaReader.cs) (revision 239ea628125c5ec4004a843abce2d269af93c431) +++ Ringtoets/Common/src/Ringtoets.Common.IO/ReferenceLinesMetaReader.cs (.../ReferenceLinesMetaReader.cs) (revision 50b203a8d5c805fc548fb6b92c70a847190da6a5) @@ -210,7 +210,7 @@ private static IEnumerable GetSectionGeometry(MapFeature lineFeature) { var mapGeometries = lineFeature.MapGeometries.ToArray(); - if (mapGeometries.Length > 1) + if (mapGeometries.Length != 1) { return Enumerable.Empty(); } Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/ReferenceLineMetaImporter/ShapeFileWithInvalidNormValue/ShapeFileWithInvalidNormValue.dbf =================================================================== diff -u Binary files differ Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/ReferenceLineMetaImporter/ShapeFileWithInvalidNormValue/ShapeFileWithInvalidNormValue.prj =================================================================== diff -u --- Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/ReferenceLineMetaImporter/ShapeFileWithInvalidNormValue/ShapeFileWithInvalidNormValue.prj (revision 0) +++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/ReferenceLineMetaImporter/ShapeFileWithInvalidNormValue/ShapeFileWithInvalidNormValue.prj (revision 50b203a8d5c805fc548fb6b92c70a847190da6a5) @@ -0,0 +1 @@ +PROJCS["Amersfoort_RD_New",GEOGCS["GCS_Amersfoort",DATUM["D_Amersfoort",SPHEROID["Bessel_1841",6377397.155,299.1528128]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Double_Stereographic"],PARAMETER["latitude_of_origin",52.15616055555555],PARAMETER["central_meridian",5.38763888888889],PARAMETER["scale_factor",0.9999079],PARAMETER["false_easting",155000],PARAMETER["false_northing",463000],UNIT["Meter",1]] \ No newline at end of file Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/ReferenceLineMetaImporter/ShapeFileWithInvalidNormValue/ShapeFileWithInvalidNormValue.qpj =================================================================== diff -u --- Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/ReferenceLineMetaImporter/ShapeFileWithInvalidNormValue/ShapeFileWithInvalidNormValue.qpj (revision 0) +++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/ReferenceLineMetaImporter/ShapeFileWithInvalidNormValue/ShapeFileWithInvalidNormValue.qpj (revision 50b203a8d5c805fc548fb6b92c70a847190da6a5) @@ -0,0 +1 @@ +PROJCS["Amersfoort / RD New",GEOGCS["Amersfoort",DATUM["Amersfoort",SPHEROID["Bessel 1841",6377397.155,299.1528128,AUTHORITY["EPSG","7004"]],TOWGS84[565.4171,50.3319,465.5524,-0.398957388243134,0.343987817378283,-1.87740163998045,4.0725],AUTHORITY["EPSG","6289"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4289"]],PROJECTION["Oblique_Stereographic"],PARAMETER["latitude_of_origin",52.15616055555555],PARAMETER["central_meridian",5.38763888888889],PARAMETER["scale_factor",0.9999079],PARAMETER["false_easting",155000],PARAMETER["false_northing",463000],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["X",EAST],AXIS["Y",NORTH],AUTHORITY["EPSG","28992"]] Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/ReferenceLineMetaImporter/ShapeFileWithInvalidNormValue/ShapeFileWithInvalidNormValue.shp =================================================================== diff -u Binary files differ Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/ReferenceLineMetaImporter/ShapeFileWithInvalidNormValue/ShapeFileWithInvalidNormValue.shx =================================================================== diff -u Binary files differ Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/ReferenceLineMetaImporter/ShapeWithoutPoints/ShapeWithoutPoints.dbf =================================================================== diff -u Binary files differ Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/ReferenceLineMetaImporter/ShapeWithoutPoints/ShapeWithoutPoints.prj =================================================================== diff -u --- Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/ReferenceLineMetaImporter/ShapeWithoutPoints/ShapeWithoutPoints.prj (revision 0) +++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/ReferenceLineMetaImporter/ShapeWithoutPoints/ShapeWithoutPoints.prj (revision 50b203a8d5c805fc548fb6b92c70a847190da6a5) @@ -0,0 +1 @@ +PROJCS["Amersfoort_RD_New",GEOGCS["GCS_Amersfoort",DATUM["D_Amersfoort",SPHEROID["Bessel_1841",6377397.155,299.1528128]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Double_Stereographic"],PARAMETER["latitude_of_origin",52.15616055555555],PARAMETER["central_meridian",5.38763888888889],PARAMETER["scale_factor",0.9999079],PARAMETER["false_easting",155000],PARAMETER["false_northing",463000],UNIT["Meter",1]] \ No newline at end of file Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/ReferenceLineMetaImporter/ShapeWithoutPoints/ShapeWithoutPoints.qpj =================================================================== diff -u --- Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/ReferenceLineMetaImporter/ShapeWithoutPoints/ShapeWithoutPoints.qpj (revision 0) +++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/ReferenceLineMetaImporter/ShapeWithoutPoints/ShapeWithoutPoints.qpj (revision 50b203a8d5c805fc548fb6b92c70a847190da6a5) @@ -0,0 +1 @@ +PROJCS["Amersfoort / RD New",GEOGCS["Amersfoort",DATUM["Amersfoort",SPHEROID["Bessel 1841",6377397.155,299.1528128,AUTHORITY["EPSG","7004"]],TOWGS84[565.4171,50.3319,465.5524,-0.398957388243134,0.343987817378283,-1.87740163998045,4.0725],AUTHORITY["EPSG","6289"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4289"]],PROJECTION["Oblique_Stereographic"],PARAMETER["latitude_of_origin",52.15616055555555],PARAMETER["central_meridian",5.38763888888889],PARAMETER["scale_factor",0.9999079],PARAMETER["false_easting",155000],PARAMETER["false_northing",463000],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["X",EAST],AXIS["Y",NORTH],AUTHORITY["EPSG","28992"]] Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/ReferenceLineMetaImporter/ShapeWithoutPoints/ShapeWithoutPoints.shp =================================================================== diff -u Binary files differ Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/ReferenceLineMetaImporter/ShapeWithoutPoints/ShapeWithoutPoints.shx =================================================================== diff -u Binary files differ Index: Ringtoets/Integration/src/Ringtoets.Integration.Forms/Commands/AssessmentSectionFromFileCommandHandler.cs =================================================================== diff -u -r2d0a1ea654af9fb4685e01c7d4883884c8a9249f -r50b203a8d5c805fc548fb6b92c70a847190da6a5 --- Ringtoets/Integration/src/Ringtoets.Integration.Forms/Commands/AssessmentSectionFromFileCommandHandler.cs (.../AssessmentSectionFromFileCommandHandler.cs) (revision 2d0a1ea654af9fb4685e01c7d4883884c8a9249f) +++ Ringtoets/Integration/src/Ringtoets.Integration.Forms/Commands/AssessmentSectionFromFileCommandHandler.cs (.../AssessmentSectionFromFileCommandHandler.cs) (revision 50b203a8d5c805fc548fb6b92c70a847190da6a5) @@ -23,13 +23,18 @@ using System.Collections.Generic; using System.Linq; using System.Windows.Forms; +using Core.Common.Gui; +using Core.Common.Gui.Forms.ViewHost; using Core.Common.IO.Exceptions; using log4net; using Ringtoets.Common.Data.AssessmentSection; +using Ringtoets.Common.Data.FailureMechanism; +using Ringtoets.Common.Forms.Helpers; using Ringtoets.Common.IO; using Ringtoets.Common.IO.Exceptions; using Ringtoets.Integration.Data; using Ringtoets.Integration.Forms.Properties; +using BaseResources = Core.Common.Base.Properties.Resources; namespace Ringtoets.Integration.Forms.Commands { @@ -39,55 +44,129 @@ public class AssessmentSectionFromFileCommandHandler { private static readonly ILog log = LogManager.GetLogger(typeof(AssessmentSectionFromFileCommandHandler)); + private readonly string shapeFileDirectory = RingtoetsSettingsHelper.GetCommonDocumentsRingtoetsShapeFileDirectory(); private readonly IWin32Window dialogParent; + private readonly IProjectOwner projectOwner; + private readonly IDocumentViewController viewController; private IEnumerable settings; private IEnumerable referenceLineMetas = new List(); /// /// Initializes a new instance of the class. /// /// The parent of the dialog. - public AssessmentSectionFromFileCommandHandler(IWin32Window dialogParent) + /// The class owning the application project. + /// The document view controller. + public AssessmentSectionFromFileCommandHandler(IWin32Window dialogParent, IProjectOwner projectOwner, IDocumentViewController viewController) { + if (dialogParent == null) + { + throw new ArgumentNullException("dialogParent"); + } + if (projectOwner == null) + { + throw new ArgumentNullException("projectOwner"); + } + if (viewController == null) + { + throw new ArgumentNullException("viewController"); + } this.dialogParent = dialogParent; + this.projectOwner = projectOwner; + this.viewController = viewController; } /// - /// Creates a new , based upon the in a dialog selected , which is derived from the shape file in . + /// Creates an /// - /// The path to the folder where a shape file should be read. - /// The newly created . - /// Thrown when: - /// - /// The shape file does not contain any polylines. - /// The shape file does not contain the required attributes. - /// The assessment section ids in the shape file are not unique or are missing. - /// - /// Thrown when: - /// - /// points to an invalid directory. - /// The path does not contain any shape files. - /// - public IAssessmentSection CreateAssessmentSectionFromFile(string folderpath) + public void CreateAssessmentSectionFromFile() { - ValidateAssessmentSectionSettings(); - ValidateReferenceLineMetas(folderpath); + if (!TryValidate()) + { + return; + } + var assessmentSection = GetAssessmentSectionFromDialog(); + if (assessmentSection == null || !(projectOwner.Project is RingtoetsProject)) + { + return; + } + + SetAssessmentSectionToProject((RingtoetsProject) projectOwner.Project, assessmentSection); + } + + #region Dialog + + private AssessmentSection GetAssessmentSectionFromDialog() + { using (var dialog = CreateReferenceLineMetaSelectionDialogWithItems()) { if (dialog.ShowDialog() != DialogResult.OK) { return null; } + var selectedItem = dialog.SelectedReferenceLineMeta; return selectedItem == null ? null : CreateAssessmentSection(selectedItem, dialog.SelectedNorm); } } - private IAssessmentSection CreateAssessmentSection(ReferenceLineMeta selectedItem, int? norm) + private ReferenceLineMetaSelectionDialog CreateReferenceLineMetaSelectionDialogWithItems() { - IAssessmentSection assessmentSection; + return new ReferenceLineMetaSelectionDialog(dialogParent, referenceLineMetas); + } + + #endregion + + #region Set AssessmentSection to Project + + private static void SetFailureMechanismsValueN(AssessmentSection assessmentSection, int n) + { + assessmentSection.GrassCoverErosionInwards.GeneralInput.N = n; + assessmentSection.HeightStructures.GeneralInput.N = n; + + } + + private void SetAssessmentSectionToProject(RingtoetsProject ringtoetsProject, AssessmentSection assessmentSection) + { + assessmentSection.Name = GetUniqueForAssessmentSectionName(ringtoetsProject.AssessmentSections, assessmentSection.Name); + ringtoetsProject.AssessmentSections.Add(assessmentSection); + ringtoetsProject.NotifyObservers(); + + viewController.OpenViewForData(assessmentSection); + } + + private static string GetUniqueForAssessmentSectionName(IEnumerable assessmentSections, string baseName) + { + return NamingHelper.GetUniqueName(assessmentSections, baseName, a => a.Name); + } + + #endregion + + #region Create AssessmentSection + + private static AssessmentSection CreateDikeAssessmentSection() + { + var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike); + return assessmentSection; + } + + private static AssessmentSection CreateDikeAssessmentSection(AssessmentSectionSettings settings) + { + var assessmentSection = CreateDikeAssessmentSection(); + SetFailureMechanismsValueN(assessmentSection, settings.N); + return assessmentSection; + } + + private static AssessmentSection CreateDuneAssessmentSection() + { + return new AssessmentSection(AssessmentSectionComposition.Dune); + } + + private AssessmentSection CreateAssessmentSection(ReferenceLineMeta selectedItem, int? norm) + { + AssessmentSection assessmentSection; var settingOfSelectedAssessmentSection = settings.FirstOrDefault(s => s.AssessmentSectionId == selectedItem.AssessmentSectionId); if (settingOfSelectedAssessmentSection == null) { @@ -123,44 +202,35 @@ return assessmentSection; } - private static void SetFailureMechanismsValueN(AssessmentSection assessmentSection, int n) - { - assessmentSection.GrassCoverErosionInwards.GeneralInput.N = n; - assessmentSection.HeightStructures.GeneralInput.N = n; - } + #endregion - private ReferenceLineMetaSelectionDialog CreateReferenceLineMetaSelectionDialogWithItems() - { - return new ReferenceLineMetaSelectionDialog(dialogParent, referenceLineMetas); - } + #region Validators - #region Create AssessmentSection - - private static AssessmentSection CreateDikeAssessmentSection() + private bool TryValidate() { - var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike); - return assessmentSection; - } + ValidateAssessmentSectionSettings(); - private static AssessmentSection CreateDikeAssessmentSection(AssessmentSectionSettings settings) - { - var assessmentSection = CreateDikeAssessmentSection(); - SetFailureMechanismsValueN(assessmentSection, settings.N); - return assessmentSection; + try + { + ValidateReferenceLineMetas(); + } + catch (CriticalFileValidationException exception) + { + MessageBox.Show(exception.Message, BaseResources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); + log.Warn(exception.Message, exception.InnerException); + return false; + } + catch (CriticalFileReadException exception) + { + log.Error(exception.Message, exception.InnerException); + return false; + } + return true; } - private static IAssessmentSection CreateDuneAssessmentSection() + private void ValidateReferenceLineMetas() { - return new AssessmentSection(AssessmentSectionComposition.Dune); - } - - #endregion - - #region Validators - - private void ValidateReferenceLineMetas(string folderpath) - { - var importer = new ReferenceLineMetaImporter(folderpath); + var importer = new ReferenceLineMetaImporter(shapeFileDirectory); referenceLineMetas = importer.GetReferenceLineMetas(); if (!referenceLineMetas.Any()) Index: Ringtoets/Integration/src/Ringtoets.Integration.Plugin/RingtoetsPlugin.cs =================================================================== diff -u -r2d0a1ea654af9fb4685e01c7d4883884c8a9249f -r50b203a8d5c805fc548fb6b92c70a847190da6a5 --- Ringtoets/Integration/src/Ringtoets.Integration.Plugin/RingtoetsPlugin.cs (.../RingtoetsPlugin.cs) (revision 2d0a1ea654af9fb4685e01c7d4883884c8a9249f) +++ Ringtoets/Integration/src/Ringtoets.Integration.Plugin/RingtoetsPlugin.cs (.../RingtoetsPlugin.cs) (revision 50b203a8d5c805fc548fb6b92c70a847190da6a5) @@ -48,7 +48,6 @@ using Ringtoets.Common.Forms.TreeNodeInfos; using Ringtoets.Common.Forms.Views; using Ringtoets.Common.IO; -using Ringtoets.Common.IO.Exceptions; using Ringtoets.GrassCoverErosionInwards.Data; using Ringtoets.GrassCoverErosionInwards.Forms.PresentationObjects; using Ringtoets.HeightStructures.Data; @@ -58,7 +57,6 @@ using Ringtoets.Integration.Data; using Ringtoets.Integration.Data.StandAlone; using Ringtoets.Integration.Data.StandAlone.SectionResults; -using Ringtoets.Integration.Forms; using Ringtoets.Integration.Forms.Commands; using Ringtoets.Integration.Forms.PresentationObjects; using Ringtoets.Integration.Forms.PropertyClasses; @@ -202,7 +200,7 @@ #endregion - private readonly string shapeFileDirectory = RingtoetsSettingsHelper.GetCommonDocumentsRingtoetsShapeFileDirectory(); + private AssessmentSectionFromFileCommandHandler assessmentSectionFromFileCommandHandler; public override IRibbonCommandHandler RibbonCommandHandler { @@ -226,64 +224,13 @@ } } - /// - /// Returns a new , based upon the in a dialog selected . - /// - /// The newly created . - /// Thrown when is null. - public IAssessmentSection GetAssessmentSectionFromFile() + public override void Activate() { - if (Gui == null) - { - throw new InvalidOperationException("Gui must be set."); - } - - IAssessmentSection assessmentSection = null; - try - { - var assessmentSectionHandler = new AssessmentSectionFromFileCommandHandler(Gui.MainWindow); - assessmentSection = assessmentSectionHandler.CreateAssessmentSectionFromFile(shapeFileDirectory); - } - catch (CriticalFileValidationException exception) - { - MessageBox.Show(exception.Message, BaseResources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); - log.Warn(exception.Message, exception.InnerException); - } - catch (CriticalFileReadException exception) - { - log.Error(exception.Message, exception.InnerException); - } - - return assessmentSection; + base.Activate(); + assessmentSectionFromFileCommandHandler = new AssessmentSectionFromFileCommandHandler(Gui.MainWindow, Gui, Gui.DocumentViewController); } /// - /// Adds to and ensures a unique name. - /// - /// The to add the to. - /// The to add. - public void SetAssessmentSectionToProject(RingtoetsProject ringtoetsProject, AssessmentSection assessmentSection) - { - if (ringtoetsProject == null) - { - throw new ArgumentNullException("ringtoetsProject"); - } - if (assessmentSection == null) - { - throw new ArgumentNullException("assessmentSection"); - } - - assessmentSection.Name = GetUniqueForAssessmentSectionName(ringtoetsProject.AssessmentSections, assessmentSection.Name); - ringtoetsProject.AssessmentSections.Add(assessmentSection); - ringtoetsProject.NotifyObservers(); - - if (Gui != null) - { - Gui.DocumentViewController.OpenViewForData(assessmentSection); - } - } - - /// /// Returns all instances provided for data of . /// public override IEnumerable GetPropertyInfos() @@ -526,7 +473,7 @@ RingtoetsCommonFormsResources.RingtoetsProject_DisplayName, RingtoetsCommonFormsResources.RingtoetsProject_ToolTip, GuiResources.PlusIcon, - (s, e) => SetAssessmentSectionFromFileToProject(nodeData)); + (s, e) => assessmentSectionFromFileCommandHandler.CreateAssessmentSectionFromFile()); return Gui.Get(nodeData, treeViewControl) .AddCustomItem(addItem) @@ -543,16 +490,6 @@ }; } - private void SetAssessmentSectionFromFileToProject(RingtoetsProject ringtoetsProject) - { - var assessmentSection = GetAssessmentSectionFromFile(); - if (!(assessmentSection is AssessmentSection)) - { - return; - } - SetAssessmentSectionToProject(ringtoetsProject, (AssessmentSection) assessmentSection); - } - private static ViewInfo, IEnumerable, TView> CreateFailureMechanismResultViewInfo() where TResult : FailureMechanismSectionResult where TView : FailureMechanismResultView Index: Ringtoets/Integration/test/Ringtoets.Integration.Forms.Test/Commands/AssessmentSectionFromFileCommandHandlerTest.cs =================================================================== diff -u -r37235d0863116292cc4b095dcf2d19cf6d14c6b2 -r50b203a8d5c805fc548fb6b92c70a847190da6a5 --- Ringtoets/Integration/test/Ringtoets.Integration.Forms.Test/Commands/AssessmentSectionFromFileCommandHandlerTest.cs (.../AssessmentSectionFromFileCommandHandlerTest.cs) (revision 37235d0863116292cc4b095dcf2d19cf6d14c6b2) +++ Ringtoets/Integration/test/Ringtoets.Integration.Forms.Test/Commands/AssessmentSectionFromFileCommandHandlerTest.cs (.../AssessmentSectionFromFileCommandHandlerTest.cs) (revision 50b203a8d5c805fc548fb6b92c70a847190da6a5) @@ -20,18 +20,22 @@ // All rights reserved. using System; +using System.Collections; +using System.Collections.Generic; using System.IO; using System.Linq; +using System.Reflection; using System.Windows.Forms; using Core.Common.Base.Geometry; using Core.Common.Controls.DataGrid; -using Core.Common.IO.Exceptions; +using Core.Common.Gui; +using Core.Common.Gui.Forms.ViewHost; using Core.Common.TestUtil; using NUnit.Extensions.Forms; using NUnit.Framework; using Rhino.Mocks; using Ringtoets.Common.Data.AssessmentSection; -using Ringtoets.Common.IO.Exceptions; +using Ringtoets.Common.Forms.Helpers; using Ringtoets.Integration.Data; using Ringtoets.Integration.Forms.Commands; @@ -43,54 +47,127 @@ private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, "ReferenceLineMetaImporter"); [Test] - public void CreateAssessmentSectionFromFile_InvalidDirectory_ThrowsCriticalFileReadException() + public void Constructor_ParentDialogNull_ThrowsArgumentNullException() { // Setup var mockRepository = new MockRepository(); - var parentDialog = mockRepository.StrictMock(); + var parentDialogMock = mockRepository.StrictMock(); + var viewControllerMock = mockRepository.StrictMock(); mockRepository.ReplayAll(); - AssessmentSectionFromFileCommandHandler assessmentSectionFromFile = new AssessmentSectionFromFileCommandHandler(parentDialog); + // Call + TestDelegate call = () => new AssessmentSectionFromFileCommandHandler(null, parentDialogMock, viewControllerMock); + + // Assert + var paramName = Assert.Throws(call).ParamName; + Assert.AreEqual("dialogParent", paramName); + } + + [Test] + public void Constructor_ProjectOwnerNull_ThrowsArgumentNullException() + { + // Setup + var mockRepository = new MockRepository(); + var parentDialogMock = mockRepository.StrictMock(); + var viewControllerMock = mockRepository.StrictMock(); + mockRepository.ReplayAll(); + + // Call + TestDelegate call = () => new AssessmentSectionFromFileCommandHandler(parentDialogMock, null, viewControllerMock); + + // Assert + var paramName = Assert.Throws(call).ParamName; + Assert.AreEqual("projectOwner", paramName); + } + + [Test] + public void Constructor_ViewControllerNull_ThrowsArgumentNullException() + { + // Setup + var mockRepository = new MockRepository(); + var parentDialogMock = mockRepository.StrictMock(); + var projectOwnerMock = mockRepository.StrictMock(); + mockRepository.ReplayAll(); + + // Call + TestDelegate call = () => new AssessmentSectionFromFileCommandHandler(parentDialogMock, projectOwnerMock, null); + + // Assert + var paramName = Assert.Throws(call).ParamName; + Assert.AreEqual("viewController", paramName); + } + + [Test] + public void CreateAssessmentSectionFromFile_InvalidDirectory_LogsWarningProjectOwnerNotUpdated() + { + // Setup + var mockRepository = new MockRepository(); + var parentDialogMock = mockRepository.StrictMock(); + var projectOwnerMock = mockRepository.StrictMock(); + var viewControllerMock = mockRepository.StrictMock(); + mockRepository.ReplayAll(); + + AssessmentSectionFromFileCommandHandler assessmentSectionFromFile = new AssessmentSectionFromFileCommandHandler(parentDialogMock, projectOwnerMock, viewControllerMock); + string pathToNonExistingFolder = Path.Combine(testDataPath, "I do not exist"); + SetShapeFileDirectory(assessmentSectionFromFile, pathToNonExistingFolder); // Call - TestDelegate call = () => assessmentSectionFromFile.CreateAssessmentSectionFromFile(pathToNonExistingFolder); + Action action = () => assessmentSectionFromFile.CreateAssessmentSectionFromFile(); // Assert - Assert.Throws(call); + var expectedMessage = string.Format("Fout bij het lezen van bestand '{0}': De map locatie is ongeldig.", pathToNonExistingFolder); + TestHelper.AssertLogMessageIsGenerated(action, expectedMessage); mockRepository.VerifyAll(); } [Test] - public void CreateAssessmentSectionFromFile_validDirectoryWithEmptyShapeFile_ThrowsCriticalFileValidationException() + public void CreateAssessmentSectionFromFile_validDirectoryWithEmptyShapeFile_LogsWarningProjectOwnerNotUpdated() { // Setup var mockRepository = new MockRepository(); - var parentDialog = mockRepository.StrictMock(); + var parentDialogMock = mockRepository.StrictMock(); + var projectOwnerMock = mockRepository.StrictMock(); + var viewControllerMock = mockRepository.StrictMock(); mockRepository.ReplayAll(); - AssessmentSectionFromFileCommandHandler assessmentSectionFromFile = new AssessmentSectionFromFileCommandHandler(parentDialog); + AssessmentSectionFromFileCommandHandler assessmentSectionFromFile = new AssessmentSectionFromFileCommandHandler(parentDialogMock, projectOwnerMock, viewControllerMock); + string pathValidFolder = Path.Combine(testDataPath, "EmptyShapeFile"); + SetShapeFileDirectory(assessmentSectionFromFile, pathValidFolder); + string messageText = null; + DialogBoxHandler = (name, wnd) => + { + var messageBox = new MessageBoxTester(wnd); + messageText = messageBox.Text; + messageBox.ClickOk(); + }; + // Call - TestDelegate call = () => assessmentSectionFromFile.CreateAssessmentSectionFromFile(pathValidFolder); + Action action = () => assessmentSectionFromFile.CreateAssessmentSectionFromFile(); // Assert - CriticalFileValidationException exception = Assert.Throws(call); - Assert.AreEqual("Er kunnen geen trajecten gelezen worden uit het shape bestand.", exception.Message); + const string expectedMessage = "Er kunnen geen trajecten gelezen worden uit het shape bestand."; + TestHelper.AssertLogMessageIsGenerated(action, expectedMessage); + Assert.AreEqual(expectedMessage, messageText); mockRepository.VerifyAll(); } [Test] - public void CreateAssessmentSectionFromFile_ValidDirectoryUserClicksCancel_ReturnsNull() + public void CreateAssessmentSectionFromFile_ValidDirectoryUserClicksCancel_ProjectOwnerNotUpdated() { // Setup var mockRepository = new MockRepository(); - var parentDialog = mockRepository.Stub(); + var parentDialogStub = mockRepository.Stub(); + var projectOwnerMock = mockRepository.StrictMock(); + var viewControllerMock = mockRepository.StrictMock(); mockRepository.ReplayAll(); - var assessmentSectionFromFile = new AssessmentSectionFromFileCommandHandler(parentDialog); + AssessmentSectionFromFileCommandHandler assessmentSectionFromFile = new AssessmentSectionFromFileCommandHandler(parentDialogStub, projectOwnerMock, viewControllerMock); + string pathValidFolder = Path.Combine(testDataPath, "ValidShapeFile"); + SetShapeFileDirectory(assessmentSectionFromFile, pathValidFolder); DialogBoxHandler = (name, wnd) => { @@ -99,23 +176,28 @@ }; // Call - IAssessmentSection assessmentSection = assessmentSectionFromFile.CreateAssessmentSectionFromFile(pathValidFolder); + assessmentSectionFromFile.CreateAssessmentSectionFromFile(); // Assert - Assert.IsNull(assessmentSection); mockRepository.VerifyAll(); } [Test] - public void CreateAssessmentSectionFromFile_ValidDirectoryOkClicked_ReturnsFirstReadAssessmentSection() + public void CreateAssessmentSectionFromFile_ValidDirectoryOkClicked_SetsFirstReadAssessmentSection() { // Setup var mockRepository = new MockRepository(); - var parentDialog = mockRepository.Stub(); + var parentDialogStub = mockRepository.Stub(); + var project = new RingtoetsProject(); + var projectOwnerStub = mockRepository.Stub(); + projectOwnerStub.Project = project; + var viewControllerMock = mockRepository.StrictMock(); + viewControllerMock.Expect(dvc => dvc.OpenViewForData(null)).IgnoreArguments().Return(true); mockRepository.ReplayAll(); - var assessmentSectionFromFile = new AssessmentSectionFromFileCommandHandler(parentDialog); + AssessmentSectionFromFileCommandHandler assessmentSectionFromFile = new AssessmentSectionFromFileCommandHandler(parentDialogStub, projectOwnerStub, viewControllerMock); string pathValidFolder = Path.Combine(testDataPath, "ValidShapeFile"); + SetShapeFileDirectory(assessmentSectionFromFile, pathValidFolder); int rowCount = 0; DialogBoxHandler = (name, wnd) => @@ -127,25 +209,72 @@ }; // Call - var assessmentSection = (AssessmentSection) assessmentSectionFromFile.CreateAssessmentSectionFromFile(pathValidFolder); + assessmentSectionFromFile.CreateAssessmentSectionFromFile(); // Assert Assert.AreEqual(3, rowCount); + var assessmentSection = project.AssessmentSections.FirstOrDefault(); Assert.IsNotNull(assessmentSection); AssertAssessmentSection(TestAssessmentSection1_2(true), assessmentSection); mockRepository.VerifyAll(); } + + [Test] + public void CreateAssessmentSectionFromFile_ValidDirectoryOkClickedForDuplicateAssessmentSection_SetsFirstReadAssessmentSectionWithUniqueName() + { + // Setup + var mockRepository = new MockRepository(); + var parentDialogStub = mockRepository.Stub(); + var project = new RingtoetsProject(); + var projectOwnerStub = mockRepository.Stub(); + projectOwnerStub.Project = project; + var viewControllerMock = mockRepository.StrictMock(); + viewControllerMock.Expect(dvc => dvc.OpenViewForData(null)).IgnoreArguments().Return(true); + mockRepository.ReplayAll(); + AssessmentSectionFromFileCommandHandler assessmentSectionFromFile = new AssessmentSectionFromFileCommandHandler(parentDialogStub, projectOwnerStub, viewControllerMock); + string pathValidFolder = Path.Combine(testDataPath, "ValidShapeFile"); + SetShapeFileDirectory(assessmentSectionFromFile, pathValidFolder); + + project.AssessmentSections.Add(TestAssessmentSection1_2(true)); + var expectedAssessmentSectionName = NamingHelper.GetUniqueName(project.AssessmentSections, "1-2", a => a.Name); + + DialogBoxHandler = (name, wnd) => + { + var selectionDialog = (ReferenceLineMetaSelectionDialog) new FormTester(name).TheObject; + new ButtonTester("Ok", selectionDialog).Click(); + }; + + // Call + assessmentSectionFromFile.CreateAssessmentSectionFromFile(); + + // Assert + Assert.AreEqual(2, project.AssessmentSections.Count); + var assessmentSection = project.AssessmentSections[1]; + Assert.IsNotNull(assessmentSection); + + var expectedAssessmentSection = TestAssessmentSection1_2(true); + expectedAssessmentSection.Name = expectedAssessmentSectionName; + AssertAssessmentSection(expectedAssessmentSection, assessmentSection); + mockRepository.VerifyAll(); + } + [Test] - public void CreateAssessmentSectionFromFile_ValidDirectoryLowLimitSelectedOkClicked_ReturnsFirstReadAssessmentSectionWithLowLimit() + public void CreateAssessmentSectionFromFile_ValidDirectoryLowLimitSelectedOkClicked_SetsFirstReadAssessmentSectionWithLowLimit() { // Setup var mockRepository = new MockRepository(); - var parentDialog = mockRepository.Stub(); + var parentDialogStub = mockRepository.Stub(); + var project = new RingtoetsProject(); + var projectOwnerStub = mockRepository.Stub(); + projectOwnerStub.Project = project; + var viewControllerMock = mockRepository.StrictMock(); + viewControllerMock.Expect(dvc => dvc.OpenViewForData(null)).IgnoreArguments().Return(true); mockRepository.ReplayAll(); - var assessmentSectionFromFile = new AssessmentSectionFromFileCommandHandler(parentDialog); + AssessmentSectionFromFileCommandHandler assessmentSectionFromFile = new AssessmentSectionFromFileCommandHandler(parentDialogStub, projectOwnerStub, viewControllerMock); string pathValidFolder = Path.Combine(testDataPath, "ValidShapeFile"); + SetShapeFileDirectory(assessmentSectionFromFile, pathValidFolder); DialogBoxHandler = (name, wnd) => { @@ -156,24 +285,31 @@ }; // Call - var assessmentSection = (AssessmentSection) assessmentSectionFromFile.CreateAssessmentSectionFromFile(pathValidFolder); + assessmentSectionFromFile.CreateAssessmentSectionFromFile(); // Assert + var assessmentSection = project.AssessmentSections.FirstOrDefault(); Assert.IsNotNull(assessmentSection); AssertAssessmentSection(TestAssessmentSection1_2(false), assessmentSection); mockRepository.VerifyAll(); } [Test] - public void CreateAssessmentSectionFromFile_SecondRowSelectedOkClicked_ReturnsSecondReadAssessmentSection() + public void CreateAssessmentSectionFromFile_SecondRowSelectedOkClicked_SetsSecondReadAssessmentSection() { // Setup var mockRepository = new MockRepository(); - var parentDialog = mockRepository.Stub(); + var parentDialogStub = mockRepository.Stub(); + var project = new RingtoetsProject(); + var projectOwnerStub = mockRepository.Stub(); + projectOwnerStub.Project = project; + var viewControllerMock = mockRepository.StrictMock(); + viewControllerMock.Expect(dvc => dvc.OpenViewForData(null)).IgnoreArguments().Return(true); mockRepository.ReplayAll(); - var assessmentSectionFromFile = new AssessmentSectionFromFileCommandHandler(parentDialog); + AssessmentSectionFromFileCommandHandler assessmentSectionFromFile = new AssessmentSectionFromFileCommandHandler(parentDialogStub, projectOwnerStub, viewControllerMock); string pathValidFolder = Path.Combine(testDataPath, "ValidShapeFile"); + SetShapeFileDirectory(assessmentSectionFromFile, pathValidFolder); int rowCount = 0; DialogBoxHandler = (name, wnd) => @@ -188,25 +324,32 @@ }; // Call - AssessmentSection assessmentSection = (AssessmentSection) assessmentSectionFromFile.CreateAssessmentSectionFromFile(pathValidFolder); + assessmentSectionFromFile.CreateAssessmentSectionFromFile(); // Assert Assert.AreEqual(3, rowCount); + var assessmentSection = project.AssessmentSections.FirstOrDefault(); Assert.IsNotNull(assessmentSection); AssertAssessmentSection(TestAssessmentSection2_1(), assessmentSection); mockRepository.VerifyAll(); } [Test] - public void CreateAssessmentSectionFromFile_ThirdRowSelectedOkClicked_ReturnsThirdReadAssessmentSection() + public void CreateAssessmentSectionFromFile_ThirdRowSelectedOkClicked_SetsThirdReadAssessmentSection() { // Setup var mockRepository = new MockRepository(); - var parentDialog = mockRepository.Stub(); + var parentDialogStub = mockRepository.Stub(); + var project = new RingtoetsProject(); + var projectOwnerStub = mockRepository.Stub(); + projectOwnerStub.Project = project; + var viewControllerMock = mockRepository.StrictMock(); + viewControllerMock.Expect(dvc => dvc.OpenViewForData(null)).IgnoreArguments().Return(true); mockRepository.ReplayAll(); - var assessmentSectionFromFile = new AssessmentSectionFromFileCommandHandler(parentDialog); + AssessmentSectionFromFileCommandHandler assessmentSectionFromFile = new AssessmentSectionFromFileCommandHandler(parentDialogStub, projectOwnerStub, viewControllerMock); string pathValidFolder = Path.Combine(testDataPath, "ValidShapeFile"); + SetShapeFileDirectory(assessmentSectionFromFile, pathValidFolder); DialogBoxHandler = (name, wnd) => { @@ -218,19 +361,129 @@ new ButtonTester("Ok", selectionDialog).Click(); }; - AssessmentSection assessmentSection = null; - // Call - Action call = () => assessmentSection = (AssessmentSection) assessmentSectionFromFile.CreateAssessmentSectionFromFile(pathValidFolder); + Action call = () => assessmentSectionFromFile.CreateAssessmentSectionFromFile(); // Assert const string expectedMessage = @"Er zijn geen instellingen gevonden voor het geselecteerde traject. Standaardinstellingen zullen gebruikt worden."; TestHelper.AssertLogMessageIsGenerated(call, expectedMessage); + var assessmentSection = project.AssessmentSections.FirstOrDefault(); Assert.IsNotNull(assessmentSection); AssertAssessmentSection(TestAssessmentSection3_3(), assessmentSection); mockRepository.VerifyAll(); } + [Test] + public void CreateAssessmentSectionFromFile_ShapeWithoutPointsOkClicked_LogsAndSetsAssessmentSectionWithoutReferenceLine() + { + // Setup + var mockRepository = new MockRepository(); + var parentDialogStub = mockRepository.Stub(); + var project = new RingtoetsProject(); + var projectOwnerStub = mockRepository.Stub(); + projectOwnerStub.Project = project; + var viewControllerMock = mockRepository.StrictMock(); + viewControllerMock.Expect(dvc => dvc.OpenViewForData(null)).IgnoreArguments().Return(true); + mockRepository.ReplayAll(); + + AssessmentSectionFromFileCommandHandler assessmentSectionFromFile = new AssessmentSectionFromFileCommandHandler(parentDialogStub, projectOwnerStub, viewControllerMock); + string pathValidFolder = Path.Combine(testDataPath, "ShapeWithoutPoints"); + SetShapeFileDirectory(assessmentSectionFromFile, pathValidFolder); + + DialogBoxHandler = (name, wnd) => + { + var selectionDialog = (ReferenceLineMetaSelectionDialog) new FormTester(name).TheObject; + var grid = (DataGridViewControl) new ControlTester("ReferenceLineMetaDataGridViewControl", selectionDialog).TheObject; + var dataGridView = grid.Controls.OfType().First(); + dataGridView[0, 1].Selected = true; + + new ButtonTester("Ok", selectionDialog).Click(); + }; + + // Call + Action call = () => assessmentSectionFromFile.CreateAssessmentSectionFromFile(); + + // Assert + const string expectedMessage = @"Het importeren van de referentielijn is mislukt."; + TestHelper.AssertLogMessageIsGenerated(call, expectedMessage); + var assessmentSection = project.AssessmentSections.FirstOrDefault(); + Assert.IsNotNull(assessmentSection); + + var expectedAssessmentSection = TestAssessmentSection1_2(true); + expectedAssessmentSection.ReferenceLine.SetGeometry(new Point2D[]{}); + + AssertAssessmentSection(expectedAssessmentSection, assessmentSection); + mockRepository.VerifyAll(); + } + + [Test] + public void CreateAssessmentSectionFromFile_ShapeWithoutInvalidNormOkClicked_LogsAndSetsAssessmentSectionWithoutNorm() + { + // Setup + var mockRepository = new MockRepository(); + var parentDialogStub = mockRepository.Stub(); + var project = new RingtoetsProject(); + var projectOwnerStub = mockRepository.Stub(); + projectOwnerStub.Project = project; + var viewControllerMock = mockRepository.StrictMock(); + viewControllerMock.Expect(dvc => dvc.OpenViewForData(null)).IgnoreArguments().Return(true); + mockRepository.ReplayAll(); + + AssessmentSectionFromFileCommandHandler assessmentSectionFromFile = new AssessmentSectionFromFileCommandHandler(parentDialogStub, projectOwnerStub, viewControllerMock); + string pathValidFolder = Path.Combine(testDataPath, "ShapeFileWithInvalidNormValue"); + SetShapeFileDirectory(assessmentSectionFromFile, pathValidFolder); + + DialogBoxHandler = (name, wnd) => + { + var selectionDialog = (ReferenceLineMetaSelectionDialog) new FormTester(name).TheObject; + var grid = (DataGridViewControl) new ControlTester("ReferenceLineMetaDataGridViewControl", selectionDialog).TheObject; + var dataGridView = grid.Controls.OfType().First(); + dataGridView[0, 0].Selected = true; + + new ButtonTester("Ok", selectionDialog).Click(); + }; + + // Call + Action call = () => assessmentSectionFromFile.CreateAssessmentSectionFromFile(); + + // Assert + string expectedMessage = string.Format("De waarde '{0}' kan niet gezet.", "0"); + TestHelper.AssertLogMessageIsGenerated(call, expectedMessage); + var assessmentSection = project.AssessmentSections.FirstOrDefault(); + Assert.IsNotNull(assessmentSection); + + var expectedAssessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike) + { + Id = "1-2", + }; + expectedAssessmentSection.GrassCoverErosionInwards.GeneralInput.N = 2; + expectedAssessmentSection.HeightStructures.GeneralInput.N = 2; + expectedAssessmentSection.ReferenceLine = new ReferenceLine(); + expectedAssessmentSection.ReferenceLine.SetGeometry(new[] + { + new Point2D(160679.9250, 475072.583), + new Point2D(160892.0751, 474315.4917) + }); + + AssertAssessmentSection(expectedAssessmentSection, assessmentSection); + mockRepository.VerifyAll(); + } + + private static void SetShapeFileDirectory(AssessmentSectionFromFileCommandHandler commandHandler, string nonExistingFolder) + { + string privateShapeFileDirectoryName = "shapeFileDirectory"; + Type commandHandlerType = commandHandler.GetType(); + FieldInfo fieldInfo = commandHandlerType.GetField(privateShapeFileDirectoryName, BindingFlags.NonPublic | BindingFlags.Instance); + if (fieldInfo == null) + { + Assert.Fail("Unable to find private field '{0}'", privateShapeFileDirectoryName); + } + else + { + fieldInfo.SetValue(commandHandler, nonExistingFolder); + } + } + #region Test Assessment Sections private static AssessmentSection TestAssessmentSection1_2(bool useSignalingValue) Index: Ringtoets/Integration/test/Ringtoets.Integration.Plugin.Test/RingtoetsPluginTest.cs =================================================================== diff -u -r2d0a1ea654af9fb4685e01c7d4883884c8a9249f -r50b203a8d5c805fc548fb6b92c70a847190da6a5 --- Ringtoets/Integration/test/Ringtoets.Integration.Plugin.Test/RingtoetsPluginTest.cs (.../RingtoetsPluginTest.cs) (revision 2d0a1ea654af9fb4685e01c7d4883884c8a9249f) +++ Ringtoets/Integration/test/Ringtoets.Integration.Plugin.Test/RingtoetsPluginTest.cs (.../RingtoetsPluginTest.cs) (revision 50b203a8d5c805fc548fb6b92c70a847190da6a5) @@ -23,18 +23,13 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Reflection; -using System.Windows.Forms; using System.Windows.Threading; -using Core.Common.Base; using Core.Common.Base.IO; using Core.Common.Base.Storage; -using Core.Common.Controls.DataGrid; using Core.Common.Controls.TreeView; using Core.Common.Gui; using Core.Common.Gui.ContextMenu; using Core.Common.Gui.Forms.MainWindow; -using Core.Common.Gui.Forms.ViewHost; using Core.Common.Gui.Plugin; using Core.Common.Gui.Settings; using Core.Common.TestUtil; @@ -53,13 +48,11 @@ using Ringtoets.HydraRing.Data; using Ringtoets.Integration.Data; using Ringtoets.Integration.Data.StandAlone.SectionResults; -using Ringtoets.Integration.Forms; using Ringtoets.Integration.Forms.PresentationObjects; using Ringtoets.Integration.Forms.PropertyClasses; using Ringtoets.Integration.Forms.Views; using Ringtoets.Integration.Forms.Views.SectionResultViews; using Ringtoets.Integration.Plugin.FileImporters; -using PropertyInfo = Core.Common.Gui.Plugin.PropertyInfo; using RingtoetsFormsResources = Ringtoets.Integration.Forms.Properties.Resources; using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources; @@ -68,8 +61,6 @@ [TestFixture] public class RingtoetsPluginTest : NUnitFormTest { - private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, "ReferenceLineMetaImporter"); - [Test] [STAThread] // For creation of XAML UI component public void DefaultConstructor_ExpectedValues() @@ -461,244 +452,5 @@ Assert.AreEqual(1, importers.Count(i => i is FailureMechanismSectionsImporter)); } } - - [Test] - public void SetAssessmentSectionToProject_ProjectIsNull_ThrowsArgumentNullException() - { - // Setup - var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike); - using (var plugin = new RingtoetsPlugin()) - { - // Call - TestDelegate call = () => plugin.SetAssessmentSectionToProject(null, assessmentSection); - - // Assert - var paramName = Assert.Throws(call).ParamName; - Assert.AreEqual("ringtoetsProject", paramName); - } - } - - [Test] - public void SetAssessmentSectionToProject_AssessmentSectionIsNull_ThrowsArgumentNullException() - { - // Setup - var project = new RingtoetsProject(); - using (var plugin = new RingtoetsPlugin()) - { - // Call - TestDelegate call = () => plugin.SetAssessmentSectionToProject(project, null); - - // Assert - var paramName = Assert.Throws(call).ParamName; - Assert.AreEqual("assessmentSection", paramName); - } - } - - [Test] - public void SetAssessmentSectionToProject_WithData_UpdateDataAndNotifyObservers() - { - // Setup - var mockRepository = new MockRepository(); - var projectObserver = mockRepository.StrictMock(); - projectObserver.Expect(o => o.UpdateObserver()); - var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike); - - var documentViewControllerMock = mockRepository.StrictMock(); - documentViewControllerMock.Expect(dvc => dvc.OpenViewForData(assessmentSection)).Return(true); - var guiMock = mockRepository.StrictMock(); - guiMock.Expect(g => g.DocumentViewController).Return(documentViewControllerMock); - guiMock.Stub(g => g.SelectionChanged += null).IgnoreArguments(); - guiMock.Stub(g => g.SelectionChanged -= null).IgnoreArguments(); - guiMock.Expect(g => g.ProjectOpened += null).IgnoreArguments(); - guiMock.Stub(g => g.ProjectOpened -= null).IgnoreArguments(); - mockRepository.ReplayAll(); - - var project = new RingtoetsProject(); - - using (var plugin = new RingtoetsPlugin()) - { - plugin.Gui = guiMock; - - project.Attach(projectObserver); - - // Precondition - CollectionAssert.IsEmpty(project.AssessmentSections); - - // Call - plugin.SetAssessmentSectionToProject(project, assessmentSection); - } - // Assert - Assert.AreEqual(1, project.AssessmentSections.Count); - CollectionAssert.Contains(project.AssessmentSections, assessmentSection); - mockRepository.VerifyAll(); - } - - [Test] - public void WhenAddingAssessmentSection_GivenProjectHasAssessmentSection_ThenAddedAssessmentSectionHasUniqueName() - { - // Setup - var project = new RingtoetsProject(); - var assessmentSection1 = new AssessmentSection(AssessmentSectionComposition.Dike); - var assessmentSection2 = new AssessmentSection(AssessmentSectionComposition.Dike); - - using (var plugin = new RingtoetsPlugin()) - { - plugin.SetAssessmentSectionToProject(project, assessmentSection1); - - // Call - plugin.SetAssessmentSectionToProject(project, assessmentSection2); - } - - // Assert - CollectionAssert.AllItemsAreUnique(project.AssessmentSections.Select(section => section.Name)); - } - - [Test] - public void GetAssessmentSectionFromFile_GuiIsNull_ThrowsInvalidOperationException() - { - using (var plugin = new RingtoetsPlugin()) - { - SetShapeFileDirectory(plugin, ""); - - // Call - TestDelegate call = () => plugin.GetAssessmentSectionFromFile(); - - // Assert - Assert.Throws(call); - } - } - - [Test] - public void GetAssessmentSectionFromFile_NoShapeFileFound_LogsWarningReturnsNull() - { - // Setup - var mockRepository = new MockRepository(); - var windows = mockRepository.Stub(); - var guiMock = mockRepository.StrictMock(); - guiMock.Stub(g => g.SelectionChanged += null).IgnoreArguments(); - guiMock.Stub(g => g.SelectionChanged -= null).IgnoreArguments(); - guiMock.Expect(g => g.ProjectOpened += null).IgnoreArguments(); - guiMock.Stub(g => g.ProjectOpened -= null).IgnoreArguments(); - guiMock.Expect(g => g.MainWindow).Return(windows); - mockRepository.ReplayAll(); - - string nonExistingFolder = @"c:\folderDoesNotExist\"; - - using (var plugin = new RingtoetsPlugin()) - { - plugin.Gui = guiMock; - - SetShapeFileDirectory(plugin, nonExistingFolder); - - // Call - IAssessmentSection assessmentSection = null; - Action action = () => assessmentSection = plugin.GetAssessmentSectionFromFile(); - - // Assert - TestHelper.AssertLogMessageIsGenerated(action, string.Format("Fout bij het lezen van bestand '{0}': De map locatie is ongeldig.", nonExistingFolder)); - Assert.IsNull(assessmentSection); - } - - mockRepository.VerifyAll(); - } - - [Test] - public void GetAssessmentSectionFromFile_validDirectoryWithEmptyShapeFile_LogsWarningShowsMessageReturnsNull() - { - // Setup - var mockRepository = new MockRepository(); - var windows = mockRepository.Stub(); - var guiMock = mockRepository.StrictMock(); - guiMock.Stub(g => g.SelectionChanged += null).IgnoreArguments(); - guiMock.Stub(g => g.SelectionChanged -= null).IgnoreArguments(); - guiMock.Expect(g => g.ProjectOpened += null).IgnoreArguments(); - guiMock.Stub(g => g.ProjectOpened -= null).IgnoreArguments(); - guiMock.Expect(g => g.MainWindow).Return(windows); - mockRepository.ReplayAll(); - - string pathValidFolder = Path.Combine(testDataPath, "EmptyShapeFile"); - - using (var plugin = new RingtoetsPlugin()) - { - plugin.Gui = guiMock; - SetShapeFileDirectory(plugin, pathValidFolder); - - string messageText = null; - DialogBoxHandler = (name, wnd) => - { - var messageBox = new MessageBoxTester(wnd); - messageText = messageBox.Text; - messageBox.ClickOk(); - }; - - // Call - IAssessmentSection assessmentSection = null; - Action action = () => assessmentSection = plugin.GetAssessmentSectionFromFile(); - - // Assert - const string expectedErrorMessage = "Er kunnen geen trajecten gelezen worden uit het shape bestand."; - TestHelper.AssertLogMessageIsGenerated(action, expectedErrorMessage); - Assert.AreEqual(expectedErrorMessage, messageText); - Assert.IsNull(assessmentSection); - } - - mockRepository.VerifyAll(); - } - - [Test] - public void GetAssessmentSectionFromFile_validDirectoryOkClicked_ReturnsFirstAssessmentSection() - { - // Setup - var mockRepository = new MockRepository(); - var windows = mockRepository.Stub(); - var guiMock = mockRepository.StrictMock(); - guiMock.Stub(g => g.SelectionChanged += null).IgnoreArguments(); - guiMock.Stub(g => g.SelectionChanged -= null).IgnoreArguments(); - guiMock.Expect(g => g.ProjectOpened += null).IgnoreArguments(); - guiMock.Stub(g => g.ProjectOpened -= null).IgnoreArguments(); - guiMock.Expect(g => g.MainWindow).Return(windows); - mockRepository.ReplayAll(); - - string pathValidFolder = Path.Combine(testDataPath, "ValidShapeFile"); - - using (var plugin = new RingtoetsPlugin()) - { - plugin.Gui = guiMock; - SetShapeFileDirectory(plugin, pathValidFolder); - - DialogBoxHandler = (name, wnd) => - { - var selectionDialog = (ReferenceLineMetaSelectionDialog) new FormTester(name).TheObject; - var grid = (DataGridViewControl) new ControlTester("ReferenceLineMetaDataGridViewControl", selectionDialog).TheObject; - var dataGridView = grid.Controls.OfType().First(); - dataGridView[0, 0].Selected = true; - new ButtonTester("Ok", selectionDialog).Click(); - }; - - // Call - IAssessmentSection assessmentSection = plugin.GetAssessmentSectionFromFile(); - - // Assert - Assert.IsInstanceOf(assessmentSection); - Assert.AreEqual("1-2", assessmentSection.Id); - } - - mockRepository.VerifyAll(); - } - - private static void SetShapeFileDirectory(RingtoetsPlugin plugin, string nonExistingFolder) - { - string privateShapeFileDirectoryName = "shapeFileDirectory"; - Type ringtoetsPluginType = plugin.GetType(); - FieldInfo fieldInfo = ringtoetsPluginType.GetField(privateShapeFileDirectoryName, BindingFlags.NonPublic | BindingFlags.Instance); - if (fieldInfo == null) - { - Assert.Fail("Unable to find private field '{0}'", privateShapeFileDirectoryName); - } - else - { - fieldInfo.SetValue(plugin, nonExistingFolder); - } - } } } \ No newline at end of file