1 16 package org.apache.commons.lang; 17 18 import java.lang.reflect.Modifier ; 19 20 import junit.framework.Test; 21 import junit.framework.TestCase; 22 import junit.framework.TestSuite; 23 import junit.textui.TestRunner; 24 25 31 public class CharRangeTest extends TestCase { 32 33 public CharRangeTest(String name) { 34 super(name); 35 } 36 37 public static void main(String [] args) { 38 TestRunner.run(suite()); 39 } 40 41 public static Test suite() { 42 TestSuite suite = new TestSuite(CharRangeTest.class); 43 suite.setName("CharRange Tests"); 44 return suite; 45 } 46 47 protected void setUp() throws Exception { 48 super.setUp(); 49 } 50 51 protected void tearDown() throws Exception { 52 super.tearDown(); 53 } 54 55 public void testClass() { 57 assertEquals(true, Modifier.isPublic(CharRange.class.getModifiers())); 58 assertEquals(true, Modifier.isFinal(CharRange.class.getModifiers())); 59 } 60 61 public void testConstructorAccessors_Char() { 63 CharRange rangea = new CharRange('a'); 64 assertEquals('a', rangea.getStart()); 65 assertEquals('a', rangea.getEnd()); 66 assertEquals(false, rangea.isNegated()); 67 assertEquals("a", rangea.toString()); 68 } 69 70 public void testConstructorAccessors_CharBoolean_Normal() { 71 CharRange rangea = new CharRange('a'); 72 assertEquals('a', rangea.getStart()); 73 assertEquals('a', rangea.getEnd()); 74 assertEquals(false, rangea.isNegated()); 75 assertEquals("a", rangea.toString()); 76 } 77 78 public void testConstructorAccessors_CharBoolean_Negated() { 79 CharRange rangea = new CharRange('a', true); 80 assertEquals('a', rangea.getStart()); 81 assertEquals('a', rangea.getEnd()); 82 assertEquals(true, rangea.isNegated()); 83 assertEquals("^a", rangea.toString()); 84 } 85 86 public void testConstructorAccessors_CharChar_Same() { 87 CharRange rangea = new CharRange('a', 'a'); 88 assertEquals('a', rangea.getStart()); 89 assertEquals('a', rangea.getEnd()); 90 assertEquals(false, rangea.isNegated()); 91 assertEquals("a", rangea.toString()); 92 } 93 94 public void testConstructorAccessors_CharChar_Normal() { 95 CharRange rangea = new CharRange('a', 'e'); 96 assertEquals('a', rangea.getStart()); 97 assertEquals('e', rangea.getEnd()); 98 assertEquals(false, rangea.isNegated()); 99 assertEquals("a-e", rangea.toString()); 100 } 101 102 public void testConstructorAccessors_CharChar_Reversed() { 103 CharRange rangea = new CharRange('e', 'a'); 104 assertEquals('a', rangea.getStart()); 105 assertEquals('e', rangea.getEnd()); 106 assertEquals(false, rangea.isNegated()); 107 assertEquals("a-e", rangea.toString()); 108 } 109 110 public void testConstructorAccessors_CharCharBoolean_Same() { 111 CharRange rangea = new CharRange('a', 'a', false); 112 assertEquals('a', rangea.getStart()); 113 assertEquals('a', rangea.getEnd()); 114 assertEquals(false, rangea.isNegated()); 115 assertEquals("a", rangea.toString()); 116 } 117 118 public void testConstructorAccessors_CharCharBoolean_Normal() { 119 CharRange rangea = new CharRange('a', 'e', false); 120 assertEquals('a', rangea.getStart()); 121 assertEquals('e', rangea.getEnd()); 122 assertEquals(false, rangea.isNegated()); 123 assertEquals("a-e", rangea.toString()); 124 } 125 126 public void testConstructorAccessors_CharCharBoolean_Reversed() { 127 CharRange rangea = new CharRange('e', 'a', false); 128 assertEquals('a', rangea.getStart()); 129 assertEquals('e', rangea.getEnd()); 130 assertEquals(false, rangea.isNegated()); 131 assertEquals("a-e", rangea.toString()); 132 } 133 134 public void testConstructorAccessors_CharCharBoolean_SameNegated() { 135 CharRange rangea = new CharRange('a', 'a', true); 136 assertEquals('a', rangea.getStart()); 137 assertEquals('a', rangea.getEnd()); 138 assertEquals(true, rangea.isNegated()); 139 assertEquals("^a", rangea.toString()); 140 } 141 142 public void testConstructorAccessors_CharCharBoolean_NormalNegated() { 143 CharRange rangea = new CharRange('a', 'e', true); 144 assertEquals('a', rangea.getStart()); 145 assertEquals('e', rangea.getEnd()); 146 assertEquals(true, rangea.isNegated()); 147 assertEquals("^a-e", rangea.toString()); 148 } 149 150 public void testConstructorAccessors_CharCharBoolean_ReversedNegated() { 151 CharRange rangea = new CharRange('e', 'a', true); 152 assertEquals('a', rangea.getStart()); 153 assertEquals('e', rangea.getEnd()); 154 assertEquals(true, rangea.isNegated()); 155 assertEquals("^a-e", rangea.toString()); 156 } 157 158 public void testEquals_Object() { 160 CharRange rangea = new CharRange('a'); 161 CharRange rangeae = new CharRange('a', 'e'); 162 CharRange rangenotbf = new CharRange('b', 'f', false); 163 164 assertEquals(false, rangea.equals(null)); 165 166 assertEquals(true, rangea.equals(rangea)); 167 assertEquals(true, rangea.equals(new CharRange('a'))); 168 assertEquals(true, rangeae.equals(rangeae)); 169 assertEquals(true, rangeae.equals(new CharRange('a', 'e'))); 170 assertEquals(true, rangenotbf.equals(rangenotbf)); 171 assertEquals(true, rangenotbf.equals(new CharRange('b', 'f', false))); 172 173 assertEquals(false, rangea.equals(rangeae)); 174 assertEquals(false, rangea.equals(rangenotbf)); 175 assertEquals(false, rangeae.equals(rangea)); 176 assertEquals(false, rangeae.equals(rangenotbf)); 177 assertEquals(false, rangenotbf.equals(rangea)); 178 assertEquals(false, rangenotbf.equals(rangeae)); 179 } 180 181 public void testHashCode() { 182 CharRange rangea = new CharRange('a'); 183 CharRange rangeae = new CharRange('a', 'e'); 184 CharRange rangenotbf = new CharRange('b', 'f', false); 185 186 assertEquals(true, rangea.hashCode() == rangea.hashCode()); 187 assertEquals(true, rangea.hashCode() == new CharRange('a').hashCode()); 188 assertEquals(true, rangeae.hashCode() == rangeae.hashCode()); 189 assertEquals(true, rangeae.hashCode() == new CharRange('a', 'e').hashCode()); 190 assertEquals(true, rangenotbf.hashCode() == rangenotbf.hashCode()); 191 assertEquals(true, rangenotbf.hashCode() == new CharRange('b', 'f', false).hashCode()); 192 193 assertEquals(false, rangea.hashCode() == rangeae.hashCode()); 194 assertEquals(false, rangea.hashCode() == rangenotbf.hashCode()); 195 assertEquals(false, rangeae.hashCode() == rangea.hashCode()); 196 assertEquals(false, rangeae.hashCode() == rangenotbf.hashCode()); 197 assertEquals(false, rangenotbf.hashCode() == rangea.hashCode()); 198 assertEquals(false, rangenotbf.hashCode() == rangeae.hashCode()); 199 } 200 201 public void testContains_Char() { 203 CharRange range = new CharRange('c'); 204 assertEquals(false, range.contains('b')); 205 assertEquals(true, range.contains('c')); 206 assertEquals(false, range.contains('d')); 207 assertEquals(false, range.contains('e')); 208 209 range = new CharRange('c', 'd'); 210 assertEquals(false, range.contains('b')); 211 assertEquals(true, range.contains('c')); 212 assertEquals(true, range.contains('d')); 213 assertEquals(false, range.contains('e')); 214 215 range = new CharRange('d', 'c'); 216 assertEquals(false, range.contains('b')); 217 assertEquals(true, range.contains('c')); 218 assertEquals(true, range.contains('d')); 219 assertEquals(false, range.contains('e')); 220 221 range = new CharRange('c', 'd', false); 222 assertEquals(false, range.contains('b')); 223 assertEquals(true, range.contains('c')); 224 assertEquals(true, range.contains('d')); 225 assertEquals(false, range.contains('e')); 226 227 range = new CharRange('c', 'd', true); 228 assertEquals(true, range.contains('b')); 229 assertEquals(false, range.contains('c')); 230 assertEquals(false, range.contains('d')); 231 assertEquals(true, range.contains('e')); 232 assertEquals(true, range.contains((char) 0)); 233 assertEquals(true, range.contains(Character.MAX_VALUE)); 234 } 235 236 public void testContains_Charrange() { 238 CharRange a = new CharRange('a'); 239 CharRange b = new CharRange('b'); 240 CharRange c = new CharRange('c'); 241 CharRange c2 = new CharRange('c'); 242 CharRange d = new CharRange('d'); 243 CharRange e = new CharRange('e'); 244 CharRange cd = new CharRange('c', 'd'); 245 CharRange bd = new CharRange('b', 'd'); 246 CharRange bc = new CharRange('b', 'c'); 247 CharRange ab = new CharRange('a', 'b'); 248 CharRange de = new CharRange('d', 'e'); 249 CharRange ef = new CharRange('e', 'f'); 250 CharRange ae = new CharRange('a', 'e'); 251 252 assertEquals(false, c.contains(b)); 254 assertEquals(true, c.contains(c)); 255 assertEquals(true, c.contains(c2)); 256 assertEquals(false, c.contains(d)); 257 258 assertEquals(false, c.contains(cd)); 259 assertEquals(false, c.contains(bd)); 260 assertEquals(false, c.contains(bc)); 261 assertEquals(false, c.contains(ab)); 262 assertEquals(false, c.contains(de)); 263 264 assertEquals(true, cd.contains(c)); 265 assertEquals(true, bd.contains(c)); 266 assertEquals(true, bc.contains(c)); 267 assertEquals(false, ab.contains(c)); 268 assertEquals(false, de.contains(c)); 269 270 assertEquals(true, ae.contains(b)); 271 assertEquals(true, ae.contains(ab)); 272 assertEquals(true, ae.contains(bc)); 273 assertEquals(true, ae.contains(cd)); 274 assertEquals(true, ae.contains(de)); 275 276 CharRange notb = new CharRange('b', 'b', true); 277 CharRange notc = new CharRange('c', 'c', true); 278 CharRange notd = new CharRange('d', 'd', true); 279 CharRange notab = new CharRange('a', 'b', true); 280 CharRange notbc = new CharRange('b', 'c', true); 281 CharRange notbd = new CharRange('b', 'd', true); 282 CharRange notcd = new CharRange('c', 'd', true); 283 CharRange notde = new CharRange('d', 'e', true); 284 CharRange notae = new CharRange('a', 'e', true); 285 CharRange all = new CharRange((char) 0, Character.MAX_VALUE); 286 CharRange allbutfirst = new CharRange((char) 1, Character.MAX_VALUE); 287 288 assertEquals(false, c.contains(notc)); 290 assertEquals(false, c.contains(notbd)); 291 assertEquals(true, all.contains(notc)); 292 assertEquals(true, all.contains(notbd)); 293 assertEquals(false, allbutfirst.contains(notc)); 294 assertEquals(false, allbutfirst.contains(notbd)); 295 296 assertEquals(true, notc.contains(a)); 298 assertEquals(true, notc.contains(b)); 299 assertEquals(false, notc.contains(c)); 300 assertEquals(true, notc.contains(d)); 301 assertEquals(true, notc.contains(e)); 302 303 assertEquals(true, notc.contains(ab)); 304 assertEquals(false, notc.contains(bc)); 305 assertEquals(false, notc.contains(bd)); 306 assertEquals(false, notc.contains(cd)); 307 assertEquals(true, notc.contains(de)); 308 assertEquals(false, notc.contains(ae)); 309 assertEquals(false, notc.contains(all)); 310 assertEquals(false, notc.contains(allbutfirst)); 311 312 assertEquals(true, notbd.contains(a)); 313 assertEquals(false, notbd.contains(b)); 314 assertEquals(false, notbd.contains(c)); 315 assertEquals(false, notbd.contains(d)); 316 assertEquals(true, notbd.contains(e)); 317 318 assertEquals(true, notcd.contains(ab)); 319 assertEquals(false, notcd.contains(bc)); 320 assertEquals(false, notcd.contains(bd)); 321 assertEquals(false, notcd.contains(cd)); 322 assertEquals(false, notcd.contains(de)); 323 assertEquals(false, notcd.contains(ae)); 324 assertEquals(true, notcd.contains(ef)); 325 assertEquals(false, notcd.contains(all)); 326 assertEquals(false, notcd.contains(allbutfirst)); 327 328 assertEquals(false, notc.contains(notb)); 330 assertEquals(true, notc.contains(notc)); 331 assertEquals(false, notc.contains(notd)); 332 333 assertEquals(false, notc.contains(notab)); 334 assertEquals(true, notc.contains(notbc)); 335 assertEquals(true, notc.contains(notbd)); 336 assertEquals(true, notc.contains(notcd)); 337 assertEquals(false, notc.contains(notde)); 338 339 assertEquals(false, notbd.contains(notb)); 340 assertEquals(false, notbd.contains(notc)); 341 assertEquals(false, notbd.contains(notd)); 342 343 assertEquals(false, notbd.contains(notab)); 344 assertEquals(false, notbd.contains(notbc)); 345 assertEquals(true, notbd.contains(notbd)); 346 assertEquals(false, notbd.contains(notcd)); 347 assertEquals(false, notbd.contains(notde)); 348 assertEquals(true, notbd.contains(notae)); 349 } 350 351 public void testSerialization() { 353 CharRange range = new CharRange('a'); 354 assertEquals(range, SerializationUtils.clone(range)); 355 range = new CharRange('a', 'e'); 356 assertEquals(range, SerializationUtils.clone(range)); 357 range = new CharRange('a', 'e', true); 358 assertEquals(range, SerializationUtils.clone(range)); 359 } 360 361 } 362 | Popular Tags |