001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs;
020:
021: import java.io.File;
022:
023: import junit.framework.AssertionFailedError;
024:
025: import org.apache.tools.ant.BuildEvent;
026: import org.apache.tools.ant.BuildFileTest;
027: import org.apache.tools.ant.BuildListener;
028: import org.apache.tools.ant.input.InputHandler;
029: import org.apache.tools.ant.input.PropertyFileInputHandler;
030: import org.apache.tools.ant.types.Path;
031:
032: /**
033: */
034: public class AntTest extends BuildFileTest {
035:
036: public AntTest(String name) {
037: super (name);
038: }
039:
040: public void setUp() {
041: configureProject("src/etc/testcases/taskdefs/ant.xml");
042: }
043:
044: public void tearDown() {
045: executeTarget("cleanup");
046: }
047:
048: public void test1() {
049: expectBuildException("test1", "recursive call");
050: }
051:
052: // target must be specified
053: public void test2() {
054: expectBuildException("test2", "required argument not specified");
055: }
056:
057: // Should fail since a recursion will occur...
058: public void test3() {
059: expectBuildException("test1", "recursive call");
060: }
061:
062: public void test4() {
063: expectBuildException("test4",
064: "target attribute must not be empty");
065: }
066:
067: public void test4b() {
068: expectBuildException("test4b", "target doesn't exist");
069: }
070:
071: public void test5() {
072: executeTarget("test5");
073: }
074:
075: public void test6() {
076: executeTarget("test6");
077: }
078:
079: public void testExplicitBasedir1() {
080: File dir1 = getProjectDir();
081: File dir2 = project.resolveFile("..");
082: testBaseDirs("explicitBasedir1", new String[] {
083: dir1.getAbsolutePath(), dir2.getAbsolutePath() });
084: }
085:
086: public void testExplicitBasedir2() {
087: File dir1 = getProjectDir();
088: File dir2 = project.resolveFile("..");
089: testBaseDirs("explicitBasedir2", new String[] {
090: dir1.getAbsolutePath(), dir2.getAbsolutePath() });
091: }
092:
093: public void testInheritBasedir() {
094: String basedir = getProjectDir().getAbsolutePath();
095: testBaseDirs("inheritBasedir",
096: new String[] { basedir, basedir });
097: }
098:
099: public void testDoNotInheritBasedir() {
100: File dir1 = getProjectDir();
101: File dir2 = project.resolveFile("ant");
102: String basedir = getProjectDir().getAbsolutePath();
103: testBaseDirs("doNotInheritBasedir", new String[] {
104: dir1.getAbsolutePath(), dir2.getAbsolutePath() });
105: }
106:
107: public void testBasedirTripleCall() {
108: File dir1 = getProjectDir();
109: File dir2 = project.resolveFile("ant");
110: testBaseDirs("tripleCall", new String[] {
111: dir1.getAbsolutePath(), dir2.getAbsolutePath(),
112: dir1.getAbsolutePath() });
113: }
114:
115: protected void testBaseDirs(String target, String[] dirs) {
116: BasedirChecker bc = new BasedirChecker(dirs);
117: project.addBuildListener(bc);
118: executeTarget(target);
119: AssertionFailedError ae = bc.getError();
120: if (ae != null) {
121: throw ae;
122: }
123: project.removeBuildListener(bc);
124: }
125:
126: public void testReferenceInheritance() {
127: Path p = Path.systemClasspath;
128: p.setProject(project);
129: project.addReference("path", p);
130: project.addReference("no-override", p);
131: testReference("testInherit", new String[] { "path", "path" },
132: new boolean[] { true, true }, p);
133: testReference("testInherit", new String[] { "no-override",
134: "no-override" }, new boolean[] { true, false }, p);
135: testReference("testInherit", new String[] { "no-override",
136: "no-override" }, new boolean[] { false, false }, null);
137: }
138:
139: public void testReferenceNoInheritance() {
140: Path p = Path.systemClasspath;
141: p.setProject(project);
142: project.addReference("path", p);
143: project.addReference("no-override", p);
144: testReference("testNoInherit", new String[] { "path", "path" },
145: new boolean[] { true, false }, p);
146: testReference("testNoInherit", new String[] { "path", "path" },
147: new boolean[] { false, true }, null);
148: testReference("testInherit", new String[] { "no-override",
149: "no-override" }, new boolean[] { true, false }, p);
150: testReference("testInherit", new String[] { "no-override",
151: "no-override" }, new boolean[] { false, false }, null);
152: }
153:
154: public void testReferenceRename() {
155: Path p = Path.systemClasspath;
156: p.setProject(project);
157: project.addReference("path", p);
158: testReference("testRename", new String[] { "path", "path" },
159: new boolean[] { true, false }, p);
160: testReference("testRename", new String[] { "path", "path" },
161: new boolean[] { false, true }, null);
162: testReference("testRename",
163: new String[] { "newpath", "newpath" }, new boolean[] {
164: false, true }, p);
165: }
166:
167: public void testInheritPath() {
168: executeTarget("testInheritPath");
169: }
170:
171: protected void testReference(String target, String[] keys,
172: boolean[] expect, Object value) {
173: ReferenceChecker rc = new ReferenceChecker(keys, expect, value);
174: project.addBuildListener(rc);
175: executeTarget(target);
176: AssertionFailedError ae = rc.getError();
177: if (ae != null) {
178: throw ae;
179: }
180: project.removeBuildListener(rc);
181: }
182:
183: public void testLogfilePlacement() {
184: File[] logFiles = new File[] {
185: getProject().resolveFile("test1.log"),
186: getProject().resolveFile("test2.log"),
187: getProject().resolveFile("ant/test3.log"),
188: getProject().resolveFile("ant/test4.log") };
189: for (int i = 0; i < logFiles.length; i++) {
190: assertTrue(logFiles[i].getName() + " doesn\'t exist",
191: !logFiles[i].exists());
192: }
193:
194: executeTarget("testLogfilePlacement");
195:
196: for (int i = 0; i < logFiles.length; i++) {
197: assertTrue(logFiles[i].getName() + " exists", logFiles[i]
198: .exists());
199: }
200: }
201:
202: public void testInputHandlerInheritance() {
203: InputHandler ih = new PropertyFileInputHandler();
204: getProject().setInputHandler(ih);
205: InputHandlerChecker ic = new InputHandlerChecker(ih);
206: getProject().addBuildListener(ic);
207: executeTarget("tripleCall");
208: AssertionFailedError ae = ic.getError();
209: if (ae != null) {
210: throw ae;
211: }
212: getProject().removeBuildListener(ic);
213: }
214:
215: public void testRefId() {
216: Path testPath = new Path(project);
217: testPath.createPath().setPath(
218: System.getProperty("java.class.path"));
219: PropertyChecker pc = new PropertyChecker("testprop",
220: new String[] { null, testPath.toString() });
221: project.addBuildListener(pc);
222: executeTarget("testRefid");
223: AssertionFailedError ae = pc.getError();
224: if (ae != null) {
225: throw ae;
226: }
227: project.removeBuildListener(pc);
228: }
229:
230: public void testUserPropertyWinsInheritAll() {
231: getProject().setUserProperty("test", "7");
232: expectLogContaining("test-property-override-inheritall-start",
233: "The value of test is 7");
234: }
235:
236: public void testUserPropertyWinsNoInheritAll() {
237: getProject().setUserProperty("test", "7");
238: expectLogContaining(
239: "test-property-override-no-inheritall-start",
240: "The value of test is 7");
241: }
242:
243: public void testOverrideWinsInheritAll() {
244: expectLogContaining("test-property-override-inheritall-start",
245: "The value of test is 4");
246: }
247:
248: public void testOverrideWinsNoInheritAll() {
249: expectLogContaining(
250: "test-property-override-no-inheritall-start",
251: "The value of test is 4");
252: }
253:
254: public void testPropertySet() {
255: executeTarget("test-propertyset");
256: assertTrue(getLog().indexOf("test1 is ${test1}") > -1);
257: assertTrue(getLog().indexOf("test2 is ${test2}") > -1);
258: assertTrue(getLog().indexOf("test1.x is 1") > -1);
259: }
260:
261: public void testInfiniteLoopViaDepends() {
262: expectBuildException("infinite-loop-via-depends",
263: "recursive call");
264: }
265:
266: public void testMultiSameProperty() {
267: expectLog("multi-same-property", "prop is two");
268: }
269:
270: public void testTopLevelTarget() {
271: expectLog("topleveltarget", "Hello world");
272: }
273:
274: public void testMultiplePropertyFileChildren() {
275: PropertyChecker pcBar = new PropertyChecker("bar",
276: new String[] { null, "Bar" });
277: PropertyChecker pcFoo = new PropertyChecker("foo",
278: new String[] { null, "Foo" });
279: project.addBuildListener(pcBar);
280: project.addBuildListener(pcFoo);
281: executeTarget("multiple-property-file-children");
282: AssertionFailedError aeBar = pcBar.getError();
283: if (aeBar != null) {
284: throw aeBar;
285: }
286: AssertionFailedError aeFoo = pcFoo.getError();
287: if (aeFoo != null) {
288: throw aeFoo;
289: }
290: project.removeBuildListener(pcBar);
291: project.removeBuildListener(pcFoo);
292: }
293:
294: public void testBlankTarget() {
295: expectBuildException("blank-target",
296: "target name must not be empty");
297: }
298:
299: public void testMultipleTargets() {
300: expectLog("multiple-targets", "tadadctbdbtc");
301: }
302:
303: public void testMultipleTargets2() {
304: expectLog("multiple-targets-2", "dadctb");
305: }
306:
307: private class BasedirChecker implements BuildListener {
308: private String[] expectedBasedirs;
309: private int calls = 0;
310: private AssertionFailedError error;
311:
312: BasedirChecker(String[] dirs) {
313: expectedBasedirs = dirs;
314: }
315:
316: public void buildStarted(BuildEvent event) {
317: }
318:
319: public void buildFinished(BuildEvent event) {
320: }
321:
322: public void targetFinished(BuildEvent event) {
323: }
324:
325: public void taskStarted(BuildEvent event) {
326: }
327:
328: public void taskFinished(BuildEvent event) {
329: }
330:
331: public void messageLogged(BuildEvent event) {
332: }
333:
334: public void targetStarted(BuildEvent event) {
335: if (event.getTarget().getName().equals("")) {
336: return;
337: }
338: if (error == null) {
339: try {
340: assertEquals(expectedBasedirs[calls++], event
341: .getProject().getBaseDir()
342: .getAbsolutePath());
343: } catch (AssertionFailedError e) {
344: error = e;
345: }
346: }
347: }
348:
349: AssertionFailedError getError() {
350: return error;
351: }
352:
353: }
354:
355: private class ReferenceChecker implements BuildListener {
356: private String[] keys;
357: private boolean[] expectSame;
358: private Object value;
359: private int calls = 0;
360: private AssertionFailedError error;
361:
362: ReferenceChecker(String[] keys, boolean[] expectSame,
363: Object value) {
364: this .keys = keys;
365: this .expectSame = expectSame;
366: this .value = value;
367: }
368:
369: public void buildStarted(BuildEvent event) {
370: }
371:
372: public void buildFinished(BuildEvent event) {
373: }
374:
375: public void targetFinished(BuildEvent event) {
376: }
377:
378: public void taskStarted(BuildEvent event) {
379: }
380:
381: public void taskFinished(BuildEvent event) {
382: }
383:
384: public void messageLogged(BuildEvent event) {
385: }
386:
387: public void targetStarted(BuildEvent event) {
388: if (event.getTarget().getName().equals("")) {
389: return;
390: }
391: if (error == null) {
392: try {
393: String msg = "Call " + calls + " refid=\'"
394: + keys[calls] + "\'";
395: if (value == null) {
396: Object o = event.getProject().getReference(
397: keys[calls]);
398: if (expectSame[calls++]) {
399: assertNull(msg, o);
400: } else {
401: assertNotNull(msg, o);
402: }
403: } else {
404: // a rather convoluted equals() test
405: Path expect = (Path) value;
406: Path received = (Path) event.getProject()
407: .getReference(keys[calls]);
408: boolean shouldBeEqual = expectSame[calls++];
409: if (received == null) {
410: assertTrue(msg, !shouldBeEqual);
411: } else {
412: String[] l1 = expect.list();
413: String[] l2 = received.list();
414: if (l1.length == l2.length) {
415: for (int i = 0; i < l1.length; i++) {
416: if (!l1[i].equals(l2[i])) {
417: assertTrue(msg, !shouldBeEqual);
418: }
419: }
420: assertTrue(msg, shouldBeEqual);
421: } else {
422: assertTrue(msg, !shouldBeEqual);
423: }
424: }
425: }
426: } catch (AssertionFailedError e) {
427: error = e;
428: }
429: }
430: }
431:
432: AssertionFailedError getError() {
433: return error;
434: }
435:
436: }
437:
438: private class InputHandlerChecker implements BuildListener {
439: private InputHandler ih;
440: private AssertionFailedError error;
441:
442: InputHandlerChecker(InputHandler value) {
443: ih = value;
444: }
445:
446: public void buildStarted(BuildEvent event) {
447: check(event);
448: }
449:
450: public void buildFinished(BuildEvent event) {
451: check(event);
452: }
453:
454: public void targetFinished(BuildEvent event) {
455: check(event);
456: }
457:
458: public void taskStarted(BuildEvent event) {
459: check(event);
460: }
461:
462: public void taskFinished(BuildEvent event) {
463: check(event);
464: }
465:
466: public void messageLogged(BuildEvent event) {
467: check(event);
468: }
469:
470: public void targetStarted(BuildEvent event) {
471: check(event);
472: }
473:
474: private void check(BuildEvent event) {
475: if (error == null) {
476: try {
477: assertNotNull(event.getProject().getInputHandler());
478: assertSame(ih, event.getProject().getInputHandler());
479: } catch (AssertionFailedError e) {
480: error = e;
481: }
482: }
483: }
484:
485: AssertionFailedError getError() {
486: return error;
487: }
488:
489: }
490:
491: private class PropertyChecker implements BuildListener {
492: private String[] expectedValues;
493: private String key;
494: private int calls = 0;
495: private AssertionFailedError error;
496:
497: PropertyChecker(String key, String[] values) {
498: this .key = key;
499: this .expectedValues = values;
500: }
501:
502: public void buildStarted(BuildEvent event) {
503: }
504:
505: public void buildFinished(BuildEvent event) {
506: }
507:
508: public void targetFinished(BuildEvent event) {
509: }
510:
511: public void taskStarted(BuildEvent event) {
512: }
513:
514: public void taskFinished(BuildEvent event) {
515: }
516:
517: public void messageLogged(BuildEvent event) {
518: }
519:
520: public void targetStarted(BuildEvent event) {
521: if (event.getTarget().getName().equals("")) {
522: return;
523: }
524: if (calls >= expectedValues.length) {
525: error = new AssertionFailedError(
526: "Unexpected invocation of" + " target "
527: + event.getTarget().getName());
528: }
529:
530: if (error == null) {
531: try {
532: assertEquals(expectedValues[calls++], event
533: .getProject().getProperty(key));
534: } catch (AssertionFailedError e) {
535: error = e;
536: }
537: }
538: }
539:
540: AssertionFailedError getError() {
541: return error;
542: }
543:
544: }
545:
546: }
|