1 16 package org.apache.commons.collections.comparators; 17 18 import java.util.Arrays ; 19 import java.util.Comparator ; 20 import java.util.LinkedList ; 21 import java.util.List ; 22 import java.util.Random ; 23 24 import junit.framework.Test; 25 import junit.framework.TestCase; 26 import junit.framework.TestSuite; 27 28 36 public class TestFixedOrderComparator extends TestCase { 37 38 39 42 public static final String topCities[] = new String [] { 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 59 public TestFixedOrderComparator(String 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 args[]) { 68 junit.textui.TestRunner.run(suite()); 69 } 70 71 75 76 77 81 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 [] keys = (String []) topCities.clone(); 90 assertComparatorYieldsOrder(keys, comparator); 91 } 92 93 96 public void testArrayConstructor() { 97 String [] keys = (String []) topCities.clone(); 98 String [] topCitiesForTest = (String []) topCities.clone(); 99 FixedOrderComparator comparator = new FixedOrderComparator(topCitiesForTest); 100 assertComparatorYieldsOrder(keys, comparator); 101 topCitiesForTest[0] = "Brighton"; 103 assertComparatorYieldsOrder(keys, comparator); 104 } 105 106 109 public void testListConstructor() { 110 String [] keys = (String []) topCities.clone(); 111 List topCitiesForTest = new LinkedList (Arrays.asList(topCities)); 112 FixedOrderComparator comparator = new FixedOrderComparator(topCitiesForTest); 113 assertComparatorYieldsOrder(keys, comparator); 114 topCitiesForTest.set(0, "Brighton"); 116 assertComparatorYieldsOrder(keys, comparator); 117 } 118 119 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 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 e) { 142 } 144 145 try { 146 comparator.addAsEqual("New York", "Minneapolis"); 147 fail("Should have thrown an UnsupportedOperationException"); 148 } catch (UnsupportedOperationException e) { 149 } 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 e) { 159 } 161 try { 162 comparator.compare("Minneapolis", "New York"); 163 fail("Should have thrown a IllegalArgumentException"); 164 } catch (IllegalArgumentException e) { 165 } 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 keys = new LinkedList (Arrays.asList(topCities)); 173 keys.addFirst("Minneapolis"); 174 assertComparatorYieldsOrder(keys.toArray(new String [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 (Arrays.asList(topCities)); 183 keys.add("Minneapolis"); 184 assertComparatorYieldsOrder(keys.toArray(new String [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 196 199 private void assertComparatorYieldsOrder(Object [] orderedObjects, 200 Comparator comparator) { 201 Object [] keys = (Object []) orderedObjects.clone(); 202 203 206 boolean isInNewOrder = false; 207 while (keys.length > 1 && isInNewOrder == false) { 208 shuffle: { 209 Random rand = new Random (); 210 for (int i = keys.length-1; i > 0; i--) { 211 Object 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 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 |