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
18 changes: 9 additions & 9 deletions src/Umbraco.Core/UdiParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Umbraco.Cms.Core;
/// </summary>
public sealed class UdiParser
{
private static readonly ConcurrentDictionary<string, Udi> RootUdis = new();
private static readonly ConcurrentDictionary<string, Udi> _rootUdis = new();

static UdiParser() =>

Expand Down Expand Up @@ -125,16 +125,16 @@ public static bool TryParse(string? s, bool knownTypes, [MaybeNullWhen(false)] o
/// <returns>A root UDI for the entity type.</returns>
/// <exception cref="ArgumentException">Thrown when the entity type is unknown.</exception>
internal static Udi GetRootUdi(string entityType) =>
RootUdis.GetOrAdd(entityType, x =>
_rootUdis.GetOrAdd(entityType, static x =>
{
if (UdiTypes.TryGetValue(x, out UdiType udiType) == false)
{
throw new ArgumentException(string.Format("Unknown entity type \"{0}\".", entityType));
throw new ArgumentException($"Unknown entity type \"{x}\".");
}

return udiType == UdiType.StringUdi
? new StringUdi(entityType, string.Empty)
: new GuidUdi(entityType, Guid.Empty);
? new StringUdi(x, string.Empty)
: new GuidUdi(x, Guid.Empty);
});

private static bool ParseInternal(string? s, bool tryParse, bool knownTypes, [MaybeNullWhen(false)] out Udi udi)
Expand All @@ -148,7 +148,7 @@ private static bool ParseInternal(string? s, bool tryParse, bool knownTypes, [Ma
return false;
}

throw new FormatException(string.Format("String \"{0}\" is not a valid udi.", s));
throw new FormatException($"String \"{s}\" is not a valid udi.");
}

var entityType = uri.Host;
Expand All @@ -167,7 +167,7 @@ private static bool ParseInternal(string? s, bool tryParse, bool knownTypes, [Ma
return false;
}

throw new FormatException(string.Format("Unknown entity type \"{0}\".", entityType));
throw new FormatException($"Unknown entity type \"{entityType}\".");
}

var path = uri.AbsolutePath.TrimStart('/');
Expand All @@ -187,7 +187,7 @@ private static bool ParseInternal(string? s, bool tryParse, bool knownTypes, [Ma
return false;
}

throw new FormatException(string.Format("String \"{0}\" is not a valid udi.", s));
throw new FormatException($"String \"{s}\" is not a valid udi.");
}

udi = new GuidUdi(uri.Host, guid);
Expand All @@ -205,7 +205,7 @@ private static bool ParseInternal(string? s, bool tryParse, bool knownTypes, [Ma
return false;
}

throw new InvalidOperationException(string.Format("Invalid udi type \"{0}\".", udiType));
throw new InvalidOperationException($"Invalid udi type \"{udiType}\".");
}

/// <summary>
Expand Down
36 changes: 36 additions & 0 deletions tests/Umbraco.Tests.UnitTests/Umbraco.Core/CoreThings/UdiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,42 @@
// because we don't throw anymore - see U4-10409
}

[Test]
public void CreateRootUdi_ForGuidEntityType_ReturnsRootGuidUdi()
{
var udi = Udi.Create(Constants.UdiEntityType.Document);
Assert.AreEqual(Constants.UdiEntityType.Document, udi.EntityType);
Assert.IsTrue(udi.IsRoot);
Assert.IsInstanceOf<GuidUdi>(udi);
Assert.AreEqual(Guid.Empty, ((GuidUdi)udi).Guid);
}

Check warning on line 172 in tests/Umbraco.Tests.UnitTests/Umbraco.Core/CoreThings/UdiTests.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Code Health Review (main)

❌ New issue: Large Assertion Blocks

The test suite contains 5 assertion blocks with at least 4 assertions, threshold = 4. This test file has several blocks of large, consecutive assert statements. Avoid adding more.

[Test]
public void CreateRootUdi_ForStringEntityType_ReturnsRootStringUdi()
{
var udi = Udi.Create(Constants.UdiEntityType.Language);
Assert.AreEqual(Constants.UdiEntityType.Language, udi.EntityType);
Assert.IsTrue(udi.IsRoot);
Assert.IsInstanceOf<StringUdi>(udi);
Assert.AreEqual(string.Empty, ((StringUdi)udi).Id);
}

[Test]
public void CreateRootUdi_ForUnknownEntityType_ThrowsArgumentException()
{
const string unknownType = "not-a-real-entity-type";
ArgumentException? ex = Assert.Throws<ArgumentException>(() => Udi.Create(unknownType));
StringAssert.Contains(unknownType, ex!.Message);
}

[Test]
public void CreateRootUdi_WhenCalledTwice_ReturnsSameCachedInstance()
{
var first = Udi.Create(Constants.UdiEntityType.Media);
var second = Udi.Create(Constants.UdiEntityType.Media);
Assert.AreSame(first, second);
}

[Test]
public void RootUdiTest()
{
Expand Down
Loading