CollectionUtils { public static <C extends Collection> boolean compareCollectionContents(C c1, C c2) { if (c1.size() != c2.size()) { return false; } else { boolean allMatched = true; for (Object o : c1) { if (!c2.contains(o)) { allMatched = false; break; } } for (Object o : c2) { if (!c1.contains(o)) { allMatched = false; break; } } return allMatched; } }  static boolean compareCollectionContents(C c1, C c2);  }
@Test public void testSame() { Collection coll = Arrays.asList(item1, item2); assertTrue("Comparing identical collections should be return true", CollectionUtils.compareCollectionContents(coll, coll)); }
@Test public void testAllEqual() { Collection coll1 = Arrays.asList(item1, item2); Collection coll2 = Arrays.asList(equalItem1, equalItem2); assertTrue("Comparing equal collections should be return true", CollectionUtils.compareCollectionContents(coll1, coll2)); Collection coll3 = Arrays.asList(item1, item2); Collection coll4 = Arrays.asList(equalItem1, equalItem2); assertTrue("Comparing equal but differently ordered collections should be return true", CollectionUtils.compareCollectionContents(coll3, coll4)); List<Object> coll5 = new ArrayList<>(); coll5.add(item1); coll5.add(item2); Set<Object> coll6 = new HashSet<>(); coll6.add(equalItem1); coll6.add(equalItem2); assertTrue("Comparing differently typed collections with same elements should be return true", CollectionUtils.compareCollectionContents(coll5, coll6)); }
@Test public void testNotAllEqual() { Collection coll1 = Arrays.asList(item1, item2); Collection coll2 = Arrays.asList(equalItem1, unequalItem2); assertFalse("Comparing unequal collections should be return false", CollectionUtils.compareCollectionContents(coll1, coll2)); }
@Test public void testAllDifferent() { Collection coll1 = Arrays.asList(item1, item2); Collection coll2 = Arrays.asList(unequalItem1, unequalItem2); assertFalse("Comparing unequal collections should be return false", CollectionUtils.compareCollectionContents(coll1, coll2)); }
@Test public void testEmpty() { Collection coll1 = Collections.emptyList(); Collection coll2 = Collections.emptyList(); assertTrue("Empty collections should be equal", CollectionUtils.compareCollectionContents(coll1, coll2)); }