-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add explicit id serializer for compatibility purposes
- Loading branch information
1 parent
86d3cad
commit c1f29bf
Showing
6 changed files
with
142 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using NMF.Models.Repository; | ||
using NMF.Models.Repository.Serialization; | ||
using NMF.Models.Tests.Railway; | ||
using NMF.Serialization; | ||
|
||
namespace NMF.Models.Tests | ||
{ | ||
[TestClass] | ||
public class ExplicitIDTests | ||
{ | ||
private ModelRepository repository; | ||
private Model railwayModel; | ||
private RailwayContainer railway; | ||
|
||
private static readonly string BaseUri = "http://github.com/NMFCode/NMF/Models/Models.Test/railway.railway"; | ||
|
||
[TestInitialize] | ||
public void LoadRailwayModel() | ||
{ | ||
Model.PromoteSingleRootElement = true; | ||
repository = new ModelRepository(); | ||
railwayModel = repository.Resolve(new Uri(BaseUri), "railway.railway").Model; | ||
Assert.IsNotNull(railwayModel); | ||
railway = railwayModel.RootElements.Single() as RailwayContainer; | ||
Assert.IsNotNull(railway); | ||
} | ||
|
||
[TestMethod] | ||
public void ExplicitIdsRenderedCorrectly() | ||
{ | ||
var serializer = new ExplicitIdSerializer(); | ||
using (var ms = new MemoryStream()) | ||
{ | ||
serializer.Serialize(railwayModel, ms); | ||
ms.Position = 0; | ||
using (var reader = new StreamReader(ms)) | ||
{ | ||
var result = reader.ReadToEnd(); | ||
var idPattern = new Regex("xmi:id", RegexOptions.Compiled); | ||
var counts = idPattern.Matches(result).Count; | ||
Assert.AreEqual(railwayModel.Descendants().Count(), counts); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
Models/Models/Repository/Serialization/ExplicitIdSerializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Xml; | ||
using NMF.Serialization; | ||
using NMF.Serialization.Xmi; | ||
|
||
namespace NMF.Models.Repository.Serialization | ||
{ | ||
/// <summary> | ||
/// Denotes a serializer that is able to read and understand all model URIs but serializes using XMI IDs | ||
/// </summary> | ||
public class ExplicitIdSerializer : ModelSerializer | ||
{ | ||
public ExplicitIdSerializer() | ||
{ | ||
} | ||
|
||
public ExplicitIdSerializer(XmlSerializationSettings settings) : base(settings) | ||
{ | ||
} | ||
|
||
public ExplicitIdSerializer(XmlSerializer parent) : base(parent) | ||
{ | ||
} | ||
|
||
public ExplicitIdSerializer(XmlSerializationSettings settings, IEnumerable<Type> knownTypes) : base(settings, knownTypes) | ||
{ | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override string GetAttributeValue(object value, ITypeSerializationInfo info, bool isCollection, XmlSerializationContext context) | ||
{ | ||
if (value is IModelElement modelElement && modelElement.Model == context.Root) | ||
{ | ||
return info.IdentifierProperty.GetValue(value, context).ToString(); | ||
} | ||
return base.GetAttributeValue(value, info, isCollection, context); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override bool WriteIdentifiedObject(XmlWriter writer, object obj, XmlIdentificationMode identificationMode, ITypeSerializationInfo info, XmlSerializationContext context) | ||
{ | ||
return false; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override IPropertySerializationInfo IdAttribute => XmiArtificialIdAttribute.Instance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters