diff --git a/framework/base/src/org/ofbiz/base/test/GenericTestCaseBase.java b/framework/base/src/org/ofbiz/base/test/GenericTestCaseBase.java index f6d389a11d..d1121de5ff 100644 --- a/framework/base/src/org/ofbiz/base/test/GenericTestCaseBase.java +++ b/framework/base/src/org/ofbiz/base/test/GenericTestCaseBase.java @@ -87,6 +87,33 @@ OUTER: if (!list.isEmpty()) fail(msg + "not enough items: " + list); } + public static void assertEquals(Set wanted, Object got) { + assertEquals(null, wanted, got); + } + + public static void assertEquals(String msg, Set wanted, Object got) { + if (wanted.equals(got)) return; + if (!(got instanceof Set)) fail(msg + "not a set"); + // Need to check the reverse, wanted may not implement equals, + // which is the case for HashMap.values() + if (got.equals(wanted)) return; + msg = msg == null ? "" : msg + ' '; + assertNotNull(msg + "expected a value", got); + Set wantedSet = new HashSet(wanted); + Set gotSet = (Set) got; + Iterator rightIt = ((Set) got).iterator(); +OUTER: + while (rightIt.hasNext()) { + Object right = rightIt.next(); + if (wantedSet.contains(right)) { + wantedSet.remove(right); + } else { + fail(msg + "couldn't find " + right); + } + } + if (!wantedSet.isEmpty()) fail(msg + "not enough items: " + wantedSet); + } + private static void assertEqualsArrayArray(String msg, Object wanted, Object got) { int i = 0; while (i < Array.getLength(wanted) && i < Array.getLength(got)) { @@ -164,6 +191,8 @@ OUTER: } else if (wanted instanceof Collection) { System.err.println("c"); assertEquals(msg, (Collection) wanted, got); + } else if (wanted instanceof Set) { + assertEquals(msg, (Set) wanted, got); } else if (wanted.getClass().isArray()) { if (got == null) { TestCase.assertEquals(msg, wanted, got); @@ -189,6 +218,29 @@ OUTER: return Arrays.asList(list); } + protected static Set set(T value) { + HashSet set = new HashSet(1); + set.add(value); + return set; + } + + protected static Set set(T... list) { + return new HashSet(Arrays.asList(list)); + } + + protected static Set set(Iterable iterable) { + return set(iterable.iterator()); + } + + protected static Set set(Iterator it) { + HashSet set = new HashSet(); + while (it.hasNext()) { + T item = it.next(); + set.add(item); + } + return set; + } + @SuppressWarnings("unchecked") protected static Map map(Object... list) { assertEquals("list has even number of elements", 0, list.length % 2);