Index: Riskeer/AssemblyTool/src/Riskeer.AssemblyTool.IO/AssemblyGmlWriter.cs =================================================================== diff -u -r153ebb918e6a398fe6a4aedabedde47badb576b7 -rd77ef3a34ead6939d21fdc69fb8cf3a47adf70ec --- Riskeer/AssemblyTool/src/Riskeer.AssemblyTool.IO/AssemblyGmlWriter.cs (.../AssemblyGmlWriter.cs) (revision 153ebb918e6a398fe6a4aedabedde47badb576b7) +++ Riskeer/AssemblyTool/src/Riskeer.AssemblyTool.IO/AssemblyGmlWriter.cs (.../AssemblyGmlWriter.cs) (revision d77ef3a34ead6939d21fdc69fb8cf3a47adf70ec) @@ -23,6 +23,7 @@ using System.Xml; using Core.Common.Base.Geometry; using Core.Common.IO.Exceptions; +using Core.Common.Util; using Riskeer.AssemblyTool.IO.Helpers; using Riskeer.AssemblyTool.IO.Model; using Riskeer.AssemblyTool.IO.Properties; @@ -33,68 +34,96 @@ /// /// Writer for writing the assembly to GML. /// - public static class AssemblyGmlWriter + public class AssemblyGmlWriter : IDisposable { + private readonly string filePath; + private XmlWriter writer; + /// + /// Creates a new instance of . + /// + /// The path of the file to write to. + /// 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 AssemblyGmlWriter(string filePath) + { + this.filePath = filePath; + IOUtils.ValidateFilePath(filePath); + } + + /// /// Writes a to a file. /// /// The to be written to the file. - /// The path to the file. /// Thrown when any parameter is null. - /// Thrown when unable to write to . - public static void Write(ExportableAssembly assembly, string filePath) + /// Thrown when unable to write the file to the provided file path. + public void Write(ExportableAssembly assembly) { if (assembly == null) { throw new ArgumentNullException(nameof(assembly)); } - if (filePath == null) - { - throw new ArgumentNullException(nameof(filePath)); - } - try { var settings = new XmlWriterSettings { Indent = true }; - using (var writer = XmlWriter.Create(filePath, settings)) - { - writer.WriteStartDocument(); - writer.WriteStartElement(AssemblyXmlIdentifiers.UboiNamespaceIdentifier, AssemblyXmlIdentifiers.AssemblyCollection, AssemblyXmlIdentifiers.UboiNamespace); - writer.WriteAttributeString(AssemblyXmlIdentifiers.XmlnsIdentifier, AssemblyXmlIdentifiers.XLinkNamespaceIdentifier, null, AssemblyXmlIdentifiers.XLinkNamespace); - writer.WriteAttributeString(AssemblyXmlIdentifiers.XmlnsIdentifier, AssemblyXmlIdentifiers.GmlNamespaceIdentifier, null, AssemblyXmlIdentifiers.GmlNamespace); - writer.WriteAttributeString(AssemblyXmlIdentifiers.XmlnsIdentifier, AssemblyXmlIdentifiers.ImwapNamespaceIdentifier, null, AssemblyXmlIdentifiers.ImwapNamespace); - writer.WriteAttributeString(AssemblyXmlIdentifiers.Id, AssemblyXmlIdentifiers.GmlNamespace, assembly.Id); + writer = XmlWriter.Create(filePath, settings); - WriteFeatureMember(() => WriteAssessmentSection(assembly.AssessmentSection, writer), writer); - WriteFeatureMember(() => WriteAssessmentProcess(assembly.AssessmentProcess, writer), writer); + writer.WriteStartDocument(); + writer.WriteStartElement(AssemblyXmlIdentifiers.UboiNamespaceIdentifier, AssemblyXmlIdentifiers.AssemblyCollection, AssemblyXmlIdentifiers.UboiNamespace); + writer.WriteAttributeString(AssemblyXmlIdentifiers.XmlnsIdentifier, AssemblyXmlIdentifiers.XLinkNamespaceIdentifier, null, AssemblyXmlIdentifiers.XLinkNamespace); + writer.WriteAttributeString(AssemblyXmlIdentifiers.XmlnsIdentifier, AssemblyXmlIdentifiers.GmlNamespaceIdentifier, null, AssemblyXmlIdentifiers.GmlNamespace); + writer.WriteAttributeString(AssemblyXmlIdentifiers.XmlnsIdentifier, AssemblyXmlIdentifiers.ImwapNamespaceIdentifier, null, AssemblyXmlIdentifiers.ImwapNamespace); + writer.WriteAttributeString(AssemblyXmlIdentifiers.Id, AssemblyXmlIdentifiers.GmlNamespace, assembly.Id); - writer.WriteEndElement(); - writer.WriteEndDocument(); - } + WriteFeatureMember(() => WriteAssessmentSection(assembly.AssessmentSection)); + WriteFeatureMember(() => WriteAssessmentProcess(assembly.AssessmentProcess)); + + writer.WriteEndElement(); + writer.WriteEndDocument(); } catch (SystemException e) { throw new CriticalFileWriteException(string.Format(CoreCommonUtilResources.Error_General_output_error_0, filePath), e); } } - private static void WriteFeatureMember(Action writeElementAction, XmlWriter writer) + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (disposing) + { + writer?.Dispose(); + } + } + + private void WriteFeatureMember(Action writeElementAction) + { writer.WriteStartElement(AssemblyXmlIdentifiers.FeatureMember, AssemblyXmlIdentifiers.UboiNamespace); writeElementAction(); writer.WriteEndElement(); } - private static void WriteAssessmentSection(ExportableAssessmentSection assessmentSection, XmlWriter writer) + private void WriteAssessmentSection(ExportableAssessmentSection assessmentSection) { - WriteStartElementWithId(AssemblyXmlIdentifiers.AssessmentSection, AssemblyXmlIdentifiers.ImwapNamespace, assessmentSection.Id, writer); + WriteStartElementWithId(AssemblyXmlIdentifiers.AssessmentSection, AssemblyXmlIdentifiers.ImwapNamespace, assessmentSection.Id); writer.WriteElementString(AssemblyXmlIdentifiers.Name, AssemblyXmlIdentifiers.ImwapNamespace, assessmentSection.Name); @@ -111,25 +140,25 @@ writer.WriteEndElement(); } - private static void WriteAssessmentProcess(ExportableAssessmentProcess assessmentProcess, XmlWriter writer) + private void WriteAssessmentProcess(ExportableAssessmentProcess assessmentProcess) { - WriteStartElementWithId(AssemblyXmlIdentifiers.AssessmentProcess, AssemblyXmlIdentifiers.UboiNamespace, assessmentProcess.Id, writer); - + WriteStartElementWithId(AssemblyXmlIdentifiers.AssessmentProcess, AssemblyXmlIdentifiers.UboiNamespace, assessmentProcess.Id); + writer.WriteElementString(AssemblyXmlIdentifiers.StartYear, AssemblyXmlIdentifiers.UboiNamespace, XmlConvert.ToString(assessmentProcess.StartYear)); writer.WriteElementString(AssemblyXmlIdentifiers.EndYear, AssemblyXmlIdentifiers.UboiNamespace, XmlConvert.ToString(assessmentProcess.EndYear)); - WriteLink(AssemblyXmlIdentifiers.Assesses, assessmentProcess.AssessmentSectionId, writer); + WriteLink(AssemblyXmlIdentifiers.Assesses, assessmentProcess.AssessmentSectionId); writer.WriteEndElement(); } - private static void WriteStartElementWithId(string elementName, string elementNamespace, string id, XmlWriter writer) + private void WriteStartElementWithId(string elementName, string elementNamespace, string id) { writer.WriteStartElement(elementName, elementNamespace); writer.WriteAttributeString(AssemblyXmlIdentifiers.Id, AssemblyXmlIdentifiers.GmlNamespace, id); } - private static void WriteLink(string elementName, string linkedId, XmlWriter writer) + private void WriteLink(string elementName, string linkedId) { writer.WriteStartElement(elementName, AssemblyXmlIdentifiers.UboiNamespace); writer.WriteAttributeString(AssemblyXmlIdentifiers.Link, AssemblyXmlIdentifiers.XLinkNamespace, linkedId); Index: Riskeer/AssemblyTool/test/Riskeer.AssemblyTool.IO.Test/AssemblyGmlWriterTest.cs =================================================================== diff -u -r63073321f76a879b6f7befcb89decc214875fde7 -rd77ef3a34ead6939d21fdc69fb8cf3a47adf70ec --- Riskeer/AssemblyTool/test/Riskeer.AssemblyTool.IO.Test/AssemblyGmlWriterTest.cs (.../AssemblyGmlWriterTest.cs) (revision 63073321f76a879b6f7befcb89decc214875fde7) +++ Riskeer/AssemblyTool/test/Riskeer.AssemblyTool.IO.Test/AssemblyGmlWriterTest.cs (.../AssemblyGmlWriterTest.cs) (revision d77ef3a34ead6939d21fdc69fb8cf3a47adf70ec) @@ -39,44 +39,68 @@ nameof(AssemblyGmlWriterTest)); [Test] - public void Write_AssemblyNull_ThrowsArgumentNullException() + public void Constructor_FilePathNull_ThrowsArgumentException() { // Call - void Call() => AssemblyGmlWriter.Write(null, string.Empty); + void Call() => new AssemblyGmlWriter(null); // Assert - var exception = Assert.Throws(Call); - Assert.AreEqual("assembly", exception.ParamName); + Assert.Throws(Call); } [Test] - public void Write_FilePathNull_ThrowsArgumentNullException() + [TestCaseSource(typeof(InvalidPathHelper), nameof(InvalidPathHelper.InvalidPaths))] + public void Constructor_InvalidFilePath_ThrowsArgumentException(string filePath) { - // Setup - ExportableAssembly assembly = CreateExportableAssembly(); + // Call + void Call() => new AssemblyGmlWriter(filePath); + // Assert + Assert.Throws(Call); + } + + [Test] + public void Constructor_ExpectedValues() + { // Call - void Call() => AssemblyGmlWriter.Write(assembly, null); + var writer = new AssemblyGmlWriter("filepath"); // Assert - var exception = Assert.Throws(Call); - Assert.AreEqual("filePath", exception.ParamName); + Assert.IsInstanceOf(writer); } [Test] + public void Write_AssemblyNull_ThrowsArgumentNullException() + { + // Setup + using (var writer = new AssemblyGmlWriter("filepath")) + { + // Call + void Call() => writer.Write(null); + + // Assert + var exception = Assert.Throws(Call); + Assert.AreEqual("assembly", exception.ParamName); + } + } + + [Test] public void Write_FilePathTooLong_ThrowCriticalFileWriteException() { // Setup ExportableAssembly assembly = CreateExportableAssembly(); var filePath = new string('a', 249); - // Call - void Call() => AssemblyGmlWriter.Write(assembly, filePath); + using (var writer = new AssemblyGmlWriter(filePath)) + { + // Call + void Call() => writer.Write(assembly); - // Assert - var exception = Assert.Throws(Call); - Assert.AreEqual($"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'.", exception.Message); - Assert.IsInstanceOf(exception.InnerException); + // Assert + var exception = Assert.Throws(Call); + Assert.AreEqual($"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'.", exception.Message); + Assert.IsInstanceOf(exception.InnerException); + } } [Test] @@ -91,8 +115,11 @@ try { - // Call - AssemblyGmlWriter.Write(assembly, filePath); + using (var writer = new AssemblyGmlWriter(filePath)) + { + // Call + writer.Write(assembly); + } // Assert Assert.IsTrue(File.Exists(filePath)); @@ -108,7 +135,7 @@ } } - private ExportableAssembly CreateExportableAssembly() + private static ExportableAssembly CreateExportableAssembly() { var assessmentSection = new ExportableAssessmentSection( "Traject A", "section1", new[]