Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions ECoreNetto.Tests/Resource/GetUriFragmentTestFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// -------------------------------------------------------------------------------------------------
// <copyright file="GetUriFragmentTestFixture.cs" company="Starion Group S.A.">
//
// Copyright 2017-2025 Starion Group S.A.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// </copyright>
// ------------------------------------------------------------------------------------------------

namespace ECoreNetto.Tests.Resource
{
using System;
using System.IO;
using System.Linq;

using ECoreNetto.Resource;

using NUnit.Framework;

/// <summary>
/// Suite of tests that verify <see cref="Resource.GetURIFragment"/> is the inverse of
/// <see cref="Resource.GetEObject(string)"/> and is appropriately guarded (see issue #76).
/// </summary>
[TestFixture]
public class GetUriFragmentTestFixture
{
private ResourceSet resourceSet = null!;

private Resource resource = null!;

private EPackage rootPackage = null!;

[SetUp]
public void SetUp()
{
var path = Path.Combine(TestContext.CurrentContext.TestDirectory, "Data", "recipe.ecore");
var uri = new Uri(Path.GetFullPath(path));

this.resourceSet = new ResourceSet();
this.resource = this.resourceSet.CreateResource(uri);
this.rootPackage = this.resource.Load(null);
}

[Test]
public void Verify_that_the_root_package_round_trips()
{
var fragment = this.resource.GetURIFragment(this.rootPackage);

Assert.That(this.resource.GetEObject(fragment), Is.SameAs(this.rootPackage));
}

[Test]
public void Verify_that_a_class_round_trips()
{
var eClass = this.rootPackage.EClassifiers.OfType<EClass>().First();

var fragment = this.resource.GetURIFragment(eClass);

Assert.Multiple(() =>
{
Assert.That(fragment, Is.EqualTo(eClass.Identifier));
Assert.That(this.resource.GetEObject(fragment), Is.SameAs(eClass));
});
}

[Test]
public void Verify_that_a_structural_feature_round_trips()
{
var feature = this.rootPackage.EClassifiers
.OfType<EClass>()
.SelectMany(eClass => eClass.EStructuralFeatures)
.First();

var fragment = this.resource.GetURIFragment(feature);

Assert.That(this.resource.GetEObject(fragment), Is.SameAs(feature));
}

[Test]
public void Verify_that_an_enum_literal_round_trips()
{
// the recipe model defines the 'Unit' enumeration with a 'PIECE' literal
var literal = this.resource.GetEObject("recipe.ecore#//Unit/PIECE");
Assert.That(literal, Is.Not.Null);

var fragment = this.resource.GetURIFragment(literal!);

Assert.That(this.resource.GetEObject(fragment), Is.SameAs(literal));
}

[Test]
public void Verify_that_a_null_object_throws()
{
Assert.That(() => this.resource.GetURIFragment(null!), Throws.ArgumentNullException);
}

[Test]
public void Verify_that_an_object_not_contained_in_the_resource_throws()
{
var eClass = this.rootPackage.EClassifiers.OfType<EClass>().First();

// a different resource does not contain the object, so a fragment cannot be produced
var otherResource = new Resource();

Assert.That(() => otherResource.GetURIFragment(eClass), Throws.TypeOf<InvalidOperationException>());
}
}
}
31 changes: 29 additions & 2 deletions ECoreNetto/Resource/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,16 @@
{ "//EGenericType", ecoreObjectFactory.EGenericType },
{ "//ETypeParameter", ecoreObjectFactory.ETypeParameter },

{ "http://www.eclipse.org/emf/2002/Ecore#//EBigDecimal", ecoreObjectFactory.EBigDecimal},

Check warning on line 116 in ECoreNetto/Resource/Resource.cs

View workflow job for this annotation

GitHub Actions / Build

Using http protocol is insecure. Use https instead.
{ "http://www.eclipse.org/emf/2002/Ecore#//EBigInteger", ecoreObjectFactory.EBigInteger},

Check warning on line 117 in ECoreNetto/Resource/Resource.cs

View workflow job for this annotation

GitHub Actions / Build

Using http protocol is insecure. Use https instead.
{ "http://www.eclipse.org/emf/2002/Ecore#//EBool", ecoreObjectFactory.EBool},

Check warning on line 118 in ECoreNetto/Resource/Resource.cs

View workflow job for this annotation

GitHub Actions / Build

Using http protocol is insecure. Use https instead.
{ "http://www.eclipse.org/emf/2002/Ecore#//EBooleanObject", ecoreObjectFactory.EBooleanObject},

Check warning on line 119 in ECoreNetto/Resource/Resource.cs

View workflow job for this annotation

GitHub Actions / Build

Using http protocol is insecure. Use https instead.
{ "http://www.eclipse.org/emf/2002/Ecore#//EByte", ecoreObjectFactory.EByte},

Check warning on line 120 in ECoreNetto/Resource/Resource.cs

View workflow job for this annotation

GitHub Actions / Build

Using http protocol is insecure. Use https instead.
{ "http://www.eclipse.org/emf/2002/Ecore#//EByteArray", ecoreObjectFactory.EByteArray},

Check warning on line 121 in ECoreNetto/Resource/Resource.cs

View workflow job for this annotation

GitHub Actions / Build

Using http protocol is insecure. Use https instead.
{ "http://www.eclipse.org/emf/2002/Ecore#//EByteObject", ecoreObjectFactory.EByteObject},

Check warning on line 122 in ECoreNetto/Resource/Resource.cs

View workflow job for this annotation

GitHub Actions / Build

Using http protocol is insecure. Use https instead.
{ "http://www.eclipse.org/emf/2002/Ecore#//EChar", ecoreObjectFactory.EChar},

Check warning on line 123 in ECoreNetto/Resource/Resource.cs

View workflow job for this annotation

GitHub Actions / Build

Using http protocol is insecure. Use https instead.
{ "http://www.eclipse.org/emf/2002/Ecore#//ECharacterObject", ecoreObjectFactory.ECharacterObject},

Check warning on line 124 in ECoreNetto/Resource/Resource.cs

View workflow job for this annotation

GitHub Actions / Build

Using http protocol is insecure. Use https instead.
{ "http://www.eclipse.org/emf/2002/Ecore#//EDate", ecoreObjectFactory.EDate},

Check warning on line 125 in ECoreNetto/Resource/Resource.cs

View workflow job for this annotation

GitHub Actions / Build

Using http protocol is insecure. Use https instead.
{ "http://www.eclipse.org/emf/2002/Ecore#//EDiagnosticChain", ecoreObjectFactory.EDiagnosticChain},
{ "http://www.eclipse.org/emf/2002/Ecore#//EDouble", ecoreObjectFactory.EDouble},
{ "http://www.eclipse.org/emf/2002/Ecore#//EDoubleObject", ecoreObjectFactory.EDoubleObject},
Expand Down Expand Up @@ -201,17 +201,44 @@
}

/// <summary>
/// Returns the URI fragment that, when passed to getEObject will return the given object.
/// Returns the URI fragment that, when passed to <see cref="GetEObject(string)"/>, will return the given object.
/// </summary>
/// <param name="eObject">
/// The object to identify
/// </param>
/// <returns>
/// the URI fragment for the object.
/// </returns>
/// <remarks>
/// The returned fragment is the name-based Ecore reference under which the object is registered in this
/// resource (its <see cref="EObject.Identifier"/>), for example <c>recipe.ecore#//Recipe</c> for a class or
/// <c>EStructuralFeature::recipe.ecore#//Recipe/ingredients</c> for a structural feature. This is exactly the
/// reference string consumed by <see cref="GetEObject(string)"/>, so the round-trip
/// <c>GetEObject(GetURIFragment(eObject))</c> returns the same instance.
/// </remarks>
/// <exception cref="ArgumentNullException">
/// Thrown when <paramref name="eObject"/> is null.
/// </exception>
/// <exception cref="InvalidOperationException">
/// Thrown when <paramref name="eObject"/> is not contained in this resource and therefore cannot be turned
/// into a resolvable URI fragment.
/// </exception>
public string GetURIFragment(EObject eObject)
{
throw new NotImplementedException();
if (eObject == null)
{
throw new ArgumentNullException(nameof(eObject));
}

var fragment = eObject.Identifier;

if (string.IsNullOrEmpty(fragment) || !this.Cache.TryGetValue(fragment, out var cached) || !ReferenceEquals(cached, eObject))
{
throw new InvalidOperationException(
$"The provided '{eObject.GetType().Name}' is not contained in this resource and cannot be turned into a URI fragment.");
}

return fragment;
}

/// <summary>
Expand Down
Loading