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.comparators;
017:
018: import java.io.Serializable;
019: import java.util.Comparator;
020: import java.util.LinkedList;
021: import java.util.List;
022:
023: import junit.framework.Test;
024: import junit.framework.TestSuite;
025:
026: /**
027: * Tests for ComparatorChain.
028: *
029: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
030: *
031: * @author Unknown
032: */
033: public class TestComparatorChain extends AbstractTestComparator {
034:
035: public TestComparatorChain(String testName) {
036: super (testName);
037: }
038:
039: public static Test suite() {
040: return new TestSuite(TestComparatorChain.class);
041: }
042:
043: public Comparator makeComparator() {
044: ComparatorChain chain = new ComparatorChain(
045: new ColumnComparator(0));
046: chain.addComparator(new ColumnComparator(1), true); // reverse the second column
047: chain.addComparator(new ColumnComparator(2), false);
048: return chain;
049: }
050:
051: public void testNoopComparatorChain() {
052: ComparatorChain chain = new ComparatorChain();
053: Integer i1 = new Integer(4);
054: Integer i2 = new Integer(6);
055: chain.addComparator(new ComparableComparator());
056:
057: int correctValue = i1.compareTo(i2);
058: assertTrue("Comparison returns the right order", chain.compare(
059: i1, i2) == correctValue);
060: }
061:
062: public void testBadNoopComparatorChain() {
063: ComparatorChain chain = new ComparatorChain();
064: Integer i1 = new Integer(4);
065: Integer i2 = new Integer(6);
066: try {
067: chain.compare(i1, i2);
068: fail("An exception should be thrown when a chain contains zero comparators.");
069: } catch (UnsupportedOperationException e) {
070:
071: }
072: }
073:
074: public void testListComparatorChain() {
075: List list = new LinkedList();
076: list.add(new ComparableComparator());
077: ComparatorChain chain = new ComparatorChain(list);
078: Integer i1 = new Integer(4);
079: Integer i2 = new Integer(6);
080:
081: int correctValue = i1.compareTo(i2);
082: assertTrue("Comparison returns the right order", chain.compare(
083: i1, i2) == correctValue);
084: }
085:
086: public void testBadListComparatorChain() {
087: List list = new LinkedList();
088: ComparatorChain chain = new ComparatorChain(list);
089: Integer i1 = new Integer(4);
090: Integer i2 = new Integer(6);
091: try {
092: chain.compare(i1, i2);
093: fail("An exception should be thrown when a chain contains zero comparators.");
094: } catch (UnsupportedOperationException e) {
095:
096: }
097: }
098:
099: public void testComparatorChainOnMinvaluedCompatator() {
100: // -1 * Integer.MIN_VALUE is less than 0,
101: // test that ComparatorChain handles this edge case correctly
102: ComparatorChain chain = new ComparatorChain();
103: chain.addComparator(new Comparator() {
104: public int compare(Object a, Object b) {
105: int result = ((Comparable) a).compareTo(b);
106: if (result < 0) {
107: return Integer.MIN_VALUE;
108: } else if (result > 0) {
109: return Integer.MAX_VALUE;
110: } else {
111: return 0;
112: }
113: }
114: }, true);
115:
116: assertTrue(chain.compare(new Integer(4), new Integer(5)) > 0);
117: assertTrue(chain.compare(new Integer(5), new Integer(4)) < 0);
118: assertTrue(chain.compare(new Integer(4), new Integer(4)) == 0);
119: }
120:
121: public List getComparableObjectsOrdered() {
122: List list = new LinkedList();
123: // this is the correct order assuming a
124: // "0th forward, 1st reverse, 2nd forward" sort
125: list.add(new PseudoRow(1, 2, 3));
126: list.add(new PseudoRow(2, 3, 5));
127: list.add(new PseudoRow(2, 2, 4));
128: list.add(new PseudoRow(2, 2, 8));
129: list.add(new PseudoRow(3, 1, 0));
130: list.add(new PseudoRow(4, 4, 4));
131: list.add(new PseudoRow(4, 4, 7));
132: return list;
133: }
134:
135: public static class PseudoRow implements Serializable {
136:
137: public int cols[] = new int[3];
138:
139: public PseudoRow(int col1, int col2, int col3) {
140: cols[0] = col1;
141: cols[1] = col2;
142: cols[2] = col3;
143: }
144:
145: public int getColumn(int colIndex) {
146: return cols[colIndex];
147: }
148:
149: public String toString() {
150: StringBuffer buf = new StringBuffer();
151: buf.append("[");
152: buf.append(cols[0]);
153: buf.append(",");
154: buf.append(cols[1]);
155: buf.append(",");
156: buf.append(cols[2]);
157: buf.append("]");
158: return buf.toString();
159: }
160:
161: public boolean equals(Object o) {
162: if (!(o instanceof PseudoRow)) {
163: return false;
164: }
165:
166: PseudoRow row = (PseudoRow) o;
167: if (getColumn(0) != row.getColumn(0)) {
168: return false;
169: }
170:
171: if (getColumn(1) != row.getColumn(1)) {
172: return false;
173: }
174:
175: if (getColumn(2) != row.getColumn(2)) {
176: return false;
177: }
178:
179: return true;
180: }
181:
182: }
183:
184: public static class ColumnComparator implements Comparator,
185: Serializable {
186:
187: protected int colIndex = 0;
188:
189: public ColumnComparator(int colIndex) {
190: this .colIndex = colIndex;
191: }
192:
193: public int compare(Object o1, Object o2) {
194:
195: int col1 = ((PseudoRow) o1).getColumn(colIndex);
196: int col2 = ((PseudoRow) o2).getColumn(colIndex);
197:
198: if (col1 > col2) {
199: return 1;
200: } else if (col1 < col2) {
201: return -1;
202: }
203:
204: return 0;
205: }
206:
207: public int hashCode() {
208: return colIndex;
209: }
210:
211: public boolean equals(Object that) {
212: if (that instanceof ColumnComparator) {
213: return colIndex == ((ColumnComparator) that).colIndex;
214: } else {
215: return false;
216: }
217: }
218:
219: private static final long serialVersionUID = -2284880866328872105L;
220: }
221: }
|