KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > ant > AntCallTriggerTest


1 package fr.jayasoft.ivy.ant;
2
3 import java.io.File JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.io.PrintStream JavaDoc;
6 import java.util.Vector JavaDoc;
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 JavaDoc {
25         assertFalse(new File JavaDoc("test/triggers/ant-call/A/out/foo.txt").exists());
26         runAnt(new File JavaDoc("test/triggers/ant-call/A/build.xml"), "resolve");
27         // should have unzipped foo.zip
28
assertTrue(new File JavaDoc("test/triggers/ant-call/A/out/foo.txt").exists());
29     }
30     
31     protected void tearDown() throws Exception JavaDoc {
32         FileUtil.forceDelete(new File JavaDoc("test/triggers/ant-call/A/out"));
33         FileUtil.forceDelete(new File JavaDoc("test/triggers/ant-call/cache"));
34     }
35     
36     private void runAnt(File JavaDoc buildFile, String JavaDoc target) throws BuildException {
37         runAnt(buildFile, target, Project.MSG_INFO);
38     }
39     private void runAnt(File JavaDoc buildFile, String JavaDoc target, int messageLevel) throws BuildException {
40         Vector JavaDoc targets = new Vector JavaDoc();
41         targets.add(target);
42         runAnt(buildFile, targets, messageLevel);
43     }
44     private void runAnt(File JavaDoc buildFile, Vector JavaDoc targets, int messageLevel) throws BuildException {
45         runBuild(buildFile, targets, messageLevel);
46         
47 // this exits the jvm at the end of the call
48
// Main.main(new String[] {"-f", buildFile.getAbsolutePath(), target});
49

50 // this does not set the good message level
51
// Ant ant = new Ant();
52
// Project project = new Project();
53
// project.setBaseDir(buildFile.getParentFile());
54
// project.init();
55
//
56
// ant.setProject(project);
57
// ant.setTaskName("ant");
58
//
59
// ant.setAntfile(buildFile.getAbsolutePath());
60
// ant.setInheritAll(false);
61
// if (target != null) {
62
// ant.setTarget(target);
63
// }
64
// ant.execute();
65
}
66
67     //////////////////////////////////////////////////////////////////////////////
68
// miserable copy (updated to simple test cases) from ant Main class:
69
// the only available way I found to easily run ant exits jvm at the end
70
private void runBuild(File JavaDoc buildFile, Vector JavaDoc targets, int messageLevel) throws BuildException {
71
72         final Project project = new Project();
73         project.setCoreLoader(null);
74
75         Throwable JavaDoc error = null;
76
77         try {
78             addBuildListeners(project, messageLevel);
79             addInputHandler(project, null);
80
81             PrintStream JavaDoc err = System.err;
82             PrintStream JavaDoc out = System.out;
83             InputStream JavaDoc in = System.in;
84
85             // use a system manager that prevents from System.exit()
86
SecurityManager JavaDoc oldsm = null;
87             oldsm = System.getSecurityManager();
88
89                 //SecurityManager can not be installed here for backwards
90
//compatibility reasons (PD). Needs to be loaded prior to
91
//ant class if we are going to implement it.
92
//System.setSecurityManager(new NoExitSecurityManager());
93
try {
94                 project.setDefaultInputStream(System.in);
95                 System.setIn(new DemuxInputStream(project));
96                 System.setOut(new PrintStream JavaDoc(new DemuxOutputStream(project, false)));
97                 System.setErr(new PrintStream JavaDoc(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                 // make sure that we have a target to execute
113
if (targets.size() == 0) {
114                     if (project.getDefaultTarget() != null) {
115                         targets.addElement(project.getDefaultTarget());
116                     }
117                 }
118
119                 project.executeTargets(targets);
120             } finally {
121                 // put back the original security manager
122
//The following will never eval to true. (PD)
123
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 JavaDoc exc) {
132             error = exc;
133             throw exc;
134         } catch (Error JavaDoc err) {
135             error = err;
136             throw err;
137         } finally {
138             project.fireBuildFinished(error);
139         }
140     }
141
142
143     /**
144      * Adds the listeners specified in the command line arguments,
145      * along with the default listener, to the specified project.
146      *
147      * @param project The project to add listeners to.
148      * Must not be <code>null</code>.
149      */

150     protected void addBuildListeners(Project project, int level) {
151
152         // Add the default listener
153
project.addBuildListener(createLogger(level));
154
155     }
156
157     /**
158      * Creates the InputHandler and adds it to the project.
159      *
160      * @param project the project instance.
161      * @param inputHandlerClassname
162      *
163      * @exception BuildException if a specified InputHandler
164      * implementation could not be loaded.
165      */

166     private void addInputHandler(Project project, String JavaDoc 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 JavaDoc e) {
178                 String JavaDoc msg = "The specified input handler class "
179                     + inputHandlerClassname
180                     + " does not implement the InputHandler interface";
181                 throw new BuildException(msg);
182             } catch (Exception JavaDoc e) {
183                 String JavaDoc 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     // XXX: (Jon Skeet) Any reason for writing a message and then using a bare
193
// RuntimeException rather than just using a BuildException here? Is it
194
// in case the message could end up being written to no loggers (as the
195
// loggers could have failed to be created due to this failure)?
196
/**
197      * Creates the default build logger for sending build events to the ant
198      * log.
199      *
200      * @return the logger instance for this build.
201      */

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