1 16 package org.apache.commons.lang; 17 18 import java.lang.reflect.Constructor ; 19 import java.lang.reflect.Modifier ; 20 21 import junit.framework.Test; 22 import junit.framework.TestCase; 23 import junit.framework.TestSuite; 24 import junit.textui.TestRunner; 25 26 33 public class BooleanUtilsTest extends TestCase { 34 35 public BooleanUtilsTest(String name) { 36 super(name); 37 } 38 39 public static void main(String [] args) { 40 TestRunner.run(suite()); 41 } 42 43 public static Test suite() { 44 TestSuite suite = new TestSuite(BooleanUtilsTest.class); 45 suite.setName("BooleanUtils Tests"); 46 return suite; 47 } 48 49 protected void setUp() throws Exception { 50 super.setUp(); 51 } 52 53 protected void tearDown() throws Exception { 54 super.tearDown(); 55 } 56 57 public void testConstructor() { 59 assertNotNull(new BooleanUtils()); 60 Constructor [] cons = BooleanUtils.class.getDeclaredConstructors(); 61 assertEquals(1, cons.length); 62 assertEquals(true, Modifier.isPublic(cons[0].getModifiers())); 63 assertEquals(true, Modifier.isPublic(BooleanUtils.class.getModifiers())); 64 assertEquals(false, Modifier.isFinal(BooleanUtils.class.getModifiers())); 65 } 66 67 public void test_negate_Boolean() { 69 assertSame(null, BooleanUtils.negate(null)); 70 assertSame(Boolean.TRUE, BooleanUtils.negate(Boolean.FALSE)); 71 assertSame(Boolean.FALSE, BooleanUtils.negate(Boolean.TRUE)); 72 } 73 74 public void test_isTrue_Boolean() { 76 assertEquals(true, BooleanUtils.isTrue(Boolean.TRUE)); 77 assertEquals(false, BooleanUtils.isTrue(Boolean.FALSE)); 78 assertEquals(false, BooleanUtils.isTrue((Boolean ) null)); 79 } 80 81 public void test_isFalse_Boolean() { 82 assertEquals(false, BooleanUtils.isFalse(Boolean.TRUE)); 83 assertEquals(true, BooleanUtils.isFalse(Boolean.FALSE)); 84 assertEquals(false, BooleanUtils.isFalse((Boolean ) null)); 85 } 86 87 public void test_toBooleanObject_boolean() { 89 assertSame(Boolean.TRUE, BooleanUtils.toBooleanObject(true)); 90 assertSame(Boolean.FALSE, BooleanUtils.toBooleanObject(false)); 91 } 92 93 public void test_toBoolean_Boolean() { 94 assertEquals(true, BooleanUtils.toBoolean(Boolean.TRUE)); 95 assertEquals(false, BooleanUtils.toBoolean(Boolean.FALSE)); 96 assertEquals(false, BooleanUtils.toBoolean((Boolean ) null)); 97 } 98 99 public void test_toBooleanDefaultIfNull_Boolean_boolean() { 100 assertEquals(true, BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, true)); 101 assertEquals(true, BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false)); 102 assertEquals(false, BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true)); 103 assertEquals(false, BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, false)); 104 assertEquals(true, BooleanUtils.toBooleanDefaultIfNull((Boolean ) null, true)); 105 assertEquals(false, BooleanUtils.toBooleanDefaultIfNull((Boolean ) null, false)); 106 } 107 108 public void test_toBoolean_int() { 111 assertEquals(true, BooleanUtils.toBoolean(1)); 112 assertEquals(true, BooleanUtils.toBoolean(-1)); 113 assertEquals(false, BooleanUtils.toBoolean(0)); 114 } 115 116 public void test_toBooleanObject_int() { 117 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject(1)); 118 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject(-1)); 119 assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject(0)); 120 } 121 122 public void test_toBooleanObject_Integer() { 123 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject(new Integer (1))); 124 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject(new Integer (-1))); 125 assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject(new Integer (0))); 126 assertEquals(null, BooleanUtils.toBooleanObject((Integer ) null)); 127 } 128 129 public void test_toBoolean_int_int_int() { 131 assertEquals(true, BooleanUtils.toBoolean(6, 6, 7)); 132 assertEquals(false, BooleanUtils.toBoolean(7, 6, 7)); 133 try { 134 BooleanUtils.toBoolean(8, 6, 7); 135 fail(); 136 } catch (IllegalArgumentException ex) {} 137 } 138 139 public void test_toBoolean_Integer_Integer_Integer() { 140 Integer six = new Integer (6); 141 Integer seven = new Integer (7); 142 143 assertEquals(true, BooleanUtils.toBoolean((Integer ) null, null, seven)); 144 assertEquals(false, BooleanUtils.toBoolean((Integer ) null, six, null)); 145 try { 146 BooleanUtils.toBoolean(null, six, seven); 147 fail(); 148 } catch (IllegalArgumentException ex) {} 149 150 assertEquals(true, BooleanUtils.toBoolean(new Integer (6), six, seven)); 151 assertEquals(false, BooleanUtils.toBoolean(new Integer (7), six, seven)); 152 try { 153 BooleanUtils.toBoolean(new Integer (8), six, seven); 154 fail(); 155 } catch (IllegalArgumentException ex) {} 156 } 157 158 public void test_toBooleanObject_int_int_int() { 160 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject(6, 6, 7, 8)); 161 assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject(7, 6, 7, 8)); 162 assertEquals(null, BooleanUtils.toBooleanObject(8, 6, 7, 8)); 163 try { 164 BooleanUtils.toBooleanObject(9, 6, 7, 8); 165 fail(); 166 } catch (IllegalArgumentException ex) {} 167 } 168 169 public void test_toBooleanObject_Integer_Integer_Integer_Integer() { 170 Integer six = new Integer (6); 171 Integer seven = new Integer (7); 172 Integer eight = new Integer (8); 173 174 assertSame(Boolean.TRUE, BooleanUtils.toBooleanObject((Integer ) null, null, seven, eight)); 175 assertSame(Boolean.FALSE, BooleanUtils.toBooleanObject((Integer ) null, six, null, eight)); 176 assertSame(null, BooleanUtils.toBooleanObject((Integer ) null, six, seven, null)); 177 try { 178 BooleanUtils.toBooleanObject(null, six, seven, eight); 179 fail(); 180 } catch (IllegalArgumentException ex) {} 181 182 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject(new Integer (6), six, seven, eight)); 183 assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject(new Integer (7), six, seven, eight)); 184 assertEquals(null, BooleanUtils.toBooleanObject(new Integer (8), six, seven, eight)); 185 try { 186 BooleanUtils.toBooleanObject(new Integer (9), six, seven, eight); 187 fail(); 188 } catch (IllegalArgumentException ex) {} 189 } 190 191 public void test_toInteger_boolean() { 193 assertEquals(1, BooleanUtils.toInteger(true)); 194 assertEquals(0, BooleanUtils.toInteger(false)); 195 } 196 197 public void test_toIntegerObject_boolean() { 198 assertEquals(new Integer (1), BooleanUtils.toIntegerObject(true)); 199 assertEquals(new Integer (0), BooleanUtils.toIntegerObject(false)); 200 } 201 202 public void test_toIntegerObject_Boolean() { 203 assertEquals(new Integer (1), BooleanUtils.toIntegerObject(Boolean.TRUE)); 204 assertEquals(new Integer (0), BooleanUtils.toIntegerObject(Boolean.FALSE)); 205 assertEquals(null, BooleanUtils.toIntegerObject((Boolean ) null)); 206 } 207 208 public void test_toInteger_boolean_int_int() { 210 assertEquals(6, BooleanUtils.toInteger(true, 6, 7)); 211 assertEquals(7, BooleanUtils.toInteger(false, 6, 7)); 212 } 213 214 public void test_toInteger_Boolean_int_int_int() { 215 assertEquals(6, BooleanUtils.toInteger(Boolean.TRUE, 6, 7, 8)); 216 assertEquals(7, BooleanUtils.toInteger(Boolean.FALSE, 6, 7, 8)); 217 assertEquals(8, BooleanUtils.toInteger(null, 6, 7, 8)); 218 } 219 220 public void test_toIntegerObject_boolean_Integer_Integer() { 221 Integer six = new Integer (6); 222 Integer seven = new Integer (7); 223 assertEquals(six, BooleanUtils.toIntegerObject(true, six, seven)); 224 assertEquals(seven, BooleanUtils.toIntegerObject(false, six, seven)); 225 } 226 227 public void test_toIntegerObject_Boolean_Integer_Integer_Integer() { 228 Integer six = new Integer (6); 229 Integer seven = new Integer (7); 230 Integer eight = new Integer (8); 231 assertEquals(six, BooleanUtils.toIntegerObject(Boolean.TRUE, six, seven, eight)); 232 assertEquals(seven, BooleanUtils.toIntegerObject(Boolean.FALSE, six, seven, eight)); 233 assertEquals(eight, BooleanUtils.toIntegerObject((Boolean ) null, six, seven, eight)); 234 assertEquals(null, BooleanUtils.toIntegerObject((Boolean ) null, six, seven, null)); 235 } 236 237 public void test_toBooleanObject_String() { 240 assertEquals(null, BooleanUtils.toBooleanObject((String ) null)); 241 assertEquals(null, BooleanUtils.toBooleanObject("")); 242 assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("false")); 243 assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("no")); 244 assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("off")); 245 assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("FALSE")); 246 assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("NO")); 247 assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("OFF")); 248 assertEquals(null, BooleanUtils.toBooleanObject("oof")); 249 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("true")); 250 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("yes")); 251 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("on")); 252 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("TRUE")); 253 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("ON")); 254 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("YES")); 255 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("TruE")); 256 } 257 258 public void test_toBooleanObject_String_String_String_String() { 259 assertSame(Boolean.TRUE, BooleanUtils.toBooleanObject((String ) null, null, "N", "U")); 260 assertSame(Boolean.FALSE, BooleanUtils.toBooleanObject((String ) null, "Y", null, "U")); 261 assertSame(null, BooleanUtils.toBooleanObject((String ) null, "Y", "N", null)); 262 try { 263 BooleanUtils.toBooleanObject((String ) null, "Y", "N", "U"); 264 fail(); 265 } catch (IllegalArgumentException ex) {} 266 267 assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("Y", "Y", "N", "U")); 268 assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("N", "Y", "N", "U")); 269 assertEquals(null, BooleanUtils.toBooleanObject("U", "Y", "N", "U")); 270 try { 271 BooleanUtils.toBooleanObject(null, "Y", "N", "U"); 272 fail(); 273 } catch (IllegalArgumentException ex) {} 274 try { 275 BooleanUtils.toBooleanObject("X", "Y", "N", "U"); 276 fail(); 277 } catch (IllegalArgumentException ex) {} 278 } 279 280 public void test_toBoolean_String() { 282 assertEquals(false, BooleanUtils.toBoolean((String ) null)); 283 assertEquals(false, BooleanUtils.toBoolean("")); 284 assertEquals(false, BooleanUtils.toBoolean("off")); 285 assertEquals(false, BooleanUtils.toBoolean("oof")); 286 assertEquals(false, BooleanUtils.toBoolean("yep")); 287 assertEquals(false, BooleanUtils.toBoolean("trux")); 288 assertEquals(false, BooleanUtils.toBoolean("false")); 289 assertEquals(false, BooleanUtils.toBoolean("a")); 290 assertEquals(true, BooleanUtils.toBoolean("true")); assertEquals(true, BooleanUtils.toBoolean(new StringBuffer ("tr").append("ue").toString())); 292 assertEquals(true, BooleanUtils.toBoolean("truE")); 293 assertEquals(true, BooleanUtils.toBoolean("trUe")); 294 assertEquals(true, BooleanUtils.toBoolean("trUE")); 295 assertEquals(true, BooleanUtils.toBoolean("tRue")); 296 assertEquals(true, BooleanUtils.toBoolean("tRuE")); 297 assertEquals(true, BooleanUtils.toBoolean("tRUe")); 298 assertEquals(true, BooleanUtils.toBoolean("tRUE")); 299 assertEquals(true, BooleanUtils.toBoolean("TRUE")); 300 assertEquals(true, BooleanUtils.toBoolean("TRUe")); 301 assertEquals(true, BooleanUtils.toBoolean("TRuE")); 302 assertEquals(true, BooleanUtils.toBoolean("TRue")); 303 assertEquals(true, BooleanUtils.toBoolean("TrUE")); 304 assertEquals(true, BooleanUtils.toBoolean("TrUe")); 305 assertEquals(true, BooleanUtils.toBoolean("TruE")); 306 assertEquals(true, BooleanUtils.toBoolean("True")); 307 assertEquals(true, BooleanUtils.toBoolean("on")); 308 assertEquals(true, BooleanUtils.toBoolean("oN")); 309 assertEquals(true, BooleanUtils.toBoolean("On")); 310 assertEquals(true, BooleanUtils.toBoolean("ON")); 311 assertEquals(true, BooleanUtils.toBoolean("yes")); 312 assertEquals(true, BooleanUtils.toBoolean("yeS")); 313 assertEquals(true, BooleanUtils.toBoolean("yEs")); 314 assertEquals(true, BooleanUtils.toBoolean("yES")); 315 assertEquals(true, BooleanUtils.toBoolean("Yes")); 316 assertEquals(true, BooleanUtils.toBoolean("YeS")); 317 assertEquals(true, BooleanUtils.toBoolean("YEs")); 318 assertEquals(true, BooleanUtils.toBoolean("YES")); 319 } 320 321 public void test_toBoolean_String_String_String() { 322 assertEquals(true, BooleanUtils.toBoolean((String ) null, null, "N")); 323 assertEquals(false, BooleanUtils.toBoolean((String ) null, "Y", null)); 324 try { 325 BooleanUtils.toBooleanObject((String ) null, "Y", "N", "U"); 326 fail(); 327 } catch (IllegalArgumentException ex) {} 328 329 assertEquals(true, BooleanUtils.toBoolean("Y", "Y", "N")); 330 assertEquals(false, BooleanUtils.toBoolean("N", "Y", "N")); 331 try { 332 BooleanUtils.toBoolean(null, "Y", "N"); 333 fail(); 334 } catch (IllegalArgumentException ex) {} 335 try { 336 BooleanUtils.toBoolean("X", "Y", "N"); 337 fail(); 338 } catch (IllegalArgumentException ex) {} 339 } 340 341 public void test_toStringTrueFalse_Boolean() { 343 assertEquals(null, BooleanUtils.toStringTrueFalse((Boolean ) null)); 344 assertEquals("true", BooleanUtils.toStringTrueFalse(Boolean.TRUE)); 345 assertEquals("false", BooleanUtils.toStringTrueFalse(Boolean.FALSE)); 346 } 347 348 public void test_toStringOnOff_Boolean() { 349 assertEquals(null, BooleanUtils.toStringOnOff((Boolean ) null)); 350 assertEquals("on", BooleanUtils.toStringOnOff(Boolean.TRUE)); 351 assertEquals("off", BooleanUtils.toStringOnOff(Boolean.FALSE)); 352 } 353 354 public void test_toStringYesNo_Boolean() { 355 assertEquals(null, BooleanUtils.toStringYesNo((Boolean ) null)); 356 assertEquals("yes", BooleanUtils.toStringYesNo(Boolean.TRUE)); 357 assertEquals("no", BooleanUtils.toStringYesNo(Boolean.FALSE)); 358 } 359 360 public void test_toString_Boolean_String_String_String() { 361 assertEquals("U", BooleanUtils.toString((Boolean ) null, "Y", "N", "U")); 362 assertEquals("Y", BooleanUtils.toString(Boolean.TRUE, "Y", "N", "U")); 363 assertEquals("N", BooleanUtils.toString(Boolean.FALSE, "Y", "N", "U")); 364 } 365 366 public void test_toStringTrueFalse_boolean() { 368 assertEquals("true", BooleanUtils.toStringTrueFalse(true)); 369 assertEquals("false", BooleanUtils.toStringTrueFalse(false)); 370 } 371 372 public void test_toStringOnOff_boolean() { 373 assertEquals("on", BooleanUtils.toStringOnOff(true)); 374 assertEquals("off", BooleanUtils.toStringOnOff(false)); 375 } 376 377 public void test_toStringYesNo_boolean() { 378 assertEquals("yes", BooleanUtils.toStringYesNo(true)); 379 assertEquals("no", BooleanUtils.toStringYesNo(false)); 380 } 381 382 public void test_toString_boolean_String_String_String() { 383 assertEquals("Y", BooleanUtils.toString(true, "Y", "N")); 384 assertEquals("N", BooleanUtils.toString(false, "Y", "N")); 385 } 386 387 public void testXor_primitive_nullInput() { 390 final boolean[] b = null; 391 try { 392 BooleanUtils.xor(b); 393 fail("Exception was not thrown for null input."); 394 } catch (IllegalArgumentException ex) {} 395 } 396 397 public void testXor_primitive_emptyInput() { 398 try { 399 BooleanUtils.xor(new boolean[] {}); 400 fail("Exception was not thrown for empty input."); 401 } catch (IllegalArgumentException ex) {} 402 } 403 404 public void testXor_primitive_validInput_2items() { 405 assertTrue( 406 "True result for (true, true)", 407 ! BooleanUtils.xor(new boolean[] { true, true })); 408 409 assertTrue( 410 "True result for (false, false)", 411 ! BooleanUtils.xor(new boolean[] { false, false })); 412 413 assertTrue( 414 "False result for (true, false)", 415 BooleanUtils.xor(new boolean[] { true, false })); 416 417 assertTrue( 418 "False result for (false, true)", 419 BooleanUtils.xor(new boolean[] { false, true })); 420 } 421 422 public void testXor_primitive_validInput_3items() { 423 assertTrue( 424 "False result for (false, false, true)", 425 BooleanUtils.xor(new boolean[] { false, false, true })); 426 427 assertTrue( 428 "False result for (false, true, false)", 429 BooleanUtils.xor(new boolean[] { false, true, false })); 430 431 assertTrue( 432 "False result for (true, false, false)", 433 BooleanUtils.xor(new boolean[] { true, false, false })); 434 435 assertTrue( 436 "True result for (true, true, true)", 437 ! BooleanUtils.xor(new boolean[] { true, true, true })); 438 439 assertTrue( 440 "True result for (false, false)", 441 ! BooleanUtils.xor(new boolean[] { false, false, false })); 442 443 assertTrue( 444 "True result for (true, true, false)", 445 ! BooleanUtils.xor(new boolean[] { true, true, false })); 446 447 assertTrue( 448 "True result for (true, false, true)", 449 ! BooleanUtils.xor(new boolean[] { true, false, true })); 450 451 assertTrue( 452 "False result for (false, true, true)", 453 ! BooleanUtils.xor(new boolean[] { false, true, true })); 454 } 455 456 public void testXor_object_nullInput() { 457 final Boolean [] b = null; 458 try { 459 BooleanUtils.xor(b); 460 fail("Exception was not thrown for null input."); 461 } catch (IllegalArgumentException ex) {} 462 } 463 464 public void testXor_object_emptyInput() { 465 try { 466 BooleanUtils.xor(new Boolean [] {}); 467 fail("Exception was not thrown for empty input."); 468 } catch (IllegalArgumentException ex) {} 469 } 470 471 public void testXor_object_nullElementInput() { 472 try { 473 BooleanUtils.xor(new Boolean [] {null}); 474 fail("Exception was not thrown for null element input."); 475 } catch (IllegalArgumentException ex) {} 476 } 477 478 public void testXor_object_validInput_2items() { 479 assertTrue( 480 "True result for (true, true)", 481 ! BooleanUtils 482 .xor(new Boolean [] { Boolean.TRUE, Boolean.TRUE }) 483 .booleanValue()); 484 485 assertTrue( 486 "True result for (false, false)", 487 ! BooleanUtils 488 .xor(new Boolean [] { Boolean.FALSE, Boolean.FALSE }) 489 .booleanValue()); 490 491 assertTrue( 492 "False result for (true, false)", 493 BooleanUtils 494 .xor(new Boolean [] { Boolean.TRUE, Boolean.FALSE }) 495 .booleanValue()); 496 497 assertTrue( 498 "False result for (false, true)", 499 BooleanUtils 500 .xor(new Boolean [] { Boolean.FALSE, Boolean.TRUE }) 501 .booleanValue()); 502 } 503 504 public void testXor_object_validInput_3items() { 505 assertTrue( 506 "False result for (false, false, true)", 507 BooleanUtils 508 .xor( 509 new Boolean [] { 510 Boolean.FALSE, 511 Boolean.FALSE, 512 Boolean.TRUE }) 513 .booleanValue()); 514 515 assertTrue( 516 "False result for (false, true, false)", 517 BooleanUtils 518 .xor( 519 new Boolean [] { 520 Boolean.FALSE, 521 Boolean.TRUE, 522 Boolean.FALSE }) 523 .booleanValue()); 524 525 assertTrue( 526 "False result for (true, false, false)", 527 BooleanUtils 528 .xor( 529 new Boolean [] { 530 Boolean.TRUE, 531 Boolean.FALSE, 532 Boolean.FALSE }) 533 .booleanValue()); 534 535 assertTrue( 536 "True result for (true, true, true)", 537 ! BooleanUtils 538 .xor(new Boolean [] { Boolean.TRUE, Boolean.TRUE, Boolean.TRUE }) 539 .booleanValue()); 540 541 assertTrue( 542 "True result for (false, false)", 543 ! BooleanUtils.xor( 544 new Boolean [] { 545 Boolean.FALSE, 546 Boolean.FALSE, 547 Boolean.FALSE }) 548 .booleanValue()); 549 550 assertTrue( 551 "True result for (true, true, false)", 552 ! BooleanUtils.xor( 553 new Boolean [] { 554 Boolean.TRUE, 555 Boolean.TRUE, 556 Boolean.FALSE }) 557 .booleanValue()); 558 559 assertTrue( 560 "True result for (true, false, true)", 561 ! BooleanUtils.xor( 562 new Boolean [] { 563 Boolean.TRUE, 564 Boolean.FALSE, 565 Boolean.TRUE }) 566 .booleanValue()); 567 568 assertTrue( 569 "False result for (false, true, true)", 570 ! BooleanUtils.xor( 571 new Boolean [] { 572 Boolean.FALSE, 573 Boolean.TRUE, 574 Boolean.TRUE }) 575 .booleanValue()); 576 577 } 578 579 } 580 | Popular Tags |