1 16 package org.apache.commons.lang; 17 18 import junit.framework.Test; 19 import junit.framework.TestCase; 20 import junit.framework.TestSuite; 21 22 30 31 public final class NumberRangeTest extends TestCase { 32 33 34 private NumberRange tenToTwenty; 35 private NumberRange fifteenToTwentyFive; 36 private NumberRange fiveToNine; 37 private Number five; 38 private Number nine; 39 private Number ten; 40 private Number fifteen; 41 private Number twenty; 42 private Number twentyFive; 43 44 public NumberRangeTest(String name) { 45 super(name); 46 } 47 48 public void setUp() { 49 five = new Integer (5); 50 nine = new Double (9.0); 51 ten = new Integer (10); 52 fifteen = new Integer (15); 53 twenty = new Integer (20); 54 twentyFive = new Integer (25); 55 56 tenToTwenty = new NumberRange(ten, twenty); 57 fifteenToTwentyFive = new NumberRange( fifteen, twentyFive); 58 fiveToNine = new NumberRange( five, nine ); 59 } 60 61 62 public static Test suite() { 63 TestSuite suite = new TestSuite(NumberRangeTest.class); 64 suite.setName("NumberRange Tests"); 65 return suite; 66 } 67 68 public void testMaxMin() { 69 boolean expected = true; 70 boolean result = tenToTwenty.getMaximum().equals(twenty); 71 assertEquals(expected, result); 72 73 expected = true; 74 result = tenToTwenty.getMinimum().equals(ten); 75 assertEquals(expected, result); 76 } 77 78 public void testEquals() { 79 boolean expected = false; 80 boolean result = tenToTwenty.equals(new NumberRange(five, ten)); 81 assertEquals(expected, result); 82 83 expected = true; 84 result = tenToTwenty.equals(new NumberRange(ten, twenty)); 85 assertEquals(expected, result); 86 87 expected = false; 88 result = tenToTwenty.equals(new NumberRange(ten, fifteen)); 89 assertEquals(expected, result); 90 91 expected = false; 92 result = tenToTwenty.equals(new NumberRange(fifteen, twenty)); 93 assertEquals(expected, result); 94 } 95 96 public void testEqualsWithOtherObject() { 97 assertEquals( "A NumberRange should not equals a String object", false, fiveToNine.equals("TEST")); 98 } 99 100 public void testEqualsWithSameReference() { 101 assertEquals( "A NumberRange should equal itself", true, fiveToNine.equals(fiveToNine)); 102 } 103 104 public void testEqualsNull() { 105 assertEquals( "A NumberRange should not equal null", false, fiveToNine.equals(null)); 106 } 107 108 public void testHashCode() { 109 NumberRange nr = new NumberRange( new Integer (5), new Double (9.0)); 110 assertEquals( "The hashCode of 5-9 should equals the hashcode of another NumberRange of the same min/max", 111 fiveToNine.hashCode(), nr.hashCode()); 112 assertTrue( "The hashCode of 10-20 should not equal the hashCode of 5-9", 113 fiveToNine.hashCode() != tenToTwenty.hashCode()); 114 } 115 116 public void testIncludesNumber() { 117 boolean expected = false; 118 boolean result = tenToTwenty.includesNumber(five); 119 assertEquals(expected, result); 120 121 expected = true; 122 result = tenToTwenty.includesNumber(ten); 123 assertEquals(expected, result); 124 125 expected = true; 126 result = tenToTwenty.includesNumber(fifteen); 127 assertEquals(expected, result); 128 129 expected = true; 130 result = tenToTwenty.includesNumber(twenty); 131 assertEquals(expected, result); 132 133 expected = false; 134 result = tenToTwenty.includesNumber(twentyFive); 135 assertEquals(expected, result); 136 } 137 138 public void testIncludesNumberNull() { 139 boolean result = tenToTwenty.includesNumber(null); 140 assertEquals("Includes number should return false for null values", false, result); 141 } 142 143 public void testIncludesRange() { 144 boolean expected = false; 145 boolean result = tenToTwenty.includesRange(new NumberRange(five, ten)); 146 assertEquals(expected, result); 147 148 expected = false; 149 result = tenToTwenty.includesRange(new NumberRange(five, fifteen)); 150 assertEquals(expected, result); 151 152 expected = true; 153 result = tenToTwenty.includesRange(new NumberRange(ten, fifteen)); 154 assertEquals(expected, result); 155 156 expected = true; 157 result = tenToTwenty.includesRange(new NumberRange(ten, twenty)); 158 assertEquals(expected, result); 159 160 expected = true; 161 result = tenToTwenty.includesRange(new NumberRange(fifteen, twenty)); 162 assertEquals(expected, result); 163 164 expected = false; 165 result = 166 tenToTwenty.includesRange(new NumberRange(fifteen, twentyFive)); 167 assertEquals(expected, result); 168 169 expected = false; 170 result = 171 tenToTwenty.includesRange(new NumberRange(twenty, twentyFive)); 172 assertEquals(expected, result); 173 } 174 175 public void testIncludesRangeNull() { 176 boolean result = tenToTwenty.includesRange(null); 177 assertEquals("Includes range should return false for null values", false, result); 178 } 179 180 public void testConstructor() { 181 NumberRange nr = new NumberRange( new Double (2.0)); 182 assertEquals("Unexpected min on NumberRange", 2.0, nr.getMinimum().doubleValue(), Double.MIN_VALUE); 183 assertEquals("Unexpected max on NumberRange", 2.0, nr.getMaximum().doubleValue(), Double.MIN_VALUE); 184 } 185 186 public void testConstructorNullParameters() { 187 try { 188 NumberRange nr = new NumberRange(null); 189 fail("NumberRange(null) did not throw an exception."); 190 } catch (Exception e) { 191 assertTrue( "NumberRange(null)", e instanceof NullPointerException ); 192 } 193 194 try { 195 NumberRange nr = new NumberRange(five, null); 196 fail("NumberRange(five, null) did not throw an exception."); 197 } catch (Exception e) { 198 assertTrue("NumberRange(five, null)", e instanceof NullPointerException ); 199 } 200 201 try { 202 NumberRange nr = new NumberRange(null, five); 203 fail("NumberRange(null, five) did not throw an exception."); 204 } catch (Exception e) { 205 assertTrue("NumberRange(null, five)", e instanceof NullPointerException ); 206 } 207 } 208 209 public void testConstructorWithMaxLessThanMin() { 210 NumberRange nr = new NumberRange( new Double (2.0), new Double (1.0)); 211 assertEquals("Unexpected min on NumberRange", 2.0, nr.getMinimum().doubleValue(), Double.MIN_VALUE); 212 assertEquals("Unexpected max on NumberRange", 2.0, nr.getMaximum().doubleValue(), Double.MIN_VALUE); 213 } 214 215 public void testOverlap() { 216 assertEquals( "5-9 should not overlap 10-20", false, fiveToNine.overlaps( tenToTwenty )); 217 assertEquals( "10-20 should overlap 15-25", true, tenToTwenty.overlaps( fifteenToTwentyFive )); 218 } 219 220 public void testOverlapNull() { 221 assertEquals( "5-9 should not overlap null", false, fiveToNine.overlaps( null )); 222 } 223 224 public void testToString() { 225 String expected = "10-20"; 226 String result = tenToTwenty.toString(); 227 assertEquals(expected, result); 228 } 229 230 public void testToStringWithNegatives() { 231 String expected = "(-20)-(-10)"; 232 NumberRange nr = new NumberRange( new Integer (-20), new Integer (-10)); 233 String result = nr.toString(); 234 assertEquals(expected, result); 235 236 expected = "(-20)-10"; 237 nr = new NumberRange( new Integer (-20), new Integer (10)); 238 result = nr.toString(); 239 assertEquals(expected, result); 240 } 241 242 243 } 244 245 | Popular Tags |