1 17 18 package org.apache.tools.ant; 19 20 import junit.framework.TestCase; 21 import junit.framework.AssertionFailedError; 22 import java.io.File ; 23 import java.lang.reflect.Method ; 24 import java.lang.reflect.InvocationTargetException ; 25 import java.util.Collections ; 26 import java.util.Enumeration ; 27 import java.util.HashMap ; 28 import java.util.Hashtable ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Locale ; 32 import java.util.Map ; 33 import org.apache.tools.ant.taskdefs.condition.Os; 34 35 39 40 public class IntrospectionHelperTest extends TestCase { 41 42 private Project p; 43 private IntrospectionHelper ih; 44 private static final String projectBasedir = File.separator; 45 46 public IntrospectionHelperTest(String name) { 47 super(name); 48 } 49 50 public void setUp() { 51 p = new Project(); 52 p.setBasedir(projectBasedir); 53 ih = IntrospectionHelper.getHelper(getClass()); 54 } 55 56 public void testIsDynamic() { 57 assertTrue("Not dynamic", false == ih.isDynamic()); 58 } 59 60 public void testIsContainer() { 61 assertTrue("Not a container", false == ih.isContainer()); 62 } 63 64 public void testAddText() throws BuildException { 65 ih.addText(p, this, "test"); 66 try { 67 ih.addText(p, this, "test2"); 68 fail("test2 shouldn\'t be equal to test"); 69 } catch (BuildException be) { 70 assertTrue(be.getException() instanceof AssertionFailedError); 71 } 72 73 ih = IntrospectionHelper.getHelper(String .class); 74 try { 75 ih.addText(p, "", "test"); 76 fail("String doesn\'t support addText"); 77 } catch (BuildException be) { 78 } 79 } 80 81 public void testGetAddTextMethod() { 82 Method m = ih.getAddTextMethod(); 83 assertMethod(m, "addText", String .class, "test", "bing!"); 84 85 ih = IntrospectionHelper.getHelper(String .class); 86 try { 87 m = ih.getAddTextMethod(); 88 } catch (BuildException e) {} 89 } 90 91 public void testSupportsCharacters() { 92 assertTrue("IntrospectionHelperTest supports addText", 93 ih.supportsCharacters()); 94 95 ih = IntrospectionHelper.getHelper(String .class); 96 assertTrue("String doesn\'t support addText", !ih.supportsCharacters()); 97 } 98 99 public void addText(String text) { 100 assertEquals("test", text); 101 } 102 103 public void testElementCreators() throws BuildException { 104 try { 105 ih.getElementType("one"); 106 fail("don't have element type one"); 107 } catch (BuildException be) { 108 } 109 try { 110 ih.getElementType("two"); 111 fail("createTwo takes arguments"); 112 } catch (BuildException be) { 113 } 114 try { 115 ih.getElementType("three"); 116 fail("createThree returns void"); 117 } catch (BuildException be) { 118 } 119 try { 120 ih.getElementType("four"); 121 fail("createFour returns array"); 122 } catch (BuildException be) { 123 } 124 try { 125 ih.getElementType("five"); 126 fail("createFive returns primitive type"); 127 } catch (BuildException be) { 128 } 129 assertEquals(String .class, ih.getElementType("six")); 130 assertEquals("test", ih.createElement(p, this, "six")); 131 132 try { 133 ih.getElementType("seven"); 134 fail("addSeven takes two arguments"); 135 } catch (BuildException be) { 136 } 137 try { 138 ih.getElementType("eight"); 139 fail("addEight takes no arguments"); 140 } catch (BuildException be) { 141 } 142 try { 143 ih.getElementType("nine"); 144 fail("nine return non void"); 145 } catch (BuildException be) { 146 } 147 try { 148 ih.getElementType("ten"); 149 fail("addTen takes array argument"); 150 } catch (BuildException be) { 151 } 152 try { 153 ih.getElementType("eleven"); 154 fail("addEleven takes primitive argument"); 155 } catch (BuildException be) { 156 } 157 try { 158 ih.getElementType("twelve"); 159 fail("no primitive constructor for java.lang.Class"); 160 } catch (BuildException be) { 161 } 162 assertEquals(StringBuffer .class, ih.getElementType("thirteen")); 163 assertEquals("test", ih.createElement(p, this, "thirteen").toString()); 164 165 try { 166 ih.createElement(p, this, "fourteen"); 167 fail("fourteen throws NullPointerException"); 168 } catch (BuildException be) { 169 assertTrue(be.getException() instanceof NullPointerException ); 170 } 171 172 try { 173 ih.createElement(p, this, "fourteen"); 174 fail("fifteen throws NullPointerException"); 175 } catch (BuildException be) { 176 assertTrue(be.getException() instanceof NullPointerException ); 177 } 178 } 179 180 private Map getExpectedNestedElements() { 181 Map elemMap = new Hashtable (); 182 elemMap.put("six", String .class); 183 elemMap.put("thirteen", StringBuffer .class); 184 elemMap.put("fourteen", StringBuffer .class); 185 elemMap.put("fifteen", StringBuffer .class); 186 return elemMap; 187 } 188 189 public void testGetNestedElements() { 190 Map elemMap = getExpectedNestedElements(); 191 Enumeration e = ih.getNestedElements(); 192 while (e.hasMoreElements()) { 193 String name = (String ) e.nextElement(); 194 Class expect = (Class ) elemMap.get(name); 195 assertNotNull("Support for "+name+" in IntrospectioNHelperTest?", 196 expect); 197 assertEquals("Return type of "+name, expect, ih.getElementType(name)); 198 elemMap.remove(name); 199 } 200 assertTrue("Found all", elemMap.isEmpty()); 201 } 202 203 public void testGetNestedElementMap() { 204 Map elemMap = getExpectedNestedElements(); 205 Map actualMap = ih.getNestedElementMap(); 206 for (Iterator i = actualMap.entrySet().iterator(); i.hasNext();) { 207 Map.Entry entry = (Map.Entry ) i.next(); 208 String elemName = (String ) entry.getKey(); 209 Class elemClass = (Class ) elemMap.get(elemName); 210 assertNotNull("Support for " + elemName + 211 " in IntrospectionHelperTest?", elemClass); 212 assertEquals("Type of " + elemName, elemClass, entry.getValue()); 213 elemMap.remove(elemName); 214 } 215 assertTrue("Found all", elemMap.isEmpty()); 216 217 try { 219 actualMap.clear(); 220 } catch (UnsupportedOperationException e) {} 221 } 222 223 public void testGetElementMethod() { 224 assertElemMethod("six", "createSix", String .class, null); 225 assertElemMethod("thirteen", "addThirteen", null, StringBuffer .class); 226 assertElemMethod("fourteen", "addFourteen", null, StringBuffer .class); 227 assertElemMethod("fifteen", "createFifteen", StringBuffer .class, null); 228 } 229 230 private void assertElemMethod(String elemName, String methodName, 231 Class returnType, Class methodArg) { 232 Method m = ih.getElementMethod(elemName); 233 assertEquals("Method name", methodName, m.getName()); 234 Class expectedReturnType = (returnType == null)? Void.TYPE: returnType; 235 assertEquals("Return type", expectedReturnType, m.getReturnType()); 236 Class [] args = m.getParameterTypes(); 237 if (methodArg != null) { 238 assertEquals("Arg Count", 1, args.length); 239 assertEquals("Arg Type", methodArg, args[0]); 240 } else { 241 assertEquals("Arg Count", 0, args.length); 242 } 243 } 244 245 public Object createTwo(String s) { 246 return null; 247 } 248 249 public void createThree() {} 250 251 public Object [] createFour() { 252 return null; 253 } 254 255 public int createFive() { 256 return 0; 257 } 258 259 public String createSix() { 260 return "test"; 261 } 262 263 public StringBuffer createFifteen() { 264 throw new NullPointerException (); 265 } 266 267 public void addSeven(String s, String s2) {} 268 269 public void addEight() {} 270 271 public String addNine(String s) { 272 return null; 273 } 274 275 public void addTen(String [] s) {} 276 277 public void addEleven(int i) {} 278 279 public void addTwelve(Class c) {} 280 281 public void addThirteen(StringBuffer sb) { 282 sb.append("test"); 283 } 284 285 public void addFourteen(StringBuffer s) { 286 throw new NullPointerException (); 287 } 288 289 public void testAttributeSetters() throws BuildException { 290 try { 291 ih.setAttribute(p, this, "one", "test"); 292 fail("setOne doesn't exist"); 293 } catch (BuildException be) { 294 } 295 try { 296 ih.setAttribute(p, this, "two", "test"); 297 fail("setTwo returns non void"); 298 } catch (BuildException be) { 299 } 300 try { 301 ih.setAttribute(p, this, "three", "test"); 302 fail("setThree takes no args"); 303 } catch (BuildException be) { 304 } 305 try { 306 ih.setAttribute(p, this, "four", "test"); 307 fail("setFour takes two args"); 308 } catch (BuildException be) { 309 } 310 try { 311 ih.setAttribute(p, this, "five", "test"); 312 fail("setFive takes array arg"); 313 } catch (BuildException be) { 314 } 315 try { 316 ih.setAttribute(p, this, "six", "test"); 317 fail("Project doesn't have a String constructor"); 318 } catch (BuildException be) { 319 } 320 ih.setAttribute(p, this, "seven", "2"); 321 try { 322 ih.setAttribute(p, this, "seven", "3"); 323 fail("2 shouldn't be equals to three"); 324 } catch (BuildException be) { 325 assertTrue(be.getException() instanceof AssertionFailedError); 326 } 327 ih.setAttribute(p, this, "eight", "2"); 328 try { 329 ih.setAttribute(p, this, "eight", "3"); 330 fail("2 shouldn't be equals to three - as int"); 331 } catch (BuildException be) { 332 assertTrue(be.getException() instanceof AssertionFailedError); 333 } 334 ih.setAttribute(p, this, "nine", "2"); 335 try { 336 ih.setAttribute(p, this, "nine", "3"); 337 fail("2 shouldn't be equals to three - as Integer"); 338 } catch (BuildException be) { 339 assertTrue(be.getException() instanceof AssertionFailedError); 340 } 341 ih.setAttribute(p, this, "ten", "2"); 342 try { 343 ih.setAttribute(p, this, "ten", "3"); 344 fail(projectBasedir+"2 shouldn't be equals to "+projectBasedir+"3"); 345 } catch (BuildException be) { 346 assertTrue(be.getException() instanceof AssertionFailedError); 347 } 348 ih.setAttribute(p, this, "eleven", "2"); 349 try { 350 ih.setAttribute(p, this, "eleven", "on"); 351 fail("on shouldn't be false"); 352 } catch (BuildException be) { 353 assertTrue(be.getException() instanceof AssertionFailedError); 354 } 355 ih.setAttribute(p, this, "twelve", "2"); 356 try { 357 ih.setAttribute(p, this, "twelve", "on"); 358 fail("on shouldn't be false"); 359 } catch (BuildException be) { 360 assertTrue(be.getException() instanceof AssertionFailedError); 361 } 362 ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.Project"); 363 try { 364 ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.ProjectHelper"); 365 fail("org.apache.tools.ant.Project shouldn't be equal to org.apache.tools.ant.ProjectHelper"); 366 } catch (BuildException be) { 367 assertTrue(be.getException() instanceof AssertionFailedError); 368 } 369 try { 370 ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.Project2"); 371 fail("org.apache.tools.ant.Project2 doesn't exist"); 372 } catch (BuildException be) { 373 assertTrue(be.getException() instanceof ClassNotFoundException ); 374 } 375 ih.setAttribute(p, this, "fourteen", "2"); 376 try { 377 ih.setAttribute(p, this, "fourteen", "on"); 378 fail("2 shouldn't be equals to three - as StringBuffer"); 379 } catch (BuildException be) { 380 assertTrue(be.getException() instanceof AssertionFailedError); 381 } 382 ih.setAttribute(p, this, "fifteen", "abcd"); 383 try { 384 ih.setAttribute(p, this, "fifteen", "on"); 385 fail("o shouldn't be equal to a"); 386 } catch (BuildException be) { 387 assertTrue(be.getException() instanceof AssertionFailedError); 388 } 389 ih.setAttribute(p, this, "sixteen", "abcd"); 390 try { 391 ih.setAttribute(p, this, "sixteen", "on"); 392 fail("o shouldn't be equal to a"); 393 } catch (BuildException be) { 394 assertTrue(be.getException() instanceof AssertionFailedError); 395 } 396 ih.setAttribute(p, this, "seventeen", "17"); 397 try { 398 ih.setAttribute(p, this, "seventeen", "3"); 399 fail("17 shouldn't be equals to three"); 400 } catch (BuildException be) { 401 assertTrue(be.getException() instanceof AssertionFailedError); 402 } 403 ih.setAttribute(p, this, "eightteen", "18"); 404 try { 405 ih.setAttribute(p, this, "eightteen", "3"); 406 fail("18 shouldn't be equals to three"); 407 } catch (BuildException be) { 408 assertTrue(be.getException() instanceof AssertionFailedError); 409 } 410 ih.setAttribute(p, this, "nineteen", "19"); 411 try { 412 ih.setAttribute(p, this, "nineteen", "3"); 413 fail("19 shouldn't be equals to three"); 414 } catch (BuildException be) { 415 assertTrue(be.getException() instanceof AssertionFailedError); 416 } 417 } 418 419 private Map getExpectedAttributes() { 420 Map attrMap = new Hashtable (); 421 attrMap.put("seven", String .class); 422 attrMap.put("eight", Integer.TYPE); 423 attrMap.put("nine", Integer .class); 424 attrMap.put("ten", File .class); 425 attrMap.put("eleven", Boolean.TYPE); 426 attrMap.put("twelve", Boolean .class); 427 attrMap.put("thirteen", Class .class); 428 attrMap.put("fourteen", StringBuffer .class); 429 attrMap.put("fifteen", Character.TYPE); 430 attrMap.put("sixteen", Character .class); 431 attrMap.put("seventeen", Byte.TYPE); 432 attrMap.put("eightteen", Short.TYPE); 433 attrMap.put("nineteen", Double.TYPE); 434 435 442 attrMap.put("name", String .class); 443 444 return attrMap; 445 } 446 447 public void testGetAttributes() { 448 Map attrMap = getExpectedAttributes(); 449 Enumeration e = ih.getAttributes(); 450 while (e.hasMoreElements()) { 451 String name = (String ) e.nextElement(); 452 Class expect = (Class ) attrMap.get(name); 453 assertNotNull("Support for "+name+" in IntrospectionHelperTest?", 454 expect); 455 assertEquals("Type of "+name, expect, ih.getAttributeType(name)); 456 attrMap.remove(name); 457 } 458 attrMap.remove("name"); 459 assertTrue("Found all", attrMap.isEmpty()); 460 } 461 462 public void testGetAttributeMap() { 463 Map attrMap = getExpectedAttributes(); 464 Map actualMap = ih.getAttributeMap(); 465 for (Iterator i = actualMap.entrySet().iterator(); i.hasNext();) { 466 Map.Entry entry = (Map.Entry ) i.next(); 467 String attrName = (String ) entry.getKey(); 468 Class attrClass = (Class ) attrMap.get(attrName); 469 assertNotNull("Support for " + attrName + 470 " in IntrospectionHelperTest?", attrClass); 471 assertEquals("Type of " + attrName, attrClass, entry.getValue()); 472 attrMap.remove(attrName); 473 } 474 attrMap.remove("name"); 475 assertTrue("Found all", attrMap.isEmpty()); 476 477 try { 479 actualMap.clear(); 480 } catch (UnsupportedOperationException e) {} 481 } 482 483 public void testGetAttributeMethod() { 484 assertAttrMethod("seven", "setSeven", String .class, 485 "2", "3"); 486 assertAttrMethod("eight", "setEight", Integer.TYPE, 487 new Integer (2), new Integer (3)); 488 assertAttrMethod("nine", "setNine", Integer .class, 489 new Integer (2), new Integer (3)); 490 assertAttrMethod("ten", "setTen", File .class, 491 new File (projectBasedir + 2), new File ("toto")); 492 assertAttrMethod("eleven", "setEleven", Boolean.TYPE, 493 Boolean.FALSE, Boolean.TRUE); 494 assertAttrMethod("twelve", "setTwelve", Boolean .class, 495 Boolean.FALSE, Boolean.TRUE); 496 assertAttrMethod("thirteen", "setThirteen", Class .class, 497 Project.class, Map .class); 498 assertAttrMethod("fourteen", "setFourteen", StringBuffer .class, 499 new StringBuffer ("2"), new StringBuffer ("3")); 500 assertAttrMethod("fifteen", "setFifteen", Character.TYPE, 501 new Character ('a'), new Character ('b')); 502 assertAttrMethod("sixteen", "setSixteen", Character .class, 503 new Character ('a'), new Character ('b')); 504 assertAttrMethod("seventeen", "setSeventeen", Byte.TYPE, 505 new Byte ((byte)17), new Byte ((byte)10)); 506 assertAttrMethod("eightteen", "setEightteen", Short.TYPE, 507 new Short ((short)18), new Short ((short)10)); 508 assertAttrMethod("nineteen", "setNineteen", Double.TYPE, 509 new Double (19), new Double ((short)10)); 510 511 try { 512 assertAttrMethod("onehundred", null, null, null, null); 513 fail("Should have raised a BuildException!"); 514 } catch (BuildException e) {} 515 } 516 517 private void assertAttrMethod(String attrName, String methodName, 518 Class methodArg, Object arg, Object badArg) { 519 Method m = ih.getAttributeMethod(attrName); 520 assertMethod(m, methodName, methodArg, arg, badArg); 521 } 522 523 public int setTwo(String s) { 524 return 0; 525 } 526 527 public void setThree() {} 528 529 public void setFour(String s1, String s2) {} 530 531 public void setFive(String [] s) {} 532 533 public void setSix(Project p) {} 534 535 public void setSeven(String s) { 536 assertEquals("2", s); 537 } 538 539 public void setEight(int i) { 540 assertEquals(2, i); 541 } 542 543 public void setNine(Integer i) { 544 assertEquals(2, i.intValue()); 545 } 546 547 public void setTen(File f) { 548 String path = f.getAbsolutePath(); 549 if (Os.isFamily("unix") || Os.isFamily("openvms")) { 550 assertEquals(projectBasedir+"2", path); 551 } else if (Os.isFamily("netware")) { 552 assertEquals(projectBasedir+"2", path.toLowerCase(Locale.US)); 553 } else { 554 assertEquals(":"+projectBasedir+"2", 555 path.toLowerCase(Locale.US).substring(1)); 556 } 557 } 558 559 public void setEleven(boolean b) { 560 assertTrue(!b); 561 } 562 563 public void setTwelve(Boolean b) { 564 assertTrue(!b.booleanValue()); 565 } 566 567 public void setThirteen(Class c) { 568 assertEquals(Project.class, c); 569 } 570 571 public void setFourteen(StringBuffer sb) { 572 assertEquals("2", sb.toString()); 573 } 574 575 public void setFifteen(char c) { 576 assertEquals(c, 'a'); 577 } 578 579 public void setSixteen(Character c) { 580 assertEquals(c.charValue(), 'a'); 581 } 582 583 public void setSeventeen(byte b) { 584 assertEquals(17, b); 585 } 586 587 public void setEightteen(short s) { 588 assertEquals(18, s); 589 } 590 591 public void setNineteen(double d) { 592 assertEquals(19, d, 1e-6); 593 } 594 595 public void testGetExtensionPoints() { 596 List extensions = ih.getExtensionPoints(); 597 final int adders = 2; 598 assertEquals("extension count", adders, extensions.size()); 599 600 610 assertExtMethod(extensions.get(adders - 2), 612 "addConfigured", Hashtable .class, 613 makeTable("key", "value"), makeTable("1", "2")); 614 615 assertExtMethod(extensions.get(adders - 1), "addConfigured", Map .class, 616 new HashMap (), makeTable("1", "2")); 617 } 618 619 private void assertExtMethod(Object mo, String methodName, Class methodArg, 620 Object arg, Object badArg) { 621 assertMethod((Method ) mo, methodName, methodArg, arg, badArg); 622 } 623 624 private void assertMethod(Method m, String methodName, Class methodArg, 625 Object arg, Object badArg) { 626 assertEquals("Method name", methodName, m.getName()); 627 assertEquals("Return type", Void.TYPE, m.getReturnType()); 628 Class [] args = m.getParameterTypes(); 629 assertEquals("Arg Count", 1, args.length); 630 assertEquals("Arg Type", methodArg, args[0]); 631 632 try { 633 m.invoke(this, new Object [] { arg }); 634 } catch (IllegalAccessException e) { 635 throw new BuildException(e); 636 } catch (InvocationTargetException e) { 637 throw new BuildException(e); 638 } 639 640 try { 641 m.invoke(this, new Object [] { badArg }); 642 fail("Should have raised an assertion exception"); 643 } catch (IllegalAccessException e) { 644 throw new BuildException(e); 645 } catch (InvocationTargetException e) { 646 Throwable t = e.getTargetException(); 647 assertTrue(t instanceof junit.framework.AssertionFailedError); 648 } 649 } 650 651 public List add(List l) { 652 return null; 654 } 655 656 662 public void add(List l, int i) { 663 } 665 666 public void addConfigured(Map m) { 667 assertTrue(m.size() == 0); 669 } 670 671 public void addConfigured(Hashtable h) { 672 assertEquals(makeTable("key", "value"), h); 674 } 675 676 private Hashtable makeTable(Object key, Object value) { 677 Hashtable table = new Hashtable (); 678 table.put(key, value); 679 return table; 680 } 681 682 } 684 | Popular Tags |