001: /*
002: * Copyright 2001-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;
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:
025: import org.apache.commons.collections.list.PredicatedList;
026:
027: /**
028: * Tests for ListUtils.
029: *
030: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
031: *
032: * @author Stephen Colebourne
033: * @author Neil O'Toole
034: * @author Matthew Hawthorne
035: */
036: public class TestListUtils extends BulkTest {
037:
038: private static final String a = "a";
039: private static final String b = "b";
040: private static final String c = "c";
041: private static final String d = "d";
042: private static final String e = "e";
043: private static final String x = "x";
044:
045: private String[] fullArray;
046: private List fullList;
047:
048: public TestListUtils(String name) {
049: super (name);
050: }
051:
052: public static Test suite() {
053: return BulkTest.makeSuite(TestListUtils.class);
054: }
055:
056: public void setUp() {
057: fullArray = new String[] { a, b, c, d, e };
058: fullList = new ArrayList(Arrays.asList(fullArray));
059: }
060:
061: public void testNothing() {
062: }
063:
064: public void testpredicatedList() {
065: Predicate predicate = new Predicate() {
066: public boolean evaluate(Object o) {
067: return o instanceof String;
068: }
069: };
070: List list = ListUtils.predicatedList(new ArrayStack(),
071: predicate);
072: assertTrue("returned object should be a PredicatedList",
073: list instanceof PredicatedList);
074: try {
075: list = ListUtils.predicatedList(new ArrayStack(), null);
076: fail("Expecting IllegalArgumentException for null predicate.");
077: } catch (IllegalArgumentException ex) {
078: // expected
079: }
080: try {
081: list = ListUtils.predicatedList(null, predicate);
082: fail("Expecting IllegalArgumentException for null list.");
083: } catch (IllegalArgumentException ex) {
084: // expected
085: }
086: }
087:
088: public void testLazyList() {
089: List list = ListUtils.lazyList(new ArrayList(), new Factory() {
090:
091: private int index;
092:
093: public Object create() {
094: index++;
095: return new Integer(index);
096: }
097: });
098:
099: assertNotNull((Integer) list.get(5));
100: assertEquals(6, list.size());
101:
102: assertNotNull((Integer) list.get(5));
103: assertEquals(6, list.size());
104: }
105:
106: public void testEquals() {
107: Collection data = Arrays.asList(new String[] { "a", "b", "c" });
108:
109: List a = new ArrayList(data);
110: List b = new ArrayList(data);
111:
112: assertEquals(true, a.equals(b));
113: assertEquals(true, ListUtils.isEqualList(a, b));
114: a.clear();
115: assertEquals(false, ListUtils.isEqualList(a, b));
116: assertEquals(false, ListUtils.isEqualList(a, null));
117: assertEquals(false, ListUtils.isEqualList(null, b));
118: assertEquals(true, ListUtils.isEqualList(null, null));
119: }
120:
121: public void testHashCode() {
122: Collection data = Arrays.asList(new String[] { "a", "b", "c" });
123:
124: List a = new ArrayList(data);
125: List b = new ArrayList(data);
126:
127: assertEquals(true, a.hashCode() == b.hashCode());
128: assertEquals(true, a.hashCode() == ListUtils.hashCodeForList(a));
129: assertEquals(true, b.hashCode() == ListUtils.hashCodeForList(b));
130: assertEquals(true, ListUtils.hashCodeForList(a) == ListUtils
131: .hashCodeForList(b));
132: a.clear();
133: assertEquals(false, ListUtils.hashCodeForList(a) == ListUtils
134: .hashCodeForList(b));
135: assertEquals(0, ListUtils.hashCodeForList(null));
136: }
137:
138: public void testRetainAll() {
139: List sub = new ArrayList();
140: sub.add(a);
141: sub.add(b);
142: sub.add(x);
143:
144: List retained = ListUtils.retainAll(fullList, sub);
145: assertTrue(retained.size() == 2);
146: sub.remove(x);
147: assertTrue(retained.equals(sub));
148: fullList.retainAll(sub);
149: assertTrue(retained.equals(fullList));
150:
151: try {
152: List list = ListUtils.retainAll(null, null);
153: fail("expecting NullPointerException");
154: } catch (NullPointerException npe) {
155: } // this is what we want
156: }
157:
158: public void testRemoveAll() {
159: List sub = new ArrayList();
160: sub.add(a);
161: sub.add(b);
162: sub.add(x);
163:
164: List remainder = ListUtils.removeAll(fullList, sub);
165: assertTrue(remainder.size() == 3);
166: fullList.removeAll(sub);
167: assertTrue(remainder.equals(fullList));
168:
169: try {
170: List list = ListUtils.removeAll(null, null);
171: fail("expecting NullPointerException");
172: } catch (NullPointerException npe) {
173: } // this is what we want
174: }
175:
176: }
|