Moved TestPredicatedCollection to decorators test package.

Added TestPredicatedBuffer, TestPredicatedList, TestPredicatedSet.
Modified TestBufferUtils, TestCollectionUtils, TestListUtils and TestSetUtils to eliminate BlkTests based on TestPredicatedCollection with simple factory tests, delegating (full coverage) functional testing to the decorator tests.


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131152 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Phil Steitz 2003-09-12 03:59:00 +00:00
parent df02176925
commit 67ea7c7742
9 changed files with 671 additions and 140 deletions

View File

@ -1,7 +1,7 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestBufferUtils.java,v 1.4 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/TestBufferUtils.java,v 1.5 2003/09/12 03:59:00 psteitz Exp $
* $Revision: 1.4 $ * $Revision: 1.5 $
* $Date: 2003/08/31 17:28:43 $ * $Date: 2003/09/12 03:59:00 $
* *
* ==================================================================== * ====================================================================
* *
@ -62,6 +62,7 @@ package org.apache.commons.collections;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import org.apache.commons.collections.decorators.PredicatedBuffer;
import junit.framework.Test; import junit.framework.Test;
@ -83,41 +84,27 @@ public class TestBufferUtils extends BulkTest {
public void testNothing() { public void testNothing() {
} }
public BulkTest bulkTestPredicatedBuffer() { public void testpredicatedBuffer() {
return new TestPredicatedCollection("") { Predicate predicate = new Predicate() {
public boolean evaluate(Object o) {
public Collection predicatedCollection() { return o instanceof String;
Predicate p = getPredicate();
return BufferUtils.predicatedBuffer(new ArrayStack(), p);
}
public BulkTest bulkTestAll() {
return new TestCollection("") {
public Collection makeCollection() {
return predicatedCollection();
}
public Collection makeConfirmedCollection() {
return new ArrayStack();
}
public Collection makeConfirmedFullCollection() {
ArrayStack list = new ArrayStack();
list.addAll(java.util.Arrays.asList(getFullElements()));
return list;
}
public Object[] getFullElements() {
return getFullNonNullStringElements();
}
public Object[] getOtherElements() {
return getOtherNonNullStringElements();
}
};
} }
}; };
Buffer buffer = BufferUtils.predicatedBuffer(new ArrayStack(), predicate);
assertTrue("returned object should be a PredicatedBuffer",
buffer instanceof PredicatedBuffer);
try {
buffer = BufferUtils.predicatedBuffer(new ArrayStack(), null);
fail("Expecting IllegalArgumentException for null predicate.");
} catch (IllegalArgumentException ex) {
// expected
}
try {
buffer = BufferUtils.predicatedBuffer(null, predicate);
fail("Expecting IllegalArgumentException for null buffer.");
} catch (IllegalArgumentException ex) {
// expected
}
} }

View File

@ -1,7 +1,7 @@
/* /*
* $Id: TestCollectionUtils.java,v 1.18 2003/09/05 02:16:33 psteitz Exp $ * $Id: TestCollectionUtils.java,v 1.19 2003/09/12 03:59:00 psteitz Exp $
* $Revision: 1.18 $ * $Revision: 1.19 $
* $Date: 2003/09/05 02:16:33 $ * $Date: 2003/09/12 03:59:00 $
* *
* ==================================================================== * ====================================================================
* *
@ -74,6 +74,8 @@ import junit.framework.Test;
import junit.framework.TestCase; import junit.framework.TestCase;
import junit.framework.TestSuite; import junit.framework.TestSuite;
import org.apache.commons.collections.decorators.PredicatedCollection;
/** /**
* Tests for CollectionUtils. * Tests for CollectionUtils.
* *
@ -81,7 +83,7 @@ import junit.framework.TestSuite;
* @author Matthew Hawthorne * @author Matthew Hawthorne
* @author Stephen Colebourne * @author Stephen Colebourne
* *
* @version $Revision: 1.18 $ $Date: 2003/09/05 02:16:33 $ * @version $Revision: 1.19 $ $Date: 2003/09/12 03:59:00 $
*/ */
public class TestCollectionUtils extends TestCase { public class TestCollectionUtils extends TestCase {
public TestCollectionUtils(String testName) { public TestCollectionUtils(String testName) {
@ -582,43 +584,34 @@ public class TestCollectionUtils extends TestCase {
assertEquals(new Integer(4), set.iterator().next()); assertEquals(new Integer(4), set.iterator().next());
} }
public void testPredicatedCollection() {
public BulkTest bulkTestPredicatedCollection1() { Predicate predicate = new Predicate() {
return new TestPredicatedCollection("") { public boolean evaluate(Object o) {
public Collection predicatedCollection() { return o instanceof String;
Predicate p = getPredicate();
return CollectionUtils.predicatedCollection(new ArrayList(), p);
}
public BulkTest bulkTestAll() {
return new TestCollection("") {
public Collection makeCollection() {
return predicatedCollection();
}
public Collection makeConfirmedCollection() {
return new ArrayList();
}
public Collection makeConfirmedFullCollection() {
ArrayList list = new ArrayList();
list.addAll(java.util.Arrays.asList(getFullElements()));
return list;
}
public Object[] getFullElements() {
return getFullNonNullStringElements();
}
public Object[] getOtherElements() {
return getOtherNonNullStringElements();
}
};
} }
}; };
Collection collection =
CollectionUtils.predicatedCollection(new ArrayList(), predicate);
assertTrue("returned object should be a PredicatedCollection",
collection instanceof PredicatedCollection);
try {
collection =
CollectionUtils.predicatedCollection(new ArrayList(), null);
fail("Expecting IllegalArgumentException for null predicate.");
} catch (IllegalArgumentException ex) {
// expected
}
try {
collection =
CollectionUtils.predicatedCollection(null, predicate);
fail("Expecting IllegalArgumentException for null collection.");
} catch (IllegalArgumentException ex) {
// expected
}
} }
public BulkTest bulkTestTypedCollection() { public BulkTest bulkTestTypedCollection() {
return new TestTypedCollection("") { return new TestTypedCollection("") {
public Collection typedCollection() { public Collection typedCollection() {

View File

@ -1,7 +1,7 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestListUtils.java,v 1.10 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/TestListUtils.java,v 1.11 2003/09/12 03:59:00 psteitz Exp $
* $Revision: 1.10 $ * $Revision: 1.11 $
* $Date: 2003/08/31 17:28:43 $ * $Date: 2003/09/12 03:59:00 $
* *
* ==================================================================== * ====================================================================
* *
@ -65,6 +65,8 @@ import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import org.apache.commons.collections.decorators.PredicatedList;
import junit.framework.Test; import junit.framework.Test;
/** /**
@ -87,31 +89,30 @@ public class TestListUtils extends BulkTest {
public void testNothing() { public void testNothing() {
} }
public BulkTest bulkTestPredicatedList() { public void testpredicatedList() {
return new TestPredicatedCollection("") { Predicate predicate = new Predicate() {
public boolean evaluate(Object o) {
public Collection predicatedCollection() { return o instanceof String;
Predicate p = getPredicate();
return ListUtils.predicatedList(new ArrayList(), p);
}
public BulkTest bulkTestAll() {
return new TestList("") {
public List makeEmptyList() {
return (List)predicatedCollection();
}
public Object[] getFullElements() {
return getFullNonNullStringElements();
}
public Object[] getOtherElements() {
return getOtherNonNullStringElements();
}
};
} }
}; };
List list =
ListUtils.predicatedList(new ArrayStack(), predicate);
assertTrue("returned object should be a PredicatedList",
list instanceof PredicatedList);
try {
list =
ListUtils.predicatedList(new ArrayStack(), null);
fail("Expecting IllegalArgumentException for null predicate.");
} catch (IllegalArgumentException ex) {
// expected
}
try {
list =
ListUtils.predicatedList(null, predicate);
fail("Expecting IllegalArgumentException for null list.");
} catch (IllegalArgumentException ex) {
// expected
}
} }
public BulkTest bulkTestTypedList() { public BulkTest bulkTestTypedList() {

View File

@ -1,7 +1,7 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestSetUtils.java,v 1.7 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/TestSetUtils.java,v 1.8 2003/09/12 03:59:00 psteitz Exp $
* $Revision: 1.7 $ * $Revision: 1.8 $
* $Date: 2003/08/31 17:28:43 $ * $Date: 2003/09/12 03:59:00 $
* *
* ==================================================================== * ====================================================================
* *
@ -65,6 +65,8 @@ import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.apache.commons.collections.decorators.PredicatedSet;
import junit.framework.Test; import junit.framework.Test;
@ -88,33 +90,30 @@ public class TestSetUtils extends BulkTest {
public void testNothing() { public void testNothing() {
} }
public BulkTest bulkTestPredicatedSet() { public void testpredicatedSet() {
return new TestPredicatedCollection("") { Predicate predicate = new Predicate() {
public boolean evaluate(Object o) {
public Collection predicatedCollection() { return o instanceof String;
Predicate p = getPredicate();
return SetUtils.predicatedSet(new HashSet(), p);
}
public BulkTest bulkTestAll() {
return new TestSet("") {
public Set makeEmptySet() {
return (Set)predicatedCollection();
}
public Object[] getFullElements() {
return getFullNonNullStringElements();
}
public Object[] getOtherElements() {
return getOtherNonNullStringElements();
}
};
} }
}; };
Set set = SetUtils.predicatedSet(new HashSet(), predicate);
assertTrue("returned object should be a PredicatedSet",
set instanceof PredicatedSet);
try {
set = SetUtils.predicatedSet(new HashSet(), null);
fail("Expecting IllegalArgumentException for null predicate.");
} catch (IllegalArgumentException ex) {
// expected
}
try {
set = SetUtils.predicatedSet(null, predicate);
fail("Expecting IllegalArgumentException for null set.");
} catch (IllegalArgumentException ex) {
// expected
}
} }
public BulkTest bulkTestTypedSet() { public BulkTest bulkTestTypedSet() {
return new TestTypedCollection("") { return new TestTypedCollection("") {

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.9 2003/09/09 22:28:36 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.10 2003/09/12 03:59:00 psteitz Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -65,7 +65,7 @@ import junit.framework.TestSuite;
* Entry point for all collections decorators tests. * Entry point for all collections decorators tests.
* *
* @since Commons Collections 3.0 * @since Commons Collections 3.0
* @version $Revision: 1.9 $ $Date: 2003/09/09 22:28:36 $ * @version $Revision: 1.10 $ $Date: 2003/09/12 03:59:00 $
* *
* @author Stephen Colebourne * @author Stephen Colebourne
*/ */
@ -100,6 +100,10 @@ public class TestAll extends TestCase {
suite.addTest(TestTransformedSortedSet.suite()); suite.addTest(TestTransformedSortedSet.suite());
suite.addTest(TestPredicatedBag.suite()); suite.addTest(TestPredicatedBag.suite());
suite.addTest(TestPredicatedSortedBag.suite()); suite.addTest(TestPredicatedSortedBag.suite());
suite.addTest(TestPredicatedCollection.suite());
suite.addTest(TestPredicatedBuffer.suite());
suite.addTest(TestPredicatedList.suite());
suite.addTest(TestPredicatedSet.suite());
return suite; return suite;
} }

View File

@ -0,0 +1,145 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/TestPredicatedBuffer.java,v 1.1 2003/09/12 03:59:00 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.ArrayList;
import java.util.Collection;
import org.apache.commons.collections.ArrayStack;
import org.apache.commons.collections.Buffer;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.BufferUnderflowException;
/**
* Extension of {@link TestPredicatedCollection} for exercising the
* {@link PredicatedBuffer} implementation.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/09/12 03:59:00 $
*
* @author Phil Steitz
*/
public class TestPredicatedBuffer extends TestPredicatedCollection {
public TestPredicatedBuffer(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestPredicatedBuffer.class);
}
public static void main(String args[]) {
String[] testCaseName = { TestPredicatedBuffer.class.getName()};
junit.textui.TestRunner.main(testCaseName);
}
//---------------------------------------------------------------
protected Buffer decorateBuffer(Buffer buffer, Predicate predicate) {
return PredicatedBuffer.decorate(buffer, predicate);
}
public Collection makeCollection() {
return decorateBuffer(new ArrayStack(), truePredicate);
}
public Collection makeConfirmedCollection() {
return new ArrayStack();
}
public Collection makeConfirmedFullCollection() {
ArrayStack list = new ArrayStack();
list.addAll(java.util.Arrays.asList(getFullElements()));
return list;
}
//------------------------------------------------------------
public Buffer makeTestBuffer() {
return decorateBuffer(new ArrayStack(), testPredicate);
}
public void testGet() {
Buffer buffer = makeTestBuffer();
try {
Object o = buffer.get();
fail("Expecting BufferUnderflowException");
} catch (BufferUnderflowException ex) {
// expected
}
buffer.add("one");
buffer.add("two");
buffer.add("three");
assertEquals("Buffer get", buffer.get(), "three");
}
public void testRemove() {
Buffer buffer = makeTestBuffer();
buffer.add("one");
assertEquals("Buffer get", buffer.remove(), "one");
try {
buffer.remove();
fail("Expecting BufferUnderflowException");
} catch (BufferUnderflowException ex) {
// expected
}
}
}

View File

@ -51,35 +51,90 @@
* information on the Apache Software Foundation, please see * information on the Apache Software Foundation, please see
* <http://www.apache.org/>. * <http://www.apache.org/>.
*/ */
package org.apache.commons.collections; package org.apache.commons.collections.decorators;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Arrays;
import org.apache.commons.collections.TestCollection;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.PredicateUtils;
import junit.framework.Test;
import junit.framework.TestSuite;
/** /**
* Extension of {@link TestCollection} for exercising the
* {@link PredicatedCollection} implementation.
* *
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/09/12 03:59:00 $
*
* @author Phil Steitz
*/ */
public abstract class TestPredicatedCollection extends BulkTest { public class TestPredicatedCollection extends TestCollection {
public TestPredicatedCollection(String name) { public TestPredicatedCollection(String name) {
super(name); super(name);
} }
public static Test suite() {
return new TestSuite(TestPredicatedCollection.class);
}
protected abstract Collection predicatedCollection(); public static void main(String args[]) {
String[] testCaseName = { TestPredicatedCollection.class.getName()};
junit.textui.TestRunner.main(testCaseName);
}
protected Predicate getPredicate() { //------------------------------------------------------------------------
return new Predicate() {
protected Predicate truePredicate = PredicateUtils.truePredicate();
protected Collection decorateCollection(Collection collection,
Predicate predicate) {
return PredicatedCollection.decorate(collection, predicate);
}
public Collection makeCollection() {
return decorateCollection(new ArrayList(), truePredicate);
}
public Collection makeConfirmedCollection() {
return new ArrayList();
}
protected Object[] getFullElements() {
return new Object[] {"1", "3", "5", "7", "2", "4", "6"};
}
protected Collection makeFullCollection() {
List list = new ArrayList();
list.addAll(Arrays.asList(getFullElements()));
return decorateCollection(list, truePredicate);
}
protected Collection makeConfirmedFullCollection() {
List list = new ArrayList();
list.addAll(Arrays.asList(getFullElements()));
return list;
}
//-----------------------------------------------------------------
protected Predicate testPredicate =
new Predicate() {
public boolean evaluate(Object o) { public boolean evaluate(Object o) {
return o instanceof String; return o instanceof String;
} }
}; };
public Collection makeTestCollection() {
return decorateCollection(new ArrayList(), testPredicate);
} }
public void testIllegalAdd() { public void testIllegalAdd() {
Collection c = predicatedCollection(); Collection c = makeTestCollection();
Integer i = new Integer(3); Integer i = new Integer(3);
try { try {
c.add(i); c.add(i);
@ -91,9 +146,8 @@ public abstract class TestPredicatedCollection extends BulkTest {
!c.contains(i)); !c.contains(i));
} }
public void testIllegalAddAll() { public void testIllegalAddAll() {
Collection c = predicatedCollection(); Collection c = makeTestCollection();
List elements = new ArrayList(); List elements = new ArrayList();
elements.add("one"); elements.add("one");
elements.add("two"); elements.add("two");

View File

@ -0,0 +1,185 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/TestPredicatedList.java,v 1.1 2003/09/12 03:59:00 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.ArrayList;
import java.util.List;
import org.apache.commons.collections.TestList;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.PredicateUtils;
/**
* Extension of {@link TestList} for exercising the
* {@link PredicatedList} implementation.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/09/12 03:59:00 $
*
* @author Phil Steitz
*/
public class TestPredicatedList extends TestList{
public TestPredicatedList(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestPredicatedList.class);
}
public static void main(String args[]) {
String[] testCaseName = { TestPredicatedList.class.getName()};
junit.textui.TestRunner.main(testCaseName);
}
//-------------------------------------------------------------------
protected Predicate truePredicate = PredicateUtils.truePredicate();
protected List decorateList(List list, Predicate predicate) {
return PredicatedList.decorate(list, predicate);
}
public List makeEmptyList() {
return decorateList(new ArrayList(), truePredicate);
}
protected Object[] getFullElements() {
return new Object[] {"1", "3", "5", "7", "2", "4", "6"};
}
//--------------------------------------------------------------------
protected Predicate testPredicate =
new Predicate() {
public boolean evaluate(Object o) {
return o instanceof String;
}
};
public List makeTestList() {
return decorateList(new ArrayList(), testPredicate);
}
public void testIllegalAdd() {
List list = makeTestList();
Integer i = new Integer(3);
try {
list.add(i);
fail("Integer should fail string predicate.");
} catch (IllegalArgumentException e) {
// expected
}
assertTrue("Collection shouldn't contain illegal element",
!list.contains(i));
}
public void testIllegalAddAll() {
List list = makeTestList();
List elements = new ArrayList();
elements.add("one");
elements.add("two");
elements.add(new Integer(3));
elements.add("four");
try {
list.addAll(0,elements);
fail("Integer should fail string predicate.");
} catch (IllegalArgumentException e) {
// expected
}
assertTrue("List shouldn't contain illegal element",
!list.contains("one"));
assertTrue("List shouldn't contain illegal element",
!list.contains("two"));
assertTrue("List shouldn't contain illegal element",
!list.contains(new Integer(3)));
assertTrue("List shouldn't contain illegal element",
!list.contains("four"));
}
public void testIllegalSet() {
List list = makeTestList();
try {
list.set(0,new Integer(3));
fail("Integer should fail string predicate.");
} catch (IllegalArgumentException e) {
// expected
}
}
public void testLegalAddAll() {
List list = makeTestList();
list.add("zero");
List elements = new ArrayList();
elements.add("one");
elements.add("two");
elements.add("three");
list.addAll(1,elements);
assertTrue("List should contain legal element",
list.contains("zero"));
assertTrue("List should contain legal element",
list.contains("one"));
assertTrue("List should contain legal element",
list.contains("two"));
assertTrue("List should contain legal element",
list.contains("three"));
}
}

View File

@ -0,0 +1,163 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/TestPredicatedSet.java,v 1.1 2003/09/12 03:59:00 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.HashSet;
import java.util.Set;
import org.apache.commons.collections.TestSet;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.PredicateUtils;
/**
* Extension of {@link TestSet} for exercising the
* {@link PredicatedSet} implementation.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/09/12 03:59:00 $
*
* @author Phil Steitz
*/
public class TestPredicatedSet extends TestSet{
public TestPredicatedSet(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestPredicatedSet.class);
}
public static void main(String args[]) {
String[] testCaseName = { TestPredicatedSet.class.getName()};
junit.textui.TestRunner.main(testCaseName);
}
//-------------------------------------------------------------------
protected Predicate truePredicate = PredicateUtils.truePredicate();
protected Set decorateSet(Set set, Predicate predicate) {
return PredicatedSet.decorate(set, predicate);
}
public Set makeEmptySet() {
return decorateSet(new HashSet(), truePredicate);
}
protected Object[] getFullElements() {
return new Object[] {"1", "3", "5", "7", "2", "4", "6"};
}
//--------------------------------------------------------------------
protected Predicate testPredicate =
new Predicate() {
public boolean evaluate(Object o) {
return o instanceof String;
}
};
public Set makeTestSet() {
return decorateSet(new HashSet(), testPredicate);
}
public void testGetSet() {
Set set = makeTestSet();
assertTrue("returned set should not be null",
((PredicatedSet) set).getSet() != null);
}
public void testIllegalAdd() {
Set set = makeTestSet();
Integer i = new Integer(3);
try {
set.add(i);
fail("Integer should fail string predicate.");
} catch (IllegalArgumentException e) {
// expected
}
assertTrue("Collection shouldn't contain illegal element",
!set.contains(i));
}
public void testIllegalAddAll() {
Set set = makeTestSet();
Set elements = new HashSet();
elements.add("one");
elements.add("two");
elements.add(new Integer(3));
elements.add("four");
try {
set.addAll(elements);
fail("Integer should fail string predicate.");
} catch (IllegalArgumentException e) {
// expected
}
assertTrue("Set shouldn't contain illegal element",
!set.contains("one"));
assertTrue("Set shouldn't contain illegal element",
!set.contains("two"));
assertTrue("Set shouldn't contain illegal element",
!set.contains(new Integer(3)));
assertTrue("Set shouldn't contain illegal element",
!set.contains("four"));
}
}