DefaultStringEntityReferenceSerializer implements EntityReferenceSerializer<String> { public String serialize(EntityReference reference) { if (reference == null) { return null; } EntityReference currentReference = reference.getRoot(); StringBuilder representation = new StringBuilder(); while (currentReference != null && currentReference != reference.getChild()) { serializeEntityReference(currentReference, representation, currentReference == reference); currentReference = currentReference.getChild(); } return representation.toString(); }  String serialize(EntityReference reference);  }
@Test public void testSerializeDocumentReference() throws Exception { EntityReference reference = resolver.resolve("wiki:space.page", EntityType.DOCUMENT); Assert.assertEquals("wiki:space.page", serializer.serialize(reference)); reference = resolver.resolve("wiki:space.", EntityType.DOCUMENT); Assert.assertEquals("wiki:space.WebHome", serializer.serialize(reference)); reference = resolver.resolve("space.", EntityType.DOCUMENT); Assert.assertEquals("xwiki:space.WebHome", serializer.serialize(reference)); reference = resolver.resolve("page", EntityType.DOCUMENT); Assert.assertEquals("xwiki:XWiki.page", serializer.serialize(reference)); reference = resolver.resolve(".", EntityType.DOCUMENT); Assert.assertEquals("xwiki:XWiki.WebHome", serializer.serialize(reference)); reference = resolver.resolve(null, EntityType.DOCUMENT); Assert.assertEquals("xwiki:XWiki.WebHome", serializer.serialize(reference)); reference = resolver.resolve("", EntityType.DOCUMENT); Assert.assertEquals("xwiki:XWiki.WebHome", serializer.serialize(reference)); reference = resolver.resolve("wiki1.wiki2:wiki3:some.space.page", EntityType.DOCUMENT); Assert.assertEquals("wiki1.wiki2:wiki3:some\\.space.page", serializer.serialize(reference)); reference = resolver.resolve("some.space.page", EntityType.DOCUMENT); Assert.assertEquals("xwiki:some\\.space.page", serializer.serialize(reference)); reference = resolver.resolve("wiki:page", EntityType.DOCUMENT); Assert.assertEquals("xwiki:XWiki.wiki:page", serializer.serialize(reference)); Assert.assertNull(serializer.serialize(null)); reference = resolver.resolve("\\.:@\\.", EntityType.DOCUMENT); Assert.assertEquals("xwiki:XWiki.\\.:@\\.", serializer.serialize(reference)); }
@Test public void testSerializeSpaceReference() throws Exception { EntityReference reference = resolver.resolve("wiki:space1.space2", EntityType.SPACE); Assert.assertEquals("wiki:space1\\.space2", serializer.serialize(reference)); }
@Test public void testSerializeAttachmentReference() throws Exception { EntityReference reference = resolver.resolve("wiki:space.page@filename", EntityType.ATTACHMENT); Assert.assertEquals("wiki:space.page@filename", serializer.serialize(reference)); reference = resolver.resolve("", EntityType.ATTACHMENT); Assert.assertEquals("xwiki:XWiki.WebHome@filename", serializer.serialize(reference)); reference = resolver.resolve("wiki:space.page@my.png", EntityType.ATTACHMENT); Assert.assertEquals("wiki:space.page@my.png", serializer.serialize(reference)); reference = resolver.resolve("some:file.name", EntityType.ATTACHMENT); Assert.assertEquals("xwiki:XWiki.WebHome@some:file.name", serializer.serialize(reference)); reference = resolver.resolve(":.\\@", EntityType.ATTACHMENT); Assert.assertEquals("xwiki:XWiki.WebHome@:.\\@", serializer.serialize(reference)); }
@Test public void testSerializeReferenceWithChild() { EntityReference reference = resolver.resolve("wiki:Space.Page", EntityType.DOCUMENT); Assert.assertEquals("wiki:Space", serializer.serialize(reference.getParent())); Assert.assertEquals("wiki", serializer.serialize(reference.getParent().getParent())); }
@Test public void testResolveAndSerializeObjectReference() { EntityReference reference = resolver.resolve("wiki:space.page^Object", EntityType.OBJECT); Assert.assertEquals("wiki:space.page^Object", serializer.serialize(reference)); reference = resolver.resolve("", EntityType.OBJECT); Assert.assertEquals("xwiki:XWiki.WebHome^Object", serializer.serialize(reference)); reference = resolver.resolve("wiki:space.page.property", EntityType.OBJECT); Assert.assertEquals("xwiki:XWiki.WebHome^wiki:space.page.property", serializer.serialize(reference)); reference = resolver.resolve("wiki:space.page^Obje\\^ct", EntityType.OBJECT); Assert.assertEquals("wiki:space.page^Obje\\^ct", serializer.serialize(reference)); reference = resolver.resolve("wiki:spa^ce.page^Obje\\^ct", EntityType.OBJECT); Assert.assertEquals("wiki:spa^ce.page^Obje\\^ct", serializer.serialize(reference)); reference = resolver.resolve(":.\\^@", EntityType.OBJECT); Assert.assertEquals("xwiki:XWiki.WebHome^:.\\^@", serializer.serialize(reference)); }
@Test public void testResolveAndSerializePropertyReference() { EntityReference reference = resolver.resolve("wiki:space.page^xwiki.class[0].prop", EntityType.OBJECT_PROPERTY); Assert.assertEquals("wiki:space.page^xwiki.class[0].prop", serializer.serialize(reference)); reference = resolver.resolve("", EntityType.OBJECT_PROPERTY); Assert.assertEquals("xwiki:XWiki.WebHome^Object.Property", serializer.serialize(reference)); reference = resolver.resolve("space^page@attachment", EntityType.OBJECT_PROPERTY); Assert .assertEquals("xwiki:XWiki.WebHome^Object.space^page@attachment", serializer.serialize(reference)); reference = resolver.resolve("wiki:space^object", EntityType.OBJECT_PROPERTY); Assert.assertEquals("xwiki:XWiki.WebHome^Object.wiki:space^object", serializer.serialize(reference)); reference = resolver.resolve("wiki:space.page^xwiki.class[0].prop\\.erty", EntityType.OBJECT_PROPERTY); Assert.assertEquals("wiki:space.page^xwiki.class[0].prop\\.erty", serializer.serialize(reference)); reference = resolver.resolve(":\\.^@", EntityType.OBJECT_PROPERTY); Assert.assertEquals("xwiki:XWiki.WebHome^Object.:\\.^@", serializer.serialize(reference)); }