KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Arrays JavaDoc;
19 import java.util.Comparator JavaDoc;
20 import java.util.LinkedList JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Random JavaDoc;
23
24 import junit.framework.Test;
25 import junit.framework.TestCase;
26 import junit.framework.TestSuite;
27
28 /**
29  * Test class for FixedOrderComparator.
30  *
31  * @version $Revision: 1.6 $ $Date: 2004/02/18 01:20:34 $
32  *
33  * @author David Leppik
34  * @author Stephen Colebourne
35  */

36 public class TestFixedOrderComparator extends TestCase {
37
38
39     /**
40      * Top cities of the world, by population including metro areas.
41      */

42     public static final String JavaDoc topCities[] = new String JavaDoc[] {
43         "Tokyo",
44         "Mexico City",
45         "Mumbai",
46         "Sao Paulo",
47         "New York",
48         "Shanghai",
49         "Lagos",
50         "Los Angeles",
51         "Calcutta",
52         "Buenos Aires"
53     };
54
55     //
56
// Initialization and busywork
57
//
58

59     public TestFixedOrderComparator(String JavaDoc name) {
60         super(name);
61     }
62
63     public static Test suite() {
64         return new TestSuite(TestFixedOrderComparator.class);
65     }
66
67     public static void main(String JavaDoc args[]) {
68         junit.textui.TestRunner.run(suite());
69     }
70
71     //
72
// Set up and tear down
73
//
74

75
76
77     //
78
// The tests
79
//
80

81     /**
82      * Tests that the constructor plus add method compares items properly.
83      */

84     public void testConstructorPlusAdd() {
85         FixedOrderComparator comparator = new FixedOrderComparator();
86         for (int i = 0; i < topCities.length; i++) {
87             comparator.add(topCities[i]);
88         }
89         String JavaDoc[] keys = (String JavaDoc[]) topCities.clone();
90         assertComparatorYieldsOrder(keys, comparator);
91     }
92
93     /**
94      * Tests that the array constructor compares items properly.
95      */

96     public void testArrayConstructor() {
97         String JavaDoc[] keys = (String JavaDoc[]) topCities.clone();
98         String JavaDoc[] topCitiesForTest = (String JavaDoc[]) topCities.clone();
99         FixedOrderComparator comparator = new FixedOrderComparator(topCitiesForTest);
100         assertComparatorYieldsOrder(keys, comparator);
101         // test that changing input after constructor has no effect
102
topCitiesForTest[0] = "Brighton";
103         assertComparatorYieldsOrder(keys, comparator);
104     }
105
106     /**
107      * Tests the list constructor.
108      */

109     public void testListConstructor() {
110         String JavaDoc[] keys = (String JavaDoc[]) topCities.clone();
111         List JavaDoc topCitiesForTest = new LinkedList JavaDoc(Arrays.asList(topCities));
112         FixedOrderComparator comparator = new FixedOrderComparator(topCitiesForTest);
113         assertComparatorYieldsOrder(keys, comparator);
114         // test that changing input after constructor has no effect
115
topCitiesForTest.set(0, "Brighton");
116         assertComparatorYieldsOrder(keys, comparator);
117     }
118
119     /**
120      * Tests addAsEqual method.
121      */

122     public void testAddAsEqual() {
123         FixedOrderComparator comparator = new FixedOrderComparator(topCities);
124         comparator.addAsEqual("New York", "Minneapolis");
125         assertEquals(0, comparator.compare("New York", "Minneapolis"));
126         assertEquals(-1, comparator.compare("Tokyo", "Minneapolis"));
127         assertEquals(1, comparator.compare("Shanghai", "Minneapolis"));
128     }
129
130     /**
131      * Tests whether or not updates are disabled after a comparison is made.
132      */

133     public void testLock() {
134         FixedOrderComparator comparator = new FixedOrderComparator(topCities);
135         assertEquals(false, comparator.isLocked());
136         comparator.compare("New York", "Tokyo");
137         assertEquals(true, comparator.isLocked());
138         try {
139             comparator.add("Minneapolis");
140             fail("Should have thrown an UnsupportedOperationException");
141         } catch (UnsupportedOperationException JavaDoc e) {
142             // success -- ignore
143
}
144
145         try {
146             comparator.addAsEqual("New York", "Minneapolis");
147             fail("Should have thrown an UnsupportedOperationException");
148         } catch (UnsupportedOperationException JavaDoc e) {
149             // success -- ignore
150
}
151     }
152
153     public void testUnknownObjectBehavior() {
154         FixedOrderComparator comparator = new FixedOrderComparator(topCities);
155         try {
156             comparator.compare("New York", "Minneapolis");
157             fail("Should have thrown a IllegalArgumentException");
158         } catch (IllegalArgumentException JavaDoc e) {
159             // success-- ignore
160
}
161         try {
162             comparator.compare("Minneapolis", "New York");
163             fail("Should have thrown a IllegalArgumentException");
164         } catch (IllegalArgumentException JavaDoc e) {
165             // success-- ignore
166
}
167         assertEquals(FixedOrderComparator.UNKNOWN_THROW_EXCEPTION, comparator.getUnknownObjectBehavior());
168
169         comparator = new FixedOrderComparator(topCities);
170         comparator.setUnknownObjectBehavior(FixedOrderComparator.UNKNOWN_BEFORE);
171         assertEquals(FixedOrderComparator.UNKNOWN_BEFORE, comparator.getUnknownObjectBehavior());
172         LinkedList JavaDoc keys = new LinkedList JavaDoc(Arrays.asList(topCities));
173         keys.addFirst("Minneapolis");
174         assertComparatorYieldsOrder(keys.toArray(new String JavaDoc[0]), comparator);
175         
176         assertEquals(-1, comparator.compare("Minneapolis", "New York"));
177         assertEquals( 1, comparator.compare("New York", "Minneapolis"));
178         assertEquals( 0, comparator.compare("Minneapolis", "St Paul"));
179
180         comparator = new FixedOrderComparator(topCities);
181         comparator.setUnknownObjectBehavior(FixedOrderComparator.UNKNOWN_AFTER);
182         keys = new LinkedList JavaDoc(Arrays.asList(topCities));
183         keys.add("Minneapolis");
184         assertComparatorYieldsOrder(keys.toArray(new String JavaDoc[0]), comparator);
185         
186         assertEquals( 1, comparator.compare("Minneapolis", "New York"));
187         assertEquals(-1, comparator.compare("New York", "Minneapolis"));
188         assertEquals( 0, comparator.compare("Minneapolis", "St Paul"));
189         
190     }
191     
192     //
193
// Helper methods
194
//
195

196     /** Shuffles the keys and asserts that the comparator sorts them back to
197      * their original order.
198      */

199     private void assertComparatorYieldsOrder(Object JavaDoc[] orderedObjects,
200                                              Comparator JavaDoc comparator) {
201         Object JavaDoc[] keys = (Object JavaDoc[]) orderedObjects.clone();
202         
203         // shuffle until the order changes. It's extremely rare that
204
// this requires more than one shuffle.
205

206         boolean isInNewOrder = false;
207         while (keys.length > 1 && isInNewOrder == false) {
208             shuffle: {
209                 Random JavaDoc rand = new Random JavaDoc();
210                 for (int i = keys.length-1; i > 0; i--) {
211                         Object JavaDoc swap = keys[i];
212                         int j = rand.nextInt(i+1);
213                         keys[i] = keys[j];
214                         keys[j] = swap;
215                     }
216             }
217         
218             testShuffle: {
219                 for (int i = 0; i < keys.length && !isInNewOrder; i++) {
220                     if( !orderedObjects[i].equals(keys[i])) {
221                         isInNewOrder = true;
222                     }
223                 }
224             }
225         }
226         
227         // The real test: sort and make sure they come out right.
228

229         Arrays.sort(keys, comparator);
230
231         for (int i = 0; i < orderedObjects.length; i++) {
232             assertEquals(orderedObjects[i], keys[i]);
233         }
234     }
235     
236 }
237
Popular Tags