1 package fr.jayasoft.ivy.ant; 2 3 import java.io.File ; 4 import java.io.InputStream ; 5 import java.io.PrintStream ; 6 import java.util.Vector ; 7 8 import junit.framework.TestCase; 9 10 import org.apache.tools.ant.BuildException; 11 import org.apache.tools.ant.BuildLogger; 12 import org.apache.tools.ant.DefaultLogger; 13 import org.apache.tools.ant.DemuxInputStream; 14 import org.apache.tools.ant.DemuxOutputStream; 15 import org.apache.tools.ant.Main; 16 import org.apache.tools.ant.Project; 17 import org.apache.tools.ant.ProjectHelper; 18 import org.apache.tools.ant.input.DefaultInputHandler; 19 import org.apache.tools.ant.input.InputHandler; 20 21 import fr.jayasoft.ivy.util.FileUtil; 22 23 public class AntCallTriggerTest extends TestCase { 24 public void test() throws Exception { 25 assertFalse(new File ("test/triggers/ant-call/A/out/foo.txt").exists()); 26 runAnt(new File ("test/triggers/ant-call/A/build.xml"), "resolve"); 27 assertTrue(new File ("test/triggers/ant-call/A/out/foo.txt").exists()); 29 } 30 31 protected void tearDown() throws Exception { 32 FileUtil.forceDelete(new File ("test/triggers/ant-call/A/out")); 33 FileUtil.forceDelete(new File ("test/triggers/ant-call/cache")); 34 } 35 36 private void runAnt(File buildFile, String target) throws BuildException { 37 runAnt(buildFile, target, Project.MSG_INFO); 38 } 39 private void runAnt(File buildFile, String target, int messageLevel) throws BuildException { 40 Vector targets = new Vector (); 41 targets.add(target); 42 runAnt(buildFile, targets, messageLevel); 43 } 44 private void runAnt(File buildFile, Vector targets, int messageLevel) throws BuildException { 45 runBuild(buildFile, targets, messageLevel); 46 47 50 } 66 67 private void runBuild(File buildFile, Vector targets, int messageLevel) throws BuildException { 71 72 final Project project = new Project(); 73 project.setCoreLoader(null); 74 75 Throwable error = null; 76 77 try { 78 addBuildListeners(project, messageLevel); 79 addInputHandler(project, null); 80 81 PrintStream err = System.err; 82 PrintStream out = System.out; 83 InputStream in = System.in; 84 85 SecurityManager oldsm = null; 87 oldsm = System.getSecurityManager(); 88 89 try { 94 project.setDefaultInputStream(System.in); 95 System.setIn(new DemuxInputStream(project)); 96 System.setOut(new PrintStream (new DemuxOutputStream(project, false))); 97 System.setErr(new PrintStream (new DemuxOutputStream(project, true))); 98 99 100 project.fireBuildStarted(); 101 102 project.init(); 103 project.setUserProperty("ant.version", Main.getAntVersion()); 104 105 106 project.setUserProperty("ant.file", 107 buildFile.getAbsolutePath()); 108 109 110 ProjectHelper.configureProject(project, buildFile); 111 112 if (targets.size() == 0) { 114 if (project.getDefaultTarget() != null) { 115 targets.addElement(project.getDefaultTarget()); 116 } 117 } 118 119 project.executeTargets(targets); 120 } finally { 121 if (oldsm != null) { 124 System.setSecurityManager(oldsm); 125 } 126 127 System.setOut(out); 128 System.setErr(err); 129 System.setIn(in); 130 } 131 } catch (RuntimeException exc) { 132 error = exc; 133 throw exc; 134 } catch (Error err) { 135 error = err; 136 throw err; 137 } finally { 138 project.fireBuildFinished(error); 139 } 140 } 141 142 143 150 protected void addBuildListeners(Project project, int level) { 151 152 project.addBuildListener(createLogger(level)); 154 155 } 156 157 166 private void addInputHandler(Project project, String inputHandlerClassname) throws BuildException { 167 InputHandler handler = null; 168 if (inputHandlerClassname == null) { 169 handler = new DefaultInputHandler(); 170 } else { 171 try { 172 handler = (InputHandler) 173 (Class.forName(inputHandlerClassname).newInstance()); 174 if (project != null) { 175 project.setProjectReference(handler); 176 } 177 } catch (ClassCastException e) { 178 String msg = "The specified input handler class " 179 + inputHandlerClassname 180 + " does not implement the InputHandler interface"; 181 throw new BuildException(msg); 182 } catch (Exception e) { 183 String msg = "Unable to instantiate specified input handler " 184 + "class " + inputHandlerClassname + " : " 185 + e.getClass().getName(); 186 throw new BuildException(msg); 187 } 188 } 189 project.setInputHandler(handler); 190 } 191 192 202 private BuildLogger createLogger(int level) { 203 BuildLogger logger = null; 204 logger = new DefaultLogger(); 205 206 logger.setMessageOutputLevel(level); 207 logger.setOutputPrintStream(System.out); 208 logger.setErrorPrintStream(System.err); 209 210 return logger; 211 } 212 213 } 214 | Popular Tags |