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.list;
017:
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: import junit.framework.Test;
022: import junit.framework.TestSuite;
023:
024: import org.apache.commons.collections.Predicate;
025: import org.apache.commons.collections.PredicateUtils;
026:
027: /**
028: * Extension of {@link TestList} for exercising the
029: * {@link PredicatedList} implementation.
030: *
031: * @since Commons Collections 3.0
032: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
033: *
034: * @author Phil Steitz
035: */
036: public class TestPredicatedList extends AbstractTestList {
037:
038: public TestPredicatedList(String testName) {
039: super (testName);
040: }
041:
042: public static Test suite() {
043: return new TestSuite(TestPredicatedList.class);
044: }
045:
046: public static void main(String args[]) {
047: String[] testCaseName = { TestPredicatedList.class.getName() };
048: junit.textui.TestRunner.main(testCaseName);
049: }
050:
051: //-------------------------------------------------------------------
052:
053: protected Predicate truePredicate = PredicateUtils.truePredicate();
054:
055: protected List decorateList(List list, Predicate predicate) {
056: return PredicatedList.decorate(list, predicate);
057: }
058:
059: public List makeEmptyList() {
060: return decorateList(new ArrayList(), truePredicate);
061: }
062:
063: public Object[] getFullElements() {
064: return new Object[] { "1", "3", "5", "7", "2", "4", "6" };
065: }
066:
067: //--------------------------------------------------------------------
068:
069: protected Predicate testPredicate = new Predicate() {
070: public boolean evaluate(Object o) {
071: return o instanceof String;
072: }
073: };
074:
075: public List makeTestList() {
076: return decorateList(new ArrayList(), testPredicate);
077: }
078:
079: public void testIllegalAdd() {
080: List list = makeTestList();
081: Integer i = new Integer(3);
082: try {
083: list.add(i);
084: fail("Integer should fail string predicate.");
085: } catch (IllegalArgumentException e) {
086: // expected
087: }
088: assertTrue("Collection shouldn't contain illegal element",
089: !list.contains(i));
090: }
091:
092: public void testIllegalAddAll() {
093: List list = makeTestList();
094: List elements = new ArrayList();
095: elements.add("one");
096: elements.add("two");
097: elements.add(new Integer(3));
098: elements.add("four");
099: try {
100: list.addAll(0, elements);
101: fail("Integer should fail string predicate.");
102: } catch (IllegalArgumentException e) {
103: // expected
104: }
105: assertTrue("List shouldn't contain illegal element", !list
106: .contains("one"));
107: assertTrue("List shouldn't contain illegal element", !list
108: .contains("two"));
109: assertTrue("List shouldn't contain illegal element", !list
110: .contains(new Integer(3)));
111: assertTrue("List shouldn't contain illegal element", !list
112: .contains("four"));
113: }
114:
115: public void testIllegalSet() {
116: List list = makeTestList();
117: try {
118: list.set(0, new Integer(3));
119: fail("Integer should fail string predicate.");
120: } catch (IllegalArgumentException e) {
121: // expected
122: }
123: }
124:
125: public void testLegalAddAll() {
126: List list = makeTestList();
127: list.add("zero");
128: List elements = new ArrayList();
129: elements.add("one");
130: elements.add("two");
131: elements.add("three");
132: list.addAll(1, elements);
133: assertTrue("List should contain legal element", list
134: .contains("zero"));
135: assertTrue("List should contain legal element", list
136: .contains("one"));
137: assertTrue("List should contain legal element", list
138: .contains("two"));
139: assertTrue("List should contain legal element", list
140: .contains("three"));
141: }
142:
143: public String getCompatibilityVersion() {
144: return "3.1";
145: }
146:
147: // public void testCreate() throws Exception {
148: // resetEmpty();
149: // writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/PredicatedList.emptyCollection.version3.1.obj");
150: // resetFull();
151: // writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/PredicatedList.fullCollection.version3.1.obj");
152: // }
153:
154: }
|