Skip to content

Commit

Permalink
XWIKI-22702: Provide parentType information in EntityReference
Browse files Browse the repository at this point in the history
  * Use the new information in RelativeStringEntityReferenceResolver
  * Fix tests
  • Loading branch information
surli committed Dec 2, 2024
1 parent 4ac12f5 commit 1835f09
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ && isParentTypeAndAllowedTypeNotMatching(types, reference.getParentType())) {
EntityReference newReference = resolveDefaultReference(reference.getParentType(), parameters);
normalizedReference = normalizedReference.appendParent(newReference);
reference = newReference;
} else if (reference.getParent() != null && (types.isEmpty() || reference.getParentType() == null)) {
} else if (reference.getParent() != null && types.isEmpty()) {
// There's a parent but no one is allowed
throw new InvalidEntityReferenceException();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,13 @@ private EntityReference appendNewReference(EntityReference reference, EntityRefe
} else {
return newReference;
}
} else if (reference != null) {
EntityReference root = reference.getRoot();
if (root.getType() == EntityType.SPACE) {
EntityReference newRoot =
new EntityReference(root, Map.of(EntityReference.PARENT_TYPE_PARAMETER, EntityType.SPACE));
return (reference == root) ? newRoot : reference.replaceParent(root, newRoot);
}
}

return reference;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,22 @@
@ComponentList({
DefaultSymbolScheme.class
})
public class RelativeStringEntityReferenceResolverTest
class RelativeStringEntityReferenceResolverTest
{
@InjectMockComponents
private RelativeStringEntityReferenceResolver resolver;

@Test
public void resolveDocumentReference()
void resolveDocumentReference()
{
EntityReference reference = this.resolver.resolve("", EntityType.DOCUMENT);
assertNull(reference);

reference = this.resolver.resolve("space.page", EntityType.DOCUMENT);
assertNull(reference.extractReference(EntityType.WIKI));
assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
EntityReference spaceReference = reference.extractReference(EntityType.SPACE);
assertEquals("space", spaceReference.getName());
assertEquals(EntityType.SPACE, spaceReference.getParentType());
assertEquals("page", reference.getName());

reference = this.resolver.resolve("wiki:space.page", EntityType.DOCUMENT);
Expand All @@ -62,7 +64,7 @@ public void resolveDocumentReference()
}

@Test
public void resolveDocumentReferenceWithBaseReference()
void resolveDocumentReferenceWithBaseReference()
{
EntityReference reference =
this.resolver.resolve("", EntityType.DOCUMENT, new EntityReference("space", EntityType.SPACE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import javax.inject.Inject;

import java.util.Map;

import org.junit.jupiter.api.Test;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.EntityReference;
Expand Down Expand Up @@ -57,7 +59,9 @@ void convertDocumentFromString()
new EntityReference("wiki", EntityType.WIKI)));
assertEquals(reference, this.converterManager.convert(EntityReference.class, "document:wiki:space.page"));

reference = new EntityReference("page", EntityType.DOCUMENT, new EntityReference("space", EntityType.SPACE));
reference = new EntityReference("page", EntityType.DOCUMENT,
new EntityReference("space", EntityType.SPACE,
Map.of(EntityReference.PARENT_TYPE_PARAMETER, EntityType.SPACE)));
assertEquals(reference, this.converterManager.convert(EntityReference.class, "document:space.page"));
assertEquals(reference, this.converterManager.convert(EntityReference.class, "space.page"));

Expand All @@ -74,7 +78,8 @@ void convertSpaceFromString()
reference = new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI));
assertEquals(reference, this.converterManager.convert(EntityReference.class, "space:wiki:space"));

reference = new EntityReference("space", EntityType.SPACE);
reference = new EntityReference("space", EntityType.SPACE, Map.of(EntityReference.PARENT_TYPE_PARAMETER,
EntityType.SPACE));
assertEquals(reference, this.converterManager.convert(EntityReference.class, "space:space"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@

import java.util.Locale;

import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.xwiki.model.EntityType;
import org.xwiki.model.internal.reference.DefaultSymbolScheme;
import org.xwiki.model.internal.reference.RelativeStringEntityReferenceResolver;
import org.xwiki.test.annotation.ComponentList;
import org.xwiki.test.mockito.MockitoComponentMockingRule;
import org.xwiki.test.junit5.mockito.ComponentTest;
import org.xwiki.test.junit5.mockito.InjectMockComponents;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Validate {@link EntityReferenceSet}.
Expand All @@ -40,11 +40,11 @@
@ComponentList({
DefaultSymbolScheme.class
})
public class EntityReferenceSetTest
@ComponentTest
class EntityReferenceSetTest
{
@Rule
public MockitoComponentMockingRule<RelativeStringEntityReferenceResolver> resolverMocker =
new MockitoComponentMockingRule<>(RelativeStringEntityReferenceResolver.class);
@InjectMockComponents
private RelativeStringEntityReferenceResolver resolver;

private EntityReferenceSet set = new EntityReferenceSet();

Expand All @@ -60,12 +60,12 @@ private void assertNotMatches(EntityReference reference)

private void assertMatches(String reference, EntityType type) throws Exception
{
assertMatches(this.resolverMocker.getComponentUnderTest().resolve(reference, type));
assertMatches(this.resolver.resolve(reference, type));
}

private void assertNotMatches(String reference, EntityType type) throws Exception
{
assertNotMatches(this.resolverMocker.getComponentUnderTest().resolve(reference, type));
assertNotMatches(this.resolver.resolve(reference, type));
}

private void assertMatchesWiki(String reference) throws Exception
Expand Down Expand Up @@ -107,7 +107,7 @@ private void includes(EntityReference reference)

private void includes(String reference, EntityType type) throws Exception
{
includes(this.resolverMocker.getComponentUnderTest().resolve(reference, type));
includes(this.resolver.resolve(reference, type));
}

private void includesWiki(String reference) throws Exception
Expand All @@ -134,7 +134,7 @@ private void excludes(EntityReference reference)

private void excludes(String reference, EntityType type) throws Exception
{
excludes(this.resolverMocker.getComponentUnderTest().resolve(reference, type));
excludes(this.resolver.resolve(reference, type));
}

private void excludesWiki(String reference) throws Exception
Expand All @@ -155,7 +155,7 @@ private void excludesDocument(String reference) throws Exception
// Tests

@Test
public void includeWiki() throws Exception
void includeWiki() throws Exception
{
includesWiki("wiki");

Expand All @@ -175,7 +175,7 @@ public void includeWiki() throws Exception
}

@Test
public void includeSpace() throws Exception
void includeSpace() throws Exception
{
includesSpace("wiki:space");

Expand All @@ -194,7 +194,7 @@ public void includeSpace() throws Exception
}

@Test
public void includeNestedSpace() throws Exception
void includeNestedSpace() throws Exception
{
includesSpace("wiki:space.nested");

Expand All @@ -218,7 +218,7 @@ public void includeNestedSpace() throws Exception
}

@Test
public void includePartialOnlySpace() throws Exception
void includePartialOnlySpace() throws Exception
{
includesSpace("space");

Expand All @@ -230,7 +230,7 @@ public void includePartialOnlySpace() throws Exception
}

@Test
public void includeDocument() throws Exception
void includeDocument() throws Exception
{
includesDocument("wiki:space.document");

Expand All @@ -242,7 +242,7 @@ public void includeDocument() throws Exception
}

@Test
public void includeLocalDocumentLocale()
void includeLocalDocumentLocale()
{
includes(new LocalDocumentReference("space", "document", Locale.ROOT));

Expand All @@ -261,7 +261,7 @@ public void includeLocalDocumentLocale()
}

@Test
public void includeDocumentInNestedSpace() throws Exception
void includeDocumentInNestedSpace() throws Exception
{
includesDocument("wiki:space.nestedspace.document");

Expand All @@ -276,7 +276,7 @@ public void includeDocumentInNestedSpace() throws Exception
}

@Test
public void includeDocumentsInNestedSpacesWithShortAfterLong() throws Exception
void includeDocumentsInNestedSpacesWithShortAfterLong() throws Exception
{
includesDocument("wiki:space.nestedspace.document");
includesDocument("wiki:space.document");
Expand All @@ -292,7 +292,7 @@ public void includeDocumentsInNestedSpacesWithShortAfterLong() throws Exception
}

@Test
public void includeDocumentsInNestedSpacesWithLongAfterShort() throws Exception
void includeDocumentsInNestedSpacesWithLongAfterShort() throws Exception
{
includesDocument("wiki:space.document");
includesDocument("wiki:space.nestedspace.document");
Expand All @@ -308,7 +308,7 @@ public void includeDocumentsInNestedSpacesWithLongAfterShort() throws Exception
}

@Test
public void excludeWiki() throws Exception
void excludeWiki() throws Exception
{
excludesWiki("wiki");

Expand All @@ -328,7 +328,7 @@ public void excludeWiki() throws Exception
}

@Test
public void excludeSpace() throws Exception
void excludeSpace() throws Exception
{
excludesSpace("wiki:space");

Expand All @@ -340,7 +340,7 @@ public void excludeSpace() throws Exception
}

@Test
public void excludeNestedSpace() throws Exception
void excludeNestedSpace() throws Exception
{
excludesSpace("wiki:space.nested");

Expand All @@ -355,10 +355,11 @@ public void excludeNestedSpace() throws Exception
}

@Test
public void excludePartial() throws Exception
void excludePartial() throws Exception
{
excludesSpace("space");

// FIXME: Assertion is false because the excluded space contains a parentType parameter since it's relative
assertNotMatches(new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI)));
assertNotMatches(new EntityReference("space", EntityType.SPACE));

Expand All @@ -367,7 +368,7 @@ public void excludePartial() throws Exception
}

@Test
public void includeLocale()
void includeLocale()
{
includes(new DocumentReference("wiki", "space", "document", Locale.ENGLISH));

Expand All @@ -387,7 +388,7 @@ public void includeLocale()
}

@Test
public void excludeLocale()
void excludeLocale()
{
excludes(new DocumentReference("wiki", "space", "document", Locale.ENGLISH));

Expand Down

0 comments on commit 1835f09

Please sign in to comment.