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:
029: public class SubAntTest extends BuildFileTest {
030:
031: public SubAntTest(String name) {
032: super (name);
033: }
034:
035: public void setUp() {
036: configureProject("src/etc/testcases/taskdefs/subant.xml");
037: }
038:
039: public void testnodirs() {
040: project.executeTarget("testnodirs");
041: expectLog("testnodirs", "No sub-builds to iterate on");
042: }
043:
044: // target must be specified
045: public void testgenericantfile() {
046: File dir1 = project.resolveFile(".");
047: File dir2 = project.resolveFile("subant/subant-test1");
048: File dir3 = project.resolveFile("subant/subant-test2");
049:
050: testBaseDirs("testgenericantfile", new String[] {
051: dir1.getAbsolutePath(), dir2.getAbsolutePath(),
052: dir3.getAbsolutePath()
053:
054: });
055: }
056:
057: public void testantfile() {
058: File dir1 = project.resolveFile(".");
059: // basedir of subant/subant-test1/subant.xml is ..
060: // therefore we expect here the subant/subant-test1 subdirectory
061: File dir2 = project.resolveFile("subant/subant-test1");
062: // basedir of subant/subant-test2/subant.xml is ..
063: // therefore we expect here the subant subdirectory
064: File dir3 = project.resolveFile("subant");
065:
066: testBaseDirs("testantfile", new String[] {
067: dir1.getAbsolutePath(), dir2.getAbsolutePath(),
068: dir3.getAbsolutePath()
069:
070: });
071:
072: }
073:
074: public void testMultipleTargets() {
075: executeTarget("multipleTargets");
076: assertLogContaining("test1-one");
077: assertLogContaining("test1-two");
078: assertLogContaining("test2-one");
079: assertLogContaining("test2-two");
080: }
081:
082: public void testMultipleTargetsOneDoesntExist_FOEfalse() {
083: executeTarget("multipleTargetsOneDoesntExist_FOEfalse");
084: assertLogContaining("Target \"three\" does not exist in the project \"subant\"");
085: }
086:
087: public void testMultipleTargetsOneDoesntExist_FOEtrue() {
088: expectBuildExceptionContaining(
089: "multipleTargetsOneDoesntExist_FOEtrue",
090: "Calling not existent target",
091: "Target \"three\" does not exist in the project \"subant\"");
092: }
093:
094: protected void testBaseDirs(String target, String[] dirs) {
095: SubAntTest.BasedirChecker bc = new SubAntTest.BasedirChecker(
096: dirs);
097: project.addBuildListener(bc);
098: executeTarget(target);
099: AssertionFailedError ae = bc.getError();
100: if (ae != null) {
101: throw ae;
102: }
103: project.removeBuildListener(bc);
104: }
105:
106: private class BasedirChecker implements BuildListener {
107: private String[] expectedBasedirs;
108: private int calls = 0;
109: private AssertionFailedError error;
110:
111: BasedirChecker(String[] dirs) {
112: expectedBasedirs = dirs;
113: }
114:
115: public void buildStarted(BuildEvent event) {
116: }
117:
118: public void buildFinished(BuildEvent event) {
119: }
120:
121: public void targetFinished(BuildEvent event) {
122: }
123:
124: public void taskStarted(BuildEvent event) {
125: }
126:
127: public void taskFinished(BuildEvent event) {
128: }
129:
130: public void messageLogged(BuildEvent event) {
131: }
132:
133: public void targetStarted(BuildEvent event) {
134: if (event.getTarget().getName().equals("")) {
135: return;
136: }
137: if (error == null) {
138: try {
139: assertEquals(expectedBasedirs[calls++], event
140: .getProject().getBaseDir()
141: .getAbsolutePath());
142: } catch (AssertionFailedError e) {
143: error = e;
144: }
145: }
146: }
147:
148: AssertionFailedError getError() {
149: return error;
150: }
151:
152: }
153:
154: }
|