SolutionCloner<TestdataSetBasedSolution> cloner = createSolutionCloner(solutionDescriptor);
TestdataValue val1 = new TestdataValue("1");
TestdataValue val2 = new TestdataValue("2");
TestdataValue val3 = new TestdataValue("3");
TestdataSetBasedEntity a = new TestdataSetBasedEntity("a", val1);
TestdataSetBasedEntity b = new TestdataSetBasedEntity("b", val1);
TestdataSetBasedEntity c = new TestdataSetBasedEntity("c", val3);
TestdataSetBasedEntity d = new TestdataSetBasedEntity("d", val3);
TestdataSetBasedSolution original = new TestdataSetBasedSolution("solution");
Comparator<TestdataValue> valueComparator = new Comparator<TestdataValue>() {
public int compare(TestdataValue a, TestdataValue b) {
return b.getCode().compareTo(a.getCode()); // Reverse alphabetic
}
};
Set<TestdataValue> valueSet = new TreeSet<TestdataValue>(valueComparator);
valueSet.addAll(Arrays.asList(val1, val2, val3));
original.setValueSet(valueSet);
Comparator<TestdataSetBasedEntity> entityComparator = new Comparator<TestdataSetBasedEntity>() {
public int compare(TestdataSetBasedEntity a, TestdataSetBasedEntity b) {
return b.getCode().compareTo(a.getCode()); // Reverse alphabetic
}
};
Set<TestdataSetBasedEntity> originalEntitySet = new TreeSet<TestdataSetBasedEntity>(entityComparator);
originalEntitySet.addAll(Arrays.asList(a, b, c, d));
original.setEntitySet(originalEntitySet);
TestdataSetBasedSolution clone = cloner.cloneSolution(original);
assertNotSame(original, clone);
assertSame(valueSet, clone.getValueSet());
Set<TestdataSetBasedEntity> cloneEntitySet = clone.getEntitySet();
assertNotSame(originalEntitySet, cloneEntitySet);
assertTrue(cloneEntitySet instanceof SortedSet);
assertSame(entityComparator, ((SortedSet) cloneEntitySet).comparator());
assertCode("solution", clone);
assertEquals(4, cloneEntitySet.size());
Iterator<TestdataSetBasedEntity> it = cloneEntitySet.iterator();
// Reverse order because they got sorted
TestdataSetBasedEntity cloneD = it.next();
TestdataSetBasedEntity cloneC = it.next();
TestdataSetBasedEntity cloneB = it.next();
TestdataSetBasedEntity cloneA = it.next();
assertSetBasedEntityClone(a, cloneA, "a", "1");
assertSetBasedEntityClone(b, cloneB, "b", "1");
assertSetBasedEntityClone(c, cloneC, "c", "3");
assertSetBasedEntityClone(d, cloneD, "d", "3");