Index: Application/Ringtoets/src/Application.Ringtoets.Storage/BackedUpFileWriter.cs =================================================================== diff -u -r065af7e201b59ec19a17c42e9d772f5e86b31338 -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Application/Ringtoets/src/Application.Ringtoets.Storage/BackedUpFileWriter.cs (.../BackedUpFileWriter.cs) (revision 065af7e201b59ec19a17c42e9d772f5e86b31338) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/BackedUpFileWriter.cs (.../BackedUpFileWriter.cs) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -45,7 +45,7 @@ /// is not a valid path. public BackedUpFileWriter(string targetFilePath) { - FileUtils.ValidateFilePath(targetFilePath); + IOUtils.ValidateFilePath(targetFilePath); this.targetFilePath = targetFilePath; temporaryFilePath = targetFilePath + temporarySuffix; Index: Application/Ringtoets/src/Application.Ringtoets.Storage/StorageSqLite.cs =================================================================== diff -u -r8e5f037ed7df5263d26ba1ada373be196e7a354b -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Application/Ringtoets/src/Application.Ringtoets.Storage/StorageSqLite.cs (.../StorageSqLite.cs) (revision 8e5f037ed7df5263d26ba1ada373be196e7a354b) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/StorageSqLite.cs (.../StorageSqLite.cs) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -250,7 +250,7 @@ /// private static string GetConnectionToExistingFile(string databaseFilePath) { - FileUtils.ValidateFilePath(databaseFilePath); + IOUtils.ValidateFilePath(databaseFilePath); return GetConnectionToFile(databaseFilePath); } @@ -269,7 +269,7 @@ /// private static string GetConnectionToNewFile(string databaseFilePath) { - FileUtils.ValidateFilePath(databaseFilePath); + IOUtils.ValidateFilePath(databaseFilePath); StorageSqliteCreator.CreateDatabaseStructure(databaseFilePath); return GetConnectionToFile(databaseFilePath); } Index: Application/Ringtoets/src/Application.Ringtoets.Storage/StorageSqliteCreator.cs =================================================================== diff -u -r0df7cded06f5afbac08b97e025242ba55c90ec57 -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Application/Ringtoets/src/Application.Ringtoets.Storage/StorageSqliteCreator.cs (.../StorageSqliteCreator.cs) (revision 0df7cded06f5afbac08b97e025242ba55c90ec57) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/StorageSqliteCreator.cs (.../StorageSqliteCreator.cs) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -47,7 +47,7 @@ /// Thrown when executing DatabaseStructure script fails. public static void CreateDatabaseStructure(string databaseFilePath) { - FileUtils.ValidateFilePath(databaseFilePath); + IOUtils.ValidateFilePath(databaseFilePath); if (File.Exists(databaseFilePath)) { Index: Application/Ringtoets/src/Application.Ringtoets/App.xaml.cs =================================================================== diff -u -rd6ef4701f85e2df60c71f0961f42b1c6444bc5ad -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Application/Ringtoets/src/Application.Ringtoets/App.xaml.cs (.../App.xaml.cs) (revision d6ef4701f85e2df60c71f0961f42b1c6444bc5ad) +++ Application/Ringtoets/src/Application.Ringtoets/App.xaml.cs (.../App.xaml.cs) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -186,7 +186,7 @@ { try { - FileUtils.ValidateFilePath(potentialPath); + IOUtils.ValidateFilePath(potentialPath); fileToOpen = potentialPath; return true; } @@ -204,7 +204,7 @@ { try { - FileUtils.DeleteOldFiles(GetLogFileDirectory(), "*.log", numberOfDaysToKeepLogFiles); + IOUtils.DeleteOldFiles(GetLogFileDirectory(), "*.log", numberOfDaysToKeepLogFiles); } catch (Exception e) { Index: Core/Common/src/Core.Common.IO/Readers/SqLiteDatabaseReaderBase.cs =================================================================== diff -u -r8aed37b83aa4d0b9a0932551f8727ceca83e5cc4 -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Core/Common/src/Core.Common.IO/Readers/SqLiteDatabaseReaderBase.cs (.../SqLiteDatabaseReaderBase.cs) (revision 8aed37b83aa4d0b9a0932551f8727ceca83e5cc4) +++ Core/Common/src/Core.Common.IO/Readers/SqLiteDatabaseReaderBase.cs (.../SqLiteDatabaseReaderBase.cs) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -51,7 +51,7 @@ { try { - FileUtils.ValidateFilePath(databaseFilePath); + IOUtils.ValidateFilePath(databaseFilePath); Path = databaseFilePath; if (!File.Exists(databaseFilePath)) Index: Core/Common/src/Core.Common.Utils/Builders/DirectoryWriterErrorMessageBuilder.cs =================================================================== diff -u --- Core/Common/src/Core.Common.Utils/Builders/DirectoryWriterErrorMessageBuilder.cs (revision 0) +++ Core/Common/src/Core.Common.Utils/Builders/DirectoryWriterErrorMessageBuilder.cs (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -0,0 +1,57 @@ +// Copyright (C) Stichting Deltares 2016. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System.Globalization; +using Core.Common.Utils.Extensions; +using Core.Common.Utils.Properties; + +namespace Core.Common.Utils.Builders +{ + /// + /// Class to help create consistent folder writer error messages. + /// + public class DirectoryWriterErrorMessageBuilder + { + private readonly string folderPath; + + /// + /// Initializes a new instance of class. + /// + /// The file path to the directory where the error occurred. + public DirectoryWriterErrorMessageBuilder(string folderPath) + { + this.folderPath = folderPath; + } + + /// + /// Builds the specified error message. + /// + /// The message about the error that occurred. + /// The full error message. + public string Build(string errorMessage) + { + return string.Format(CultureInfo.CurrentCulture, + Resources.Error_Writing_to_Directory_0_CustomMessage_1_, + folderPath, + errorMessage.FirstToLower()); + } + } +} \ No newline at end of file Index: Core/Common/src/Core.Common.Utils/Builders/FileWriterErrorMessageBuilder.cs =================================================================== diff -u -r49c5da81f49a23dd6e66526d264a08bf510e6963 -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Core/Common/src/Core.Common.Utils/Builders/FileWriterErrorMessageBuilder.cs (.../FileWriterErrorMessageBuilder.cs) (revision 49c5da81f49a23dd6e66526d264a08bf510e6963) +++ Core/Common/src/Core.Common.Utils/Builders/FileWriterErrorMessageBuilder.cs (.../FileWriterErrorMessageBuilder.cs) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -48,7 +48,7 @@ public string Build(string errorMessage) { return string.Format(CultureInfo.CurrentCulture, - Resources.Error_Writing_To_File_0_1, + Resources.Error_Writing_to_File_0_CustomMessage_1_, filePath, errorMessage); } Index: Core/Common/src/Core.Common.Utils/Core.Common.Utils.csproj =================================================================== diff -u -r23d1e296e2da4364fbfe346e68d582dfcf966bb0 -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Core/Common/src/Core.Common.Utils/Core.Common.Utils.csproj (.../Core.Common.Utils.csproj) (revision 23d1e296e2da4364fbfe346e68d582dfcf966bb0) +++ Core/Common/src/Core.Common.Utils/Core.Common.Utils.csproj (.../Core.Common.Utils.csproj) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -91,6 +91,7 @@ + @@ -102,7 +103,7 @@ - + Fisheye: Tag 16bd8076863c03907abeee60b16704aba1bdb1a0 refers to a dead (removed) revision in file `Core/Common/src/Core.Common.Utils/FileUtils.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Core/Common/src/Core.Common.Utils/IOUtils.cs =================================================================== diff -u --- Core/Common/src/Core.Common.Utils/IOUtils.cs (revision 0) +++ Core/Common/src/Core.Common.Utils/IOUtils.cs (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -0,0 +1,245 @@ +// Copyright (C) Stichting Deltares 2016. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Security; +using Core.Common.Utils.Builders; +using Core.Common.Utils.Properties; + +namespace Core.Common.Utils +{ + /// + /// Class with reusable file and folder related utility methods. + /// + public static class IOUtils + { + /// + /// Validates the folder path. + /// + /// The folder path to be validated. + /// true if the folder path is valid; false otherwise. + /// A valid folder path: + /// + /// is not empty nor contains only whitespaces. + /// has no access rights to that location. + /// isn't too long. + /// does not contain an invalid ':' character. + /// + public static bool IsValidFolderPath(string path) + { + try + { + ValidateFolderPath(path); + } + catch (ArgumentException) + { + return false; + } + return true; + } + + /// + /// Validates the folder path. + /// + /// The folder path to be validated. + /// Thrown when: + /// + /// The folder path is empty or contains only whitespaces. + /// Caller has no access rights to the folder path. + /// The folder path is too long. + /// The folder path contains an invalid ':' character. + /// + public static void ValidateFolderPath(string path) + { + if (string.IsNullOrWhiteSpace(path)) + { + var message = new DirectoryWriterErrorMessageBuilder(path) + .Build(Resources.FileUtils_ValidateFolderPath_Path_cannot_be_empty); + throw new ArgumentException(message); + } + try + { + string fullPath = Path.GetFullPath(path); + } + catch (SecurityException e) + { + var message = new DirectoryWriterErrorMessageBuilder(path) + .Build(Resources.FileUtils_ValidateFolderPath_No_access_rights_to_folder); + throw new ArgumentException(message, e); + } + catch (PathTooLongException e) + { + var message = new DirectoryWriterErrorMessageBuilder(path) + .Build(Resources.FileUtils_ValidateFolderPath_Folder_path_too_long); + throw new ArgumentException(message, e); + } + catch (NotSupportedException e) + { + var message = new DirectoryWriterErrorMessageBuilder(path) + .Build(Resources.FileUtils_ValidateFolderPath_Folder_path_contains_invalid_character); + throw new ArgumentException(message, e); + } + } + + /// + /// Validates the file path. + /// + /// The file path to be validated. + /// Thrown when is invalid. + /// A valid path: + /// + /// is not empty or null, + /// does not consist out of only whitespace characters, + /// does not contain an invalid character, + /// does not end with a directory or path separator (empty file name). + /// + public static void ValidateFilePath(string path) + { + if (string.IsNullOrWhiteSpace(path)) + { + var message = new FileReaderErrorMessageBuilder(path).Build(Resources.Error_Path_must_be_specified); + throw new ArgumentException(message); + } + + string name; + try + { + name = Path.GetFileName(path); + } + catch (ArgumentException e) + { + var message = new FileReaderErrorMessageBuilder(path) + .Build(string.Format(CultureInfo.CurrentCulture, + Resources.Error_Path_cannot_contain_Characters_0_, + string.Join(", ", Path.GetInvalidFileNameChars()))); + throw new ArgumentException(message, e); + } + if (string.IsNullOrEmpty(name)) + { + var message = new FileReaderErrorMessageBuilder(path).Build(Resources.Error_Path_must_not_point_to_empty_file_name); + throw new ArgumentException(message); + } + } + + /// + /// Validates the file path. + /// + /// The file path to be validated. + /// true if the file path is valid, false otherwise. + /// A valid path: + /// + /// contains not only whitespace, + /// does not contain an invalid character, + /// is not empty or null, + /// does not end with a directory or path separator (empty file name). + /// + public static bool IsValidFilePath(string path) + { + try + { + ValidateFilePath(path); + } + catch (ArgumentException) + { + return false; + } + return true; + } + + /// + /// Searches the files in that match and + /// deletes the files older than days. + /// + /// The directory to search. + /// The search string to match against the names of files in path. + /// The maximum number days since the file was created. + /// Thrown when or is null, is a zero-length string, + /// contains only white space, or contains one or more invalid characters. + /// Thrown when an error occurred while trying to search and delete files in . + public static void DeleteOldFiles(string path, string searchPattern, int numberOfDaysToKeepFiles) + { + if (string.IsNullOrWhiteSpace(path)) + { + throw new ArgumentException(@"No valid value for 'path'.", nameof(path)); + } + if (string.IsNullOrWhiteSpace(searchPattern)) + { + throw new ArgumentException(@"No valid value for 'searchPattern'.", nameof(searchPattern)); + } + try + { + foreach (string logFile in Directory.GetFiles(path, searchPattern).Where( + l => (DateTime.Now - File.GetCreationTime(l)).TotalDays > numberOfDaysToKeepFiles)) + { + File.Delete(logFile); + } + } + catch (Exception e) + { + if (e is ArgumentException || e is IOException || e is NotSupportedException || e is UnauthorizedAccessException) + { + var message = string.Format(CultureInfo.CurrentCulture, + Resources.FileUtils_DeleteOldFiles_Error_occurred_deleting_files_in_folder_0, + path); + throw new IOException(message, e); + } + throw; + } + } + + /// + /// Validates if the file path is writable. + /// + /// The file path to be validated. + /// Thrown when is invalid. + /// A valid path: + /// + /// is not empty or null, + /// does not consist out of only whitespace characters, + /// does not contain an invalid character, + /// does not end with a directory or path separator (empty file name), + /// is writable. + /// + public static void ValidateFilePathIsWritable(string path) + { + ValidateFilePath(path); + + var canWrite = false; + try + { + using (var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write)) + { + canWrite = fs.CanWrite; + } + } + finally + { + if (!canWrite) + { + throw new ArgumentException(string.Format(Resources.Error_General_output_error_0, path), nameof(path)); + } + } + } + } +} \ No newline at end of file Index: Core/Common/src/Core.Common.Utils/Properties/Resources.Designer.cs =================================================================== diff -u -r36c6449038e383f66174db2c09a5bff079f758b2 -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Core/Common/src/Core.Common.Utils/Properties/Resources.Designer.cs (.../Resources.Designer.cs) (revision 36c6449038e383f66174db2c09a5bff079f758b2) +++ Core/Common/src/Core.Common.Utils/Properties/Resources.Designer.cs (.../Resources.Designer.cs) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -22,7 +22,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.17929 +// Runtime Version:4.0.30319.42000 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -181,11 +181,20 @@ } /// + /// Looks up a localized string similar to Fout bij het schrijven naar bestandsmap '{0}': {1}. + /// + public static string Error_Writing_to_Directory_0_CustomMessage_1_ { + get { + return ResourceManager.GetString("Error_Writing_to_Directory_0_CustomMessage_1_", resourceCulture); + } + } + + /// /// Looks up a localized string similar to Fout bij het schrijven naar bestand '{0}': {1}. /// - public static string Error_Writing_To_File_0_1 { + public static string Error_Writing_to_File_0_CustomMessage_1_ { get { - return ResourceManager.GetString("Error_Writing_To_File_0_1", resourceCulture); + return ResourceManager.GetString("Error_Writing_to_File_0_CustomMessage_1_", resourceCulture); } } @@ -209,6 +218,42 @@ } /// + /// Looks up a localized string similar to Het pad naar bestandsmap bevat een ':' op een ongeldige plek.. + /// + public static string FileUtils_ValidateFolderPath_Folder_path_contains_invalid_character { + get { + return ResourceManager.GetString("FileUtils_ValidateFolderPath_Folder_path_contains_invalid_character", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Het pad naar bestandsmap is te lang.. + /// + public static string FileUtils_ValidateFolderPath_Folder_path_too_long { + get { + return ResourceManager.GetString("FileUtils_ValidateFolderPath_Folder_path_too_long", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Geen toegangsrechten tot bestandsmap.. + /// + public static string FileUtils_ValidateFolderPath_No_access_rights_to_folder { + get { + return ResourceManager.GetString("FileUtils_ValidateFolderPath_No_access_rights_to_folder", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Pad naar bestandsmap mag niet leeg of ongedefinieerd zijn.. + /// + public static string FileUtils_ValidateFolderPath_Path_cannot_be_empty { + get { + return ResourceManager.GetString("FileUtils_ValidateFolderPath_Path_cannot_be_empty", resourceCulture); + } + } + + /// /// Looks up a localized string similar to op regel {0}. /// public static string TextFile_On_LineNumber_0_ { Index: Core/Common/src/Core.Common.Utils/Properties/Resources.resx =================================================================== diff -u -r36c6449038e383f66174db2c09a5bff079f758b2 -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Core/Common/src/Core.Common.Utils/Properties/Resources.resx (.../Resources.resx) (revision 36c6449038e383f66174db2c09a5bff079f758b2) +++ Core/Common/src/Core.Common.Utils/Properties/Resources.resx (.../Resources.resx) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -141,7 +141,7 @@ '{0}' is geen geldige expressie voor deze methode. - + Fout bij het schrijven naar bestand '{0}': {1} @@ -165,4 +165,19 @@ Fout bij het lezen van bestand '{0}'{1}{2}: {3} + + Fout bij het schrijven naar bestandsmap '{0}': {1} + + + Pad naar bestandsmap mag niet leeg of ongedefinieerd zijn. + + + Geen toegangsrechten tot bestandsmap. + + + Het pad naar bestandsmap is te lang. + + + Het pad naar bestandsmap bevat een ':' op een ongeldige plek. + \ No newline at end of file Index: Core/Common/test/Core.Common.TestUtil/FileDisposeHelper.cs =================================================================== diff -u -rd4213a7531c7effcd982ca96e799c2f8231b7627 -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Core/Common/test/Core.Common.TestUtil/FileDisposeHelper.cs (.../FileDisposeHelper.cs) (revision d4213a7531c7effcd982ca96e799c2f8231b7627) +++ Core/Common/test/Core.Common.TestUtil/FileDisposeHelper.cs (.../FileDisposeHelper.cs) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -108,7 +108,7 @@ /// private static void CreateFile(string filePath) { - if (FileUtils.IsValidFilePath(filePath)) + if (IOUtils.IsValidFilePath(filePath)) { try { @@ -146,7 +146,7 @@ /// private static void DeleteFile(string filePath) { - if (FileUtils.IsValidFilePath(filePath)) + if (IOUtils.IsValidFilePath(filePath)) { try { Index: Core/Common/test/Core.Common.Utils.Test/Builders/DirectoryWriterErrorMessageBuilderTest.cs =================================================================== diff -u --- Core/Common/test/Core.Common.Utils.Test/Builders/DirectoryWriterErrorMessageBuilderTest.cs (revision 0) +++ Core/Common/test/Core.Common.Utils.Test/Builders/DirectoryWriterErrorMessageBuilderTest.cs (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -0,0 +1,46 @@ +// Copyright (C) Stichting Deltares 2016. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System.IO; +using Core.Common.Utils.Builders; +using NUnit.Framework; + +namespace Core.Common.Utils.Test.Builders +{ + [TestFixture] + public class DirectoryWriterErrorMessageBuilderTest + { + [Test] + public void Build_WithMessage_ReturnsDirectoryWriterErrorMessage() + { + // Setup + string folderPath = Directory.GetCurrentDirectory(); + string customMessage = ""; + + // Call + string message = new DirectoryWriterErrorMessageBuilder(folderPath).Build(customMessage); + + // Assert + string expectedMessage = $"Fout bij het schrijven naar bestandsmap '{folderPath}': {customMessage}"; + Assert.AreEqual(expectedMessage, message); + } + } +} \ No newline at end of file Index: Core/Common/test/Core.Common.Utils.Test/Core.Common.Utils.Test.csproj =================================================================== diff -u -r6a5d7b40b7ba4dcb73e393075338352d194e97c2 -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Core/Common/test/Core.Common.Utils.Test/Core.Common.Utils.Test.csproj (.../Core.Common.Utils.Test.csproj) (revision 6a5d7b40b7ba4dcb73e393075338352d194e97c2) +++ Core/Common/test/Core.Common.Utils.Test/Core.Common.Utils.Test.csproj (.../Core.Common.Utils.Test.csproj) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -84,6 +84,7 @@ + @@ -96,7 +97,7 @@ - + Fisheye: Tag 16bd8076863c03907abeee60b16704aba1bdb1a0 refers to a dead (removed) revision in file `Core/Common/test/Core.Common.Utils.Test/FileUtilsTest.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Core/Common/test/Core.Common.Utils.Test/IOUtilsTest.cs =================================================================== diff -u --- Core/Common/test/Core.Common.Utils.Test/IOUtilsTest.cs (revision 0) +++ Core/Common/test/Core.Common.Utils.Test/IOUtilsTest.cs (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -0,0 +1,394 @@ +// Copyright (C) Stichting Deltares 2016. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System; +using System.IO; +using System.Text; +using System.Threading; +using Core.Common.TestUtil; +using NUnit.Framework; + +namespace Core.Common.Utils.Test +{ + [TestFixture] + public class IOUtilsTest + { + [Test] + [TestCase(null)] + [TestCase("")] + [TestCase(" ")] + public void IsValidFolderPath_InvalidEmptyPath_ReturnFalse(string invalidPath) + { + // Call + bool isFolderPathValid = IOUtils.IsValidFolderPath(invalidPath); + + // Assert + Assert.IsFalse(isFolderPathValid); + } + + [Test] + public void IsValidFolderPath_PathTooLong_ReturnFalse() + { + // Setup + var stringBuilder = new StringBuilder(); + stringBuilder.Append(@"C:\"); + for (int i = 0; i < 300; i++) + { + stringBuilder.Append("A"); + } + stringBuilder.Append(Path.DirectorySeparatorChar); + string tooLongFolderPath = stringBuilder.ToString(); + + // Call + bool isFolderPathValid = IOUtils.IsValidFolderPath(tooLongFolderPath); + + // Assert + Assert.IsFalse(isFolderPathValid); + } + + [Test] + public void IsValidFolderPath_InvalidColonCharacterInPath_ReturnFalse() + { + // Setup + string pathWithInvalidColonCharacter = @"C:\Left:Right"; + + // Call + bool isFolderPathValid = IOUtils.IsValidFolderPath(pathWithInvalidColonCharacter); + + // Assert + Assert.IsFalse(isFolderPathValid); + } + + [Test] + public void IsValidFolderPath_ValidPath_ReturnTrue() + { + // Setup + string path = TestHelper.GetTestDataPath(TestDataPath.Core.Common.Utils); + + // Call + bool isFolderPathValid = IOUtils.IsValidFilePath(path); + + // Assert + Assert.IsTrue(isFolderPathValid); + } + + [Test] + [TestCase(null)] + [TestCase("")] + [TestCase(" ")] + public void ValidateFolderPath_InvalidEmptyPath_ThrowsArgumentException(string invalidPath) + { + // Call + TestDelegate call = () => IOUtils.ValidateFolderPath(invalidPath); + + // Assert + string message = $"Fout bij het schrijven naar bestandsmap '{invalidPath}': pad naar bestandsmap mag niet leeg of ongedefinieerd zijn."; + TestHelper.AssertThrowsArgumentExceptionAndTestMessage(call, message); + } + + [Test] + public void ValidateFolderPath_PathTooLong_ThrowsArgumentException() + { + // Setup + var stringBuilder = new StringBuilder(); + stringBuilder.Append(@"C:\"); + for (int i = 0; i < 300; i++) + { + stringBuilder.Append("A"); + } + stringBuilder.Append(Path.DirectorySeparatorChar); + string tooLongFolderPath = stringBuilder.ToString(); + + // Call + TestDelegate call = () => IOUtils.ValidateFolderPath(tooLongFolderPath); + + // Assert + string message = $"Fout bij het schrijven naar bestandsmap '{tooLongFolderPath}': het pad naar bestandsmap is te lang."; + TestHelper.AssertThrowsArgumentExceptionAndTestMessage(call, message); + } + + [Test] + public void ValidateFolderPath_InvalidColonCharacterInPath_ThrowsArgumentException() + { + // Setup + string folderWithInvalidColonCharacter = @"C:\Left:Right"; + + // Call + TestDelegate call = () => IOUtils.ValidateFolderPath(folderWithInvalidColonCharacter); + + // Assert + string message = $"Fout bij het schrijven naar bestandsmap '{folderWithInvalidColonCharacter}': het pad naar bestandsmap bevat een ':' op een ongeldige plek."; + TestHelper.AssertThrowsArgumentExceptionAndTestMessage(call, message); + } + + [Test] + public void ValidateFolderPath_ValidPath_DoesNotThrow() + { + // Setup + string path = TestHelper.GetTestDataPath(TestDataPath.Core.Common.Utils); + + // Call + TestDelegate call = () => IOUtils.ValidateFolderPath(path); + + // Assert + Assert.DoesNotThrow(call); + } + + [Test] + public void ValidateFilePath_ValidPath_DoesNotThrowAnyExceptions() + { + // Setup + string path = TestHelper.GetTestDataPath(TestDataPath.Core.Common.Utils, "validFile.txt"); + + // Call + TestDelegate call = () => IOUtils.ValidateFilePath(path); + + // Assert + Assert.DoesNotThrow(call); + } + + [Test] + [TestCase(null)] + [TestCase("")] + [TestCase(" ")] + public void ValidateFilePath_InvalidEmptyPath_ThrowsArgumentException(string invalidPath) + { + // Call + TestDelegate call = () => IOUtils.ValidateFilePath(invalidPath); + + // Assert + var exception = Assert.Throws(call); + var expectedMessage = string.Format("Fout bij het lezen van bestand '{0}': bestandspad mag niet leeg of ongedefinieerd zijn.", invalidPath); + Assert.AreEqual(expectedMessage, exception.Message); + } + + [Test] + public void ValidateFilePath_PathContainingInvalidFileCharacters_ThrowsArgumentException() + { + // Setup + string path = TestHelper.GetTestDataPath(TestDataPath.Core.Common.Utils, "validFile.txt"); + char[] invalidFileNameChars = Path.GetInvalidFileNameChars(); + string invalidPath = path.Replace('d', invalidFileNameChars[0]); + + // Call + TestDelegate call = () => IOUtils.ValidateFilePath(invalidPath); + + // Assert + var exception = Assert.Throws(call); + var expectedMessage = string.Format("Fout bij het lezen van bestand '{0}': bestandspad mag niet de volgende tekens bevatten: {1}", + invalidPath, string.Join(", ", invalidFileNameChars)); + Assert.AreEqual(expectedMessage, exception.Message); + } + + [Test] + public void ValidateFilePath_PathEndsWithEmptyFileName_ThrowsArgumentException() + { + // Setup + string folderPath = TestHelper.GetTestDataPath(TestDataPath.Core.Common.Utils) + Path.DirectorySeparatorChar; + + // Call + TestDelegate call = () => IOUtils.ValidateFilePath(folderPath); + + // Assert + var exception = Assert.Throws(call); + var expectedMessage = string.Format("Fout bij het lezen van bestand '{0}': bestandspad mag niet verwijzen naar een lege bestandsnaam.", folderPath); + Assert.AreEqual(expectedMessage, exception.Message); + } + + [Test] + public void IsValidFilePath_ValidPath_ReturnsTrue() + { + // Setup + string path = TestHelper.GetTestDataPath(TestDataPath.Core.Common.Utils, "validFile.txt"); + + // Call + var valid = IOUtils.IsValidFilePath(path); + + // Assert + Assert.IsTrue(valid); + } + + [Test] + [TestCase(null)] + [TestCase("")] + [TestCase(" ")] + public void IsValidFilePath_InvalidEmptyPath_ReturnsFalse(string invalidPath) + { + // Call + bool valid = IOUtils.IsValidFilePath(invalidPath); + + // Assert + Assert.IsFalse(valid); + } + + [Test] + public void IsValidFilePath_PathContainingInvalidFileCharacters_ReturnsFalse() + { + // Setup + string path = TestHelper.GetTestDataPath(TestDataPath.Core.Common.Utils, "validFile.txt"); + char[] invalidFileNameChars = Path.GetInvalidFileNameChars(); + string invalidPath = path.Replace('d', invalidFileNameChars[0]); + + // Call + bool valid = IOUtils.IsValidFilePath(invalidPath); + + // Assert + Assert.IsFalse(valid); + } + + [Test] + public void IsValidFilePath_PathIsActuallyFolder_ReturnsFalse() + { + // Setup + string folderPath = TestHelper.GetTestDataPath(TestDataPath.Core.Common.Utils) + Path.DirectorySeparatorChar; + + // Call + bool valid = IOUtils.IsValidFilePath(folderPath); + + // Assert + Assert.IsFalse(valid); + } + + [Test] + [TestCase(null)] + [TestCase("")] + [TestCase(" ")] + public void DeleteOldFiles_InvalidEmptyPath_ThrowsArgumentException(string invalidPath) + { + // Call + TestDelegate call = () => IOUtils.DeleteOldFiles(invalidPath, "", 0); + + // Assert + string invalidParameterName = Assert.Throws(call).ParamName; + Assert.AreEqual("path", invalidParameterName); + } + + [Test] + [TestCase(null)] + [TestCase("")] + [TestCase(" ")] + public void DeleteOldFiles_InvalidSearchPattern_ThrowsArgumentException(string invalidSearchPattern) + { + // Setup + string path = TestHelper.GetTestDataPath(TestDataPath.Core.Common.Utils); + + // Call + TestDelegate call = () => IOUtils.DeleteOldFiles(path, invalidSearchPattern, 0); + + // Assert + string invalidParameterName = Assert.Throws(call).ParamName; + Assert.AreEqual("searchPattern", invalidParameterName); + } + + [Test] + public void DeleteOldFiles_PathDoesNotExist_ThrowsIOException() + { + // Setup + string path = TestHelper.GetTestDataPath(TestDataPath.Core.Common.Utils, "doesNotExist"); + + // Precondition + Assert.IsFalse(Directory.Exists(path)); + + // Call + TestDelegate call = () => IOUtils.DeleteOldFiles(path, "*.log", 0); + + // Assert + IOException exception = Assert.Throws(call); + var message = string.Format("Er is een fout opgetreden bij het verwijderen van bestanden in de map '{0}'.", path); + Assert.AreEqual(message, exception.Message); + Assert.IsInstanceOf(exception.InnerException); + } + + [Test] + public void DeleteOldFiles_ValidPathWithFile_DeletesFile() + { + // Setup + string path = TestHelper.GetTestDataPath(TestDataPath.Core.Common.Utils); + string filePath = Path.Combine(path, "fileToDelete.log"); + + using (new FileDisposeHelper(filePath)) + { + Thread.Sleep(1); // Sleep 1 ms to make sure File. Create has had enough time to create the file. + + // Call + IOUtils.DeleteOldFiles(path, "*.log", 0); + + // Assert + Thread.Sleep(1); // Sleep 1 ms to make sure File. Delete has had enough time to remove the file. + Assert.IsFalse(File.Exists(filePath)); + } + } + + [Test] + [TestCase("")] + [TestCase(" ")] + [TestCase(null)] + public void ValidateFilePathIsWritable_FilePathNullOrWhiteSpace_ThrowsArgumentException(string filePath) + { + // Call + TestDelegate call = () => IOUtils.ValidateFilePathIsWritable(filePath); + + // Assert + ArgumentException exception = Assert.Throws(call); + Assert.AreEqual($"Fout bij het lezen van bestand '{filePath}': bestandspad mag niet leeg of ongedefinieerd zijn.", exception.Message); + } + + [Test] + public void ValidateFilePathIsWritable_FileNotWritable_ThrowsArgumentException() + { + // Setup + string filename = Path.GetRandomFileName(); + string filePath = TestHelper.GetTestDataPath(TestDataPath.Core.Common.Utils, filename); + + using (new FileDisposeHelper(filePath)) + { + FileAttributes attributes = File.GetAttributes(filePath); + File.SetAttributes(filePath, attributes | FileAttributes.ReadOnly); + + // Call + TestDelegate call = () => IOUtils.ValidateFilePathIsWritable(filePath); + + // Assert + string expectedMessage = $"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'."; + TestHelper.AssertThrowsArgumentExceptionAndTestMessage(call, expectedMessage); + + File.SetAttributes(filePath, attributes); + } + } + + [Test] + public void ValidateFilePathIsWritable_FileWritable_DoesNotThrowException() + { + // Setup + string filename = Path.GetRandomFileName(); + string filePath = TestHelper.GetTestDataPath(TestDataPath.Core.Common.Utils, filename); + + using (new FileDisposeHelper(filePath)) + { + // Call + TestDelegate call = () => IOUtils.ValidateFilePathIsWritable(filePath); + + // Assert + Assert.DoesNotThrow(call); + } + } + } +} \ No newline at end of file Index: Core/Components/src/Core.Components.DotSpatial.Forms/Core.Components.DotSpatial.Forms.csproj =================================================================== diff -u -re13bcfb136c9a532fce9dc2f74bf9576321a90b0 -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Core/Components/src/Core.Components.DotSpatial.Forms/Core.Components.DotSpatial.Forms.csproj (.../Core.Components.DotSpatial.Forms.csproj) (revision e13bcfb136c9a532fce9dc2f74bf9576321a90b0) +++ Core/Components/src/Core.Components.DotSpatial.Forms/Core.Components.DotSpatial.Forms.csproj (.../Core.Components.DotSpatial.Forms.csproj) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -34,6 +34,10 @@ AllRules.ruleset + + ..\..\..\..\packages\BruTile.0.19.0\lib\net40\BruTile.dll + True + False ..\..\..\..\lib\DotSpatial.1.8\DotSpatial.Controls.dll @@ -102,6 +106,7 @@ Copying.Lesser.licenseheader + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + + ..\Resources\stubTile.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + \ No newline at end of file Index: Core/Components/test/Core.Components.DotSpatial.TestUtil/Resources/stubTile.png =================================================================== diff -u Binary files differ Index: Core/Components/test/Core.Components.DotSpatial.TestUtil/TestTileSourceFactory.cs =================================================================== diff -u --- Core/Components/test/Core.Components.DotSpatial.TestUtil/TestTileSourceFactory.cs (revision 0) +++ Core/Components/test/Core.Components.DotSpatial.TestUtil/TestTileSourceFactory.cs (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -0,0 +1,29 @@ +using System.Collections.Generic; +using BruTile; +using BruTile.Wmts; +using Core.Components.DotSpatial.Layer.BruTile.Configurations; +using Core.Components.Gis.Data; + +namespace Core.Components.DotSpatial.TestUtil +{ + public class TestTileSourceFactory : ITileSourceFactory + { + private readonly TestWmtsTileSource wmtsTileSource; + + public TestTileSourceFactory(WmtsMapData backgroundMapData) + { + if (backgroundMapData.IsConfigured) + { + wmtsTileSource = new TestWmtsTileSource(backgroundMapData); + } + } + + public IEnumerable GetWmtsTileSources(string capabilitiesUrl) + { + if (wmtsTileSource != null) + { + yield return wmtsTileSource; + } + } + } +} \ No newline at end of file Index: Core/Components/test/Core.Components.DotSpatial.TestUtil/TestWmtsTileSource.cs =================================================================== diff -u --- Core/Components/test/Core.Components.DotSpatial.TestUtil/TestWmtsTileSource.cs (revision 0) +++ Core/Components/test/Core.Components.DotSpatial.TestUtil/TestWmtsTileSource.cs (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -0,0 +1,100 @@ +// Copyright (C) Stichting Deltares 2016. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System; +using System.Drawing.Imaging; +using System.IO; +using System.Reflection; +using System.Text.RegularExpressions; +using BruTile; +using BruTile.Web; +using BruTile.Wmts; +using Core.Components.DotSpatial.TestUtil.Properties; +using Core.Components.Gis.Data; + +namespace Core.Components.DotSpatial.TestUtil +{ + /// + /// Defines a suitable to most unit test cases related to + /// dealing with . + /// + public class TestWmtsTileSource : HttpTileSource + { + private static byte[] pngTileDataStub; + + /// + /// Create a new instance of suitable to work for + /// a given . + /// + /// The map data to work with. + public TestWmtsTileSource(WmtsMapData backgroundMapData) + : base(CreateWmtsTileSchema(backgroundMapData), null, "Stub schema", null, GetStubTile) + { + var imageFormatExtension = backgroundMapData.PreferredFormat.Split('/')[1]; + if (imageFormatExtension != "png") + { + throw new NotImplementedException($"Please extend this class to support the '*.{imageFormatExtension}' extension."); + } + } + + private static WmtsTileSchema CreateWmtsTileSchema(WmtsMapData backgroundMapData) + { + WmtsTileSchema schema = ConstructWmtsTileSchema(); + schema.Title = backgroundMapData.Name; + schema.Format = backgroundMapData.PreferredFormat; + + schema.Resolutions["1"] = new Resolution("1", 1); + + var regex1 = new Regex(@"(?.+)\((?.+)\)"); + Match match = regex1.Match(backgroundMapData.SelectedCapabilityIdentifier); + schema.Layer = match.Groups["Layer"].Value; + schema.TileMatrixSet = match.Groups["TileMatrixSet"].Value; + + var regex2 = new Regex(@"EPSG:(?\d+)"); + Match potentialMatch = regex2.Match(schema.TileMatrixSet); + schema.Srs = potentialMatch.Success ? + $"EPSG:{potentialMatch.Groups["SrsNumber"]}" : + "EPSG:3857"; + + return schema; + } + + private static WmtsTileSchema ConstructWmtsTileSchema() + { + return (WmtsTileSchema) typeof(WmtsTileSchema) + .GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null) + .Invoke(null); + } + + private static byte[] GetStubTile(Uri arg) + { + if (pngTileDataStub == null) + { + using (var stream = new MemoryStream()) + { + Resources.stubTile.Save(stream, ImageFormat.Png); + pngTileDataStub = stream.ToArray(); + } + } + return pngTileDataStub; + } + } +} \ No newline at end of file Index: Core/Components/test/Core.Components.DotSpatial.TestUtil/UseCustomTileSourceFactoryConfig.cs =================================================================== diff -u --- Core/Components/test/Core.Components.DotSpatial.TestUtil/UseCustomTileSourceFactoryConfig.cs (revision 0) +++ Core/Components/test/Core.Components.DotSpatial.TestUtil/UseCustomTileSourceFactoryConfig.cs (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -0,0 +1,60 @@ +// Copyright (C) Stichting Deltares 2016. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System; +using Core.Components.DotSpatial.Layer.BruTile.Configurations; +using Core.Components.Gis.Data; + +namespace Core.Components.DotSpatial.TestUtil +{ + /// + /// Configures to temporarily use a different + /// instance. + /// + public sealed class UseCustomTileSourceFactoryConfig : IDisposable + { + private readonly ITileSourceFactory originalFactory; + + /// + /// Creates a new instance of . + /// + /// The temporary factory to be used. + public UseCustomTileSourceFactoryConfig(ITileSourceFactory factory) + { + originalFactory = TileSourceFactory.Instance; + TileSourceFactory.Instance = factory; + } + + /// + /// Creates a new instance of that + /// initializes test stubs to work for a instance. + /// + /// The map data to work with. + public UseCustomTileSourceFactoryConfig(WmtsMapData backgroundMapData) : this(new TestTileSourceFactory(backgroundMapData)) + { + } + + public void Dispose() + { + TileSourceFactory.Instance = originalFactory; + } + } +} \ No newline at end of file Index: Core/Components/test/Core.Components.DotSpatial.TestUtil/app.config =================================================================== diff -u --- Core/Components/test/Core.Components.DotSpatial.TestUtil/app.config (revision 0) +++ Core/Components/test/Core.Components.DotSpatial.TestUtil/app.config (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file Index: Core/Components/test/Core.Components.DotSpatial.TestUtil/packages.config =================================================================== diff -u --- Core/Components/test/Core.Components.DotSpatial.TestUtil/packages.config (revision 0) +++ Core/Components/test/Core.Components.DotSpatial.TestUtil/packages.config (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -0,0 +1,4 @@ + + + + \ No newline at end of file Index: Core/Components/test/Core.Components.Gis.Test/Core.Components.Gis.Test.csproj =================================================================== diff -u -r6a5d7b40b7ba4dcb73e393075338352d194e97c2 -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Core/Components/test/Core.Components.Gis.Test/Core.Components.Gis.Test.csproj (.../Core.Components.Gis.Test.csproj) (revision 6a5d7b40b7ba4dcb73e393075338352d194e97c2) +++ Core/Components/test/Core.Components.Gis.Test/Core.Components.Gis.Test.csproj (.../Core.Components.Gis.Test.csproj) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -65,6 +65,7 @@ Copying.Lesser.licenseheader + Index: Core/Components/test/Core.Components.Gis.Test/Data/WmtsMapDataTest.cs =================================================================== diff -u -r804df9b58213c186181b3ba38e487747c5860e4c -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Core/Components/test/Core.Components.Gis.Test/Data/WmtsMapDataTest.cs (.../WmtsMapDataTest.cs) (revision 804df9b58213c186181b3ba38e487747c5860e4c) +++ Core/Components/test/Core.Components.Gis.Test/Data/WmtsMapDataTest.cs (.../WmtsMapDataTest.cs) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -162,14 +162,30 @@ // Assert Assert.AreEqual("PDOK achtergrondkaart", mapData.Name); Assert.AreEqual("https://geodata.nationaalgeoregister.nl/wmts/top10nlv2?VERSION=1.0.0&request=GetCapabilities", mapData.SourceCapabilitiesUrl); - Assert.AreEqual("brtachtergrondkaart", mapData.SelectedCapabilityIdentifier); + Assert.AreEqual("brtachtergrondkaart(EPSG:28992)", mapData.SelectedCapabilityIdentifier); Assert.AreEqual("image/png", mapData.PreferredFormat); Assert.AreEqual(0.0, mapData.Transparency.Value); Assert.IsTrue(mapData.IsConfigured); Assert.IsTrue(mapData.IsVisible); } [Test] + public void CreateAlternativePdokMapData_ReturnsInitializedWmtsMapData() + { + // Call + WmtsMapData mapData = WmtsMapData.CreateAlternativePdokMapData(); + + // Assert + Assert.AreEqual("PDOK achtergrondkaart", mapData.Name); + Assert.AreEqual("https://geodata.nationaalgeoregister.nl/wmts/top10nlv2?VERSION=1.0.0&request=GetCapabilities", mapData.SourceCapabilitiesUrl); + Assert.AreEqual("brtachtergrondkaart(EPSG:25831:RWS)", mapData.SelectedCapabilityIdentifier); + Assert.AreEqual("image/png", mapData.PreferredFormat); + Assert.AreEqual(0.0, mapData.Transparency.Value); + Assert.IsTrue(mapData.IsConfigured); + Assert.IsTrue(mapData.IsVisible); + } + + [Test] public void CreateUnconnectedMapData_ReturnsUnconfiguredWmtsMapData() { // Call Index: Core/Components/test/Core.Components.Gis.Test/app.config =================================================================== diff -u --- Core/Components/test/Core.Components.Gis.Test/app.config (revision 0) +++ Core/Components/test/Core.Components.Gis.Test/app.config (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file Index: Ringtoets/ClosingStructures/src/Ringtoets.ClosingStructures.Forms/Views/ClosingStructuresFailureMechanismView.cs =================================================================== diff -u -r4c580b17f584bdfcaae96992b11f90025647ca20 -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Ringtoets/ClosingStructures/src/Ringtoets.ClosingStructures.Forms/Views/ClosingStructuresFailureMechanismView.cs (.../ClosingStructuresFailureMechanismView.cs) (revision 4c580b17f584bdfcaae96992b11f90025647ca20) +++ Ringtoets/ClosingStructures/src/Ringtoets.ClosingStructures.Forms/Views/ClosingStructuresFailureMechanismView.cs (.../ClosingStructuresFailureMechanismView.cs) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -21,6 +21,7 @@ using System.Collections.Generic; using System.Linq; +using System.Net.Mime; using System.Windows.Forms; using Core.Common.Base; using Core.Components.Gis.Data; @@ -147,7 +148,7 @@ SetMapDataFeatures(); - mapControl.Data = mapDataCollection; + Map.Data = mapDataCollection; } } } Index: Ringtoets/Common/src/Ringtoets.Common.IO/DikeProfiles/DikeProfileDataReader.cs =================================================================== diff -u -ra633912c0e5c538b2a0849eea7c1ab4957aaadeb -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Ringtoets/Common/src/Ringtoets.Common.IO/DikeProfiles/DikeProfileDataReader.cs (.../DikeProfileDataReader.cs) (revision a633912c0e5c538b2a0849eea7c1ab4957aaadeb) +++ Ringtoets/Common/src/Ringtoets.Common.IO/DikeProfiles/DikeProfileDataReader.cs (.../DikeProfileDataReader.cs) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -98,7 +98,7 @@ /// Thrown when the read id is not an accepted id. public DikeProfileData ReadDikeProfileData(string filePath) { - FileUtils.ValidateFilePath(filePath); + IOUtils.ValidateFilePath(filePath); if (!File.Exists(filePath)) { string message = new FileReaderErrorMessageBuilder(filePath) Index: Ringtoets/Common/src/Ringtoets.Common.IO/DikeProfiles/ProfileLocationReader.cs =================================================================== diff -u -re60dbf2fd41434270cad4efba20446e19ede0d2e -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Ringtoets/Common/src/Ringtoets.Common.IO/DikeProfiles/ProfileLocationReader.cs (.../ProfileLocationReader.cs) (revision e60dbf2fd41434270cad4efba20446e19ede0d2e) +++ Ringtoets/Common/src/Ringtoets.Common.IO/DikeProfiles/ProfileLocationReader.cs (.../ProfileLocationReader.cs) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -56,7 +56,7 @@ /// public ProfileLocationReader(string shapeFilePath) { - FileUtils.ValidateFilePath(shapeFilePath); + IOUtils.ValidateFilePath(shapeFilePath); if (!File.Exists(shapeFilePath)) { string message = new FileReaderErrorMessageBuilder(shapeFilePath) Index: Ringtoets/Common/src/Ringtoets.Common.IO/FailureMechanismSectionReader.cs =================================================================== diff -u -r8aed37b83aa4d0b9a0932551f8727ceca83e5cc4 -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Ringtoets/Common/src/Ringtoets.Common.IO/FailureMechanismSectionReader.cs (.../FailureMechanismSectionReader.cs) (revision 8aed37b83aa4d0b9a0932551f8727ceca83e5cc4) +++ Ringtoets/Common/src/Ringtoets.Common.IO/FailureMechanismSectionReader.cs (.../FailureMechanismSectionReader.cs) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -59,7 +59,7 @@ /// public FailureMechanismSectionReader(string shapeFilePath) { - FileUtils.ValidateFilePath(shapeFilePath); + IOUtils.ValidateFilePath(shapeFilePath); filePath = shapeFilePath; polylineShapeFileReader = new PolylineShapeFileReader(filePath); } Index: Ringtoets/Common/src/Ringtoets.Common.IO/HydraRing/HydraulicDatabaseHelper.cs =================================================================== diff -u -rb2b9fdf365e70928a05c57966eeed30d9050e528 -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Ringtoets/Common/src/Ringtoets.Common.IO/HydraRing/HydraulicDatabaseHelper.cs (.../HydraulicDatabaseHelper.cs) (revision b2b9fdf365e70928a05c57966eeed30d9050e528) +++ Ringtoets/Common/src/Ringtoets.Common.IO/HydraRing/HydraulicDatabaseHelper.cs (.../HydraulicDatabaseHelper.cs) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -50,7 +50,7 @@ { try { - FileUtils.ValidateFilePath(filePath); + IOUtils.ValidateFilePath(filePath); } catch (ArgumentException e) { Index: Ringtoets/Common/src/Ringtoets.Common.IO/Hydraulics/HydraulicBoundaryLocationsExporter.cs =================================================================== diff -u -rb2b9fdf365e70928a05c57966eeed30d9050e528 -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Ringtoets/Common/src/Ringtoets.Common.IO/Hydraulics/HydraulicBoundaryLocationsExporter.cs (.../HydraulicBoundaryLocationsExporter.cs) (revision b2b9fdf365e70928a05c57966eeed30d9050e528) +++ Ringtoets/Common/src/Ringtoets.Common.IO/Hydraulics/HydraulicBoundaryLocationsExporter.cs (.../HydraulicBoundaryLocationsExporter.cs) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -70,7 +70,7 @@ throw new ArgumentNullException(nameof(waveHeightName)); } - FileUtils.ValidateFilePath(filePath); + IOUtils.ValidateFilePath(filePath); this.hydraulicBoundaryLocations = hydraulicBoundaryLocations; this.filePath = filePath; Index: Ringtoets/Common/src/Ringtoets.Common.IO/ReferenceLines/ReferenceLineExporter.cs =================================================================== diff -u -r4e578730273a943bb02a2861c694a2707c8ef852 -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Ringtoets/Common/src/Ringtoets.Common.IO/ReferenceLines/ReferenceLineExporter.cs (.../ReferenceLineExporter.cs) (revision 4e578730273a943bb02a2861c694a2707c8ef852) +++ Ringtoets/Common/src/Ringtoets.Common.IO/ReferenceLines/ReferenceLineExporter.cs (.../ReferenceLineExporter.cs) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -49,7 +49,7 @@ /// Thrown when is invalid. public ReferenceLineExporter(ReferenceLine referenceLine, string id, string filePath) { - FileUtils.ValidateFilePath(filePath); + IOUtils.ValidateFilePath(filePath); this.referenceLine = referenceLine; this.filePath = filePath; Index: Ringtoets/Common/src/Ringtoets.Common.IO/ReferenceLines/ReferenceLineReader.cs =================================================================== diff -u -r065af7e201b59ec19a17c42e9d772f5e86b31338 -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Ringtoets/Common/src/Ringtoets.Common.IO/ReferenceLines/ReferenceLineReader.cs (.../ReferenceLineReader.cs) (revision 065af7e201b59ec19a17c42e9d772f5e86b31338) +++ Ringtoets/Common/src/Ringtoets.Common.IO/ReferenceLines/ReferenceLineReader.cs (.../ReferenceLineReader.cs) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -57,7 +57,7 @@ /// public ReferenceLine ReadReferenceLine(string shapeFilePath) { - FileUtils.ValidateFilePath(shapeFilePath); + IOUtils.ValidateFilePath(shapeFilePath); if (!File.Exists(shapeFilePath)) { string message = new FileReaderErrorMessageBuilder(shapeFilePath) Index: Ringtoets/Common/src/Ringtoets.Common.IO/ReferenceLines/ReferenceLinesMetaReader.cs =================================================================== diff -u -r0df7cded06f5afbac08b97e025242ba55c90ec57 -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Ringtoets/Common/src/Ringtoets.Common.IO/ReferenceLines/ReferenceLinesMetaReader.cs (.../ReferenceLinesMetaReader.cs) (revision 0df7cded06f5afbac08b97e025242ba55c90ec57) +++ Ringtoets/Common/src/Ringtoets.Common.IO/ReferenceLines/ReferenceLinesMetaReader.cs (.../ReferenceLinesMetaReader.cs) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -100,7 +100,7 @@ /// Thrown when does not exist. private static void ValidateFilePath(string shapeFilePath) { - FileUtils.ValidateFilePath(shapeFilePath); + IOUtils.ValidateFilePath(shapeFilePath); if (!File.Exists(shapeFilePath)) { string message = new FileReaderErrorMessageBuilder(shapeFilePath) Index: Ringtoets/Common/src/Ringtoets.Common.IO/Structures/StructureLocationReader.cs =================================================================== diff -u -re5646456f4fb82606d19442702a09fff38786e8a -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Ringtoets/Common/src/Ringtoets.Common.IO/Structures/StructureLocationReader.cs (.../StructureLocationReader.cs) (revision e5646456f4fb82606d19442702a09fff38786e8a) +++ Ringtoets/Common/src/Ringtoets.Common.IO/Structures/StructureLocationReader.cs (.../StructureLocationReader.cs) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -55,7 +55,7 @@ /// public StructureLocationReader(string shapeFilePath) { - FileUtils.ValidateFilePath(shapeFilePath); + IOUtils.ValidateFilePath(shapeFilePath); if (!File.Exists(shapeFilePath)) { string message = new FileReaderErrorMessageBuilder(shapeFilePath) Index: Ringtoets/Common/src/Ringtoets.Common.IO/Structures/StructuresCharacteristicsCsvReader.cs =================================================================== diff -u -r4fda2f3f6be17051ed305f0ea2d1b15fa87fc55f -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Ringtoets/Common/src/Ringtoets.Common.IO/Structures/StructuresCharacteristicsCsvReader.cs (.../StructuresCharacteristicsCsvReader.cs) (revision 4fda2f3f6be17051ed305f0ea2d1b15fa87fc55f) +++ Ringtoets/Common/src/Ringtoets.Common.IO/Structures/StructuresCharacteristicsCsvReader.cs (.../StructuresCharacteristicsCsvReader.cs) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -66,7 +66,7 @@ /// Thrown when is invalid. public StructuresCharacteristicsCsvReader(string path) { - FileUtils.ValidateFilePath(path); + IOUtils.ValidateFilePath(path); filePath = path; } Index: Ringtoets/Common/src/Ringtoets.Common.Service/HydraRingSettingsDatabaseHelper.cs =================================================================== diff -u -rd73c509c93f18a87da76086c871eca2a2c903e12 -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Ringtoets/Common/src/Ringtoets.Common.Service/HydraRingSettingsDatabaseHelper.cs (.../HydraRingSettingsDatabaseHelper.cs) (revision d73c509c93f18a87da76086c871eca2a2c903e12) +++ Ringtoets/Common/src/Ringtoets.Common.Service/HydraRingSettingsDatabaseHelper.cs (.../HydraRingSettingsDatabaseHelper.cs) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -51,7 +51,7 @@ /// public static void AssignSettingsFromDatabase(HydraRingCalculationInput calculationInput, string hydraulicBoundaryDatabaseFilePath) { - FileUtils.ValidateFilePath(hydraulicBoundaryDatabaseFilePath); + IOUtils.ValidateFilePath(hydraulicBoundaryDatabaseFilePath); var locationId = calculationInput.HydraulicBoundaryLocationId; var settingsDatabaseFileName = HydraulicDatabaseHelper.GetHydraulicBoundarySettingsDatabase(hydraulicBoundaryDatabaseFilePath); Index: Ringtoets/DuneErosion/src/Ringtoets.DuneErosion.IO/DuneLocationsExporter.cs =================================================================== diff -u -re261f847e5058d2f9612f091421a6a463cb17edf -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Ringtoets/DuneErosion/src/Ringtoets.DuneErosion.IO/DuneLocationsExporter.cs (.../DuneLocationsExporter.cs) (revision e261f847e5058d2f9612f091421a6a463cb17edf) +++ Ringtoets/DuneErosion/src/Ringtoets.DuneErosion.IO/DuneLocationsExporter.cs (.../DuneLocationsExporter.cs) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -54,7 +54,7 @@ throw new ArgumentNullException(nameof(duneLocations)); } - FileUtils.ValidateFilePath(filePath); + IOUtils.ValidateFilePath(filePath); this.duneLocations = duneLocations; this.filePath = filePath; Index: Ringtoets/Piping/src/Ringtoets.Piping.Data/StochasticSoilModelCollection.cs =================================================================== diff -u -r6aaa17dbb25d6299115d68b221312aa1482d97a9 -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Ringtoets/Piping/src/Ringtoets.Piping.Data/StochasticSoilModelCollection.cs (.../StochasticSoilModelCollection.cs) (revision 6aaa17dbb25d6299115d68b221312aa1482d97a9) +++ Ringtoets/Piping/src/Ringtoets.Piping.Data/StochasticSoilModelCollection.cs (.../StochasticSoilModelCollection.cs) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -121,7 +121,7 @@ { throw new ArgumentNullException(nameof(filePath)); } - if (!FileUtils.IsValidFilePath(filePath)) + if (!IOUtils.IsValidFilePath(filePath)) { throw new ArgumentException($"'{filePath}' is not a valid filepath.", nameof(filePath)); } Index: Ringtoets/Piping/src/Ringtoets.Piping.Forms/Views/PipingFailureMechanismView.cs =================================================================== diff -u -r5a453b9ac4ccaa5fc6f767036f0bcbf87ad05ff4 -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Ringtoets/Piping/src/Ringtoets.Piping.Forms/Views/PipingFailureMechanismView.cs (.../PipingFailureMechanismView.cs) (revision 5a453b9ac4ccaa5fc6f767036f0bcbf87ad05ff4) +++ Ringtoets/Piping/src/Ringtoets.Piping.Forms/Views/PipingFailureMechanismView.cs (.../PipingFailureMechanismView.cs) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -131,6 +131,7 @@ calculationGroupObserver.Observable = null; calculationObserver.Observable = null; + Map.BackgroundMapData = null; Map.Data = null; } else @@ -146,6 +147,7 @@ SetMapDataFeatures(); + Map.BackgroundMapData = data.Parent.BackgroundMapData; Map.Data = mapDataCollection; } } Index: Ringtoets/Piping/src/Ringtoets.Piping.IO/SurfaceLines/CharacteristicPointsCsvReader.cs =================================================================== diff -u -r4fda2f3f6be17051ed305f0ea2d1b15fa87fc55f -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Ringtoets/Piping/src/Ringtoets.Piping.IO/SurfaceLines/CharacteristicPointsCsvReader.cs (.../CharacteristicPointsCsvReader.cs) (revision 4fda2f3f6be17051ed305f0ea2d1b15fa87fc55f) +++ Ringtoets/Piping/src/Ringtoets.Piping.IO/SurfaceLines/CharacteristicPointsCsvReader.cs (.../CharacteristicPointsCsvReader.cs) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -72,7 +72,7 @@ /// Thrown when is invalid. public CharacteristicPointsCsvReader(string path) { - FileUtils.ValidateFilePath(path); + IOUtils.ValidateFilePath(path); filePath = path; } Index: Ringtoets/Piping/src/Ringtoets.Piping.IO/SurfaceLines/PipingSurfaceLinesCsvReader.cs =================================================================== diff -u -r4fda2f3f6be17051ed305f0ea2d1b15fa87fc55f -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Ringtoets/Piping/src/Ringtoets.Piping.IO/SurfaceLines/PipingSurfaceLinesCsvReader.cs (.../PipingSurfaceLinesCsvReader.cs) (revision 4fda2f3f6be17051ed305f0ea2d1b15fa87fc55f) +++ Ringtoets/Piping/src/Ringtoets.Piping.IO/SurfaceLines/PipingSurfaceLinesCsvReader.cs (.../PipingSurfaceLinesCsvReader.cs) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -76,7 +76,7 @@ /// is invalid. public PipingSurfaceLinesCsvReader(string path) { - FileUtils.ValidateFilePath(path); + IOUtils.ValidateFilePath(path); filePath = path; } Index: Ringtoets/Piping/test/Ringtoets.Piping.Forms.Test/Views/PipingFailureMechanismViewTest.cs =================================================================== diff -u -r6aaa17dbb25d6299115d68b221312aa1482d97a9 -r16bd8076863c03907abeee60b16704aba1bdb1a0 --- Ringtoets/Piping/test/Ringtoets.Piping.Forms.Test/Views/PipingFailureMechanismViewTest.cs (.../PipingFailureMechanismViewTest.cs) (revision 6aaa17dbb25d6299115d68b221312aa1482d97a9) +++ Ringtoets/Piping/test/Ringtoets.Piping.Forms.Test/Views/PipingFailureMechanismViewTest.cs (.../PipingFailureMechanismViewTest.cs) (revision 16bd8076863c03907abeee60b16704aba1bdb1a0) @@ -23,6 +23,7 @@ using System.Collections.ObjectModel; using System.Linq; using System.Windows.Forms; +using Core.Common.Base; using Core.Common.Base.Geometry; using Core.Components.DotSpatial.Forms; using Core.Components.Gis.Data; @@ -154,12 +155,17 @@ public void Data_EmptyPipingFailureMechanismContext_NoMapDataSet() { // Setup + var mocks = new MockRepository(); + var assessmentSection = mocks.Stub(); + var backgroundMapData = WmtsMapData.CreateUnconnectedMapData(); + assessmentSection.Stub(s => s.BackgroundMapData).Return(backgroundMapData); + assessmentSection.Stub(s => s.Id).Return("A"); + assessmentSection.Stub(s => s.Attach(Arg.Is.NotNull)); + assessmentSection.Stub(s => s.Detach(Arg.Is.NotNull)); + mocks.ReplayAll(); + using (var view = new PipingFailureMechanismView()) { - var mockRepository = new MockRepository(); - var assessmentSection = mockRepository.Stub(); - mockRepository.ReplayAll(); - var failureMechanismContext = new PipingFailureMechanismContext( new PipingFailureMechanism(), assessmentSection); @@ -169,12 +175,72 @@ // Assert Assert.AreSame(failureMechanismContext, view.Data); AssertEmptyMapData(view.Map.Data); + Assert.AreSame(backgroundMapData, view.Map.BackgroundMapData); + } + mocks.VerifyAll(); + } - mockRepository.VerifyAll(); + [Test] + [TestCase(true)] + [TestCase(false)] + public void Data_AssessmentSectionWithBackgroundMapData_BackgroundMapDataSet(bool isConfigured) + { + WmtsMapData backgroundMapData = isConfigured ? WmtsMapData.CreateAlternativePdokMapData() : WmtsMapData.CreateUnconnectedMapData(); + + var mocks = new MockRepository(); + var assessmentSection = mocks.Stub(); + assessmentSection.Stub(s => s.BackgroundMapData).Return(backgroundMapData); + assessmentSection.Stub(s => s.Id).Return("A"); + assessmentSection.Stub(s => s.Attach(Arg.Is.NotNull)); + assessmentSection.Stub(s => s.Detach(Arg.Is.NotNull)); + mocks.ReplayAll(); + + // Setup + using (var view = new PipingFailureMechanismView()) + { + var failureMechanismContext = new PipingFailureMechanismContext(new PipingFailureMechanism(), assessmentSection); + + // Call + view.Data = failureMechanismContext; + + // Assert + Assert.AreSame(backgroundMapData, view.Map.BackgroundMapData); } + mocks.VerifyAll(); } [Test] + public void Data_SetToNull_ClearMapDataProperties() + { + // Setup + var mocks = new MockRepository(); + var assessmentSection = mocks.Stub(); + assessmentSection.Stub(s => s.BackgroundMapData).Return(WmtsMapData.CreateUnconnectedMapData()); + assessmentSection.Stub(s => s.Id).Return("A"); + assessmentSection.Stub(s => s.Attach(Arg.Is.NotNull)); + assessmentSection.Stub(s => s.Detach(Arg.Is.NotNull)); + mocks.ReplayAll(); + + using (var view = new PipingFailureMechanismView()) + { + view.Data = new PipingFailureMechanismContext( + new PipingFailureMechanism(), assessmentSection); + + // Precondition + Assert.IsNotNull(view.Map.Data); + Assert.IsNotNull(view.Map.BackgroundMapData); + + // Call + view.Data = null; + + // Assert + Assert.IsNull(view.Map.Data); + Assert.IsNull(view.Map.BackgroundMapData); + } + mocks.VerifyAll(); + } + + [Test] public void Data_PipingFailureMechanismContext_DataUpdatedToCollectionOfFilledMapData() { // Setup