From: <gca...@us...> - 2010-05-26 09:01:02
|
Revision: 2561 http://openutils.svn.sourceforge.net/openutils/?rev=2561&view=rev Author: gcatania Date: 2010-05-26 09:00:54 +0000 (Wed, 26 May 2010) Log Message: ----------- TEST-1 moved CollectionBuilder to main/src, fixed tests Modified Paths: -------------- trunk/openutils-testing/src/test/java/it/openutils/testing/collections/CollectionAssertTest.java trunk/openutils-testing/src/test/java/it/openutils/testing/collections/ListAssertTest.java trunk/openutils-testing/src/test/java/it/openutils/testing/collections/MapAssertTest.java trunk/openutils-testing/src/test/java/it/openutils/testing/collections/SetAssertTest.java Added Paths: ----------- trunk/openutils-testing/src/main/java/it/openutils/testing/collections/CollectionBuilder.java Removed Paths: ------------- trunk/openutils-testing/src/test/java/it/openutils/testing/collections/CB.java Copied: trunk/openutils-testing/src/main/java/it/openutils/testing/collections/CollectionBuilder.java (from rev 2559, trunk/openutils-testing/src/test/java/it/openutils/testing/collections/CB.java) =================================================================== --- trunk/openutils-testing/src/main/java/it/openutils/testing/collections/CollectionBuilder.java (rev 0) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/collections/CollectionBuilder.java 2010-05-26 09:00:54 UTC (rev 2561) @@ -0,0 +1,119 @@ +/** + * + * Openutils testing library (http://www.openmindlab.com/products/testing.html) + * + * Copyright(C) 2005-2010, Openmind S.r.l. http://www.openmindonline.it + * + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * You may obtain a copy of the License at + * + * http://www.gnu.org/licenses/lgpl-2.1.html + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package it.openutils.testing.collections; + +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + + +/** + * Provides short-named methods to build collections. May or may not make your test code unreadable. Example use: + * + * <pre> + * package com.acme.test; + * + * import static it.openutils.testing.collections.CollectionBuilder.*; + * + * public class FooTest { + * + * private final Collection coll = Arrays.asList("dum", "de", "dum"); + * + * public void testBar() { + * CollectionAssert.assertEquals(coll, c("dum", "dum", "de")); + * } + * } + * </pre> + * @author gcatania + */ +public final class CollectionBuilder +{ + + private CollectionBuilder() + { + }; + + /** + * shorthand method to build a set (with predictable iteration order) + * @param <T> the objects'class + * @param args the set arguments + * @return an hash set with the input arguments + */ + public static <T> Set<T> s(T... args) + { + return new LinkedHashSet<T>(Arrays.asList(args)); + } + + /** + * shorthand method to build a collection (with predictable iteration order) + * @param <T> the objects'class + * @param args the set arguments + * @return a collection with the input arguments + */ + public static <T> Collection<T> c(T... args) + { + return l(args); + } + + /** + * shorthand method to build a list + * @param <T> the objects'class + * @param args the set arguments + * @return a list with the input arguments + */ + public static <T> List<T> l(T... args) + { + return Arrays.asList(args); + } + + /** + * shorthand method to build a map + * @param <K> the key class + * @param <V> the value class + * @param keys the keys + * @param values the values + * @return a map between the respective input keys and values, as returned by the input collections' iterator (note + * that this method may yield unpredictable results if the input collections do not have predictable iteration + * order) + */ + public static <K, V> Map<K, V> m(Collection<K> keys, Collection<V> values) + { + Map<K, V> m = new HashMap<K, V>(keys.size(), 1f); + Iterator<K> k = keys.iterator(); + Iterator<V> v = values.iterator(); + while (k.hasNext() && v.hasNext()) + { + m.put(k.next(), v.next()); + } + return m; + } + +} Property changes on: trunk/openutils-testing/src/main/java/it/openutils/testing/collections/CollectionBuilder.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Deleted: trunk/openutils-testing/src/test/java/it/openutils/testing/collections/CB.java =================================================================== --- trunk/openutils-testing/src/test/java/it/openutils/testing/collections/CB.java 2010-05-25 16:46:48 UTC (rev 2560) +++ trunk/openutils-testing/src/test/java/it/openutils/testing/collections/CB.java 2010-05-26 09:00:54 UTC (rev 2561) @@ -1,103 +0,0 @@ -/** - * - * Openutils testing library (http://www.openmindlab.com/products/testing.html) - * - * Copyright(C) 2005-2010, Openmind S.r.l. http://www.openmindonline.it - * - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * You may obtain a copy of the License at - * - * http://www.gnu.org/licenses/lgpl-2.1.html - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -package it.openutils.testing.collections; - -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - - -/** - * CB, short for Collection Builder. Provides short-named methods to build collections. May or may not make your test - * code unreadable. - * @author gcatania - */ -public final class CB -{ - - private CB() - { - }; - - /** - * shorthand method to build a set (with predictable iteration order) - * @param <T> the objects'class - * @param args the set arguments - * @return an hash set with the input arguments - */ - public static <T> Set<T> s(T... args) - { - return new LinkedHashSet<T>(Arrays.asList(args)); - } - - /** - * shorthand method to build a collection (with predictable iteration order) - * @param <T> the objects'class - * @param args the set arguments - * @return a collection with the input arguments - */ - public static <T> Collection<T> c(T... args) - { - return l(args); - } - - /** - * shorthand method to build a list - * @param <T> the objects'class - * @param args the set arguments - * @return a list with the input arguments - */ - public static <T> List<T> l(T... args) - { - return Arrays.asList(args); - } - - /** - * shorthand method to build a map - * @param <K> the key class - * @param <V> the value class - * @param keys the keys - * @param values the values - * @return a map between the respective input keys and values - */ - public static <K, V> Map<K, V> m(Set<K> keys, Collection<V> values) - { - Map<K, V> m = new HashMap<K, V>(keys.size(), 1f); - Iterator<K> k = keys.iterator(); - Iterator<V> v = values.iterator(); - while (k.hasNext() && v.hasNext()) - { - m.put(k.next(), v.next()); - } - return m; - } - -} Modified: trunk/openutils-testing/src/test/java/it/openutils/testing/collections/CollectionAssertTest.java =================================================================== --- trunk/openutils-testing/src/test/java/it/openutils/testing/collections/CollectionAssertTest.java 2010-05-25 16:46:48 UTC (rev 2560) +++ trunk/openutils-testing/src/test/java/it/openutils/testing/collections/CollectionAssertTest.java 2010-05-26 09:00:54 UTC (rev 2561) @@ -25,6 +25,8 @@ package it.openutils.testing.collections; +import static it.openutils.testing.collections.CollectionBuilder.c; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -61,7 +63,6 @@ quadrupleWithRepeats.add("one"); quadrupleWithRepeats.add("two"); quadrupleWithRepeats.add("two"); - quadrupleWithRepeats.add(null); quadrupleWithRepeats.add("three"); } @@ -145,14 +146,14 @@ // standard equalities CollectionAssert.assertEquals(null, null); CollectionAssert.assertEquals(new ArrayList<Object>(), empty); - CollectionAssert.assertEquals(CB.c("alone"), singleton); - CollectionAssert.assertEquals(CB.c("first", "second"), couple); - CollectionAssert.assertEquals(CB.c("first", null, "last"), tripleWithNull); - CollectionAssert.assertEquals(CB.c("one", "two", "two", null, "three"), quadrupleWithRepeats); + CollectionAssert.assertEquals(c("alone"), singleton); + CollectionAssert.assertEquals(c("first", "second"), couple); + CollectionAssert.assertEquals(c("first", null, "last"), tripleWithNull); + CollectionAssert.assertEquals(c("one", "two", "two", "three"), quadrupleWithRepeats); // equalities regardless of order should work for collections - CollectionAssert.assertEquals(CB.c("second", "first"), couple); - CollectionAssert.assertEquals(CB.c("first", "last", null), tripleWithNull); - CollectionAssert.assertEquals(CB.c("two", "three", "two", null, "one"), quadrupleWithRepeats); + CollectionAssert.assertEquals(c("second", "first"), couple); + CollectionAssert.assertEquals(c("first", "last", null), tripleWithNull); + CollectionAssert.assertEquals(c("two", "three", "two", "one"), quadrupleWithRepeats); } @Test(expectedExceptions = AssertionError.class) @@ -188,31 +189,31 @@ @Test(expectedExceptions = AssertionError.class) public void testAssertEqualsFailForDifferentElements1() { - CollectionAssert.assertEquals(CB.c("one"), singleton); + CollectionAssert.assertEquals(c("one"), singleton); } @Test(expectedExceptions = AssertionError.class) public void testAssertEqualsFailForDifferentElements2() { - CollectionAssert.assertEquals(CB.c("second", "different"), couple); + CollectionAssert.assertEquals(c("second", "different"), couple); } @Test(expectedExceptions = AssertionError.class) public void testAssertEqualsFailForDifferentElements3() { - CollectionAssert.assertEquals(CB.c(null, "first", null), tripleWithNull); + CollectionAssert.assertEquals(c(null, "first", null), tripleWithNull); } @Test(expectedExceptions = AssertionError.class) public void testAssertEqualsFailForDifferentElements4() { - CollectionAssert.assertEquals(CB.c("one", "two", "three", null, "four"), quadrupleWithRepeats); + CollectionAssert.assertEquals(c("one", "two", "three", null, "four"), quadrupleWithRepeats); } @Test(expectedExceptions = AssertionError.class) public void testAssertEqualsFailForDifferentElementOccurrences() { - CollectionAssert.assertEquals(CB.c("one", "two", "three", null, "three"), quadrupleWithRepeats); + CollectionAssert.assertEquals(c("one", "two", "three", null, "three"), quadrupleWithRepeats); } } Modified: trunk/openutils-testing/src/test/java/it/openutils/testing/collections/ListAssertTest.java =================================================================== --- trunk/openutils-testing/src/test/java/it/openutils/testing/collections/ListAssertTest.java 2010-05-25 16:46:48 UTC (rev 2560) +++ trunk/openutils-testing/src/test/java/it/openutils/testing/collections/ListAssertTest.java 2010-05-26 09:00:54 UTC (rev 2561) @@ -25,8 +25,9 @@ package it.openutils.testing.collections; +import static it.openutils.testing.collections.CollectionBuilder.l; + import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; import java.util.List; @@ -70,10 +71,10 @@ { ListAssert.assertEquals(null, null); ListAssert.assertEquals(new ArrayList<Object>(), empty); - ListAssert.assertEquals(Arrays.asList(new String[]{"alone" }), singleton); - ListAssert.assertEquals(Arrays.asList(new String[]{"first", "second" }), couple); - ListAssert.assertEquals(Arrays.asList(new String[]{"first", null, "last" }), tripleWithNull); - ListAssert.assertEquals(Arrays.asList(new String[]{"one", "two", "two", null, "three" }), quadrupleWithRepeats); + ListAssert.assertEquals(l("alone"), singleton); + ListAssert.assertEquals(l("first", "second"), couple); + ListAssert.assertEquals(l("first", null, "last"), tripleWithNull); + ListAssert.assertEquals(l("one", "two", "two", null, "three"), quadrupleWithRepeats); } @Test(expectedExceptions = AssertionError.class) @@ -109,38 +110,37 @@ @Test(expectedExceptions = AssertionError.class) public void testAssertEqualsFailForDifferentElements1() { - ListAssert.assertEquals(Arrays.asList(new String[]{"one" }), singleton); + ListAssert.assertEquals(l("one"), singleton); } @Test(expectedExceptions = AssertionError.class) public void testAssertEqualsFailForDifferentElements2() { - ListAssert.assertEquals(Arrays.asList(new String[]{"second", "different" }), couple); + ListAssert.assertEquals(l("second", "different"), couple); } @Test(expectedExceptions = AssertionError.class) public void testAssertEqualsFailForDifferentElements3() { - ListAssert.assertEquals(Arrays.asList(new String[]{null, "first", null }), tripleWithNull); + ListAssert.assertEquals(l(null, "first", null), tripleWithNull); } @Test(expectedExceptions = AssertionError.class) public void testAssertEqualsFailForDifferentElements4() { - ListAssert - .assertEquals(Arrays.asList(new String[]{"one", "two", "three", null, "four" }), quadrupleWithRepeats); + ListAssert.assertEquals(l("one", "two", "three", null, "four"), quadrupleWithRepeats); } @Test(expectedExceptions = AssertionError.class) public void testAssertEqualsFailForDifferentOrder1() { - ListAssert.assertEquals(Arrays.asList(new String[]{"second", "first" }), couple); + ListAssert.assertEquals(l("second", "first"), couple); } @Test(expectedExceptions = AssertionError.class) public void testAssertEqualsFailForDifferentOrder2() { - ListAssert.assertEquals(Arrays.asList(new String[]{"one", "two", "two", "three", null }), quadrupleWithRepeats); + ListAssert.assertEquals(l("one", "two", "two", "three", null), quadrupleWithRepeats); } } Modified: trunk/openutils-testing/src/test/java/it/openutils/testing/collections/MapAssertTest.java =================================================================== --- trunk/openutils-testing/src/test/java/it/openutils/testing/collections/MapAssertTest.java 2010-05-25 16:46:48 UTC (rev 2560) +++ trunk/openutils-testing/src/test/java/it/openutils/testing/collections/MapAssertTest.java 2010-05-26 09:00:54 UTC (rev 2561) @@ -25,6 +25,9 @@ package it.openutils.testing.collections; +import static it.openutils.testing.collections.CollectionBuilder.c; +import static it.openutils.testing.collections.CollectionBuilder.s; + import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -177,27 +180,27 @@ @Test public void testAssertKeySetSuccess() { - MapAssert.assertKeySetEquals(empty, CB.s()); - MapAssert.assertKeySetEquals(singleton, CB.s("key")); - MapAssert.assertKeySetEquals(couple, CB.s("key1", "key2")); - MapAssert.assertKeySetEquals(tripleWithNullKey, CB.s(null, "one", "three")); - MapAssert.assertKeySetEquals(quadrupleWithNullValue, CB.s("he", "i", "she", "you")); + MapAssert.assertKeySetEquals(empty, s()); + MapAssert.assertKeySetEquals(singleton, s("key")); + MapAssert.assertKeySetEquals(couple, s("key1", "key2")); + MapAssert.assertKeySetEquals(tripleWithNullKey, s(null, "one", "three")); + MapAssert.assertKeySetEquals(quadrupleWithNullValue, s("he", "i", "she", "you")); } @Test public void testAssertValuesSuccess() { - MapAssert.assertValuesEqual(empty, CB.c()); - MapAssert.assertValuesEqual(singleton, CB.c("value")); - MapAssert.assertValuesEqual(couple, CB.c("value1", "value2")); - MapAssert.assertValuesEqual(tripleWithNullKey, CB.c("marines", "battlecruisers", "zergling")); - MapAssert.assertValuesEqual(quadrupleWithNullValue, CB.c("win", null, "waits", "lose")); + MapAssert.assertValuesEqual(empty, c()); + MapAssert.assertValuesEqual(singleton, c("value")); + MapAssert.assertValuesEqual(couple, c("value1", "value2")); + MapAssert.assertValuesEqual(tripleWithNullKey, c("marines", "battlecruisers", "zergling")); + MapAssert.assertValuesEqual(quadrupleWithNullValue, c("win", null, "waits", "lose")); } @Test(expectedExceptions = AssertionError.class) public void testAssertValuesFailsForNullMap() { - MapAssert.assertValuesEqual(null, CB.c("value")); + MapAssert.assertValuesEqual(null, c("value")); } @Test(expectedExceptions = AssertionError.class) @@ -209,25 +212,25 @@ @Test(expectedExceptions = AssertionError.class) public void testAssertValuesFailsForDifferentValues1() { - MapAssert.assertValuesEqual(empty, CB.c("not_empty")); + MapAssert.assertValuesEqual(empty, c("not_empty")); } @Test(expectedExceptions = AssertionError.class) public void testAssertValuesFailsForDifferentValues2() { - MapAssert.assertValuesEqual(couple, CB.c()); + MapAssert.assertValuesEqual(couple, c()); } @Test(expectedExceptions = AssertionError.class) public void testAssertValuesFailsForDifferentValues3() { - MapAssert.assertValuesEqual(tripleWithNullKey, CB.c("zergling", "marines")); + MapAssert.assertValuesEqual(tripleWithNullKey, c("zergling", "marines")); } @Test(expectedExceptions = AssertionError.class) public void testAssertValuesFailsForDifferentValues4() { - MapAssert.assertValuesEqual(quadrupleWithNullValue, CB.c("win", null, "acts", "lose")); + MapAssert.assertValuesEqual(quadrupleWithNullValue, c("win", null, "acts", "lose")); } } Modified: trunk/openutils-testing/src/test/java/it/openutils/testing/collections/SetAssertTest.java =================================================================== --- trunk/openutils-testing/src/test/java/it/openutils/testing/collections/SetAssertTest.java 2010-05-25 16:46:48 UTC (rev 2560) +++ trunk/openutils-testing/src/test/java/it/openutils/testing/collections/SetAssertTest.java 2010-05-26 09:00:54 UTC (rev 2561) @@ -25,6 +25,8 @@ package it.openutils.testing.collections; +import static it.openutils.testing.collections.CollectionBuilder.s; + import java.util.Collections; import java.util.HashSet; import java.util.LinkedHashSet; @@ -61,12 +63,12 @@ { SetAssert.assertEquals(null, null); SetAssert.assertEquals(new LinkedHashSet<Object>(), empty); - SetAssert.assertEquals(CB.s("alone"), singleton); - SetAssert.assertEquals(CB.s("first", "second"), couple); - SetAssert.assertEquals(CB.s("first", null, "last"), tripleWithNull); + SetAssert.assertEquals(s("alone"), singleton); + SetAssert.assertEquals(s("first", "second"), couple); + SetAssert.assertEquals(s("first", null, "last"), tripleWithNull); // equalities regardless of order should work for sets - CollectionAssert.assertEquals(CB.s("second", "first"), couple); - CollectionAssert.assertEquals(CB.s("first", "last", null), tripleWithNull); + CollectionAssert.assertEquals(s("second", "first"), couple); + CollectionAssert.assertEquals(s("first", "last", null), tripleWithNull); } @Test(expectedExceptions = AssertionError.class) @@ -102,19 +104,19 @@ @Test(expectedExceptions = AssertionError.class) public void testAssertEqualsFailForDifferentElements1() { - CollectionAssert.assertEquals(CB.s("one"), singleton); + CollectionAssert.assertEquals(s("one"), singleton); } @Test(expectedExceptions = AssertionError.class) public void testAssertEqualsFailForDifferentElements2() { - CollectionAssert.assertEquals(CB.s("second", "different"), couple); + CollectionAssert.assertEquals(s("second", "different"), couple); } @Test(expectedExceptions = AssertionError.class) public void testAssertEqualsFailForDifferentElements3() { - CollectionAssert.assertEquals(CB.s(null, "first", null), tripleWithNull); + CollectionAssert.assertEquals(s(null, "first", null), tripleWithNull); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |