CollectionInstanceBuilder extends AbstractInstanceBuilder { @Override Object buildClone(Object cloneOwner, Field field, Object collection, CloneConfiguration configuration) { assert collection instanceof Collection; Collection<?> container = (Collection<?>) collection; if (container instanceof IndirectCollection<?>) { container = (Collection<?>) ((IndirectCollection<?>) container).unwrap(); } if (Collections.emptyList() == container) { return Collections.emptyList(); } if (Collections.emptySet() == container) { return Collections.emptySet(); } Object clone = cloneUsingDefaultConstructor(cloneOwner, field, container, configuration); if (clone == null) { clone = buildInstanceOfSpecialCollection(cloneOwner, field, container, configuration); } if (clone == null) { clone = buildDefaultCollectionInstance(cloneOwner, field, container, configuration); } clone = uow.createIndirectCollection(clone, cloneOwner, field); return clone; } CollectionInstanceBuilder(CloneBuilderImpl builder, UnitOfWorkImpl uow);   }
@Test public void buildCloneCreatesDefaultListWhenUnknownListImplementationIsPassedAsArgument() throws Exception { final CollectionOwner owner = new CollectionOwner(); owner.list = new TestList<>(owner); IntStream.range(0, 10).forEach(i -> owner.list.add("String" + i)); final Object result = builder.buildClone(owner, CollectionOwner.listField(), owner.list, new CloneConfiguration(descriptor)); assertNotNull(result); assertTrue(result instanceof List); final List<?> lstResult = (List<?>) result; owner.list.forEach(e -> assertTrue(lstResult.contains(e))); }
@Test public void buildCloneCreatesDefaultSetWhenUnknownSetImplementationIsPassedAsArgument() throws Exception { final CollectionOwner owner = new CollectionOwner(); owner.set = new TestSet<>(owner); IntStream.range(0, 10).forEach(i -> owner.set.add("String" + i)); final Object result = builder.buildClone(owner, CollectionOwner.setField(), owner.set, new CloneConfiguration(descriptor)); assertNotNull(result); assertTrue(result instanceof Set); final Set<?> setResult = (Set<?>) result; owner.set.forEach(e -> assertTrue(setResult.contains(e))); }
@Test public void buildCloneThrowsUnsupportedCollectionTypeExceptionWhenCollectionIsNeitherListNorSet() throws Exception { final CollectionOwner owner = new CollectionOwner(); owner.queue = new TestQueue<>(owner); thrown.expect(OWLPersistenceException.class); thrown.expectMessage("Cannot clone unsupported collection instance of type " + owner.queue.getClass() + "."); builder.buildClone(owner, CollectionOwner.queueField(), owner.queue, new CloneConfiguration(descriptor)); }
@Test public void buildingSingletonSetCloneRegistersElementCloneInUoW() throws Exception { final OWLClassJ owner = new OWLClassJ(Generators.createIndividualIdentifier()); final OWLClassA aOrig = Generators.generateOwlClassAInstance(); final OWLClassA aClone = new OWLClassA(aOrig); owner.setOwlClassA(Collections.singleton(aOrig)); when(uowMock.registerExistingObject(aOrig, descriptor, Collections.emptyList())).thenReturn(aClone); when(uowMock.isEntityType(OWLClassA.class)).thenReturn(true); final Set<?> clone = (Set) builder.buildClone(owner, OWLClassJ.getOwlClassAField(), owner.getOwlClassA(), new CloneConfiguration(descriptor)); assertEquals(owner.getOwlClassA().size(), clone.size()); assertSame(aClone, clone.iterator().next()); verify(uowMock).registerExistingObject(aOrig, descriptor, Collections.emptyList()); }
@Test public void buildCloneClonesSingletonListWithContent() throws Exception { final OWLClassC owner = new OWLClassC(Generators.createIndividualIdentifier()); final OWLClassA aOrig = Generators.generateOwlClassAInstance(); final OWLClassA aClone = new OWLClassA(aOrig); owner.setSimpleList(Collections.singletonList(aOrig)); when(uowMock.registerExistingObject(aOrig, descriptor, Collections.emptyList())).thenReturn(aClone); when(uowMock.isEntityType(OWLClassA.class)).thenReturn(true); final List<?> clone = (List) builder.buildClone(owner, OWLClassC.getSimpleListField(), owner.getSimpleList(), new CloneConfiguration(descriptor)); assertEquals(1, clone.size()); assertSame(aClone, clone.get(0)); verify(uowMock).registerExistingObject(aOrig, descriptor, Collections.emptyList()); }
@Test public void buildCloneClonesArrayAsListWithContent() throws Exception { final OWLClassC owner = new OWLClassC(Generators.createIndividualIdentifier()); final OWLClassA aOneOrig = Generators.generateOwlClassAInstance(); final OWLClassA aOneClone = new OWLClassA(aOneOrig); final OWLClassA aTwoOrig = Generators.generateOwlClassAInstance(); final OWLClassA aTwoClone = new OWLClassA(aTwoOrig); owner.setSimpleList(Arrays.asList(aOneOrig, aTwoOrig)); when(uowMock.registerExistingObject(aOneOrig, descriptor, Collections.emptyList())).thenReturn(aOneClone); when(uowMock.registerExistingObject(aTwoOrig, descriptor, Collections.emptyList())).thenReturn(aTwoClone); when(uowMock.isEntityType(OWLClassA.class)).thenReturn(true); final List<?> clone = (List) builder.buildClone(owner, OWLClassC.getSimpleListField(), owner.getSimpleList(), new CloneConfiguration(descriptor)); assertEquals(2, clone.size()); assertSame(aOneClone, clone.get(0)); assertSame(aTwoClone, clone.get(1)); verify(uowMock).registerExistingObject(aOneOrig, descriptor, Collections.emptyList()); verify(uowMock).registerExistingObject(aTwoOrig, descriptor, Collections.emptyList()); }