public void WriteAssembly_FullyConfiguredAssembly_ReturnsExpectedFile() { // Setup string folderPath = TestHelper.GetScratchPadPath(nameof(WriteAssembly_FullyConfiguredAssembly_ReturnsExpectedFile)); Directory.CreateDirectory(folderPath); string filePath = Path.Combine(folderPath, "actualAssembly.gml"); SerializableAssembly assembly = CreateSerializableAssembly(); try { // Call SerializableAssemblyWriter.WriteAssembly(assembly, filePath); // Assert Assert.IsTrue(File.Exists(filePath)); string pathToExpectedFile = Path.Combine(testDataPath, "configuredAssembly.gml"); string expectedXml = File.ReadAllText(pathToExpectedFile); string actualXml = File.ReadAllText(filePath); Assert.AreEqual(expectedXml, actualXml); } finally { DirectoryHelper.TryDelete(folderPath); } }
/// <summary> /// Writes a <see cref="SerializableAssembly"/> to a file. /// </summary> /// <param name="serializableAssembly">The <see cref="SerializableAssembly"/> to be written to the file.</param> /// <param name="filePath">The path to the file.</param> /// <exception cref="ArgumentNullException">Thrown when any parameter is <c>null</c>.</exception> /// <exception cref="CriticalFileWriteException">Thrown when unable to write to <paramref name="filePath"/>.</exception> public static void WriteAssembly(SerializableAssembly serializableAssembly, string filePath) { if (serializableAssembly == null) { throw new ArgumentNullException(nameof(serializableAssembly)); } if (filePath == null) { throw new ArgumentNullException(nameof(filePath)); } TextWriter writer = null; try { var serializer = new XmlSerializer(typeof(SerializableAssembly)); writer = new StreamWriter(filePath); var xmlns = new XmlSerializerNamespaces(); xmlns.Add(AssemblyXmlIdentifiers.GmlNamespaceIdentifier, AssemblyXmlIdentifiers.GmlNamespace); xmlns.Add(AssemblyXmlIdentifiers.AssemblyNamespaceIdentifier, AssemblyXmlIdentifiers.AssemblyNamespace); serializer.Serialize(writer, serializableAssembly, xmlns); } catch (SystemException e) { throw new CriticalFileWriteException(string.Format(CoreCommonUtilResources.Error_General_output_error_0, filePath), e); } finally { writer?.Close(); } }
public static AssemblyDTG AssemblyDtg(SerializableAssembly assemblyModel) { TypeDtgDictionary = new Dictionary <string, TypeDTG>(); return(new AssemblyDTG() { Name = assemblyModel.Name, MetadataName = assemblyModel.MetadataName, Namespaces = assemblyModel.SerializableNamespaces?.Select(NamespaceDtg).ToList() }); }
public void Serialize(AssemblyDTG assembly) { string fileName = Settings1.Default.path; SerializableAssembly serRefl = new SerializableAssembly(assembly); FileStream file = new FileStream(fileName, FileMode.Create, FileAccess.Write); DataContractSerializer SerializerObj = new DataContractSerializer(typeof(SerializableAssembly), null, int.MaxValue, false, true, null); SerializerObj.WriteObject(file, serRefl); file.Close(); }
private static string GetSerializedAssembly(SerializableAssembly assembly) { var serializer = new XmlSerializer(typeof(SerializableAssembly)); var xmlns = new XmlSerializerNamespaces(); xmlns.Add("gml", "http://www.opengis.net/gml/3.2"); xmlns.Add("asm", "http://localhost/standaarden/assemblage"); var writer = new StringWriterUtf8(); serializer.Serialize(writer, assembly, xmlns); return(writer.ToString()); }
public AssemblyDTG Deserialize() { string fileName = Settings1.Default.path; DataContractSerializer SerializerObj = new DataContractSerializer(typeof(SerializableAssembly), null, int.MaxValue, false, true, null); FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); SerializableAssembly assembly = (SerializableAssembly)SerializerObj.ReadObject(file); AssemblyDTG assemblyDTG = MapperXml.AssemblyDtg(assembly); file.Close(); return(assemblyDTG); }
public void DefaultConstructor_ReturnsDefaultValues() { // Call var assembly = new SerializableAssembly(); // Assert Assert.IsNull(assembly.Id); Assert.IsNull(assembly.Boundary); Assert.IsNull(assembly.FeatureMembers); object[] serializableAttributes = typeof(SerializableAssembly).GetCustomAttributes(typeof(SerializableAttribute), false); Assert.AreEqual(1, serializableAttributes.Length); var xmlRootAttribute = (XmlRootAttribute)typeof(SerializableAssembly).GetCustomAttributes(typeof(XmlRootAttribute), false).Single(); Assert.AreEqual("Assemblage", xmlRootAttribute.ElementName); Assert.AreEqual("http://localhost/standaarden/assemblage", xmlRootAttribute.Namespace); const string gmlNamespace = "http://www.opengis.net/gml/3.2"; SerializableAttributeTestHelper.AssertXmlAttributeAttribute <SerializableAssembly>( nameof(SerializableAssembly.Id), "id", gmlNamespace); SerializableAttributeTestHelper.AssertXmlElementAttribute <SerializableAssembly>( nameof(SerializableAssembly.Boundary), "boundedBy", gmlNamespace); XmlArrayAttribute xmlArrayAttribute = TypeUtils.GetPropertyAttributes <SerializableAssembly, XmlArrayAttribute>(nameof(SerializableAssembly.FeatureMembers)).Single(); Assert.AreEqual("featureMember", xmlArrayAttribute.ElementName); IEnumerable <XmlArrayItemAttribute> xmlArrayItemAttributes = TypeUtils.GetPropertyAttributes <SerializableAssembly, XmlArrayItemAttribute>(nameof(SerializableAssembly.FeatureMembers)); Assert.AreEqual(8, xmlArrayItemAttributes.Count()); Assert.AreEqual(typeof(SerializableAssessmentSection), xmlArrayItemAttributes.ElementAt(0).Type); Assert.AreEqual(typeof(SerializableAssessmentProcess), xmlArrayItemAttributes.ElementAt(1).Type); Assert.AreEqual(typeof(SerializableTotalAssemblyResult), xmlArrayItemAttributes.ElementAt(2).Type); Assert.AreEqual(typeof(SerializableFailureMechanism), xmlArrayItemAttributes.ElementAt(3).Type); Assert.AreEqual(typeof(SerializableFailureMechanismSectionAssembly), xmlArrayItemAttributes.ElementAt(4).Type); Assert.AreEqual(typeof(SerializableCombinedFailureMechanismSectionAssembly), xmlArrayItemAttributes.ElementAt(5).Type); Assert.AreEqual(typeof(SerializableFailureMechanismSectionCollection), xmlArrayItemAttributes.ElementAt(6).Type); Assert.AreEqual(typeof(SerializableFailureMechanismSection), xmlArrayItemAttributes.ElementAt(7).Type); }
public void WriteAssembly_ValidData_ValidFile() { // Setup string filePath = TestHelper.GetScratchPadPath(nameof(WriteAssembly_ValidData_ValidFile)); SerializableAssembly assembly = CreateSerializableAssembly(); try { // Call SerializableAssemblyWriter.WriteAssembly(assembly, filePath); // Assert Assert.IsTrue(File.Exists(filePath)); string fileContent = File.ReadAllText(filePath); Assert.AreEqual(GetSerializedAssembly(assembly), fileContent); } finally { File.Delete(filePath); } }
public void GivenFullyConfiguredAssembly_WhenWrittenToFile_ThenValidFileCreated() { // Given SerializableAssembly assembly = CreateSerializableAssembly(); string filePath = TestHelper.GetScratchPadPath(nameof(GivenFullyConfiguredAssembly_WhenWrittenToFile_ThenValidFileCreated)); try { // When SerializableAssemblyWriter.WriteAssembly(assembly, filePath); // Then Assert.IsTrue(File.Exists(filePath)); string fileContent = File.ReadAllText(filePath); Console.WriteLine(fileContent); var schema = new XmlSchemaSet(); schema.Add("http://localhost/standaarden/assemblage", Path.Combine(testDataPath, "assemblage.xsd")); XDocument doc = XDocument.Parse(fileContent); var msg = string.Empty; doc.Validate(schema, (o, e) => { msg += e.Message + Environment.NewLine; }); if (msg == string.Empty) { Assert.Pass("Serialized document is valid" + Environment.NewLine); } else { Assert.Fail("Serialized document is invalid:" + Environment.NewLine + msg); } } finally { File.Delete(filePath); } }
public void WriteAssembly_InvalidData_ThrowsCriticalFileWriteException() { // Setup string filePath = TestHelper.GetScratchPadPath(nameof(WriteAssembly_InvalidData_ThrowsCriticalFileWriteException)); var assembly = new SerializableAssembly( "id", new Point2D(0.0, 10.0), new Point2D(10.0, 20.0), new SerializableAssessmentSection(), new SerializableAssessmentProcess(), new SerializableTotalAssemblyResult(), new[] { new SerializableFailureMechanism() }, Enumerable.Empty <SerializableFailureMechanismSectionAssembly>(), Enumerable.Empty <SerializableCombinedFailureMechanismSectionAssembly>(), Enumerable.Empty <SerializableFailureMechanismSectionCollection>(), Enumerable.Empty <SerializableFailureMechanismSection>()); try { // Call void Call() => SerializableAssemblyWriter.WriteAssembly(assembly, filePath); // Assert var exception = Assert.Throws <CriticalFileWriteException>(Call); Assert.AreEqual($"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'.", exception.Message); Assert.IsInstanceOf <InvalidOperationException>(exception.InnerException); } finally { File.Delete(filePath); } }
public void CreateSerializableAssembly_WithValidArguments_ReturnsSerializableAssembly() { // Setup const string assessmentSectionName = "assessmentSectionName"; const string assessmentSectionId = "assessmentSectionId"; IEnumerable <Point2D> geometry = CreateGeometry(); ExportableAssessmentSectionAssemblyResult assessmentSectionAssembly = ExportableAssessmentSectionAssemblyResultTestFactory.CreateResult(); IEnumerable <ExportableFailureMechanism> failureMechanisms = new[] { CreateFailureMechanism(), CreateFailureMechanism(), CreateFailureMechanism(), CreateFailureMechanism() }; var exportableAssessmentSection = new ExportableAssessmentSection(assessmentSectionName, assessmentSectionId, geometry, assessmentSectionAssembly, failureMechanisms, new[] { CreateCombinedSectionAssembly(CreateCombinedFailureMechanismSection()), CreateCombinedSectionAssembly(CreateCombinedFailureMechanismSection()) }); // Call SerializableAssembly serializableAssembly = SerializableAssemblyCreator.Create(exportableAssessmentSection); // Assert Assert.AreEqual("Assemblage.0", serializableAssembly.Id); AssertSerializableBoundary(exportableAssessmentSection.Geometry, serializableAssembly.Boundary); SerializableFeatureMember[] serializableAssemblyFeatureMembers = serializableAssembly.FeatureMembers; Assert.AreEqual(24, serializableAssemblyFeatureMembers.Length); var serializableAssessmentSection = (SerializableAssessmentSection)serializableAssemblyFeatureMembers[0]; AssertSerializableAssessmentSection($"Wks.{assessmentSectionId}", assessmentSectionName, geometry, serializableAssessmentSection); var serializableAssessmentProcess = (SerializableAssessmentProcess)serializableAssemblyFeatureMembers[1]; AssertSerializableAssessmentProcess("Bp.0", serializableAssessmentSection, serializableAssessmentProcess); var serializableTotalAssemblyResult = (SerializableTotalAssemblyResult)serializableAssemblyFeatureMembers[2]; AssertSerializableTotalAssemblyResult("Vo.0", assessmentSectionAssembly, serializableAssessmentProcess, serializableTotalAssemblyResult); AssertFailureMechanismConnections(3, 0, serializableTotalAssemblyResult, serializableAssemblyFeatureMembers); AssertFailureMechanismConnections(4, 1, serializableTotalAssemblyResult, serializableAssemblyFeatureMembers); AssertFailureMechanismConnections(5, 2, serializableTotalAssemblyResult, serializableAssemblyFeatureMembers); AssertFailureMechanismConnections(6, 3, serializableTotalAssemblyResult, serializableAssemblyFeatureMembers); var combinedFailureMechanismSectionCollection = (SerializableFailureMechanismSectionCollection)serializableAssemblyFeatureMembers[17]; AssertSerializableFailureMechanismSectionCollection("Vi.4", combinedFailureMechanismSectionCollection); AssertCombinedFailureMechanismSectionAssemblyConnections(11, 4, 0, combinedFailureMechanismSectionCollection, serializableTotalAssemblyResult, serializableAssemblyFeatureMembers); AssertCombinedFailureMechanismSectionAssemblyConnections(12, 5, 1, combinedFailureMechanismSectionCollection, serializableTotalAssemblyResult, serializableAssemblyFeatureMembers); }
public void Constructor_WithValidData_ReturnsExpectedValues() { // Setup const string id = "assemblyId1"; var random = new Random(39); var lowerCorner = new Point2D(random.NextDouble(), random.NextDouble()); var upperCorner = new Point2D(random.NextDouble(), random.NextDouble()); var assessmentSection = new SerializableAssessmentSection(); var assessmentProcess = new SerializableAssessmentProcess(); var totalAssemblyResult = new SerializableTotalAssemblyResult(); var failureMechanism = new SerializableFailureMechanism(); var failureMechanismSectionAssembly = new SerializableFailureMechanismSectionAssembly(); var combinedFailureMechanismSectionAssembly = new SerializableCombinedFailureMechanismSectionAssembly(); var failureMechanismSections = new SerializableFailureMechanismSectionCollection(); var failureMechanismSection = new SerializableFailureMechanismSection(); // Call var assembly = new SerializableAssembly(id, lowerCorner, upperCorner, assessmentSection, assessmentProcess, totalAssemblyResult, new[] { failureMechanism }, new[] { failureMechanismSectionAssembly }, new[] { combinedFailureMechanismSectionAssembly }, new[] { failureMechanismSections }, new[] { failureMechanismSection }); // Assert Assert.AreEqual(id, assembly.Id); Assert.AreEqual(lowerCorner.X.ToString(CultureInfo.InvariantCulture) + " " + lowerCorner.Y.ToString(CultureInfo.InvariantCulture), assembly.Boundary.Envelope.LowerCorner); Assert.AreEqual(upperCorner.X.ToString(CultureInfo.InvariantCulture) + " " + upperCorner.Y.ToString(CultureInfo.InvariantCulture), assembly.Boundary.Envelope.UpperCorner); CollectionAssert.AreEqual(new SerializableFeatureMember[] { assessmentSection, assessmentProcess, totalAssemblyResult, failureMechanism, failureMechanismSectionAssembly, combinedFailureMechanismSectionAssembly, failureMechanismSections, failureMechanismSection }, assembly.FeatureMembers); }
private static SerializableAssembly CreateSerializableAssembly() { var assessmentSection = new SerializableAssessmentSection("section1", "Traject A", new[] { new Point2D(0, 0), new Point2D(100.0, 0.0) }); var assessmentProcess = new SerializableAssessmentProcess("beoordelingsproces1", assessmentSection); var totalAssemblyResult = new SerializableTotalAssemblyResult( "veiligheidsoordeel_1", assessmentProcess, SerializableAssemblyMethod.BOI2A1, SerializableAssemblyMethod.BOI2B1, SerializableAssessmentSectionAssemblyGroup.B, 0.00068354); var failureMechanism = new SerializableFailureMechanism( "toetsspoorGABI", SerializableFailureMechanismType.Generic, "GABI", "Faalmechanisme GABI", totalAssemblyResult, new SerializableFailureMechanismAssemblyResult(0.08419, SerializableAssemblyMethod.BOI1A1)); var specificFailureMechanism = new SerializableFailureMechanism( "specifiekFaalmechanisme", SerializableFailureMechanismType.Specific, "NIEUW", "Specifiek faalmechanisme", totalAssemblyResult, new SerializableFailureMechanismAssemblyResult(0.002834, SerializableAssemblyMethod.BOI1A1)); var sections1 = new SerializableFailureMechanismSectionCollection("vakindelingGABI"); var section1 = new SerializableFailureMechanismSection( "vak_GABI_1", sections1, 0.12, 10.23, new[] { new Point2D(0.23, 0.24), new Point2D(10.23, 10.24) }, SerializableFailureMechanismSectionType.FailureMechanism); var result = new SerializableFailureMechanismSectionAssembly( "resultaat_GABI_1", failureMechanism, section1, new SerializableFailureMechanismSectionAssemblyResult( SerializableAssemblyMethod.BOI0A2, SerializableAssemblyMethod.BOI0B1, SerializableFailureMechanismSectionAssemblyGroup.III, 0.00073)); var sections2 = new SerializableFailureMechanismSectionCollection("vakindeling_gecombineerd"); var section2 = new SerializableFailureMechanismSection( "vak_gecombineerd_1", sections2, 0.12, 10.23, new[] { new Point2D(0.23, 0.24), new Point2D(10.23, 10.24) }, SerializableFailureMechanismSectionType.Combined, SerializableAssemblyMethod.BOI3A1); var combinedResult = new SerializableCombinedFailureMechanismSectionAssembly( "resultaat_gecombineerd_1", totalAssemblyResult, section2, new[] { new SerializableCombinedFailureMechanismSectionAssemblyResult( SerializableAssemblyMethod.BOI3B1, SerializableFailureMechanismType.Generic, "HTKW", "Hoogte kunstwerk", SerializableFailureMechanismSectionAssemblyGroup.III), new SerializableCombinedFailureMechanismSectionAssemblyResult( SerializableAssemblyMethod.BOI3B1, SerializableFailureMechanismType.Generic, "STPH", "Piping", SerializableFailureMechanismSectionAssemblyGroup.II), new SerializableCombinedFailureMechanismSectionAssemblyResult( SerializableAssemblyMethod.BOI3B1, SerializableFailureMechanismType.Specific, "NIEUW", "Specifiek", SerializableFailureMechanismSectionAssemblyGroup.Zero) }, new SerializableFailureMechanismSubSectionAssemblyResult( SerializableAssemblyMethod.BOI3C1, SerializableFailureMechanismSectionAssemblyGroup.I)); var assembly = new SerializableAssembly( "assemblage_1", new Point2D(12.0, 34.0), new Point2D(56.053, 78.0002345), assessmentSection, assessmentProcess, totalAssemblyResult, new[] { failureMechanism, specificFailureMechanism }, new[] { result }, new[] { combinedResult }, new[] { sections1, sections2 }, new[] { section1, section2 }); return(assembly); }