CollectionChangeDetector implements ChangeDetector { @Override public boolean hasChanges(Object clone, Object original) { assert clone != null; assert original != null; Collection<?> origCol = (Collection<?>) original; Collection<?> cloneCol = (Collection<?>) clone; if (origCol.size() != cloneCol.size()) { return true; } if (origCol.isEmpty()) { return false; } if (origCol instanceof Set) { return setChanged(cloneCol, origCol); } else { return orderedCollectionChanged(cloneCol, origCol); } } CollectionChangeDetector(ChangeDetector changeDetector, MetamodelProvider metamodelProvider); @Override boolean hasChanges(Object clone, Object original);  }
@Test public void hasChangesReturnsFalseForTwoSetsOfManagedClassInstances() { final Set<OWLClassA> original = initSet(); final Set<OWLClassA> clone = initSet(); when(providerMock.isEntityType(OWLClassA.class)).thenReturn(true); assertFalse(changeDetector.hasChanges(clone, original)); }
@Test public void hasChangesReturnsFalseWhenCollectionOfManagedTypesHasAttributeValueUpdate() { final Set<OWLClassA> original = initSet(); final Set<OWLClassA> clone = initSet(); when(providerMock.isEntityType(OWLClassA.class)).thenReturn(true); clone.iterator().next().setStringAttribute("updated"); assertFalse(changeDetector.hasChanges(clone, original)); }
@Test public void hasChangesReturnsTrueWhenItemIsReplacedInCollectionOfManagedTypes() { final Set<OWLClassA> original = initSet(); final Set<OWLClassA> clone = initSet(); when(providerMock.isEntityType(OWLClassA.class)).thenReturn(true); final OWLClassA newItem = new OWLClassA(Generators.createIndividualIdentifier()); removeItemFromCollection(clone); clone.add(newItem); assertTrue(changeDetector.hasChanges(clone, original)); }
@Test public void hasChangesReturnsTrueWhenItemIsReplacedByNewObjectInCollectionOfManagedTypes() { final Set<OWLClassA> original = initSet(); final Set<OWLClassA> clone = initSet(); when(providerMock.isEntityType(OWLClassA.class)).thenReturn(true); final OWLClassA newItem = new OWLClassA(); removeItemFromCollection(clone); clone.add(newItem); assertTrue(changeDetector.hasChanges(clone, original)); }
@Test public void hasChangesReturnsFalseForTwoEmptyCollections() { final Set<String> original = new HashSet<>(); final Set<String> clone = new HashSet<>(); assertFalse(changeDetector.hasChanges(clone, original)); }
@Test public void hasChangesReturnsFalseForTwoIdenticalCollectionsOfNonManagedTypes() { final List<Integer> lst = Arrays .asList(Generators.randomInt(100), Generators.randomInt(200), Generators.randomInt(500), Generators.randomInt(400), Generators.randomInt(300)); final Set<Integer> original = new HashSet<>(lst); final Set<Integer> clone = new HashSet<>(lst); assertFalse(changeDetector.hasChanges(clone, original)); }
@Test public void hasChangesReturnsTrueWhenCollectionSizeChanges() { final Set<OWLClassA> original = initSet(); final Set<OWLClassA> clone = initSet(); removeItemFromCollection(clone); assertTrue(changeDetector.hasChanges(clone, original)); }
@Test public void hasChangesReturnsTrueWhenOrderedCollectionOfNonManagedInstancesChanges() { final List<String> original = new ArrayList<>(); final List<String> clone = new ArrayList<>(); final int cnt = Generators.randomPositiveInt(117); for (int i = 0; i < cnt; i++) { original.add(Generators.createIndividualIdentifier().toString()); clone.add(Generators.createIndividualIdentifier().toString()); } assertTrue(changeDetector.hasChanges(clone, original)); }