001: /*
002: * Copyright 2003-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.collection;
017:
018: import java.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.Collection;
021: import java.util.List;
022:
023: import junit.framework.Test;
024: import junit.framework.TestSuite;
025:
026: import org.apache.commons.collections.Predicate;
027: import org.apache.commons.collections.PredicateUtils;
028:
029: /**
030: * Extension of {@link TestCollection} for exercising the
031: * {@link PredicatedCollection} implementation.
032: *
033: * @since Commons Collections 3.0
034: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
035: *
036: * @author Phil Steitz
037: */
038: public class TestPredicatedCollection extends AbstractTestCollection {
039:
040: public TestPredicatedCollection(String name) {
041: super (name);
042: }
043:
044: public static Test suite() {
045: return new TestSuite(TestPredicatedCollection.class);
046: }
047:
048: public static void main(String args[]) {
049: String[] testCaseName = { TestPredicatedCollection.class
050: .getName() };
051: junit.textui.TestRunner.main(testCaseName);
052: }
053:
054: //------------------------------------------------------------------------
055:
056: protected Predicate truePredicate = PredicateUtils.truePredicate();
057:
058: protected Collection decorateCollection(Collection collection,
059: Predicate predicate) {
060: return PredicatedCollection.decorate(collection, predicate);
061: }
062:
063: public Collection makeCollection() {
064: return decorateCollection(new ArrayList(), truePredicate);
065: }
066:
067: public Collection makeConfirmedCollection() {
068: return new ArrayList();
069: }
070:
071: public Object[] getFullElements() {
072: return new Object[] { "1", "3", "5", "7", "2", "4", "6" };
073: }
074:
075: public Collection makeFullCollection() {
076: List list = new ArrayList();
077: list.addAll(Arrays.asList(getFullElements()));
078: return decorateCollection(list, truePredicate);
079: }
080:
081: public Collection makeConfirmedFullCollection() {
082: List list = new ArrayList();
083: list.addAll(Arrays.asList(getFullElements()));
084: return list;
085: }
086:
087: //-----------------------------------------------------------------
088: protected Predicate testPredicate = new Predicate() {
089: public boolean evaluate(Object o) {
090: return o instanceof String;
091: }
092: };
093:
094: public Collection makeTestCollection() {
095: return decorateCollection(new ArrayList(), testPredicate);
096: }
097:
098: public void testIllegalAdd() {
099: Collection c = makeTestCollection();
100: Integer i = new Integer(3);
101: try {
102: c.add(i);
103: fail("Integer should fail string predicate.");
104: } catch (IllegalArgumentException e) {
105: // expected
106: }
107: assertTrue("Collection shouldn't contain illegal element", !c
108: .contains(i));
109: }
110:
111: public void testIllegalAddAll() {
112: Collection c = makeTestCollection();
113: List elements = new ArrayList();
114: elements.add("one");
115: elements.add("two");
116: elements.add(new Integer(3));
117: elements.add("four");
118: try {
119: c.addAll(elements);
120: fail("Integer should fail string predicate.");
121: } catch (IllegalArgumentException e) {
122: // expected
123: }
124: assertTrue("Collection shouldn't contain illegal element", !c
125: .contains("one"));
126: assertTrue("Collection shouldn't contain illegal element", !c
127: .contains("two"));
128: assertTrue("Collection shouldn't contain illegal element", !c
129: .contains(new Integer(3)));
130: assertTrue("Collection shouldn't contain illegal element", !c
131: .contains("four"));
132: }
133:
134: public String getCompatibilityVersion() {
135: return "3.1";
136: }
137:
138: // public void testCreate() throws Exception {
139: // resetEmpty();
140: // writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/PredicatedCollection.emptyCollection.version3.1.obj");
141: // resetFull();
142: // writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/PredicatedCollection.fullCollection.version3.1.obj");
143: // }
144:
145: }
|