Added tests for PredicatedBag, PredicatedSortedBag

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131146 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Phil Steitz 2003-09-09 03:03:57 +00:00
parent abc443ff77
commit 9c62f649f4
3 changed files with 303 additions and 2 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/decorators/Attic/TestAll.java,v 1.7 2003/09/03 23:54:25 scolebourne 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.8 2003/09/09 03:03:57 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.7 $ $Date: 2003/09/03 23:54:25 $
* @version $Revision: 1.8 $ $Date: 2003/09/09 03:03:57 $
*
* @author Stephen Colebourne
*/
@ -98,6 +98,8 @@ public class TestAll extends TestCase {
suite.addTest(TestTransformedSortedBag.suite());
suite.addTest(TestTransformedSortedMap.suite());
suite.addTest(TestTransformedSortedSet.suite());
suite.addTest(TestPredicatedBag.suite());
suite.addTest(TestPredicatedSortedBag.suite());
return suite;
}

View File

@ -0,0 +1,158 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/TestPredicatedBag.java,v 1.1 2003/09/09 03:03:57 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;
import org.apache.commons.collections.Predicate;
/**
* Extension of {@link TestBag} for exercising the {@link PredicatedBag}
* implementation.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/09/09 03:03:57 $
*
* @author Phil Steitz
*/
public class TestPredicatedBag extends TestBag {
public TestPredicatedBag(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestPredicatedBag.class);
}
public static void main(String args[]) {
String[] testCaseName = { TestPredicatedBag.class.getName()};
junit.textui.TestRunner.main(testCaseName);
}
protected Predicate getPredicate() {
return new Predicate() {
public boolean evaluate(Object o) {
return o instanceof String;
}
};
}
protected Bag decorateBag(HashBag bag, Predicate predicate) {
return PredicatedBag.decorate(bag, predicate);
}
public Bag makeBag() {
return decorateBag(new HashBag(), getPredicate());
}
public void testlegalAddRemove() {
Bag bag = makeBag();
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 = makeBag();
Integer i = new Integer(3);
try {
bag.add(i);
fail("Integer should fail string predicate.");
} 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, getPredicate());
fail("Bag contains an element that should fail the predicate.");
} 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/TestPredicatedSortedBag.java,v 1.1 2003/09/09 03:03:57 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.Predicate;
import org.apache.commons.collections.TestBag;
/**
* Extension of {@link TestBag} for exercising the {@link PredicatedSortedBag}
* implementation.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/09/09 03:03:57 $
*
* @author Phil Steitz
*/
public class TestPredicatedSortedBag extends TestBag {
private SortedBag emptyBag = new TreeBag();
private SortedBag nullBag = null;
public TestPredicatedSortedBag(String testName) {
super(testName);
}
protected Predicate getPredicate() {
return new Predicate() {
public boolean evaluate(Object o) {
return o instanceof String;
}
};
}
protected SortedBag decorateBag(SortedBag bag, Predicate predicate) {
return PredicatedSortedBag.decorate(bag, predicate);
}
public Bag makeBag() {
return decorateBag(emptyBag, getPredicate());
}
public static Test suite() {
return new TestSuite(TestPredicatedSortedBag.class);
}
public static void main(String args[]) {
String[] testCaseName = { TestPredicatedSortedBag.class.getName()};
junit.textui.TestRunner.main(testCaseName);
}
public void testDecorate() {
SortedBag bag = decorateBag(emptyBag, getPredicate());
SortedBag bag2 = ((PredicatedSortedBag) bag).getSortedBag();
try {
SortedBag bag3 = decorateBag(emptyBag, null);
fail("Expecting IllegalArgumentException for null predicate");
} catch (IllegalArgumentException e) {}
try {
SortedBag bag4 = decorateBag(nullBag, getPredicate());
fail("Expecting IllegalArgumentException for null bag");
} catch (IllegalArgumentException e) {}
}
public void testSortOrder() {
PredicatedSortedBag bag =
(PredicatedSortedBag) decorateBag(emptyBag, getPredicate());
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);
}
}