1 16 package org.apache.commons.collections.comparators; 17 18 import java.util.ArrayList ; 19 import java.util.Comparator ; 20 import java.util.List ; 21 22 import junit.framework.Test; 23 import junit.framework.TestSuite; 24 25 32 public class TestBooleanComparator extends AbstractTestComparator { 33 34 37 public TestBooleanComparator(String testName) { 38 super(testName); 39 } 40 41 public static Test suite() { 42 return new TestSuite(TestBooleanComparator.class); 43 } 44 45 48 public Comparator makeComparator() { 49 return new BooleanComparator(); 50 } 51 52 public List getComparableObjectsOrdered() { 53 List list = new ArrayList (); 54 list.add(new Boolean (false)); 55 list.add(Boolean.FALSE); 56 list.add(new Boolean (false)); 57 list.add(Boolean.TRUE); 58 list.add(new Boolean (true)); 59 list.add(Boolean.TRUE); 60 return list; 61 } 62 63 public String getCompatibilityVersion() { 64 return "3"; 65 } 66 67 70 public void testConstructors() { 71 allTests(false,new BooleanComparator()); 72 allTests(false,new BooleanComparator(false)); 73 allTests(true,new BooleanComparator(true)); 74 } 75 76 public void testStaticFactoryMethods() { 77 allTests(false,BooleanComparator.getFalseFirstComparator()); 78 allTests(false,BooleanComparator.getBooleanComparator(false)); 79 allTests(true,BooleanComparator.getTrueFirstComparator()); 80 allTests(true,BooleanComparator.getBooleanComparator(true)); 81 } 82 83 public void testEqualsCompatibleInstance() { 84 assertEquals(new BooleanComparator(),new BooleanComparator(false)); 85 assertEquals(new BooleanComparator(false),new BooleanComparator(false)); 86 assertEquals(new BooleanComparator(false),BooleanComparator.getFalseFirstComparator()); 87 assertSame(BooleanComparator.getFalseFirstComparator(),BooleanComparator.getBooleanComparator(false)); 88 89 assertEquals(new BooleanComparator(true),new BooleanComparator(true)); 90 assertEquals(new BooleanComparator(true),BooleanComparator.getTrueFirstComparator()); 91 assertSame(BooleanComparator.getTrueFirstComparator(),BooleanComparator.getBooleanComparator(true)); 92 93 assertTrue(!(new BooleanComparator().equals(new BooleanComparator(true)))); 94 assertTrue(!(new BooleanComparator(true).equals(new BooleanComparator(false)))); 95 } 96 97 100 protected void allTests(boolean trueFirst, BooleanComparator comp) { 101 orderIndependentTests(comp); 102 if(trueFirst) { 103 trueFirstTests(comp); 104 } else { 105 falseFirstTests(comp); 106 } 107 } 108 109 protected void trueFirstTests(BooleanComparator comp) { 110 assertNotNull(comp); 111 assertEquals(0,comp.compare(Boolean.TRUE,Boolean.TRUE)); 112 assertEquals(0,comp.compare(Boolean.FALSE,Boolean.FALSE)); 113 assertTrue(comp.compare(Boolean.FALSE,Boolean.TRUE) > 0); 114 assertTrue(comp.compare(Boolean.TRUE,Boolean.FALSE) < 0); 115 116 assertEquals(0,comp.compare((Object )(Boolean.TRUE),(Object )(Boolean.TRUE))); 117 assertEquals(0,comp.compare((Object )(Boolean.FALSE),(Object )(Boolean.FALSE))); 118 assertTrue(comp.compare((Object )(Boolean.FALSE),(Object )(Boolean.TRUE)) > 0); 119 assertTrue(comp.compare((Object )(Boolean.TRUE),(Object )(Boolean.FALSE)) < 0); 120 } 121 122 protected void falseFirstTests(BooleanComparator comp) { 123 assertNotNull(comp); 124 assertEquals(0,comp.compare(Boolean.TRUE,Boolean.TRUE)); 125 assertEquals(0,comp.compare(Boolean.FALSE,Boolean.FALSE)); 126 assertTrue(comp.compare(Boolean.FALSE,Boolean.TRUE) < 0); 127 assertTrue(comp.compare(Boolean.TRUE,Boolean.FALSE) > 0); 128 129 assertEquals(0,comp.compare((Object )(Boolean.TRUE),(Object )(Boolean.TRUE))); 130 assertEquals(0,comp.compare((Object )(Boolean.FALSE),(Object )(Boolean.FALSE))); 131 assertTrue(comp.compare((Object )(Boolean.FALSE),(Object )(Boolean.TRUE)) < 0); 132 assertTrue(comp.compare((Object )(Boolean.TRUE),(Object )(Boolean.FALSE)) > 0); 133 } 134 135 protected void orderIndependentTests(BooleanComparator comp) { 136 nullArgumentTests(comp); 137 nonBooleanArgumentTests(comp); 138 nullAndNonBooleanArgumentsTests(comp); 139 } 140 141 protected void nullArgumentTests(BooleanComparator comp) { 142 assertNotNull(comp); 143 try { 144 comp.compare(null,null); 145 fail("Expected NullPointerException"); 146 } catch(NullPointerException e) { 147 } 149 try { 150 comp.compare(Boolean.TRUE,null); 151 fail("Expected NullPointerException"); 152 } catch(NullPointerException e) { 153 } 155 try { 156 comp.compare(Boolean.FALSE,null); 157 fail("Expected NullPointerException"); 158 } catch(NullPointerException e) { 159 } 161 try { 162 comp.compare(null,Boolean.TRUE); 163 fail("Expected NullPointerException"); 164 } catch(NullPointerException e) { 165 } 167 try { 168 comp.compare(null,Boolean.FALSE); 169 fail("Expected NullPointerException"); 170 } catch(NullPointerException e) { 171 } 173 } 174 175 protected void nonBooleanArgumentTests(BooleanComparator comp) { 176 assertNotNull(comp); 177 try { 178 comp.compare("string","string"); 179 fail("Expected ClassCastException"); 180 } catch(ClassCastException e) { 181 } 183 try { 184 comp.compare(Boolean.TRUE,"string"); 185 fail("Expected ClassCastException"); 186 } catch(ClassCastException e) { 187 } 189 try { 190 comp.compare("string",Boolean.TRUE); 191 fail("Expected ClassCastException"); 192 } catch(ClassCastException e) { 193 } 195 try { 196 comp.compare("string",new Integer (3)); 197 fail("Expected ClassCastException"); 198 } catch(ClassCastException e) { 199 } 201 try { 202 comp.compare(new Integer (3),"string"); 203 fail("Expected ClassCastException"); 204 } catch(ClassCastException e) { 205 } 207 } 208 209 protected void nullAndNonBooleanArgumentsTests(BooleanComparator comp) { 210 assertNotNull(comp); 211 try { 212 comp.compare(null,"string"); 213 fail("Expected ClassCast or NullPointer Exception"); 214 } catch(ClassCastException e) { 215 } catch(NullPointerException e) { 217 } 219 try { 220 comp.compare("string",null); 221 fail("Expected ClassCast or NullPointer Exception"); 222 } catch(ClassCastException e) { 223 } catch(NullPointerException e) { 225 } 227 } 228 229 } 230 | Popular Tags |