Index: Ringtoets/Common/test/Ringtoets.Common.IO.TestUtil/CustomCalculationConfigurationWriterDesignGuidelinesTestFixture.cs =================================================================== diff -u -rb3b6c13cf736c134476b3db34281332d01ca86b1 -re507c88f2863ff7bc93505caf71c5f8025fb48c0 --- Ringtoets/Common/test/Ringtoets.Common.IO.TestUtil/CustomCalculationConfigurationWriterDesignGuidelinesTestFixture.cs (.../CustomCalculationConfigurationWriterDesignGuidelinesTestFixture.cs) (revision b3b6c13cf736c134476b3db34281332d01ca86b1) +++ Ringtoets/Common/test/Ringtoets.Common.IO.TestUtil/CustomCalculationConfigurationWriterDesignGuidelinesTestFixture.cs (.../CustomCalculationConfigurationWriterDesignGuidelinesTestFixture.cs) (revision e507c88f2863ff7bc93505caf71c5f8025fb48c0) @@ -26,97 +26,123 @@ using Core.Common.IO.Exceptions; using Core.Common.TestUtil; using NUnit.Framework; -using Ringtoets.Common.Data.Calculation; +using Rhino.Mocks; +using Ringtoets.Common.IO.Configurations; using Ringtoets.Common.IO.Configurations.Export; namespace Ringtoets.Common.IO.TestUtil { [TestFixture] - public abstract class CustomCalculationConfigurationWriterDesignGuidelinesTestFixture - where TCalculation : class, ICalculation - where TWriter : CalculationConfigurationWriter, new() + public abstract class CustomCalculationConfigurationWriterDesignGuidelinesTestFixture + where TConfiguration : class, IConfigurationItem + where TWriter : CalculationConfigurationWriter { [Test] - public void Constructor_ExpectedValues() + public void Write_CalculationOfTypeOtherThanGiven_ThrowsCriticalFileWriteExceptionWithInnerArgumentException() { + // Setup + var mocks = new MockRepository(); + var calculation = mocks.Stub(); + mocks.ReplayAll(); + + string filePath = TestHelper.GetScratchPadPath("test.xml"); + + try + { + TWriter writer = CreateWriterInstance(filePath); + + // Call + TestDelegate test = () => writer.Write(new[] + { + calculation + }); + + // Assert + var exception = Assert.Throws(test); + Exception innerException = exception.InnerException; + Assert.IsNotNull(innerException); + Assert.AreEqual($"Cannot write calculation of type '{calculation.GetType()}' using this writer.", innerException.Message); + } + finally + { + File.Delete(filePath); + } + mocks.VerifyAll(); + } + + [Test] + public void Constructor_WithoutFilePath_ThrowsArgumentException() + { // Call - var writer = new TWriter(); + TestDelegate call = () => CreateWriterInstance(null); // Assert - AssertDefaultConstructedInstance(writer); + AssertNullFilePath(Assert.Throws(call)); } [Test] - public void Write_ConfigurationNull_ThrowArgumentNullException() + [TestCaseSource(typeof(InvalidPathHelper), nameof(InvalidPathHelper.InvalidPaths))] + public void Constructor_WithoutValidFilePath_ThrowsArgumentException(string filePath) { - // Setup - var writer = new TWriter(); - // Call - TestDelegate test = () => writer.Write(null, string.Empty); + TestDelegate call = () => CreateWriterInstance(filePath); // Assert - var exception = Assert.Throws(test); - AssertNullConfiguration(exception); + Assert.Throws(call); } [Test] - public void Write_FilePathNull_ThrowArgumentNullException() + public void Write_FilePathTooLong_ThrowCriticalFileWriteException() { // Setup - var writer = new TWriter(); + var filePath = new string('a', 249); + TWriter writerInstance = CreateWriterInstance(filePath); // Call - TestDelegate test = () => writer.Write(Enumerable.Empty(), null); + TestDelegate call = () => writerInstance.Write(Enumerable.Empty()); // Assert - var exception = Assert.Throws(test); - AssertNullFilePath(exception); + var exception = Assert.Throws(call); + AssertTooLongPath(exception, filePath); } [Test] - [TestCaseSource(typeof(InvalidPathHelper), nameof(InvalidPathHelper.InvalidPaths))] - public void Write_FilePathInvalid_ThrowCriticalFileWriteException(string filePath) + public void Constructor_ExpectedValues() { - // Setup - var writer = new TWriter(); - // Call - TestDelegate call = () => writer.Write(Enumerable.Empty(), filePath); + TWriter writer = CreateWriterInstance("//validpath"); // Assert - var exception = Assert.Throws(call); - AssertInvalidFilePath(exception, filePath); + AssertDefaultConstructedInstance(writer); } [Test] - public void Write_FilePathTooLong_ThrowCriticalFileWriteException() + public void Write_ConfigurationNull_ThrowArgumentNullException() { // Setup - var filePath = new string('a', 249); - var writer = new TWriter(); + TWriter writer = CreateWriterInstance("//validpath"); // Call - TestDelegate call = () => writer.Write(Enumerable.Empty(), filePath); + TestDelegate test = () => writer.Write(null); // Assert - var exception = Assert.Throws(call); - AssertTooLongPath(exception, filePath); + var exception = Assert.Throws(test); + AssertNullConfiguration(exception); } [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); + TWriter writer = CreateWriterInstance(filePath); // Call - TestDelegate call = () => writer.Write(Enumerable.Empty(), filePath); + TestDelegate call = () => writer.Write(Enumerable.Empty()); // Assert var exception = Assert.Throws(call); @@ -129,24 +155,26 @@ { // Setup string path = TestHelper.GetScratchPadPath(nameof(Write_FileInUse_ThrowCriticalFileWriteException)); - var writer = new TWriter(); using (var fileDisposeHelper = new FileDisposeHelper(path)) { fileDisposeHelper.LockFiles(); + TWriter writer = CreateWriterInstance(path); // Call - TestDelegate call = () => writer.Write(Enumerable.Empty(), path); + TestDelegate call = () => writer.Write(Enumerable.Empty()); // Assert var exception = Assert.Throws(call); AssertFileInUse(exception, path); } } + protected abstract TWriter CreateWriterInstance(string filePath); + protected virtual void AssertDefaultConstructedInstance(TWriter writer) { - Assert.IsInstanceOf>(writer); + Assert.IsInstanceOf>(writer); } protected virtual void AssertNullConfiguration(ArgumentNullException exception) @@ -155,18 +183,11 @@ Assert.AreEqual("configuration", exception.ParamName); } - protected virtual void AssertNullFilePath(ArgumentNullException exception) + protected virtual void AssertNullFilePath(ArgumentException 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);