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: package org.apache.ivy.ant;
019:
020: import java.io.File;
021: import java.io.InputStream;
022: import java.io.PrintStream;
023: import java.util.Vector;
024:
025: import junit.framework.TestCase;
026:
027: import org.apache.ivy.util.FileUtil;
028: import org.apache.tools.ant.BuildException;
029: import org.apache.tools.ant.BuildLogger;
030: import org.apache.tools.ant.DefaultLogger;
031: import org.apache.tools.ant.DemuxInputStream;
032: import org.apache.tools.ant.DemuxOutputStream;
033: import org.apache.tools.ant.Main;
034: import org.apache.tools.ant.Project;
035: import org.apache.tools.ant.ProjectHelper;
036: import org.apache.tools.ant.input.DefaultInputHandler;
037: import org.apache.tools.ant.input.InputHandler;
038:
039: public class AntCallTriggerTest extends TestCase {
040: public void test() throws Exception {
041: assertFalse(new File("test/triggers/ant-call/A/out/foo.txt")
042: .exists());
043: runAnt(new File("test/triggers/ant-call/A/build.xml"),
044: "resolve");
045: // should have unzipped foo.zip
046: assertTrue(new File("test/triggers/ant-call/A/out/foo.txt")
047: .exists());
048: }
049:
050: protected void tearDown() throws Exception {
051: FileUtil.forceDelete(new File("test/triggers/ant-call/A/out"));
052: FileUtil.forceDelete(new File("test/triggers/ant-call/cache"));
053: }
054:
055: private void runAnt(File buildFile, String target)
056: throws BuildException {
057: runAnt(buildFile, target, Project.MSG_INFO);
058: }
059:
060: private void runAnt(File buildFile, String target, int messageLevel)
061: throws BuildException {
062: Vector targets = new Vector();
063: targets.add(target);
064: runAnt(buildFile, targets, messageLevel);
065: }
066:
067: private void runAnt(File buildFile, Vector targets, int messageLevel)
068: throws BuildException {
069: runBuild(buildFile, targets, messageLevel);
070:
071: // this exits the jvm at the end of the call
072: // Main.main(new String[] {"-f", buildFile.getAbsolutePath(), target});
073:
074: // this does not set the good message level
075: // Ant ant = new Ant();
076: // Project project = new Project();
077: // project.setBaseDir(buildFile.getParentFile());
078: // project.init();
079: //
080: // ant.setProject(project);
081: // ant.setTaskName("ant");
082: //
083: // ant.setAntfile(buildFile.getAbsolutePath());
084: // ant.setInheritAll(false);
085: // if (target != null) {
086: // ant.setTarget(target);
087: // }
088: // ant.execute();
089: }
090:
091: // ////////////////////////////////////////////////////////////////////////////
092: // miserable copy (updated to simple test cases) from ant Main class:
093: // the only available way I found to easily run ant exits jvm at the end
094: private void runBuild(File buildFile, Vector targets,
095: int messageLevel) throws BuildException {
096:
097: final Project project = new Project();
098: project.setCoreLoader(null);
099:
100: Throwable error = null;
101:
102: try {
103: addBuildListeners(project, messageLevel);
104: addInputHandler(project, null);
105:
106: PrintStream err = System.err;
107: PrintStream out = System.out;
108: InputStream in = System.in;
109:
110: // use a system manager that prevents from System.exit()
111: SecurityManager oldsm = null;
112: oldsm = System.getSecurityManager();
113:
114: // SecurityManager can not be installed here for backwards
115: // compatibility reasons (PD). Needs to be loaded prior to
116: // ant class if we are going to implement it.
117: // System.setSecurityManager(new NoExitSecurityManager());
118: try {
119: project.setDefaultInputStream(System.in);
120: System.setIn(new DemuxInputStream(project));
121: System.setOut(new PrintStream(new DemuxOutputStream(
122: project, false)));
123: System.setErr(new PrintStream(new DemuxOutputStream(
124: project, true)));
125:
126: project.fireBuildStarted();
127:
128: project.init();
129: project.setUserProperty("ant.version", Main
130: .getAntVersion());
131:
132: project.setUserProperty("ant.file", buildFile
133: .getAbsolutePath());
134:
135: ProjectHelper.configureProject(project, buildFile);
136:
137: // make sure that we have a target to execute
138: if (targets.size() == 0) {
139: if (project.getDefaultTarget() != null) {
140: targets.addElement(project.getDefaultTarget());
141: }
142: }
143:
144: project.executeTargets(targets);
145: } finally {
146: // put back the original security manager
147: // The following will never eval to true. (PD)
148: if (oldsm != null) {
149: System.setSecurityManager(oldsm);
150: }
151:
152: System.setOut(out);
153: System.setErr(err);
154: System.setIn(in);
155: }
156: } catch (RuntimeException exc) {
157: error = exc;
158: throw exc;
159: } catch (Error err) {
160: error = err;
161: throw err;
162: } finally {
163: project.fireBuildFinished(error);
164: }
165: }
166:
167: /**
168: * Adds the listeners specified in the command line arguments, along with the default listener,
169: * to the specified project.
170: *
171: * @param project
172: * The project to add listeners to. Must not be <code>null</code>.
173: */
174: protected void addBuildListeners(Project project, int level) {
175:
176: // Add the default listener
177: project.addBuildListener(createLogger(level));
178:
179: }
180:
181: /**
182: * Creates the InputHandler and adds it to the project.
183: *
184: * @param project
185: * the project instance.
186: * @param inputHandlerClassname
187: * @exception BuildException
188: * if a specified InputHandler implementation could not be loaded.
189: */
190: private void addInputHandler(Project project,
191: String inputHandlerClassname) throws BuildException {
192: InputHandler handler = null;
193: if (inputHandlerClassname == null) {
194: handler = new DefaultInputHandler();
195: } else {
196: try {
197: handler = (InputHandler) (Class
198: .forName(inputHandlerClassname).newInstance());
199: if (project != null) {
200: project.setProjectReference(handler);
201: }
202: } catch (ClassCastException e) {
203: String msg = "The specified input handler class "
204: + inputHandlerClassname
205: + " does not implement the InputHandler interface";
206: throw new BuildException(msg);
207: } catch (Exception e) {
208: String msg = "Unable to instantiate specified input handler "
209: + "class "
210: + inputHandlerClassname
211: + " : "
212: + e.getClass().getName();
213: throw new BuildException(msg);
214: }
215: }
216: project.setInputHandler(handler);
217: }
218:
219: // XXX: (Jon Skeet) Any reason for writing a message and then using a bare
220: // RuntimeException rather than just using a BuildException here? Is it
221: // in case the message could end up being written to no loggers (as the
222: // loggers could have failed to be created due to this failure)?
223: /**
224: * Creates the default build logger for sending build events to the ant log.
225: *
226: * @return the logger instance for this build.
227: */
228: private BuildLogger createLogger(int level) {
229: BuildLogger logger = null;
230: logger = new DefaultLogger();
231:
232: logger.setMessageOutputLevel(level);
233: logger.setOutputPrintStream(System.out);
234: logger.setErrorPrintStream(System.err);
235:
236: return logger;
237: }
238:
239: }
|