1 54 55 package junitx.extensions; 56 57 import junit.framework.AssertionFailedError; 58 import junit.framework.TestCase; 59 import junitx.framework.Assert; 60 import junitx.framework.ComparableAssert; 61 62 76 public abstract class ComparabilityTestCase 77 extends TestCase { 78 79 private Comparable less; 80 private Comparable equal1; 81 private Comparable equal2; 82 private Comparable greater; 83 84 89 public ComparabilityTestCase(String name) { 90 super(name); 91 } 92 93 101 protected abstract Comparable createLessInstance() 102 throws Exception ; 103 104 111 protected abstract Comparable createEqualInstance() 112 throws Exception ; 113 114 122 protected abstract Comparable createGreaterInstance() 123 throws Exception ; 124 125 130 protected void setUp() 131 throws Exception { 132 super.setUp(); 133 134 less = createLessInstance(); 135 equal1 = createEqualInstance(); 136 equal2 = createEqualInstance(); 137 greater = createGreaterInstance(); 138 139 try { 141 assertNotNull("createLessInstance() returned null", less); 142 assertNotNull("createEqualInstance() returned null", equal1); 143 assertNotNull("2nd createEqualInstance() returned null", equal2); 144 assertNotNull("createGreaterInstance() returned null", greater); 145 146 assertEquals("less and equal1 of different classes", 147 less.getClass(), 148 equal1.getClass()); 149 assertEquals("less and equal2 of different classes", 150 less.getClass(), 151 equal2.getClass()); 152 assertEquals("less and greater of different classes", 153 less.getClass(), 154 greater.getClass()); 155 156 checkForEquality(equal1, equal2); 157 } catch (AssertionFailedError ex) { 158 throw new IllegalArgumentException (ex.getMessage()); 159 } 160 } 161 162 167 protected void checkForEquality(Comparable c1, 168 Comparable c2) { 169 Assert.assertNotSame("1st equal instance same as 2nd", equal1, equal2); 170 assertEquals("1st equal not equal to 2nd", equal1, equal2); 171 } 172 173 177 public final void testForReverseSigns() { 178 assertEquals( 179 "less vs. equal1", 180 sgn(less.compareTo(equal1)), 181 -sgn(equal1.compareTo(less))); 182 assertEquals( 183 "less vs. equal2", 184 sgn(less.compareTo(equal2)), 185 -sgn(equal2.compareTo(less))); 186 assertEquals( 187 "less vs. greater", 188 sgn(less.compareTo(greater)), 189 -sgn(greater.compareTo(less))); 190 assertEquals( 191 "equal1 vs. equal2", 192 sgn(equal1.compareTo(equal2)), 193 -sgn(equal2.compareTo(equal1))); 194 assertEquals( 195 "equal1 vs. greater", 196 sgn(equal1.compareTo(greater)), 197 -sgn(greater.compareTo(equal1))); 198 assertEquals( 199 "equal2 vs. greater", 200 sgn(equal2.compareTo(greater)), 201 -sgn(greater.compareTo(equal2))); 202 } 203 204 208 public final void testForSameSigns() { 209 assertEquals( 210 "equal vs. less", 211 sgn(equal1.compareTo(less)), 212 sgn(equal2.compareTo(less))); 213 assertEquals( 214 "equal vs. greater", 215 sgn(equal1.compareTo(greater)), 216 sgn(equal2.compareTo(greater))); 217 } 218 219 226 public final void testReturnValues() { 227 ComparableAssert.assertLesser(equal1, less); 228 ComparableAssert.assertLesser(equal2, less); 229 ComparableAssert.assertGreater(less, greater); 230 ComparableAssert.assertEquals(equal1, equal2); 231 ComparableAssert.assertGreater(equal1, greater); 232 ComparableAssert.assertGreater(equal2, greater); 233 } 234 235 239 public final void testForClassCastException() throws Exception { 240 try { 241 less.compareTo(new Object ()); 242 } catch (ClassCastException success) { 243 return; 244 } 245 fail("should have thrown ClassCastException"); 246 } 247 248 private int sgn(int x) { 249 return (x == 0) ? 0 : (x / Math.abs(x)); 250 } 251 252 } 253 | Popular Tags |