Added Tests for BagUtils and typed bags.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131164 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Phil Steitz 2003-09-20 02:51:50 +00:00
parent 2daa7e098e
commit ab33c74b1e
5 changed files with 569 additions and 4 deletions

View File

@ -1,5 +1,5 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestAll.java,v 1.45 2003/08/31 17:28:43 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestAll.java,v 1.46 2003/09/20 02:51:50 psteitz Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -64,7 +64,7 @@ import junit.framework.TestSuite;
/**
* Entry point for all Collections package tests.
*
* @version $Revision: 1.45 $ $Date: 2003/08/31 17:28:43 $
* @version $Revision: 1.46 $ $Date: 2003/09/20 02:51:50 $
*
* @author Rodney Waldhoff
* @author Stephen Colebourne
@ -112,6 +112,7 @@ public class TestAll extends TestCase {
suite.addTest(TestUnboundedFifoBuffer.suite());
suite.addTest(TestReferenceMap.suite());
suite.addTest(TestIteratorUtils.suite());
suite.addTest(TestBagUtils.suite());
return suite;
}

View File

@ -0,0 +1,259 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestBagUtils.java,v 1.1 2003/09/20 02:51:50 psteitz Exp $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowledgements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections;
import org.apache.commons.collections.decorators.SynchronizedBag;
import org.apache.commons.collections.decorators.UnmodifiableBag;
import org.apache.commons.collections.decorators.PredicatedBag;
import org.apache.commons.collections.decorators.TypedBag;
import org.apache.commons.collections.decorators.TransformedBag;
import org.apache.commons.collections.decorators.SynchronizedSortedBag;
import org.apache.commons.collections.decorators.UnmodifiableSortedBag;
import org.apache.commons.collections.decorators.PredicatedSortedBag;
import org.apache.commons.collections.decorators.TypedSortedBag;
import org.apache.commons.collections.decorators.TransformedSortedBag;
import junit.framework.Test;
/**
* Tests for BagUtils factory methods.
*
* @author Phil Steitz
*
* @version $Revision: 1.1 $ $Date: 2003/09/20 02:51:50 $
*/
public class TestBagUtils extends BulkTest {
public TestBagUtils(String name) {
super(name);
}
public static Test suite() {
return BulkTest.makeSuite(TestBagUtils.class);
}
//----------------------------------------------------------------------
protected Class stringClass = this.getName().getClass();
protected Predicate truePredicate = PredicateUtils.truePredicate();
protected Transformer nopTransformer = TransformerUtils.nopTransformer();
//----------------------------------------------------------------------
public void testSynchronizedBag() {
Bag bag = BagUtils.synchronizedBag(new HashBag());
assertTrue("Returned object should be a SynchronizedBag.",
bag instanceof SynchronizedBag);
try {
bag = BagUtils.synchronizedBag(null);
fail("Expecting IllegalArgumentException for null bag.");
} catch (IllegalArgumentException ex) {
// expected
}
}
public void testUnmodifiableBag() {
Bag bag = BagUtils.unmodifiableBag(new HashBag());
assertTrue("Returned object should be an UnmodifiableBag.",
bag instanceof UnmodifiableBag);
try {
bag = BagUtils.unmodifiableBag(null);
fail("Expecting IllegalArgumentException for null bag.");
} catch (IllegalArgumentException ex) {
// expected
}
}
public void testPredicatedBag() {
Bag bag = BagUtils.predicatedBag(new HashBag(), truePredicate);
assertTrue("Returned object should be a PredicatedBag.",
bag instanceof PredicatedBag);
try {
bag = BagUtils.predicatedBag(null,truePredicate);
fail("Expecting IllegalArgumentException for null bag.");
} catch (IllegalArgumentException ex) {
// expected
}
try {
bag = BagUtils.predicatedBag(new HashBag(), null);
fail("Expecting IllegalArgumentException for null predicate.");
} catch (IllegalArgumentException ex) {
// expected
}
}
public void testTypedBag() {
Bag bag = BagUtils.typedBag(new HashBag(), stringClass);
assertTrue("Returned object should be a TypedBag.",
bag instanceof PredicatedBag);
try {
bag = BagUtils.typedBag(null, stringClass);
fail("Expecting IllegalArgumentException for null bag.");
} catch (IllegalArgumentException ex) {
// expected
}
try {
bag = BagUtils.typedBag(new HashBag(), null);
fail("Expecting IllegalArgumentException for null type.");
} catch (IllegalArgumentException ex) {
// expected
}
}
public void testTransformedBag() {
Bag bag = BagUtils.transformedBag(new HashBag(), nopTransformer);
assertTrue("Returned object should be an TransformedBag.",
bag instanceof TransformedBag);
try {
bag = BagUtils.transformedBag(null, nopTransformer);
fail("Expecting IllegalArgumentException for null bag.");
} catch (IllegalArgumentException ex) {
// expected
}
try {
bag = BagUtils.transformedBag(new HashBag(), null);
fail("Expecting IllegalArgumentException for null transformer.");
} catch (IllegalArgumentException ex) {
// expected
}
}
public void testSynchronizedSortedBag() {
Bag bag = BagUtils.synchronizedSortedBag(new TreeBag());
assertTrue("Returned object should be a SynchronizedSortedBag.",
bag instanceof SynchronizedSortedBag);
try {
bag = BagUtils.synchronizedSortedBag(null);
fail("Expecting IllegalArgumentException for null bag.");
} catch (IllegalArgumentException ex) {
// expected
}
}
public void testUnmodifiableSortedBag() {
Bag bag = BagUtils.unmodifiableSortedBag(new TreeBag());
assertTrue("Returned object should be an UnmodifiableSortedBag.",
bag instanceof UnmodifiableSortedBag);
try {
bag = BagUtils.unmodifiableSortedBag(null);
fail("Expecting IllegalArgumentException for null bag.");
} catch (IllegalArgumentException ex) {
// expected
}
}
public void testPredicatedSortedBag() {
Bag bag = BagUtils.predicatedSortedBag(new TreeBag(), truePredicate);
assertTrue("Returned object should be a PredicatedSortedBag.",
bag instanceof PredicatedSortedBag);
try {
bag = BagUtils.predicatedSortedBag(null, truePredicate);
fail("Expecting IllegalArgumentException for null bag.");
} catch (IllegalArgumentException ex) {
// expected
}
try {
bag = BagUtils.predicatedSortedBag(new TreeBag(), null);
fail("Expecting IllegalArgumentException for null predicate.");
} catch (IllegalArgumentException ex) {
// expected
}
}
public void testTypedSortedBag() {
Bag bag = BagUtils.typedSortedBag(new TreeBag(), stringClass);
assertTrue("Returned object should be a TypedSortedBag.",
bag instanceof PredicatedBag);
try {
bag = BagUtils.typedSortedBag(null, stringClass);
fail("Expecting IllegalArgumentException for null bag.");
} catch (IllegalArgumentException ex) {
// expected
}
try {
bag = BagUtils.typedSortedBag(new TreeBag(), null);
fail("Expecting IllegalArgumentException for null type.");
} catch (IllegalArgumentException ex) {
// expected
}
}
public void testTransformedSortedBag() {
Bag bag = BagUtils.transformedSortedBag(new TreeBag(), nopTransformer);
assertTrue("Returned object should be an TransformedSortedBag",
bag instanceof TransformedSortedBag);
try {
bag = BagUtils.transformedSortedBag(null, nopTransformer);
fail("Expecting IllegalArgumentException for null bag.");
} catch (IllegalArgumentException ex) {
// expected
}
try {
bag = BagUtils.transformedSortedBag(new TreeBag(), null);
fail("Expecting IllegalArgumentException for null transformer.");
} catch (IllegalArgumentException ex) {
// expected
}
}
}

View File

@ -1,5 +1,5 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/TestAll.java,v 1.13 2003/09/15 03:50:41 psteitz Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/TestAll.java,v 1.14 2003/09/20 02:51:50 psteitz Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -65,7 +65,7 @@ import junit.framework.TestSuite;
* Entry point for all collections decorators tests.
*
* @since Commons Collections 3.0
* @version $Revision: 1.13 $ $Date: 2003/09/15 03:50:41 $
* @version $Revision: 1.14 $ $Date: 2003/09/20 02:51:50 $
*
* @author Stephen Colebourne
*/
@ -109,6 +109,8 @@ public class TestAll extends TestCase {
suite.addTest(TestLazyMap.suite());
suite.addTest(TestLazySortedMap.suite());
suite.addTest(TestBlockingBuffer.suite());
suite.addTest(TestTypedBag.suite());
suite.addTest(TestTypedSortedBag.suite());
return suite;
}

View File

@ -0,0 +1,162 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/TestTypedBag.java,v 1.1 2003/09/20 02:51:50 psteitz Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowledgements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections.decorators;
import junit.framework.Test;
import junit.framework.TestSuite;
import java.util.Set;
import org.apache.commons.collections.Bag;
import org.apache.commons.collections.HashBag;
import org.apache.commons.collections.TestBag;
/**
* Extension of {@link TestBag} for exercising the {@link TypedBag}
* implementation.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/09/20 02:51:50 $
*
* @author Phil Steitz
*/
public class TestTypedBag extends TestBag {
public TestTypedBag(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestTypedBag.class);
}
public static void main(String args[]) {
String[] testCaseName = { TestTypedBag.class.getName()};
junit.textui.TestRunner.main(testCaseName);
}
//--------------------------------------------------------------------------
protected Class stringClass = this.getName().getClass();
private Object obj = new Object();
protected Class objectClass = obj.getClass();
protected Bag decorateBag(HashBag bag, Class claz) {
return TypedBag.decorate(bag, claz);
}
public Bag makeBag() {
return decorateBag(new HashBag(), objectClass);
}
public Bag makeTestBag() {
return decorateBag(new HashBag(), stringClass);
}
//--------------------------------------------------------------------------
public void testlegalAddRemove() {
Bag bag = makeTestBag();
assertEquals(0, bag.size());
Object[] els = new Object[] {"1", "3", "5", "7", "2", "4", "1"};
for (int i = 0; i < els.length; i++) {
bag.add(els[i]);
assertEquals(i + 1, bag.size());
assertEquals(true, bag.contains(els[i]));
}
Set set = ((PredicatedBag) bag).uniqueSet();
assertTrue("Unique set contains the first element",set.contains(els[0]));
assertEquals(true, bag.remove(els[0]));
set = ((PredicatedBag) bag).uniqueSet();
assertTrue("Unique set now does not contain the first element",
!set.contains(els[0]));
}
public void testIllegalAdd() {
Bag bag = makeTestBag();
Integer i = new Integer(3);
try {
bag.add(i);
fail("Integer should fail type check.");
} catch (IllegalArgumentException e) {
// expected
}
assertTrue("Collection shouldn't contain illegal element",
!bag.contains(i));
}
public void testIllegalDecorate() {
HashBag elements = new HashBag();
elements.add("one");
elements.add("two");
elements.add(new Integer(3));
elements.add("four");
try {
Bag bag = decorateBag(elements, stringClass);
fail("Bag contains an element that should fail the type test.");
} catch (IllegalArgumentException e) {
// expected
}
try {
Bag bag = decorateBag(new HashBag(), null);
fail("Expectiing IllegalArgumentException for null predicate.");
} catch (IllegalArgumentException e) {
// expected
}
}
}

View File

@ -0,0 +1,141 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/TestTypedSortedBag.java,v 1.1 2003/09/20 02:51:50 psteitz Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowledgements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections.decorators;
import junit.framework.Test;
import junit.framework.TestSuite;
import java.util.Comparator;
import org.apache.commons.collections.Bag;
import org.apache.commons.collections.SortedBag;
import org.apache.commons.collections.TreeBag;
import org.apache.commons.collections.TestBag;
/**
* Extension of {@link TestBag} for exercising the {@link TypedSortedBag}
* implementation.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/09/20 02:51:50 $
*
* @author Phil Steitz
*/
public class TestTypedSortedBag extends TestBag {
public TestTypedSortedBag(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestTypedSortedBag.class);
}
public static void main(String args[]) {
String[] testCaseName = { TestTypedSortedBag.class.getName()};
junit.textui.TestRunner.main(testCaseName);
}
//--------------------------------------------------------------------------
protected Class stringClass = this.getName().getClass();
private Object obj = new Object();
protected Class objectClass = obj.getClass();
protected SortedBag emptyBag = new TreeBag();
protected SortedBag nullBag = null;
protected SortedBag decorateBag(SortedBag bag, Class claz) {
return TypedSortedBag.decorate(bag, claz);
}
public Bag makeBag() {
return decorateBag(emptyBag, objectClass);
}
public Bag makeTestBag() {
return decorateBag(emptyBag, stringClass);
}
//--------------------------------------------------------------------------
public void testDecorate() {
SortedBag bag = decorateBag(emptyBag, stringClass);
try {
SortedBag bag3 = decorateBag(emptyBag, null);
fail("Expecting IllegalArgumentException for null predicate");
} catch (IllegalArgumentException e) {}
try {
SortedBag bag4 = decorateBag(nullBag, stringClass);
fail("Expecting IllegalArgumentException for null bag");
} catch (IllegalArgumentException e) {}
}
public void testSortOrder() {
SortedBag bag = decorateBag(emptyBag, stringClass);
String one = "one";
String two = "two";
String three = "three";
bag.add(one);
bag.add(two);
bag.add(three);
assertEquals("first element", bag.first(), one);
assertEquals("last element", bag.last(), two);
Comparator c = bag.comparator();
assertTrue("natural order, so comparator should be null",
c == null);
}
}