KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > comparators > TestComparatorChain


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.comparators;
17
18 import java.io.Serializable JavaDoc;
19 import java.util.Comparator JavaDoc;
20 import java.util.LinkedList JavaDoc;
21 import java.util.List JavaDoc;
22
23 import junit.framework.Test;
24 import junit.framework.TestSuite;
25
26 /**
27  * Tests for ComparatorChain.
28  *
29  * @version $Revision: 1.10 $ $Date: 2004/02/18 01:20:34 $
30  *
31  * @author Unknown
32  */

33 public class TestComparatorChain extends AbstractTestComparator {
34
35     public TestComparatorChain(String JavaDoc testName) {
36         super(testName);
37     }
38
39     public static Test suite() {
40         return new TestSuite(TestComparatorChain.class);
41     }
42
43     public Comparator JavaDoc makeComparator() {
44         ComparatorChain chain = new ComparatorChain(new ColumnComparator(0));
45         chain.addComparator(new ColumnComparator(1),true); // reverse the second column
46
chain.addComparator(new ColumnComparator(2),false);
47         return chain;
48     }
49
50     public void testNoopComparatorChain() {
51         ComparatorChain chain = new ComparatorChain();
52         Integer JavaDoc i1 = new Integer JavaDoc(4);
53         Integer JavaDoc i2 = new Integer JavaDoc(6);
54         chain.addComparator(new ComparableComparator());
55
56         int correctValue = i1.compareTo(i2);
57         assertTrue("Comparison returns the right order",chain.compare(i1,i2) == correctValue);
58     }
59
60     public void testBadNoopComparatorChain() {
61         ComparatorChain chain = new ComparatorChain();
62         Integer JavaDoc i1 = new Integer JavaDoc(4);
63         Integer JavaDoc i2 = new Integer JavaDoc(6);
64         try {
65             chain.compare(i1,i2);
66             fail("An exception should be thrown when a chain contains zero comparators.");
67         } catch (UnsupportedOperationException JavaDoc e) {
68
69         }
70     }
71
72     public void testListComparatorChain() {
73         List JavaDoc list = new LinkedList JavaDoc();
74         list.add(new ComparableComparator());
75         ComparatorChain chain = new ComparatorChain(list);
76         Integer JavaDoc i1 = new Integer JavaDoc(4);
77         Integer JavaDoc i2 = new Integer JavaDoc(6);
78
79         int correctValue = i1.compareTo(i2);
80         assertTrue("Comparison returns the right order",chain.compare(i1,i2) == correctValue);
81     }
82
83     public void testBadListComparatorChain() {
84         List JavaDoc list = new LinkedList JavaDoc();
85         ComparatorChain chain = new ComparatorChain(list);
86         Integer JavaDoc i1 = new Integer JavaDoc(4);
87         Integer JavaDoc i2 = new Integer JavaDoc(6);
88         try {
89             chain.compare(i1,i2);
90             fail("An exception should be thrown when a chain contains zero comparators.");
91         } catch (UnsupportedOperationException JavaDoc e) {
92
93         }
94     }
95
96
97     public void testComparatorChainOnMinvaluedCompatator() {
98         // -1 * Integer.MIN_VALUE is less than 0,
99
// test that ComparatorChain handles this edge case correctly
100
ComparatorChain chain = new ComparatorChain();
101         chain.addComparator(
102             new Comparator JavaDoc() {
103                 public int compare(Object JavaDoc a, Object JavaDoc b) {
104                     int result = ((Comparable JavaDoc)a).compareTo(b);
105                     if(result < 0) {
106                         return Integer.MIN_VALUE;
107                     } else if(result > 0) {
108                         return Integer.MAX_VALUE;
109                     } else {
110                         return 0;
111                     }
112                 }
113             }, true);
114
115         assertTrue(chain.compare(new Integer JavaDoc(4), new Integer JavaDoc(5)) > 0);
116         assertTrue(chain.compare(new Integer JavaDoc(5), new Integer JavaDoc(4)) < 0);
117         assertTrue(chain.compare(new Integer JavaDoc(4), new Integer JavaDoc(4)) == 0);
118     }
119
120     public List JavaDoc getComparableObjectsOrdered() {
121         List JavaDoc list = new LinkedList JavaDoc();
122         // this is the correct order assuming a
123
// "0th forward, 1st reverse, 2nd forward" sort
124
list.add(new PseudoRow(1,2,3));
125         list.add(new PseudoRow(2,3,5));
126         list.add(new PseudoRow(2,2,4));
127         list.add(new PseudoRow(2,2,8));
128         list.add(new PseudoRow(3,1,0));
129         list.add(new PseudoRow(4,4,4));
130         list.add(new PseudoRow(4,4,7));
131         return list;
132     }
133
134     public static class PseudoRow implements Serializable JavaDoc {
135
136         public int cols[] = new int[3];
137
138         public PseudoRow(int col1, int col2, int col3) {
139             cols[0] = col1;
140             cols[1] = col2;
141             cols[2] = col3;
142         }
143
144         public int getColumn(int colIndex) {
145             return cols[colIndex];
146         }
147
148         public String JavaDoc toString() {
149             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
150             buf.append("[");
151             buf.append(cols[0]);
152             buf.append(",");
153             buf.append(cols[1]);
154             buf.append(",");
155             buf.append(cols[2]);
156             buf.append("]");
157             return buf.toString();
158         }
159
160         public boolean equals(Object JavaDoc o) {
161             if (!(o instanceof PseudoRow)) {
162                 return false;
163             }
164
165             PseudoRow row = (PseudoRow) o;
166             if (getColumn(0) != row.getColumn(0)) {
167                 return false;
168             }
169
170             if (getColumn(1) != row.getColumn(1)) {
171                 return false;
172             }
173             
174             if (getColumn(2) != row.getColumn(2)) {
175                 return false;
176             }
177
178             return true;
179         }
180
181     }
182
183     public static class ColumnComparator implements Comparator JavaDoc,Serializable JavaDoc {
184
185         protected int colIndex = 0;
186
187         public ColumnComparator(int colIndex) {
188             this.colIndex = colIndex;
189         }
190
191         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
192
193             int col1 = ( (PseudoRow) o1).getColumn(colIndex);
194             int col2 = ( (PseudoRow) o2).getColumn(colIndex);
195
196             if (col1 > col2) {
197                 return 1;
198             } else if (col1 < col2) {
199                 return -1;
200             }
201
202             return 0;
203         }
204         
205         public int hashCode() {
206             return colIndex;
207         }
208         
209         public boolean equals(Object JavaDoc that) {
210             if(that instanceof ColumnComparator) {
211                 return colIndex == ((ColumnComparator)that).colIndex;
212             } else {
213                 return false;
214             }
215         }
216         
217         private static final long serialVersionUID = -2284880866328872105L;
218     }
219 }
220
Popular Tags