From: <gca...@us...> - 2010-05-25 16:45:10
|
Revision: 2559 http://openutils.svn.sourceforge.net/openutils/?rev=2559&view=rev Author: gcatania Date: 2010-05-25 16:45:03 +0000 (Tue, 25 May 2010) Log Message: ----------- TEST-1 removed confusing inheritance, added shorthand collection constructor, unit tests, fixed a couple bugs Modified Paths: -------------- trunk/openutils-testing/src/main/java/it/openutils/testing/collections/CollectionAssert.java trunk/openutils-testing/src/main/java/it/openutils/testing/collections/ListAssert.java trunk/openutils-testing/src/main/java/it/openutils/testing/collections/MapAssert.java trunk/openutils-testing/src/main/java/it/openutils/testing/collections/SetAssert.java Added Paths: ----------- trunk/openutils-testing/src/test/java/it/openutils/testing/collections/ trunk/openutils-testing/src/test/java/it/openutils/testing/collections/CB.java 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 Modified: trunk/openutils-testing/src/main/java/it/openutils/testing/collections/CollectionAssert.java =================================================================== --- trunk/openutils-testing/src/main/java/it/openutils/testing/collections/CollectionAssert.java 2010-05-25 14:23:59 UTC (rev 2558) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/collections/CollectionAssert.java 2010-05-25 16:45:03 UTC (rev 2559) @@ -83,6 +83,17 @@ } /** + * Asserts that a collection is not null + * @param c the collection + * @param message a custom error message + * @throws AssertionError if assertion fails + */ + public static void assertNotNull(Collection< ? > c) throws AssertionError + { + assertNotNull(c, null); + } + + /** * Asserts that a collection is empty. Fails when the collection is null. * @param c the collection * @param message a custom error message @@ -206,8 +217,10 @@ } /** - * Asserts that two collections are equal, regardless of element order. Handles multiple instances of the same - * element in any of the collections. + * Asserts that two collections are equal, regardless of element order. Allows multiple instances of the same + * element in any of the collections, and checks that they occur the same number of times in both collections. If + * you need to assert list equality, use {@code ListAssert} instead. If you need to assert set equality, use {@code + * SetAssert}. * @param actual the actual collection * @param expected the expected collection * @throws AssertionError if assertion fails Modified: trunk/openutils-testing/src/main/java/it/openutils/testing/collections/ListAssert.java =================================================================== --- trunk/openutils-testing/src/main/java/it/openutils/testing/collections/ListAssert.java 2010-05-25 14:23:59 UTC (rev 2558) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/collections/ListAssert.java 2010-05-25 16:45:03 UTC (rev 2559) @@ -25,13 +25,15 @@ package it.openutils.testing.collections; +import it.openutils.testing.AssertFormatter; + import java.util.List; /** * @author gcatania */ -public class ListAssert extends CollectionAssert +public class ListAssert extends AssertFormatter { protected ListAssert() @@ -63,7 +65,7 @@ { Object e = expected.get(y); Object a = actual.get(y); - if ((e != null && !e.equals(a)) || a != null) + if ((e != null && !e.equals(a)) || (e == null && a != null)) { throw assertionFailed(message, "At position {0}: expected {1}, found {2} instead.", y, e, a); // yea! } Modified: trunk/openutils-testing/src/main/java/it/openutils/testing/collections/MapAssert.java =================================================================== --- trunk/openutils-testing/src/main/java/it/openutils/testing/collections/MapAssert.java 2010-05-25 14:23:59 UTC (rev 2558) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/collections/MapAssert.java 2010-05-25 16:45:03 UTC (rev 2559) @@ -27,6 +27,7 @@ import it.openutils.testing.AssertFormatter; +import java.util.Collection; import java.util.Map; import java.util.Set; @@ -42,30 +43,120 @@ } /** + * Asserts that a map is null + * @param m the map + * @param message a custom error message + * @throws AssertionError if assertion fails + */ + public static void assertNull(Map< ? , ? > m, String message) throws AssertionError + { + if (m != null) + { + throw assertionFailed(message, "Expected a null {0}, found {1} values instead.", typeName(m), m.size()); + } + } + + /** + * Asserts that a map is null + * @param m the map + * @throws AssertionError if assertion fails + */ + public static void assertNull(Map< ? , ? > m) throws AssertionError + { + assertNull(m, null); + } + + /** * Asserts that a map is not null - * @param map a map + * @param m the map * @param message a custom error message * @throws AssertionError if assertion fails */ - public static void assertNotNull(Map< ? , ? > map, String message) throws AssertionError + public static void assertNotNull(Map< ? , ? > m, String message) throws AssertionError { - if (map == null) + if (m == null) { - throw assertionFailed(message, "{0} is null.", capitalizedTypeName(map)); + throw assertionFailed(message, "{0} is null.", capitalizedTypeName(m)); } } /** * Asserts that a map is not null - * @param map a map + * @param m the map + * @param message a custom error message * @throws AssertionError if assertion fails */ - public static void assertNotNull(Map< ? , ? > map) throws AssertionError + public static void assertNotNull(Map< ? , ? > m) throws AssertionError { - assertNotNull(map, null); + assertNotNull(m, null); } /** + * Asserts that a map is empty. Fails when the collection is null. + * @param m the map + * @param message a custom error message + * @throws AssertionError if assertion fails + */ + public static void assertEmpty(Map< ? , ? > m, String message) throws AssertionError + { + assertNotNull(m, message); + if (!m.isEmpty()) + { + throw assertionFailed(message, "Expected an empty {0}, found {1} values instead.", typeName(m), m.size()); + } + } + + /** + * Asserts that a map is empty. Fails when the collection is null. + * @param m the map + * @throws AssertionError if assertion fails + */ + public static void assertEmpty(Map< ? , ? > m) throws AssertionError + { + assertEmpty(m, null); + } + + /** + * Asserts that a map has the expected size. Fails if the input collection is null. + * @param actual the actual collection + * @param expectedSize the expected size + * @param message a custom error message + * @throws AssertionError if assertion fails + */ + public static void assertSizeEquals(Map< ? , ? > actual, int expectedSize, String message) throws AssertionError + { + assertNotNull(actual, message); + if (expectedSize == 0) + { + assertEmpty(actual, message); + } + else + { + int actualSize = actual.size(); + if (actualSize != expectedSize) + { + throw assertionFailed( + message, + "{0} has wrong size: expected {1}, found {2} instead.", + capitalizedTypeName(actual), + expectedSize, + actualSize); + } + } + } + + /** + * Asserts that a map has the expected size. Fails if the input collection is null. + * @param actual the actual collection + * @param expectedSize the expected size + * @throws AssertionError if assertion fails + */ + public static void assertSizeEquals(Map< ? , ? > actual, int expectedSize) throws AssertionError + { + assertSizeEquals(actual, expectedSize, null); + } + + /** * Asserts that a map's key set equals an input key set * @param actual the actual map * @param expectedKeySet the expected key set @@ -91,6 +182,31 @@ } /** + * Asserts that a map's value collection equals an input collection + * @param actual the actual map + * @param expectedValues the expected values + * @param message a custom error message + * @throws AssertionError if assertion fails + */ + public static void assertValuesEqual(Map< ? , ? > actual, Collection< ? > expectedValues, String message) + throws AssertionError + { + assertNotNull(actual, message); + CollectionAssert.assertEquals(actual.values(), expectedValues, message); + } + + /** + * Asserts that a map's value collection equals an input collection + * @param actual the actual map + * @param expectedValues the expected values + * @throws AssertionError if assertion fails + */ + public static void assertValuesEqual(Map< ? , ? > actual, Collection< ? > expectedValues) throws AssertionError + { + assertValuesEqual(actual, expectedValues, null); + } + + /** * Asserts that the input map contains an entry with the input key and value * @param actual the actual map * @param expectedKey the key expected to be found in the map Modified: trunk/openutils-testing/src/main/java/it/openutils/testing/collections/SetAssert.java =================================================================== --- trunk/openutils-testing/src/main/java/it/openutils/testing/collections/SetAssert.java 2010-05-25 14:23:59 UTC (rev 2558) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/collections/SetAssert.java 2010-05-25 16:45:03 UTC (rev 2559) @@ -25,13 +25,15 @@ package it.openutils.testing.collections; +import it.openutils.testing.AssertFormatter; + import java.util.Set; /** * @author gcatania */ -public class SetAssert extends CollectionAssert +public class SetAssert extends AssertFormatter { protected SetAssert() @@ -49,15 +51,15 @@ { if (expected == null) { - assertNull(actual, message); + CollectionAssert.assertNull(actual, message); } else if (expected.isEmpty()) { - assertEmpty(actual, message); + CollectionAssert.assertEmpty(actual, message); } else { - assertSizeEquals(actual, expected.size(), message); + CollectionAssert.assertSizeEquals(actual, expected.size(), message); for (Object o : expected) { if (!actual.contains(o)) Added: trunk/openutils-testing/src/test/java/it/openutils/testing/collections/CB.java =================================================================== --- trunk/openutils-testing/src/test/java/it/openutils/testing/collections/CB.java (rev 0) +++ trunk/openutils-testing/src/test/java/it/openutils/testing/collections/CB.java 2010-05-25 16:45:03 UTC (rev 2559) @@ -0,0 +1,103 @@ +/** + * + * 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; + } + +} Property changes on: trunk/openutils-testing/src/test/java/it/openutils/testing/collections/CB.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-testing/src/test/java/it/openutils/testing/collections/CollectionAssertTest.java =================================================================== --- trunk/openutils-testing/src/test/java/it/openutils/testing/collections/CollectionAssertTest.java (rev 0) +++ trunk/openutils-testing/src/test/java/it/openutils/testing/collections/CollectionAssertTest.java 2010-05-25 16:45:03 UTC (rev 2559) @@ -0,0 +1,218 @@ +/** + * + * 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.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Stack; + +import org.testng.annotations.Test; + + +/** + * @author gcatania + */ +public class CollectionAssertTest +{ + + private static final Collection< ? > empty = Collections.EMPTY_LIST; + + private static final Collection< ? > singleton = Collections.singleton("alone"); + + private static final Collection<Object> couple = new Stack<Object>(); + + private static final Collection<Object> tripleWithNull = new HashSet<Object>(3, 1f); + + private static final Collection<Object> quadrupleWithRepeats = new LinkedList<Object>(); + + static + { + couple.add("first"); + couple.add("second"); + tripleWithNull.add("first"); + tripleWithNull.add(null); + tripleWithNull.add("last"); + quadrupleWithRepeats.add("one"); + quadrupleWithRepeats.add("two"); + quadrupleWithRepeats.add("two"); + quadrupleWithRepeats.add(null); + quadrupleWithRepeats.add("three"); + } + + @Test + public void testAssertNullSuccess() + { + CollectionAssert.assertNull(null); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertNullFailForEmpty() + { + CollectionAssert.assertNull(empty); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertNullFailForNotEmpty() + { + CollectionAssert.assertNull(singleton); + } + + @Test + public void testAssertNotNullSuccess() + { + CollectionAssert.assertNotNull(empty); + CollectionAssert.assertNotNull(singleton); + CollectionAssert.assertNotNull(couple); + CollectionAssert.assertNotNull(tripleWithNull); + CollectionAssert.assertNotNull(quadrupleWithRepeats); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertNotNullFail() + { + CollectionAssert.assertNotNull(null); + } + + @Test + public void testAssertEmptySuccess() + { + CollectionAssert.assertEmpty(empty); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEmptyFailForNull() + { + CollectionAssert.assertEmpty(null); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEmptyFailForNotEmpty() + { + CollectionAssert.assertEmpty(singleton); + } + + @Test + public void testAssertSizeSuccess() + { + CollectionAssert.assertSizeEquals(empty, 0); + CollectionAssert.assertSizeEquals(singleton, 1); + CollectionAssert.assertSizeEquals(couple, 2); + CollectionAssert.assertSizeEquals(tripleWithNull, 3); + CollectionAssert.assertSizeEquals(quadrupleWithRepeats, 4); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertSizeFailForNull() + { + CollectionAssert.assertSizeEquals(null, 0); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertSizeFailForDifferentSize() + { + CollectionAssert.assertSizeEquals(couple, 1); + } + + @Test + public void testAssertEqualsSuccess() + { + // 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); + // 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); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForNull1() + { + CollectionAssert.assertEquals(null, empty); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForNull2() + { + CollectionAssert.assertEquals(empty, null); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForSize1() + { + CollectionAssert.assertEquals(singleton, couple); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForSize2() + { + CollectionAssert.assertEquals(couple, empty); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForSize3() + { + CollectionAssert.assertEquals(tripleWithNull, empty); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForDifferentElements1() + { + CollectionAssert.assertEquals(CB.c("one"), singleton); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForDifferentElements2() + { + CollectionAssert.assertEquals(CB.c("second", "different"), couple); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForDifferentElements3() + { + CollectionAssert.assertEquals(CB.c(null, "first", null), tripleWithNull); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForDifferentElements4() + { + CollectionAssert.assertEquals(CB.c("one", "two", "three", null, "four"), quadrupleWithRepeats); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForDifferentElementOccurrences() + { + CollectionAssert.assertEquals(CB.c("one", "two", "three", null, "three"), quadrupleWithRepeats); + } + +} Property changes on: trunk/openutils-testing/src/test/java/it/openutils/testing/collections/CollectionAssertTest.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-testing/src/test/java/it/openutils/testing/collections/ListAssertTest.java =================================================================== --- trunk/openutils-testing/src/test/java/it/openutils/testing/collections/ListAssertTest.java (rev 0) +++ trunk/openutils-testing/src/test/java/it/openutils/testing/collections/ListAssertTest.java 2010-05-25 16:45:03 UTC (rev 2559) @@ -0,0 +1,146 @@ +/** + * + * 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.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Stack; + +import org.testng.annotations.Test; + + +/** + * @author gcatania + */ +public class ListAssertTest +{ + + private static final List< ? > empty = Collections.EMPTY_LIST; + + private static final List< ? > singleton = Collections.singletonList("alone"); + + private static final List<Object> couple = new Stack<Object>(); + + private static final List<Object> tripleWithNull = new LinkedList<Object>(); + + private static final List<Object> quadrupleWithRepeats = new ArrayList<Object>(4); + + static + { + couple.add("first"); + couple.add("second"); + tripleWithNull.add("first"); + tripleWithNull.add(null); + tripleWithNull.add("last"); + quadrupleWithRepeats.add("one"); + quadrupleWithRepeats.add("two"); + quadrupleWithRepeats.add("two"); + quadrupleWithRepeats.add(null); + quadrupleWithRepeats.add("three"); + } + + @Test + public void testAssertEqualsSuccess() + { + 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); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForNull1() + { + ListAssert.assertEquals(null, empty); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForNull2() + { + ListAssert.assertEquals(empty, null); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForSize1() + { + ListAssert.assertEquals(singleton, couple); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForSize2() + { + ListAssert.assertEquals(couple, empty); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForSize3() + { + ListAssert.assertEquals(tripleWithNull, empty); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForDifferentElements1() + { + ListAssert.assertEquals(Arrays.asList(new String[]{"one" }), singleton); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForDifferentElements2() + { + ListAssert.assertEquals(Arrays.asList(new String[]{"second", "different" }), couple); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForDifferentElements3() + { + ListAssert.assertEquals(Arrays.asList(new String[]{null, "first", null }), tripleWithNull); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForDifferentElements4() + { + ListAssert + .assertEquals(Arrays.asList(new String[]{"one", "two", "three", null, "four" }), quadrupleWithRepeats); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForDifferentOrder1() + { + ListAssert.assertEquals(Arrays.asList(new String[]{"second", "first" }), couple); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForDifferentOrder2() + { + ListAssert.assertEquals(Arrays.asList(new String[]{"one", "two", "two", "three", null }), quadrupleWithRepeats); + } + +} Property changes on: trunk/openutils-testing/src/test/java/it/openutils/testing/collections/ListAssertTest.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-testing/src/test/java/it/openutils/testing/collections/MapAssertTest.java =================================================================== --- trunk/openutils-testing/src/test/java/it/openutils/testing/collections/MapAssertTest.java (rev 0) +++ trunk/openutils-testing/src/test/java/it/openutils/testing/collections/MapAssertTest.java 2010-05-25 16:45:03 UTC (rev 2559) @@ -0,0 +1,233 @@ +/** + * + * 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.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.TreeMap; + +import org.testng.annotations.Test; + + +/** + * @author gcatania + */ +public class MapAssertTest +{ + + private static final Map< ? , ? > empty = Collections.EMPTY_MAP; + + private static final Map< ? , ? > singleton = Collections.singletonMap("key", "value"); + + private static final Map<Object, Object> couple = new HashMap<Object, Object>(2, 1f); + + private static final Map<Object, Object> tripleWithNullKey = new HashMap<Object, Object>(3, 1f); + + private static final Map<Object, Object> quadrupleWithNullValue = new TreeMap<Object, Object>(); + static + { + couple.put("key1", "value1"); + couple.put("key2", "value2"); + tripleWithNullKey.put("one", "zergling"); + tripleWithNullKey.put(null, "marines"); + tripleWithNullKey.put("three", "battlecruisers"); + quadrupleWithNullValue.put("i", "win"); + quadrupleWithNullValue.put("you", "lose"); + quadrupleWithNullValue.put("she", null); + quadrupleWithNullValue.put("he", "waits"); + } + + @Test + public void testAssertNullSuccess() + { + MapAssert.assertNull(null); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertNullFailForEmpty() + { + MapAssert.assertNull(empty); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertNullFailForNotEmpty() + { + MapAssert.assertNull(singleton); + } + + @Test + public void testAssertNotNullSuccess() + { + MapAssert.assertNotNull(empty); + MapAssert.assertNotNull(singleton); + MapAssert.assertNotNull(couple); + MapAssert.assertNotNull(tripleWithNullKey); + MapAssert.assertNotNull(quadrupleWithNullValue); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertNotNullFail() + { + MapAssert.assertNotNull(null); + } + + @Test + public void testAssertEmptySuccess() + { + MapAssert.assertEmpty(empty); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEmptyFailForNull() + { + MapAssert.assertEmpty(null); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEmptyFailForNotEmpty() + { + MapAssert.assertEmpty(singleton); + } + + @Test + public void testAssertSizeSuccess() + { + MapAssert.assertSizeEquals(empty, 0); + MapAssert.assertSizeEquals(singleton, 1); + MapAssert.assertSizeEquals(couple, 2); + MapAssert.assertSizeEquals(tripleWithNullKey, 3); + MapAssert.assertSizeEquals(quadrupleWithNullValue, 4); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertSizeFailForNull() + { + MapAssert.assertSizeEquals(null, 0); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertSizeFailForDifferentSize() + { + MapAssert.assertSizeEquals(couple, 1); + } + + @Test + public void testAssertEntrySuccess() + { + MapAssert.assertEntry(singleton, "key", "value"); + MapAssert.assertEntry(couple, "key2", "value2"); + MapAssert.assertEntry(tripleWithNullKey, null, "marines"); + MapAssert.assertEntry(quadrupleWithNullValue, "she", null); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEntryFailsForNullMap() + { + MapAssert.assertEntry(null, "key", "value"); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEntryFailsForWrongNullKey() + { + MapAssert.assertEntry(singleton, null, "value"); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEntryFailsForWrongKey() + { + MapAssert.assertEntry(singleton, "wrong_key", "value"); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEntryFailsForWrongNullValue() + { + MapAssert.assertEntry(singleton, "key", null); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEntryFailsForWrongValue() + { + MapAssert.assertEntry(singleton, "key", "wrong_value"); + } + + @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")); + } + + @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")); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertValuesFailsForNullMap() + { + MapAssert.assertValuesEqual(null, CB.c("value")); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertValuesFailsForNullValues() + { + MapAssert.assertValuesEqual(empty, null); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertValuesFailsForDifferentValues1() + { + MapAssert.assertValuesEqual(empty, CB.c("not_empty")); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertValuesFailsForDifferentValues2() + { + MapAssert.assertValuesEqual(couple, CB.c()); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertValuesFailsForDifferentValues3() + { + MapAssert.assertValuesEqual(tripleWithNullKey, CB.c("zergling", "marines")); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertValuesFailsForDifferentValues4() + { + MapAssert.assertValuesEqual(quadrupleWithNullValue, CB.c("win", null, "acts", "lose")); + } + +} Property changes on: trunk/openutils-testing/src/test/java/it/openutils/testing/collections/MapAssertTest.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-testing/src/test/java/it/openutils/testing/collections/SetAssertTest.java =================================================================== --- trunk/openutils-testing/src/test/java/it/openutils/testing/collections/SetAssertTest.java (rev 0) +++ trunk/openutils-testing/src/test/java/it/openutils/testing/collections/SetAssertTest.java 2010-05-25 16:45:03 UTC (rev 2559) @@ -0,0 +1,120 @@ +/** + * + * 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.Collections; +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.Set; + +import org.testng.annotations.Test; + + +/** + * @author gcatania + */ +public class SetAssertTest +{ + + private static final Set< ? > empty = Collections.EMPTY_SET; + + private static final Set< ? > singleton = Collections.singleton("alone"); + + private static final Set<Object> couple = new HashSet<Object>(2, 1f); + + private static final Set<Object> tripleWithNull = new LinkedHashSet<Object>(3, 1f); + + static + { + couple.add("first"); + couple.add("second"); + tripleWithNull.add("first"); + tripleWithNull.add(null); + tripleWithNull.add("last"); + } + + @Test + public void testAssertEqualsSuccess() + { + 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); + // equalities regardless of order should work for sets + CollectionAssert.assertEquals(CB.s("second", "first"), couple); + CollectionAssert.assertEquals(CB.s("first", "last", null), tripleWithNull); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForNull1() + { + SetAssert.assertEquals(null, empty); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForNull2() + { + SetAssert.assertEquals(empty, null); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForSize1() + { + SetAssert.assertEquals(singleton, couple); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForSize2() + { + SetAssert.assertEquals(couple, empty); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForSize3() + { + SetAssert.assertEquals(tripleWithNull, empty); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForDifferentElements1() + { + CollectionAssert.assertEquals(CB.s("one"), singleton); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForDifferentElements2() + { + CollectionAssert.assertEquals(CB.s("second", "different"), couple); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsFailForDifferentElements3() + { + CollectionAssert.assertEquals(CB.s(null, "first", null), tripleWithNull); + } + +} Property changes on: trunk/openutils-testing/src/test/java/it/openutils/testing/collections/SetAssertTest.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |