From: <fg...@us...> - 2009-02-17 09:02:58
|
Revision: 1012 http://openutils.svn.sourceforge.net/openutils/?rev=1012&view=rev Author: fgiust Date: 2009-02-17 09:02:53 +0000 (Tue, 17 Feb 2009) Log Message: ----------- automatic switch to *identity operations for ms sqlserver databases Modified Paths: -------------- trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitExecution.java trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitTestContext.java trunk/openutils-testing/src/site/changes/changes.xml Modified: trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitExecution.java =================================================================== --- trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitExecution.java 2009-02-17 08:50:04 UTC (rev 1011) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitExecution.java 2009-02-17 09:02:53 UTC (rev 1012) @@ -77,7 +77,9 @@ Class< ? extends DatabaseOperation> truncateOperation() default org.dbunit.operation.DeleteAllOperation.class; /** - * The database operation that will be used to load datasets. Defaults to org.dbunit.operation.InsertOperation + * The database operation that will be used to load datasets. Defaults to org.dbunit.operation.InsertOperation. For + * Sql Server databases the default is automatically set to MsSqlIndentityInsertOperation (allow writing of identity + * columns). */ Class< ? extends DatabaseOperation> insertOperation() default org.dbunit.operation.InsertOperation.class; Modified: trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitTestContext.java =================================================================== --- trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitTestContext.java 2009-02-17 08:50:04 UTC (rev 1011) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitTestContext.java 2009-02-17 09:02:53 UTC (rev 1012) @@ -15,6 +15,10 @@ */ package it.openutils.testing; +import it.openutils.testing.dbunit.CleanInsertOperation; +import it.openutils.testing.dbunit.MsSqlIndentityCleanInsertOperation; +import it.openutils.testing.dbunit.MsSqlIndentityInsertOperation; + import java.io.IOException; import java.net.URL; import java.sql.SQLException; @@ -146,6 +150,23 @@ } DatabaseOperation dbOperation = getDatabaseOperation(dbUnitExecution.insertOperation()); + String dbtype = connection.getConnection().getMetaData().getDatabaseProductName(); + + // default + if (InsertOperation.class.equals(dbOperation.getClass())) + { + if ("Microsoft SQL Server".equals(dbtype)) + { + dbOperation = new MsSqlIndentityInsertOperation(); + } + } + else if (CleanInsertOperation.class.equals(dbOperation.getClass())) + { + if ("Microsoft SQL Server".equals(dbtype)) + { + dbOperation = new MsSqlIndentityCleanInsertOperation(); + } + } for (String datasetFile : datasets) { importDataSet(createDataset(datasetFile), connection, tableFilter, dataSourceName, dbOperation); Modified: trunk/openutils-testing/src/site/changes/changes.xml =================================================================== --- trunk/openutils-testing/src/site/changes/changes.xml 2009-02-17 08:50:04 UTC (rev 1011) +++ trunk/openutils-testing/src/site/changes/changes.xml 2009-02-17 09:02:53 UTC (rev 1012) @@ -8,7 +8,10 @@ <author email="fgiust(at)users.sourceforge.net">Fabrizio Giustina</author> </properties> <body> - <release version="2.1" date="2008-04-27" description="2.1"> + <release version="2.1.1" date="2009-02-17" description="2.1.1"> + <action type="add" dev="fgiust"> Detection of ms sqlserver databases and automatic disabling of identities + (switch from CleanInsertOperation to MsSqlIndentityCleanInsertOperation and from InsertOperation to IndentityCleanInsertOperation).</action> + </release> <release version="2.1" date="2008-04-27" description="2.1"> <action type="update" dev="fgiust">Legacy base test classes (DbUnitTestCase, GenericsDbUnitTestCase) have been dropped. Stick with 2.0.x if you are still use them or upgrade to 2.1 for a forced migration</action> <action type="update" dev="fgiust">Updated spring, slf4j and commons-lang dependencies</action> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fg...@us...> - 2009-03-29 09:46:44
|
Revision: 1119 http://openutils.svn.sourceforge.net/openutils/?rev=1119&view=rev Author: fgiust Date: 2009-03-29 09:46:38 +0000 (Sun, 29 Mar 2009) Log Message: ----------- flatxml support not fully working Modified Paths: -------------- trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitUtils.java trunk/openutils-testing/src/site/changes/changes.xml Modified: trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitUtils.java =================================================================== --- trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitUtils.java 2009-03-28 18:41:36 UTC (rev 1118) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitUtils.java 2009-03-29 09:46:38 UTC (rev 1119) @@ -210,7 +210,20 @@ if (is != null) { log.debug("loading dataset {}", url.getFile()); - dataSet = url.getFile().endsWith(".xls") ? new XlsDataSet(is) : new XmlDataSet(is); + + if (datasetType == DatasetType.EXCEL || (url.getFile().endsWith(".xls"))) + { + dataSet = new XlsDataSet(is); + } + else if (datasetType == DatasetType.FLATXML) + { + dataSet = new FlatXmlDataSet(is); + } + else if (datasetType == DatasetType.XML) + { + dataSet = new XmlDataSet(is); + } + is.close(); } return dataSet; Modified: trunk/openutils-testing/src/site/changes/changes.xml =================================================================== --- trunk/openutils-testing/src/site/changes/changes.xml 2009-03-28 18:41:36 UTC (rev 1118) +++ trunk/openutils-testing/src/site/changes/changes.xml 2009-03-29 09:46:38 UTC (rev 1119) @@ -8,7 +8,10 @@ <author email="fgiust(at)users.sourceforge.net">Fabrizio Giustina</author> </properties> <body> - <release version="2.2" date="2009-03-28" description="2.1.2"> + <release version="2.2.1" date="2009-03-29" description=""> + <action type="fix" dev="fgiust">Annotation "dataseType" added in 2.2.1 was respected for flatxml import.</action> + </release> + <release version="2.2" date="2009-03-28" description=""> <action type="add" dev="fgiust">Added support for flat xml format.</action> <action type="update" dev="fgiust">Updated slf4j dependencies.</action> <action type="update" dev="fgiust">Updated dbunit version to 2.4.4</action> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
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. |
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. |
From: <gca...@us...> - 2010-05-28 12:49:40
|
Revision: 2583 http://openutils.svn.sourceforge.net/openutils/?rev=2583&view=rev Author: gcatania Date: 2010-05-28 12:49:34 +0000 (Fri, 28 May 2010) Log Message: ----------- TEST-1 added sort asserts, unit tests Modified Paths: -------------- trunk/openutils-testing/src/main/java/it/openutils/testing/collections/CollectionAssert.java trunk/openutils-testing/src/test/java/it/openutils/testing/collections/CollectionAssertTest.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-28 10:33:58 UTC (rev 2582) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/collections/CollectionAssert.java 2010-05-28 12:49:34 UTC (rev 2583) @@ -28,8 +28,10 @@ import it.openutils.testing.AssertFormatter; import java.util.Collection; +import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; import java.util.Map; import java.util.Set; @@ -231,6 +233,195 @@ } /** + * Asserts that a collection is sorted. + * @param <T> the type of elements in the collection, must be comparable + * @param c the collection + * @param strict if true and two elements {@code e1} and {@code e2} such that {@code e1.compareTo(e2) == 0} are + * found, the assertion will fail + * @param message a custom error message + * @throws AssertionError if assertion fails + * @throws NullPointerException if any element in the input collection is null + */ + public static <T extends Comparable<T>> void assertSorted(Collection<T> c, boolean strict, String message) + throws AssertionError, NullPointerException + { + assertNotNull(c, message); + if (c.size() < 2) + { + return; + } + Iterator<T> i = c.iterator(); + T previous = i.next(); + do + { + T next = i.next(); + int compare = next.compareTo(previous); + if (compare < 0) + { + throw assertionFailed( + message, + "{0} is not sorted: element {1} is greater than element {2} but occurs first.", + capitalizedTypeName(c), + previous, + next); + } + if (strict && compare == 0) + { + throw assertionFailed( + message, + "{0} is not strictly sorted: element {1} compares equally to element {2}.", + capitalizedTypeName(c), + previous, + next); + } + previous = next; + } + while (i.hasNext()); + + } + + /** + * Asserts that a collection is sorted. + * @param <T> the type of elements in the collection, must be comparable + * @param c the collection + * @param strict if true and two elements {@code e1} and {@code e2} such that {@code e1.compareTo(e2) == 0} are + * found, the assertion will fail + * @throws AssertionError if assertion fails + * @throws NullPointerException if any element in the input collection is null + */ + public static <T extends Comparable<T>> void assertSorted(Collection<T> c, boolean strict) throws AssertionError, + NullPointerException + { + assertSorted(c, strict, null); + } + + /** + * Asserts that a collection is loosely sorted, i.e that for every two subsequent elements {@code e1} and {@code e2} + * in the collection, {@code e1.compareTo(e2) >=0} is true. + * @param <T> the type of elements in the collection, must be comparable + * @param c the collection + * @param message a custom error message + * @throws AssertionError if assertion fails + * @throws NullPointerException if any element in the input collection is null + */ + public static <T extends Comparable<T>> void assertSorted(Collection<T> c, String message) throws AssertionError, + NullPointerException + { + assertSorted(c, false, message); + } + + /** + * Asserts that a collection is loosely sorted, i.e that for every two subsequent elements {@code e1} and {@code e2} + * in the collection, {@code e1.compareTo(e2) >=0} is true. + * @param <T> the type of elements in the collection, must be comparable + * @param c the collection + * @throws AssertionError if assertion fails + * @throws NullPointerException if any element in the input collection is null + */ + public static <T extends Comparable<T>> void assertSorted(Collection<T> c) throws AssertionError, + NullPointerException + { + assertSorted(c, false, null); + } + + /** + * Asserts that a collection is sorted. + * @param <T> the type of elements in the collection + * @param c the collection + * @param comparator defines an ordering on the collection's element + * @param strict if true and two elements {@code e1} and {@code e2} such that {@code comparator.compare(e1, e2) == + * 0} are found, the assertion will fail + * @param message a custom error message + * @throws AssertionError if assertion fails + * @throws any exception thrown by {@code comparator} when comparing elements of the input collection + */ + public static <T> void assertSorted(Collection<T> c, Comparator<T> comparator, boolean strict, String message) + throws AssertionError + { + assertNotNull(c, message); + if (c.size() < 2) + { + return; + } + Iterator<T> i = c.iterator(); + T previous = i.next(); + do + { + T next = i.next(); + int compare = comparator.compare(previous, next); + if (compare < 0) + { + throw assertionFailed( + message, + "{0} is not sorted according to {3}: element {1} is greater than element {2} but occurs first.", + capitalizedTypeName(c), + previous, + next, + comparator); + } + if (strict && compare == 0) + { + throw assertionFailed( + message, + "{0} is not strictly sorted according to {3}: element {1} compares equally to element {2}.", + capitalizedTypeName(c), + previous, + next, + comparator); + } + previous = next; + } + while (i.hasNext()); + + } + + /** + * Asserts that a collection is sorted. + * @param <T> the type of elements in the collection + * @param c the collection + * @param comparator defines an ordering on the collection's element + * @param strict if true and two elements {@code e1} and {@code e2} such that {@code comparator.compare(e1, e2) == + * 0} are found, the assertion will fail + * @throws AssertionError if assertion fails + * @throws any exception thrown by {@code comparator} when comparing elements of the input collection + */ + public static <T> void assertSorted(Collection<T> c, Comparator<T> comparator, boolean strict) + throws AssertionError + { + assertSorted(c, comparator, strict, null); + } + + /** + * Asserts that a collection is loosely sorted, i.e. that for every two subsequent elements {@code e1} and {@code + * e2} in the collection, {@code comparator.compare(e1, e2) >= 0} is true. + * @param <T> the type of elements in the collection + * @param c the collection + * @param comparator defines an ordering on the collection's element + * @param message a custom error message + * @throws AssertionError if assertion fails + * @throws any exception thrown by {@code comparator} when comparing elements of the input collection + */ + public static <T> void assertSorted(Collection<T> c, Comparator<T> comparator, String message) + throws AssertionError + { + assertSorted(c, comparator, false, message); + } + + /** + * Asserts that a collection is loosely sorted, i.e. that for every two subsequent elements {@code e1} and {@code + * e2} in the collection, {@code comparator.compare(e1, e2) >= 0} is true. + * @param <T> the type of elements in the collection + * @param c the collection + * @param comparator defines an ordering on the collection's element + * @throws AssertionError if assertion fails + * @throws any exception thrown by {@code comparator} when comparing elements of the input collection + */ + public static <T> void assertSorted(Collection<T> c, Comparator<T> comparator) throws AssertionError + { + assertSorted(c, comparator, false, null); + } + + /** * internal utility method to retrieve the occurrences of distinct objects in a collection. * @param c the collection * @return a map between objects and their occurrences in the collection 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-28 10:33:58 UTC (rev 2582) +++ trunk/openutils-testing/src/test/java/it/openutils/testing/collections/CollectionAssertTest.java 2010-05-28 12:49:34 UTC (rev 2583) @@ -30,6 +30,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.HashSet; import java.util.LinkedList; import java.util.Stack; @@ -43,18 +44,27 @@ public class CollectionAssertTest { - private static final Collection< ? > empty = Collections.EMPTY_LIST; + private final Collection< ? > empty = Collections.EMPTY_LIST; - private static final Collection< ? > singleton = Collections.singleton("alone"); + private final Collection< ? > singleton = Collections.singleton("alone"); - private static final Collection<Object> couple = new Stack<Object>(); + private final Collection<Object> couple = new Stack<Object>(); - private static final Collection<Object> tripleWithNull = new HashSet<Object>(3, 1f); + private final Collection<Object> tripleWithNull = new HashSet<Object>(3, 1f); - private static final Collection<Object> quadrupleWithRepeats = new LinkedList<Object>(); + private final Collection<Object> quadrupleWithRepeats = new LinkedList<Object>(); - static + private final Comparator<String> secondCharComparator = new Comparator<String>() { + + public int compare(String o1, String o2) + { + return new Character(o2.charAt(1)).compareTo(o1.charAt(1)); + } + }; + + public CollectionAssertTest() + { couple.add("first"); couple.add("second"); tripleWithNull.add("first"); @@ -216,4 +226,61 @@ CollectionAssert.assertEquals(c("one", "two", "three", null, "three"), quadrupleWithRepeats); } + @Test + public void testAssertSortedSuccess() + { + CollectionAssert.assertSorted(Collections.<Integer> emptyList(), true); + CollectionAssert.assertSorted(c(23), true); + CollectionAssert.assertSorted(c(11, 32, 77, 99), true); + CollectionAssert.assertSorted(c(-8d, 3d, 4d, 4d, 77.3d), false); + CollectionAssert.assertSorted(c("abacus", "banana", "choir", "dandy", "dedalus", "zebra"), true); + CollectionAssert.assertSorted(c("", "a", "aa", "z", "zz"), true); + CollectionAssert.assertSorted(c("", "a", "a", "z", "zz"), false); + + CollectionAssert.assertSorted(c("z2a", "a3z", "341", "y59"), secondCharComparator, true); + CollectionAssert.assertSorted(c("z2a", "a3z", "331", "y59"), secondCharComparator, false); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertSortedFailsForNull() + { + CollectionAssert.assertSorted((Collection<String>) null, true); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertSortedFailsForNotSorted1() + { + CollectionAssert.assertSorted(c(11, 32, 99, 77), true); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertSortedFailsForNotSorted2() + { + CollectionAssert.assertSorted(c("abacus", "banana", "dandy", "choir", "dedalus", "zebra"), true); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertSortedFailsForNotSorted3() + { + CollectionAssert.assertSorted(c("z2a", "a3z", "311", "y59"), secondCharComparator, false); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertSortedFailsForNotStrict1() + { + CollectionAssert.assertSorted(c(1, 2, 3, 4, 5, 5, 6, 7, 8), true); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertSortedFailsForNotStrict2() + { + CollectionAssert.assertSorted(c("abacus", "banana", "choir", "dandy", "dandy", "dedalus", "zebra"), true); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertSortedFailsForNotStrict3() + { + CollectionAssert.assertSorted(c("z2a", "a3z", "321", "y59"), secondCharComparator, false); + } + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <gca...@us...> - 2010-06-11 14:39:28
|
Revision: 2647 http://openutils.svn.sourceforge.net/openutils/?rev=2647&view=rev Author: gcatania Date: 2010-06-11 14:39:22 +0000 (Fri, 11 Jun 2010) Log Message: ----------- TEST-1 cleanup, added tests for dateAssert Modified Paths: -------------- trunk/openutils-testing/src/main/java/it/openutils/testing/DateAssert.java trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitExecution.java trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitTestContext.java trunk/openutils-testing/src/main/java/it/openutils/testing/TestUtils.java trunk/openutils-testing/src/main/java/it/openutils/testing/collections/CollectionAssert.java trunk/openutils-testing/src/main/java/it/openutils/testing/collections/MapAssert.java Added Paths: ----------- trunk/openutils-testing/src/test/java/it/openutils/testing/DateAssertTest.java Modified: trunk/openutils-testing/src/main/java/it/openutils/testing/DateAssert.java =================================================================== --- trunk/openutils-testing/src/main/java/it/openutils/testing/DateAssert.java 2010-06-11 08:00:38 UTC (rev 2646) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/DateAssert.java 2010-06-11 14:39:22 UTC (rev 2647) @@ -139,4 +139,37 @@ performComparison(expectedDateAsString, date.getTime(), dtf); } + /** + * Asserts that either two dates are null or the comparison between them is zero. + * @param expectedDate the expected date. + * @param actualDate the actual date + */ + public static void assertEquals(Date expectedDate, Date actualDate) + { + if (expectedDate == null) + { + if (actualDate == null) + { + return; + } + } + else if (actualDate != null && actualDate.compareTo(expectedDate) == 0) + { + return; + } + throw new AssertionError("Expected <" + expectedDate + ">, found <" + actualDate + ">"); + } + + /** + * Asserts that either two calendars are null or the comparison between them is zero. + * @param expectedDate the expected date. + * @param actualDate the actual date. + */ + public static void assertEquals(Calendar expectedDate, Calendar actualDate) + { + Date expected = expectedDate != null ? expectedDate.getTime() : null; + Date actual = actualDate != null ? actualDate.getTime() : null; + assertEquals(expected, actual); + } + } Modified: trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitExecution.java =================================================================== --- trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitExecution.java 2010-06-11 08:00:38 UTC (rev 2646) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitExecution.java 2010-06-11 14:39:22 UTC (rev 2647) @@ -105,11 +105,11 @@ * A list of dbunit features that should be enabled or disabled on the connection. See * http://www.dbunit.org/properties.html. */ - DbUnitFeature[] features() default {}; + DbUnitFeature[] features() default { }; /** * A list of dbunit properties that should be set on the connection. See http://www.dbunit.org/properties.html. */ - DbUnitProperty[] properties() default {}; + DbUnitProperty[] properties() default { }; } \ No newline at end of file Modified: trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitTestContext.java =================================================================== --- trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitTestContext.java 2010-06-11 08:00:38 UTC (rev 2646) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/DbUnitTestContext.java 2010-06-11 14:39:22 UTC (rev 2647) @@ -411,7 +411,6 @@ * @param name datasource name. * @return Datasource instance */ - @SuppressWarnings("unchecked") protected DataSource getDatasource(String name) { if (StringUtils.isEmpty(name)) Modified: trunk/openutils-testing/src/main/java/it/openutils/testing/TestUtils.java =================================================================== --- trunk/openutils-testing/src/main/java/it/openutils/testing/TestUtils.java 2010-06-11 08:00:38 UTC (rev 2646) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/TestUtils.java 2010-06-11 14:39:22 UTC (rev 2647) @@ -25,16 +25,18 @@ package it.openutils.testing; -import java.util.ArrayList; -import java.util.HashSet; +import it.openutils.testing.collections.CollectionBuilder; + import java.util.List; import java.util.Set; /** + * @deprecated in favor of {@link CollectionBuilder} * @author fgiust * @version $Id$ */ +@Deprecated public final class TestUtils { @@ -50,12 +52,7 @@ */ public static <T> Set<T> setWith(T... elements) { - Set<T> newSet = new HashSet<T>(elements.length); - for (T element : elements) - { - newSet.add(element); - } - return newSet; + return CollectionBuilder.s(elements); } /** @@ -66,11 +63,6 @@ */ public static <T> List<T> listWith(T... elements) { - List<T> newList = new ArrayList<T>(elements.length); - for (T element : elements) - { - newList.add(element); - } - return newList; + return CollectionBuilder.l(elements); } } 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-06-11 08:00:38 UTC (rev 2646) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/collections/CollectionAssert.java 2010-06-11 14:39:22 UTC (rev 2647) @@ -87,7 +87,6 @@ /** * 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 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-06-11 08:00:38 UTC (rev 2646) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/collections/MapAssert.java 2010-06-11 14:39:22 UTC (rev 2647) @@ -83,7 +83,6 @@ /** * Asserts that a map is not null * @param m the map - * @param message a custom error message * @throws AssertionError if assertion fails */ public static void assertNotNull(Map< ? , ? > m) throws AssertionError Added: trunk/openutils-testing/src/test/java/it/openutils/testing/DateAssertTest.java =================================================================== --- trunk/openutils-testing/src/test/java/it/openutils/testing/DateAssertTest.java (rev 0) +++ trunk/openutils-testing/src/test/java/it/openutils/testing/DateAssertTest.java 2010-06-11 14:39:22 UTC (rev 2647) @@ -0,0 +1,76 @@ +/** + * + * 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; + +import java.util.Calendar; +import java.util.GregorianCalendar; + +import org.testng.annotations.Test; + + +/** + * @author gcatania + */ +public class DateAssertTest +{ + + @Test + public void testAssertSuccess() + { + DateAssert.assertEquals((Calendar) null, (Calendar) null); + Calendar c1 = new GregorianCalendar(2010, 3, 4, 5, 6, 8); + Calendar c2 = new GregorianCalendar(2010, 3, 4, 5, 6, 8); + DateAssert.assertEquals(c1, c2); + c1.setLenient(true); + c2.setLenient(false); + DateAssert.assertEquals(c1, c2); + c1.setFirstDayOfWeek(3); + c1.setFirstDayOfWeek(4); + DateAssert.assertEquals(c1, c2); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertFailsForNullActual() + { + DateAssert.assertEquals(Calendar.getInstance(), null); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertFailsForNullExpected() + { + DateAssert.assertEquals(null, Calendar.getInstance()); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertFailsForDifferent() + { + Calendar c1 = new GregorianCalendar(2010, 3, 4, 5, 6, 8); + Calendar c2 = new GregorianCalendar(2010, 3, 4, 5, 6, 8); + c2.set(Calendar.MILLISECOND, 3); + DateAssert.assertEquals(c1, c2); + } + +} Property changes on: trunk/openutils-testing/src/test/java/it/openutils/testing/DateAssertTest.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. |