KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > TestListUtils


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Arrays JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.List JavaDoc;
22
23 import junit.framework.Test;
24
25 import org.apache.commons.collections.list.PredicatedList;
26
27 /**
28  * Tests for ListUtils.
29  *
30  * @version $Revision: 1.19 $ $Date: 2004/06/02 22:12:14 $
31  *
32  * @author Stephen Colebourne
33  * @author Neil O'Toole
34  * @author Matthew Hawthorne
35  */

36 public class TestListUtils extends BulkTest {
37
38     public TestListUtils(String JavaDoc name) {
39         super(name);
40     }
41
42     public static Test suite() {
43         return BulkTest.makeSuite(TestListUtils.class);
44     }
45
46     public void testNothing() {
47     }
48     
49     public void testpredicatedList() {
50         Predicate predicate = new Predicate() {
51             public boolean evaluate(Object JavaDoc o) {
52                 return o instanceof String JavaDoc;
53             }
54         };
55         List JavaDoc list =
56         ListUtils.predicatedList(new ArrayStack(), predicate);
57         assertTrue("returned object should be a PredicatedList",
58             list instanceof PredicatedList);
59         try {
60             list =
61             ListUtils.predicatedList(new ArrayStack(), null);
62             fail("Expecting IllegalArgumentException for null predicate.");
63         } catch (IllegalArgumentException JavaDoc ex) {
64             // expected
65
}
66         try {
67             list =
68             ListUtils.predicatedList(null, predicate);
69             fail("Expecting IllegalArgumentException for null list.");
70         } catch (IllegalArgumentException JavaDoc ex) {
71             // expected
72
}
73     }
74
75     public void testLazyList() {
76         List JavaDoc list = ListUtils.lazyList(new ArrayList JavaDoc(), new Factory() {
77
78             private int index;
79
80             public Object JavaDoc create() {
81                 index++;
82                 return new Integer JavaDoc(index);
83             }
84         });
85
86         assertNotNull((Integer JavaDoc)list.get(5));
87         assertEquals(6, list.size());
88
89         assertNotNull((Integer JavaDoc)list.get(5));
90         assertEquals(6, list.size());
91     }
92
93     public void testEquals() {
94         Collection JavaDoc data = Arrays.asList( new String JavaDoc[] { "a", "b", "c" });
95         
96         List JavaDoc a = new ArrayList JavaDoc( data );
97         List JavaDoc b = new ArrayList JavaDoc( data );
98         
99         assertEquals(true, a.equals(b));
100         assertEquals(true, ListUtils.isEqualList(a, b));
101         a.clear();
102         assertEquals(false, ListUtils.isEqualList(a, b));
103         assertEquals(false, ListUtils.isEqualList(a, null));
104         assertEquals(false, ListUtils.isEqualList(null, b));
105         assertEquals(true, ListUtils.isEqualList(null, null));
106     }
107     
108     public void testHashCode() {
109         Collection JavaDoc data = Arrays.asList( new String JavaDoc[] { "a", "b", "c" });
110             
111         List JavaDoc a = new ArrayList JavaDoc( data );
112         List JavaDoc b = new ArrayList JavaDoc( data );
113         
114         assertEquals(true, a.hashCode() == b.hashCode());
115         assertEquals(true, a.hashCode() == ListUtils.hashCodeForList(a));
116         assertEquals(true, b.hashCode() == ListUtils.hashCodeForList(b));
117         assertEquals(true, ListUtils.hashCodeForList(a) == ListUtils.hashCodeForList(b));
118         a.clear();
119         assertEquals(false, ListUtils.hashCodeForList(a) == ListUtils.hashCodeForList(b));
120         assertEquals(0, ListUtils.hashCodeForList(null));
121     }
122     
123 }
124
Popular Tags