Modified TestPredicatedBag, TestPredicatedSortedBag to use identically true predicate in makeBag override. Previous versions depended on the fact that TestBag only adds Strings.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131163 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Phil Steitz 2003-09-19 22:21:53 +00:00
parent c9aca369b7
commit 2daa7e098e
2 changed files with 47 additions and 26 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/TestPredicatedBag.java,v 1.1 2003/09/09 03:03:57 psteitz Exp $ * $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.2 2003/09/19 22:21:53 psteitz Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -65,13 +65,14 @@ import org.apache.commons.collections.Bag;
import org.apache.commons.collections.HashBag; import org.apache.commons.collections.HashBag;
import org.apache.commons.collections.TestBag; import org.apache.commons.collections.TestBag;
import org.apache.commons.collections.Predicate; import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.PredicateUtils;
/** /**
* Extension of {@link TestBag} for exercising the {@link PredicatedBag} * Extension of {@link TestBag} for exercising the {@link PredicatedBag}
* implementation. * implementation.
* *
* @since Commons Collections 3.0 * @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/09/09 03:03:57 $ * @version $Revision: 1.2 $ $Date: 2003/09/19 22:21:53 $
* *
* @author Phil Steitz * @author Phil Steitz
*/ */
@ -90,7 +91,9 @@ public class TestPredicatedBag extends TestBag {
junit.textui.TestRunner.main(testCaseName); junit.textui.TestRunner.main(testCaseName);
} }
protected Predicate getPredicate() { //--------------------------------------------------------------------------
protected Predicate stringPredicate() {
return new Predicate() { return new Predicate() {
public boolean evaluate(Object o) { public boolean evaluate(Object o) {
return o instanceof String; return o instanceof String;
@ -98,16 +101,24 @@ public class TestPredicatedBag extends TestBag {
}; };
} }
protected Predicate truePredicate = PredicateUtils.truePredicate();
protected Bag decorateBag(HashBag bag, Predicate predicate) { protected Bag decorateBag(HashBag bag, Predicate predicate) {
return PredicatedBag.decorate(bag, predicate); return PredicatedBag.decorate(bag, predicate);
} }
public Bag makeBag() { public Bag makeBag() {
return decorateBag(new HashBag(), getPredicate()); return decorateBag(new HashBag(), truePredicate);
} }
public Bag makeTestBag() {
return decorateBag(new HashBag(), stringPredicate());
}
//--------------------------------------------------------------------------
public void testlegalAddRemove() { public void testlegalAddRemove() {
Bag bag = makeBag(); Bag bag = makeTestBag();
assertEquals(0, bag.size()); assertEquals(0, bag.size());
Object[] els = new Object[] {"1", "3", "5", "7", "2", "4", "1"}; Object[] els = new Object[] {"1", "3", "5", "7", "2", "4", "1"};
for (int i = 0; i < els.length; i++) { for (int i = 0; i < els.length; i++) {
@ -124,7 +135,7 @@ public class TestPredicatedBag extends TestBag {
} }
public void testIllegalAdd() { public void testIllegalAdd() {
Bag bag = makeBag(); Bag bag = makeTestBag();
Integer i = new Integer(3); Integer i = new Integer(3);
try { try {
bag.add(i); bag.add(i);
@ -143,7 +154,7 @@ public class TestPredicatedBag extends TestBag {
elements.add(new Integer(3)); elements.add(new Integer(3));
elements.add("four"); elements.add("four");
try { try {
Bag bag = decorateBag(elements, getPredicate()); Bag bag = decorateBag(elements, stringPredicate());
fail("Bag contains an element that should fail the predicate."); fail("Bag contains an element that should fail the predicate.");
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // 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/TestPredicatedSortedBag.java,v 1.1 2003/09/09 03:03:57 psteitz Exp $ * $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.2 2003/09/19 22:21:53 psteitz Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -66,13 +66,14 @@ import org.apache.commons.collections.SortedBag;
import org.apache.commons.collections.TreeBag; import org.apache.commons.collections.TreeBag;
import org.apache.commons.collections.Predicate; import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.TestBag; import org.apache.commons.collections.TestBag;
import org.apache.commons.collections.PredicateUtils;
/** /**
* Extension of {@link TestBag} for exercising the {@link PredicatedSortedBag} * Extension of {@link TestBag} for exercising the {@link PredicatedSortedBag}
* implementation. * implementation.
* *
* @since Commons Collections 3.0 * @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/09/09 03:03:57 $ * @version $Revision: 1.2 $ $Date: 2003/09/19 22:21:53 $
* *
* @author Phil Steitz * @author Phil Steitz
*/ */
@ -85,22 +86,6 @@ public class TestPredicatedSortedBag extends TestBag {
super(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() { public static Test suite() {
return new TestSuite(TestPredicatedSortedBag.class); return new TestSuite(TestPredicatedSortedBag.class);
} }
@ -110,22 +95,47 @@ public class TestPredicatedSortedBag extends TestBag {
junit.textui.TestRunner.main(testCaseName); junit.textui.TestRunner.main(testCaseName);
} }
//--------------------------------------------------------------------------
protected Predicate stringPredicate() {
return new Predicate() {
public boolean evaluate(Object o) {
return o instanceof String;
}
};
}
protected Predicate truePredicate = PredicateUtils.truePredicate();
protected SortedBag decorateBag(SortedBag bag, Predicate predicate) {
return PredicatedSortedBag.decorate(bag, predicate);
}
public Bag makeBag() {
return decorateBag(emptyBag, truePredicate);
}
public Bag makeTestBag() {
return decorateBag(emptyBag, stringPredicate());
}
//--------------------------------------------------------------------------
public void testDecorate() { public void testDecorate() {
SortedBag bag = decorateBag(emptyBag, getPredicate()); SortedBag bag = decorateBag(emptyBag, stringPredicate());
SortedBag bag2 = ((PredicatedSortedBag) bag).getSortedBag(); SortedBag bag2 = ((PredicatedSortedBag) bag).getSortedBag();
try { try {
SortedBag bag3 = decorateBag(emptyBag, null); SortedBag bag3 = decorateBag(emptyBag, null);
fail("Expecting IllegalArgumentException for null predicate"); fail("Expecting IllegalArgumentException for null predicate");
} catch (IllegalArgumentException e) {} } catch (IllegalArgumentException e) {}
try { try {
SortedBag bag4 = decorateBag(nullBag, getPredicate()); SortedBag bag4 = decorateBag(nullBag, stringPredicate());
fail("Expecting IllegalArgumentException for null bag"); fail("Expecting IllegalArgumentException for null bag");
} catch (IllegalArgumentException e) {} } catch (IllegalArgumentException e) {}
} }
public void testSortOrder() { public void testSortOrder() {
PredicatedSortedBag bag = SortedBag bag = decorateBag(emptyBag, stringPredicate());
(PredicatedSortedBag) decorateBag(emptyBag, getPredicate());
String one = "one"; String one = "one";
String two = "two"; String two = "two";
String three = "three"; String three = "three";