### Question:
EntityReferenceFactory { public <E extends EntityReference> E getReference(E reference) { if (reference == null) { return null; } EntityReference entityReference = this.cache.get(reference); if (entityReference == null || !reference.getClass().isAssignableFrom(entityReference.getClass())) { entityReference = unique(reference); this.cache.put(entityReference, entityReference); } return (E) entityReference; }  E getReference(E reference);  }### Answer:
@Test void getReferenceWhenNull() { assertNull(this.factory.getReference(null)); }
@Test void getReference() { PageReference page = new PageReference("wiki", "parent", "page"); PageReference pageClone = new PageReference("wiki", "parent", "page"); assertSame(page, this.factory.getReference(page)); assertSame(page, this.factory.getReference(pageClone)); assertSame(page.getParent(), this.factory.getReference(pageClone.getParent())); PageReference page2 = new PageReference("wiki", "parent", "page2"); PageReference page2Cached = this.factory.getReference(page2); assertEquals(page2, page2Cached); assertNotSame(page, page2Cached); assertSame(page.getParent(), page2Cached.getParent()); }
@Test void getReferenceWhenExistingIsLowerType() { PageReference page = new PageReference("wiki", "parent", "page"); EntityReference entity = new EntityReference(page); assertSame(entity, this.factory.getReference(entity)); assertSame(page, this.factory.getReference(page)); assertEquals(entity, this.factory.getReference(entity)); assertNotSame(entity, this.factory.getReference(entity)); }