1 17 18 package org.apache.tools.ant.taskdefs; 19 20 import java.io.File ; 21 22 import junit.framework.AssertionFailedError; 23 24 import org.apache.tools.ant.BuildEvent; 25 import org.apache.tools.ant.BuildFileTest; 26 import org.apache.tools.ant.BuildListener; 27 import org.apache.tools.ant.input.InputHandler; 28 import org.apache.tools.ant.input.PropertyFileInputHandler; 29 import org.apache.tools.ant.types.Path; 30 31 33 public class AntTest extends BuildFileTest { 34 35 public AntTest(String name) { 36 super(name); 37 } 38 39 public void setUp() { 40 configureProject("src/etc/testcases/taskdefs/ant.xml"); 41 } 42 43 public void tearDown() { 44 executeTarget("cleanup"); 45 } 46 47 public void test1() { 48 expectBuildException("test1", "recursive call"); 49 } 50 51 public void test2() { 53 expectBuildException("test2", "required argument not specified"); 54 } 55 56 public void test3() { 58 expectBuildException("test1", "recursive call"); 59 } 60 61 public void test4() { 62 expectBuildException("test4", "target attribute must not be empty"); 63 } 64 65 public void test4b() { 66 expectBuildException("test4b", "target doesn't exist"); 67 } 68 69 public void test5() { 70 executeTarget("test5"); 71 } 72 73 public void test6() { 74 executeTarget("test6"); 75 } 76 77 public void testExplicitBasedir1() { 78 File dir1 = getProjectDir(); 79 File dir2 = project.resolveFile(".."); 80 testBaseDirs("explicitBasedir1", 81 new String [] {dir1.getAbsolutePath(), 82 dir2.getAbsolutePath() 83 }); 84 } 85 86 public void testExplicitBasedir2() { 87 File dir1 = getProjectDir(); 88 File dir2 = project.resolveFile(".."); 89 testBaseDirs("explicitBasedir2", 90 new String [] {dir1.getAbsolutePath(), 91 dir2.getAbsolutePath() 92 }); 93 } 94 95 public void testInheritBasedir() { 96 String basedir = getProjectDir().getAbsolutePath(); 97 testBaseDirs("inheritBasedir", new String [] {basedir, basedir}); 98 } 99 100 public void testDoNotInheritBasedir() { 101 File dir1 = getProjectDir(); 102 File dir2 = project.resolveFile("ant"); 103 String basedir = getProjectDir().getAbsolutePath(); 104 testBaseDirs("doNotInheritBasedir", 105 new String [] {dir1.getAbsolutePath(), 106 dir2.getAbsolutePath() 107 }); 108 } 109 110 public void testBasedirTripleCall() { 111 File dir1 = getProjectDir(); 112 File dir2 = project.resolveFile("ant"); 113 testBaseDirs("tripleCall", 114 new String [] {dir1.getAbsolutePath(), 115 dir2.getAbsolutePath(), 116 dir1.getAbsolutePath() 117 }); 118 } 119 120 protected void testBaseDirs(String target, String [] dirs) { 121 BasedirChecker bc = new BasedirChecker(dirs); 122 project.addBuildListener(bc); 123 executeTarget(target); 124 AssertionFailedError ae = bc.getError(); 125 if (ae != null) { 126 throw ae; 127 } 128 project.removeBuildListener(bc); 129 } 130 131 public void testReferenceInheritance() { 132 Path p = Path.systemClasspath; 133 p.setProject(project); 134 project.addReference("path", p); 135 project.addReference("no-override", p); 136 testReference("testInherit", new String [] {"path", "path"}, 137 new boolean[] {true, true}, p); 138 testReference("testInherit", 139 new String [] {"no-override", "no-override"}, 140 new boolean[] {true, false}, p); 141 testReference("testInherit", 142 new String [] {"no-override", "no-override"}, 143 new boolean[] {false, false}, null); 144 } 145 146 public void testReferenceNoInheritance() { 147 Path p = Path.systemClasspath; 148 p.setProject(project); 149 project.addReference("path", p); 150 project.addReference("no-override", p); 151 testReference("testNoInherit", new String [] {"path", "path"}, 152 new boolean[] {true, false}, p); 153 testReference("testNoInherit", new String [] {"path", "path"}, 154 new boolean[] {false, true}, null); 155 testReference("testInherit", 156 new String [] {"no-override", "no-override"}, 157 new boolean[] {true, false}, p); 158 testReference("testInherit", 159 new String [] {"no-override", "no-override"}, 160 new boolean[] {false, false}, null); 161 } 162 163 public void testReferenceRename() { 164 Path p = Path.systemClasspath; 165 p.setProject(project); 166 project.addReference("path", p); 167 testReference("testRename", new String [] {"path", "path"}, 168 new boolean[] {true, false}, p); 169 testReference("testRename", new String [] {"path", "path"}, 170 new boolean[] {false, true}, null); 171 testReference("testRename", new String [] {"newpath", "newpath"}, 172 new boolean[] {false, true}, p); 173 } 174 175 public void testInheritPath() { 176 executeTarget("testInheritPath"); 177 } 178 179 protected void testReference(String target, String [] keys, 180 boolean[] expect, Object value) { 181 ReferenceChecker rc = new ReferenceChecker(keys, expect, value); 182 project.addBuildListener(rc); 183 executeTarget(target); 184 AssertionFailedError ae = rc.getError(); 185 if (ae != null) { 186 throw ae; 187 } 188 project.removeBuildListener(rc); 189 } 190 191 public void testLogfilePlacement() { 192 File [] logFiles = new File [] { 193 getProject().resolveFile("test1.log"), 194 getProject().resolveFile("test2.log"), 195 getProject().resolveFile("ant/test3.log"), 196 getProject().resolveFile("ant/test4.log") 197 }; 198 for (int i=0; i<logFiles.length; i++) { 199 assertTrue(logFiles[i].getName()+" doesn\'t exist", 200 !logFiles[i].exists()); 201 } 202 203 executeTarget("testLogfilePlacement"); 204 205 for (int i=0; i<logFiles.length; i++) { 206 assertTrue(logFiles[i].getName()+" exists", 207 logFiles[i].exists()); 208 } 209 } 210 211 public void testInputHandlerInheritance() { 212 InputHandler ih = new PropertyFileInputHandler(); 213 getProject().setInputHandler(ih); 214 InputHandlerChecker ic = new InputHandlerChecker(ih); 215 getProject().addBuildListener(ic); 216 executeTarget("tripleCall"); 217 AssertionFailedError ae = ic.getError(); 218 if (ae != null) { 219 throw ae; 220 } 221 getProject().removeBuildListener(ic); 222 } 223 224 public void testRefId() { 225 Path testPath = new Path(project); 226 testPath.createPath().setPath(System.getProperty("java.class.path")); 227 PropertyChecker pc = 228 new PropertyChecker("testprop", 229 new String [] {null, 230 testPath.toString()}); 231 project.addBuildListener(pc); 232 executeTarget("testRefid"); 233 AssertionFailedError ae = pc.getError(); 234 if (ae != null) { 235 throw ae; 236 } 237 project.removeBuildListener(pc); 238 } 239 240 public void testUserPropertyWinsInheritAll() { 241 getProject().setUserProperty("test", "7"); 242 expectLogContaining("test-property-override-inheritall-start", 243 "The value of test is 7"); 244 } 245 246 public void testUserPropertyWinsNoInheritAll() { 247 getProject().setUserProperty("test", "7"); 248 expectLogContaining("test-property-override-no-inheritall-start", 249 "The value of test is 7"); 250 } 251 252 public void testOverrideWinsInheritAll() { 253 expectLogContaining("test-property-override-inheritall-start", 254 "The value of test is 4"); 255 } 256 257 public void testOverrideWinsNoInheritAll() { 258 expectLogContaining("test-property-override-no-inheritall-start", 259 "The value of test is 4"); 260 } 261 262 public void testPropertySet() { 263 executeTarget("test-propertyset"); 264 assertTrue(getLog().indexOf("test1 is ${test1}") > -1); 265 assertTrue(getLog().indexOf("test2 is ${test2}") > -1); 266 assertTrue(getLog().indexOf("test1.x is 1") > -1); 267 } 268 269 public void testInfiniteLoopViaDepends() { 270 expectBuildException("infinite-loop-via-depends", "recursive call"); 271 } 272 273 public void testMultiSameProperty() { 274 expectLog("multi-same-property", "prop is two"); 275 } 276 277 public void testTopLevelTarget() { 278 expectLog("topleveltarget", "Hello world"); 279 } 280 281 public void testMultiplePropertyFileChildren() { 282 PropertyChecker pcBar = new PropertyChecker("bar", 283 new String [] {null, "Bar"}); 284 PropertyChecker pcFoo = new PropertyChecker("foo", 285 new String [] {null, "Foo"}); 286 project.addBuildListener(pcBar); 287 project.addBuildListener(pcFoo); 288 executeTarget("multiple-property-file-children"); 289 AssertionFailedError aeBar = pcBar.getError(); 290 if (aeBar != null) { 291 throw aeBar; 292 } 293 AssertionFailedError aeFoo = pcFoo.getError(); 294 if (aeFoo != null) { 295 throw aeFoo; 296 } 297 project.removeBuildListener(pcBar); 298 project.removeBuildListener(pcFoo); 299 } 300 301 public void testBlankTarget() { 302 expectBuildException("blank-target", "target name must not be empty"); 303 } 304 305 public void testMultipleTargets() { 306 expectLog("multiple-targets", "tadadctbdbtc"); 307 } 308 309 public void testMultipleTargets2() { 310 expectLog("multiple-targets-2", "dadctb"); 311 } 312 313 private class BasedirChecker implements BuildListener { 314 private String [] expectedBasedirs; 315 private int calls = 0; 316 private AssertionFailedError error; 317 318 BasedirChecker(String [] dirs) { 319 expectedBasedirs = dirs; 320 } 321 322 public void buildStarted(BuildEvent event) {} 323 public void buildFinished(BuildEvent event) {} 324 public void targetFinished(BuildEvent event){} 325 public void taskStarted(BuildEvent event) {} 326 public void taskFinished(BuildEvent event) {} 327 public void messageLogged(BuildEvent event) {} 328 329 public void targetStarted(BuildEvent event) { 330 if (event.getTarget().getName().equals("")) { 331 return; 332 } 333 if (error == null) { 334 try { 335 assertEquals(expectedBasedirs[calls++], 336 event.getProject().getBaseDir().getAbsolutePath()); 337 } catch (AssertionFailedError e) { 338 error = e; 339 } 340 } 341 } 342 343 AssertionFailedError getError() { 344 return error; 345 } 346 347 } 348 349 private class ReferenceChecker implements BuildListener { 350 private String [] keys; 351 private boolean[] expectSame; 352 private Object value; 353 private int calls = 0; 354 private AssertionFailedError error; 355 356 ReferenceChecker(String [] keys, boolean[] expectSame, Object value) { 357 this.keys = keys; 358 this.expectSame = expectSame; 359 this.value = value; 360 } 361 362 public void buildStarted(BuildEvent event) {} 363 public void buildFinished(BuildEvent event) {} 364 public void targetFinished(BuildEvent event){} 365 public void taskStarted(BuildEvent event) {} 366 public void taskFinished(BuildEvent event) {} 367 public void messageLogged(BuildEvent event) {} 368 369 public void targetStarted(BuildEvent event) { 370 if (event.getTarget().getName().equals("")) { 371 return; 372 } 373 if (error == null) { 374 try { 375 String msg = 376 "Call " + calls + " refid=\'" + keys[calls] + "\'"; 377 if (value == null) { 378 Object o = event.getProject().getReference(keys[calls]); 379 if (expectSame[calls++]) { 380 assertNull(msg, o); 381 } else { 382 assertNotNull(msg, o); 383 } 384 } else { 385 Path expect = (Path) value; 387 Path received = (Path) event.getProject().getReference(keys[calls]); 388 boolean shouldBeEqual = expectSame[calls++]; 389 if (received == null) { 390 assertTrue(msg, !shouldBeEqual); 391 } else { 392 String [] l1 = expect.list(); 393 String [] l2 = received.list(); 394 if (l1.length == l2.length) { 395 for (int i=0; i<l1.length; i++) { 396 if (!l1[i].equals(l2[i])) { 397 assertTrue(msg, !shouldBeEqual); 398 } 399 } 400 assertTrue(msg, shouldBeEqual); 401 } else { 402 assertTrue(msg, !shouldBeEqual); 403 } 404 } 405 } 406 } catch (AssertionFailedError e) { 407 error = e; 408 } 409 } 410 } 411 412 AssertionFailedError getError() { 413 return error; 414 } 415 416 } 417 418 private class InputHandlerChecker implements BuildListener { 419 private InputHandler ih; 420 private AssertionFailedError error; 421 422 InputHandlerChecker(InputHandler value) { 423 ih = value; 424 } 425 426 public void buildStarted(BuildEvent event) { 427 check(event); 428 } 429 public void buildFinished(BuildEvent event) { 430 check(event); 431 } 432 public void targetFinished(BuildEvent event) { 433 check(event); 434 } 435 public void taskStarted(BuildEvent event) { 436 check(event); 437 } 438 public void taskFinished(BuildEvent event) { 439 check(event); 440 } 441 public void messageLogged(BuildEvent event) { 442 check(event); 443 } 444 445 public void targetStarted(BuildEvent event) { 446 check(event); 447 } 448 449 private void check(BuildEvent event) { 450 if (error == null) { 451 try { 452 assertNotNull(event.getProject().getInputHandler()); 453 assertSame(ih, event.getProject().getInputHandler()); 454 } catch (AssertionFailedError e) { 455 error = e; 456 } 457 } 458 } 459 460 AssertionFailedError getError() { 461 return error; 462 } 463 464 } 465 466 private class PropertyChecker implements BuildListener { 467 private String [] expectedValues; 468 private String key; 469 private int calls = 0; 470 private AssertionFailedError error; 471 472 PropertyChecker(String key, String [] values) { 473 this.key = key; 474 this.expectedValues = values; 475 } 476 477 public void buildStarted(BuildEvent event) {} 478 public void buildFinished(BuildEvent event) {} 479 public void targetFinished(BuildEvent event){} 480 public void taskStarted(BuildEvent event) {} 481 public void taskFinished(BuildEvent event) {} 482 public void messageLogged(BuildEvent event) {} 483 484 public void targetStarted(BuildEvent event) { 485 if (event.getTarget().getName().equals("")) { 486 return; 487 } 488 if (calls >= expectedValues.length) { 489 error = new AssertionFailedError("Unexpected invocation of" 490 + " target " 491 + event.getTarget().getName()); 492 } 493 494 if (error == null) { 495 try { 496 assertEquals(expectedValues[calls++], 497 event.getProject().getProperty(key)); 498 } catch (AssertionFailedError e) { 499 error = e; 500 } 501 } 502 } 503 504 AssertionFailedError getError() { 505 return error; 506 } 507 508 } 509 510 511 } 512 | Popular Tags |