|
Author: doogie
Date: Tue Dec 22 06:08:43 2009 New Revision: 893077 URL: http://svn.apache.org/viewvc?rev=893077&view=rev Log: Some set helper methods. Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/test/GenericTestCaseBase.java Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/test/GenericTestCaseBase.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/test/GenericTestCaseBase.java?rev=893077&r1=893076&r2=893077&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/test/GenericTestCaseBase.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/test/GenericTestCaseBase.java Tue Dec 22 06:08:43 2009 @@ -87,6 +87,33 @@ if (!list.isEmpty()) fail(msg + "not enough items: " + list); } + public static <T> void assertEquals(Set<T> wanted, Object got) { + assertEquals(null, wanted, got); + } + + public static <T> void assertEquals(String msg, Set<T> 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<T> wantedSet = new HashSet<T>(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 <T> void assertEqualsArrayArray(String msg, Object wanted, Object got) { int i = 0; while (i < Array.getLength(wanted) && i < Array.getLength(got)) { @@ -164,6 +191,8 @@ } 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 @@ return Arrays.asList(list); } + protected static <T> Set<T> set(T value) { + HashSet<T> set = new HashSet<T>(1); + set.add(value); + return set; + } + + protected static <T> Set<T> set(T... list) { + return new HashSet<T>(Arrays.asList(list)); + } + + protected static <T> Set<T> set(Iterable<T> iterable) { + return set(iterable.iterator()); + } + + protected static <T> Set<T> set(Iterator<T> it) { + HashSet<T> set = new HashSet<T>(); + while (it.hasNext()) { + T item = it.next(); + set.add(item); + } + return set; + } + @SuppressWarnings("unchecked") protected static <K, V> Map<K, V> map(Object... list) { assertEquals("list has even number of elements", 0, list.length % 2); |
| Free forum by Nabble | Edit this page |
