// 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 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 General Public License for more details. // // You should have received a copy of the GNU 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.Linq; using System.Security.AccessControl; using Core.Common.IO.Exceptions; using Core.Common.TestUtil; using NUnit.Framework; using Ringtoets.Common.Data.Calculation; using Ringtoets.Common.IO.Writers; namespace Ringtoets.Common.IO.TestUtil { [TestFixture] public abstract class CustomCalculationConfigurationWriterDesignGuidelinesTestFixture where TCalculation : class, ICalculation where TWriter : CalculationConfigurationWriter, new() { [Test] public void Constructor_ExpectedValues() { // Call var writer = new TWriter(); // Assert AssertDefaultConstructedInstance(writer); } [Test] public void Write_ConfigurationNull_ThrowArgumentNullException() { // Setup var writer = new TWriter(); // Call TestDelegate test = () => writer.Write(null, string.Empty); // Assert ArgumentNullException exception = Assert.Throws(test); AssertNullConfiguration(exception); } [Test] public void Write_FilePathNull_ThrowArgumentNullException() { // Setup var writer = new TWriter(); // Call TestDelegate test = () => writer.Write(Enumerable.Empty(), null); // Assert ArgumentNullException exception = Assert.Throws(test); AssertNullFilePath(exception); } [Test] [TestCaseSource(typeof(InvalidPathHelper), nameof(InvalidPathHelper.InvalidPaths))] public void Write_FilePathInvalid_ThrowCriticalFileWriteException(string filePath) { // Setup var writer = new TWriter(); // Call TestDelegate call = () => writer.Write(Enumerable.Empty(), filePath); // Assert CriticalFileWriteException exception = Assert.Throws(call); AssertInvalidFilePath(exception, filePath); } [Test] public void Write_FilePathTooLong_ThrowCriticalFileWriteException() { // Setup var filePath = new string('a', 249); var writer = new TWriter(); // Call TestDelegate call = () => writer.Write(Enumerable.Empty(), filePath); // Assert CriticalFileWriteException exception = Assert.Throws(call); AssertTooLongPath(exception, filePath); } [Test] public void Write_InvalidDirectoryRights_ThrowCriticalFileWriteException() { // Setup var writer = new TWriter(); string directoryPath = TestHelper.GetScratchPadPath(nameof(Write_InvalidDirectoryRights_ThrowCriticalFileWriteException)); using (var disposeHelper = new DirectoryDisposeHelper(TestHelper.GetScratchPadPath(), nameof(Write_InvalidDirectoryRights_ThrowCriticalFileWriteException))) { string filePath = Path.Combine(directoryPath, "test.xml"); disposeHelper.LockDirectory(FileSystemRights.Write); // Call TestDelegate call = () => writer.Write(Enumerable.Empty(), filePath); // Assert CriticalFileWriteException exception = Assert.Throws(call); AssertInvalidDirectoryRights(exception, filePath); } } [Test] public void Write_FileInUse_ThrowCriticalFileWriteException() { // Setup string path = TestHelper.GetScratchPadPath(nameof(Write_FileInUse_ThrowCriticalFileWriteException)); var writer = new TWriter(); using (var fileDisposeHelper = new FileDisposeHelper(path)) { fileDisposeHelper.LockFiles(); // Call TestDelegate call = () => writer.Write(Enumerable.Empty(), path); // Assert CriticalFileWriteException exception = Assert.Throws(call); AssertFileInUse(exception, path); } } protected virtual void AssertDefaultConstructedInstance(TWriter writer) { Assert.IsInstanceOf>(writer); } protected virtual void AssertNullConfiguration(ArgumentNullException exception) { Assert.IsNotNull(exception); Assert.AreEqual("configuration", exception.ParamName); } protected virtual void AssertNullFilePath(ArgumentNullException exception) { Assert.IsNotNull(exception); Assert.AreEqual("filePath", exception.ParamName); } protected virtual void AssertInvalidFilePath(CriticalFileWriteException exception, string filePath) { Assert.IsNotNull(exception); Assert.AreEqual($"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'.", exception.Message); } protected virtual void AssertTooLongPath(CriticalFileWriteException exception, string filePath) { Assert.IsNotNull(exception); Assert.AreEqual($"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'.", exception.Message); Assert.IsInstanceOf(exception.InnerException); } protected virtual void AssertInvalidDirectoryRights(CriticalFileWriteException exception, string filePath) { Assert.IsNotNull(exception); Assert.AreEqual($"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'.", exception.Message); Assert.IsInstanceOf(exception.InnerException); } protected virtual void AssertFileInUse(CriticalFileWriteException exception, string filePath) { Assert.IsNotNull(exception); Assert.AreEqual($"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'.", exception.Message); Assert.IsInstanceOf(exception.InnerException); } } }