Skip to content

Commit beed054

Browse files
.
1 parent 30daa83 commit beed054

File tree

3 files changed

+332
-17
lines changed

3 files changed

+332
-17
lines changed

SysML2.NET.Serializer.Xmi.Tests/RoundTripTestFixture.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ namespace SysML2.NET.Serializer.Xmi.Tests
2323
using System;
2424
using System.IO;
2525
using System.Linq;
26+
using System.Threading;
27+
using System.Threading.Tasks;
2628
using System.Xml;
2729

2830
using Microsoft.Extensions.DependencyInjection;
@@ -239,5 +241,57 @@ public void VerifySerializedXmlCorrespondToOriginalFile()
239241

240242
Assert.That(xmlDiff.HasDifferences, Is.False);
241243
}
244+
245+
[Test]
246+
public async Task Verify_that_multi_file_SerializeAsync_writes_expected_files()
247+
{
248+
var deSerializer = new DeSerializer(this.loggerFactory);
249+
var originMap = new XmiElementOriginMap();
250+
251+
var filePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "Resources", "Domain Libraries", "Quantities and Units", "Quantities.sysmlx");
252+
var fileUri = new Uri(filePath);
253+
254+
var rootNamespace = await deSerializer.DeSerializeAsync(fileUri, originMap);
255+
256+
var outputDirPath = Path.Combine(TestContext.CurrentContext.TestDirectory, "AsyncMultiFileOutput");
257+
var outputDir = new DirectoryInfo(outputDirPath);
258+
259+
try
260+
{
261+
var serializer = new Serializer(this.loggerFactory);
262+
263+
await serializer.SerializeAsync(rootNamespace, originMap, outputDir, this.writerOptions, CancellationToken.None);
264+
265+
Assert.That(outputDir.Exists, Is.True, "Output directory should exist");
266+
267+
var outputFiles = outputDir.GetFiles("*", SearchOption.AllDirectories);
268+
Assert.That(outputFiles, Is.Not.Empty, "Output directory should contain files");
269+
270+
// The serializer only writes files for namespaces it can resolve from the deserialized graph.
271+
// Referenced external files in the origin map may not have their namespaces available.
272+
Assert.That(outputFiles.Length, Is.GreaterThanOrEqualTo(1), "At least the root namespace file should be written");
273+
Assert.That(outputFiles.Length, Is.LessThanOrEqualTo(originMap.GetAllSourceFiles().Count()), "Should not write more files than source files in origin map");
274+
275+
foreach (var outputFile in outputFiles)
276+
{
277+
Assert.That(outputFile.Length, Is.GreaterThan(0), $"File {outputFile.Name} should not be empty");
278+
279+
var xmlDoc = new XmlDocument();
280+
Assert.That(() => xmlDoc.Load(outputFile.FullName), Throws.Nothing, $"File {outputFile.Name} should be well-formed XML");
281+
282+
var rootElement = xmlDoc.DocumentElement;
283+
Assert.That(rootElement, Is.Not.Null, $"File {outputFile.Name} should have a root element");
284+
Assert.That(rootElement.LocalName, Is.EqualTo("Namespace"), $"File {outputFile.Name} should have root element 'Namespace'");
285+
Assert.That(rootElement.GetAttribute("id", "http://www.omg.org/spec/XMI/20131001"), Is.Not.Empty, $"File {outputFile.Name} should have an xmi:id attribute");
286+
}
287+
}
288+
finally
289+
{
290+
if (Directory.Exists(outputDirPath))
291+
{
292+
Directory.Delete(outputDirPath, true);
293+
}
294+
}
295+
}
242296
}
243297
}

SysML2.NET.Serializer.Xmi/ISerializer.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ public interface ISerializer
6767
/// </param>
6868
void Serialize(INamespace @namespace, XmiWriterOptions writerOptions, Stream stream, IXmiElementOriginMap elementOriginMap, Uri currentFileUri);
6969

70+
/// <summary>
71+
/// Serialize an <see cref="INamespace"/> to multiple XMI files based on the element origin map
72+
/// </summary>
73+
/// <param name="rootNamespace">The root <see cref="INamespace"/> containing all elements</param>
74+
/// <param name="elementOriginMap">The <see cref="IXmiElementOriginMap"/> tracking element-to-file associations</param>
75+
/// <param name="outputDirectory">The target directory for output files</param>
76+
/// <param name="writerOptions">The <see cref="XmiWriterOptions"/> instance that provides writer output configuration</param>
77+
void Serialize(INamespace rootNamespace, IXmiElementOriginMap elementOriginMap, DirectoryInfo outputDirectory, XmiWriterOptions writerOptions);
78+
7079
/// <summary>
7180
/// Asynchronously serialize an <see cref="INamespace"/> as XMI to a target <see cref="Stream"/>
7281
/// </summary>
@@ -83,12 +92,37 @@ public interface ISerializer
8392
Task SerializeAsync(INamespace @namespace, XmiWriterOptions writerOptions, Stream stream, CancellationToken cancellationToken);
8493

8594
/// <summary>
86-
/// Serialize an <see cref="INamespace"/> to multiple XMI files based on the element origin map
95+
/// Asynchronously serialize an <see cref="INamespace"/> as XMI to a target <see cref="Stream"/>,
96+
/// using an origin map for href reconstruction
97+
/// </summary>
98+
/// <param name="namespace">
99+
/// The <see cref="INamespace"/> that shall be serialized
100+
/// </param>
101+
/// <param name="writerOptions">The <see cref="XmiWriterOptions"/> instance that provides writer output configuration</param>
102+
/// <param name="stream">
103+
/// The target <see cref="Stream"/>
104+
/// </param>
105+
/// <param name="elementOriginMap">
106+
/// The optional <see cref="IXmiElementOriginMap"/> for href reconstruction
107+
/// </param>
108+
/// <param name="currentFileUri">
109+
/// The optional <see cref="Uri"/> of the current output file for relative href computation
110+
/// </param>
111+
/// <param name="cancellationToken">
112+
/// The <see cref="CancellationToken"/> used to cancel the operation
113+
/// </param>
114+
Task SerializeAsync(INamespace @namespace, XmiWriterOptions writerOptions, Stream stream, IXmiElementOriginMap elementOriginMap, Uri currentFileUri, CancellationToken cancellationToken);
115+
116+
/// <summary>
117+
/// Asynchronously serialize an <see cref="INamespace"/> to multiple XMI files based on the element origin map
87118
/// </summary>
88119
/// <param name="rootNamespace">The root <see cref="INamespace"/> containing all elements</param>
89120
/// <param name="elementOriginMap">The <see cref="IXmiElementOriginMap"/> tracking element-to-file associations</param>
90121
/// <param name="outputDirectory">The target directory for output files</param>
91122
/// <param name="writerOptions">The <see cref="XmiWriterOptions"/> instance that provides writer output configuration</param>
92-
void Serialize(INamespace rootNamespace, IXmiElementOriginMap elementOriginMap, DirectoryInfo outputDirectory, XmiWriterOptions writerOptions);
123+
/// <param name="cancellationToken">
124+
/// The <see cref="CancellationToken"/> used to cancel the operation
125+
/// </param>
126+
Task SerializeAsync(INamespace rootNamespace, IXmiElementOriginMap elementOriginMap, DirectoryInfo outputDirectory, XmiWriterOptions writerOptions, CancellationToken cancellationToken);
93127
}
94128
}

0 commit comments

Comments
 (0)