KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > builders > AntBuilderTest


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.builders;
38
39 import java.io.BufferedWriter JavaDoc;
40 import java.io.File JavaDoc;
41 import java.io.FileWriter JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.util.ArrayList JavaDoc;
44 import java.util.HashMap JavaDoc;
45 import java.util.Hashtable JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.List JavaDoc;
48
49 import junit.framework.TestCase;
50 import net.sourceforge.cruisecontrol.CruiseControlException;
51
52 import org.jdom.Element;
53
54 public class AntBuilderTest extends TestCase {
55     private final List JavaDoc filesToClear = new ArrayList JavaDoc();
56     private AntBuilder builder;
57     private AntBuilder unixBuilder;
58     private AntBuilder windowsBuilder;
59     private Hashtable JavaDoc properties;
60     private static final String JavaDoc UNIX_PATH = "/usr/java/jdk1.5.0/lib/tools.jar:"
61       + "/home/joris/java/cruisecontrol-2.2/main/dist/cruisecontrol.jar:"
62       + "/home/joris/java/cruisecontrol-2.2/main/lib/log4j.jar:"
63       + "/home/joris/java/cruisecontrol-2.2/main/lib/jdom.jar:"
64       + "/home/joris/java/cruisecontrol-2.2/main/lib/ant:"
65       + "/home/joris/java/cruisecontrol-2.2/main/lib/ant/ant.jar:"
66       + "/home/joris/java/cruisecontrol-2.2/main/lib/ant/ant-launcher.jar:"
67       + "/home/joris/java/cruisecontrol-2.2/main/lib/xerces.jar:"
68       + "/home/joris/java/cruisecontrol-2.2/main/lib/xalan.jar:"
69       + "/home/joris/java/cruisecontrol-2.2/main/lib/jakarta-oro-2.0.3.jar:"
70       + "/home/joris/java/cruisecontrol-2.2/main/lib/mail.jar:"
71       + "/home/joris/java/cruisecontrol-2.2/main/lib/junit.jar:"
72       + "/home/joris/java/cruisecontrol-2.2/main/lib/activation.jar:"
73       + "/home/joris/java/cruisecontrol-2.2/main/lib/commons-net-1.1.0.jar:"
74       + "/home/joris/java/cruisecontrol-2.2/main/lib/starteam-sdk.jar:"
75       + "/home/joris/java/cruisecontrol-2.2/main/lib/mx4j.jar:"
76       + "/home/joris/java/cruisecontrol-2.2/main/lib/mx4j-tools.jar:"
77       + "/home/joris/java/cruisecontrol-2.2/main/lib/mx4j-remote.jar:"
78       + "/home/joris/java/cruisecontrol-2.2/main/lib/smack.jar:.";
79     private static final String JavaDoc WINDOWS_PATH = "C:\\Progra~1\\IBM\\WSAD\\tools.jar;"
80       + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\dist\\cruisecontrol.jar;"
81       + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\log4j.jar;"
82       + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\jdom.jar;"
83       + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\ant;"
84       + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\ant\\ant.jar;"
85       + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\ant\\ant-launcher.jar;"
86       + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\xerces.jar;"
87       + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\xalan.jar;"
88       + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\jakarta-oro-2.0.3.jar;"
89       + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\mail.jar;"
90       + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\junit.jar;"
91       + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\activation.jar;"
92       + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\commons-net-1.1.0.jar;"
93       + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\starteam-sdk.jar;"
94       + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\mx4j.jar;"
95       + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\mx4j-tools.jar;"
96       + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\mx4j-remote.jar;"
97       + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\smack.jar;"
98       + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\comm.jar;"
99       + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\x10.jar;.";
100
101     protected void setUp() throws Exception JavaDoc {
102         builder = new AntBuilder();
103         builder.setTarget("target");
104         builder.setBuildFile("buildfile");
105         
106         properties = new Hashtable JavaDoc();
107         properties.put("label", "200.1.23");
108         
109         unixBuilder = new AntBuilder() {
110             protected String JavaDoc getSystemClassPath() {
111                 return UNIX_PATH;
112             }
113         };
114         unixBuilder.setTarget("target");
115         unixBuilder.setBuildFile("buildfile");
116         
117         windowsBuilder = new AntBuilder() {
118             protected String JavaDoc getSystemClassPath() {
119                 return WINDOWS_PATH;
120             }
121         };
122         windowsBuilder.setTarget("target");
123         windowsBuilder.setBuildFile("buildfile");
124     }
125
126     public void tearDown() {
127         for (Iterator JavaDoc iterator = filesToClear.iterator(); iterator.hasNext();) {
128             File JavaDoc file = (File JavaDoc) iterator.next();
129             if (file.exists()) {
130                 file.delete();
131             }
132         }
133         
134         builder = null;
135         unixBuilder = null;
136         windowsBuilder = null;
137         properties = null;
138     }
139
140     public void testValidate() {
141         builder = new AntBuilder();
142
143         try {
144             builder.validate();
145         } catch (CruiseControlException e) {
146             fail("antbuilder has no required attributes");
147         }
148
149         builder.setTime("0100");
150         builder.setBuildFile("buildfile");
151         builder.setTarget("target");
152
153         try {
154             builder.validate();
155         } catch (CruiseControlException e) {
156             fail("validate should not throw exceptions when options are set.");
157         }
158     }
159      
160     public void testValidateShouldThrowExceptionWhenSaveLogDirDoesntExist() {
161         builder.setSaveLogDir("I/hope/this/dir/does/not/exist/");
162         try {
163             builder.validate();
164             fail();
165         } catch (CruiseControlException expected) {
166         }
167     }
168     
169     public void testValidateShouldThrowExceptionWhenMultipleAndTimeAreBothSet() {
170         builder.setTime("0100");
171         builder.setMultiple(2);
172
173         try {
174             builder.validate();
175             fail();
176         } catch (CruiseControlException expected) {
177         }
178     }
179
180     public void testValidateAntHomeNotExist() {
181         builder = new AntBuilder();
182         builder.setAntHome("/this/directory/doesnt/exist");
183         try {
184             builder.validate();
185             fail("validate should throw exceptions when the specified anthome doesn't exist");
186         } catch (CruiseControlException e) {
187             assertTrue("wrong exception caught [" + e.getMessage() + "]", e
188                     .getMessage().indexOf(
189                             "'antHome' must exist and be a directory") >= 0);
190         }
191     }
192
193     public void testValidateAntHomeExistButNoAntScript() {
194         builder = new AntBuilder();
195         builder.setAntHome("/");
196         try {
197             builder.validate();
198             fail("validate should throw exceptions when the specified anthome doesn't contain the antscript");
199         } catch (CruiseControlException e) {
200             assertTrue(
201                     "wrong exception caught [" + e.getMessage() + "]",
202                     e.getMessage().indexOf(
203                             "'antHome' must contain an ant execution script") >= 0);
204         }
205     }
206
207     public void testValidateAntHomeAndAntscriptSet() {
208         builder = new AntBuilder();
209         builder.setAntHome("/");
210         builder.setAntScript("foo.bat");
211         try {
212             builder.validate();
213             fail("validate should throw exceptions when anthome and antscript are both set");
214         } catch (CruiseControlException e) {
215             assertTrue(
216                     "wrong exception caught [" + e.getMessage() + "]",
217                     e.getMessage().indexOf(
218                             "'antHome' and 'antscript' cannot both be set") >= 0);
219         }
220     }
221
222     public void testGetCommandLineArgs_DebugAndQuiet() {
223         builder.setUseDebug(true);
224         builder.setUseQuiet(true);
225         try {
226             builder.validate();
227             fail("validate() should throw CruiseControlException when both useDebug and useQuiet are true");
228         } catch (CruiseControlException expected) {
229         }
230     }
231
232     public void testGetAntLogAsElement() throws IOException JavaDoc, CruiseControlException {
233         Element buildLogElement = new Element("build");
234         File JavaDoc logFile = new File JavaDoc("_tempAntLog.xml");
235         filesToClear.add(logFile);
236         BufferedWriter JavaDoc bw2 = new BufferedWriter JavaDoc(new FileWriter JavaDoc(logFile));
237         bw2.write(
238                 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<?xml-stylesheet "
239                 + "type=\"text/xsl\" HREF=\"log.xsl\"?>\n<build></build>");
240         bw2.flush();
241         bw2.close();
242
243         assertEquals(
244                 buildLogElement.toString(),
245                 AntBuilder.getAntLogAsElement(logFile).toString());
246     }
247
248     public void testGetAntLogAsElement_NoLogFile() {
249         File JavaDoc doesNotExist = new File JavaDoc("blah blah blah does not exist");
250         try {
251             AntBuilder.getAntLogAsElement(doesNotExist);
252             fail();
253         } catch (CruiseControlException expected) {
254             assertEquals("ant logfile " + doesNotExist.getAbsolutePath() + " does not exist.", expected.getMessage());
255         }
256     }
257
258     public void testBuild() throws Exception JavaDoc {
259         assertTrue("testbuild.xml expected in working dir", new File JavaDoc("testbuild.xml").exists());
260         builder.setBuildFile("testbuild.xml");
261         builder.setTempFile("notLog.xml");
262         builder.setTarget("init");
263         builder.validate();
264         HashMap JavaDoc buildProperties = new HashMap JavaDoc();
265         Element buildElement = builder.build(buildProperties);
266         int initCount = getInitCount(buildElement);
267         assertEquals(1, initCount);
268
269         builder.setTarget("init init");
270         buildElement = builder.build(buildProperties);
271         initCount = getInitCount(buildElement);
272         assertEquals(2, initCount);
273     }
274
275     public int getInitCount(Element buildElement) {
276         int initFoundCount = 0;
277         Iterator JavaDoc targetIterator = buildElement.getChildren("target").iterator();
278         String JavaDoc name;
279         while (targetIterator.hasNext()) {
280             name = ((Element) targetIterator.next()).getAttributeValue("name");
281             if (name.equals("init")) {
282                 initFoundCount++;
283             }
284         }
285         return initFoundCount;
286     }
287
288     public void testBuildTimeout() throws Exception JavaDoc {
289         assertTrue("testbuild.xml expected in working dir", new File JavaDoc("testbuild.xml").exists());
290         builder.setBuildFile("testbuild.xml");
291         builder.setTarget("timeout-test-target");
292         builder.setTimeout(5);
293         builder.setUseDebug(true);
294         builder.setUseLogger(true);
295         builder.validate();
296
297         HashMap JavaDoc buildProperties = new HashMap JavaDoc();
298         long startTime = System.currentTimeMillis();
299         Element buildElement = builder.build(buildProperties);
300         assertTrue((System.currentTimeMillis() - startTime) < 9 * 1000L);
301         assertTrue(buildElement.getAttributeValue("error").indexOf("timeout") >= 0);
302
303         // test we don't fail when there is no ant log file
304
builder.setTimeout(1);
305         builder.setUseDebug(false);
306         builder.setUseLogger(false);
307         builder.setTempFile("shouldNot.xml");
308         buildElement = builder.build(buildProperties);
309         assertTrue(buildElement.getAttributeValue("error").indexOf("timeout") >= 0);
310     }
311
312     public void testSaveAntLog() throws IOException JavaDoc {
313         String JavaDoc originalDirName = "target";
314         String JavaDoc logName = "log.xml";
315         String JavaDoc saveDirName = "target/reports/ant";
316
317         builder.setSaveLogDir(saveDirName);
318         builder.setTempFile(logName);
319
320         File JavaDoc originalDir = new File JavaDoc(originalDirName);
321         File JavaDoc originalLog = new File JavaDoc(originalDir, logName);
322         originalDir.mkdirs();
323         originalLog.createNewFile();
324         
325         File JavaDoc saveDir = new File JavaDoc(saveDirName);
326         File JavaDoc savedLog = new File JavaDoc(saveDir, logName);
327         saveDir.mkdirs();
328         savedLog.delete();
329
330         builder.saveAntLog(originalLog);
331         assertTrue(savedLog.exists());
332
333         savedLog.delete();
334
335         builder.setSaveLogDir("");
336         builder.saveAntLog(originalLog);
337         assertFalse(savedLog.exists());
338
339         builder.setSaveLogDir(null);
340         builder.saveAntLog(originalLog);
341         assertFalse(savedLog.exists());
342     }
343
344     public void testFindAntScriptNonWindows() throws CruiseControlException {
345         builder.setAntHome("/foo/bar");
346         assertEquals("/foo/bar/bin/ant", builder.findAntScript(false));
347     }
348
349     public void testFindAntScriptWindows() throws CruiseControlException {
350         builder.setAntHome("c:\\foo\\bar");
351         assertEquals("c:\\foo\\bar\\bin\\ant.bat", builder.findAntScript(true));
352     }
353
354     public void testFindAntScriptWindowsNoAntHome() {
355         try {
356             builder.findAntScript(false);
357             fail("expected exception");
358         } catch (CruiseControlException expected) {
359             //expected...
360
}
361
362         try {
363             builder.findAntScript(true);
364             fail("expected exception");
365         } catch (CruiseControlException expected) {
366             //expected...
367
}
368     }
369     
370     public void testValidateBuildFileWorksForNonDefaultDirectory() throws IOException JavaDoc, CruiseControlException {
371         File JavaDoc antworkdir = new File JavaDoc("antworkdir");
372         antworkdir.mkdir();
373         File JavaDoc file = File.createTempFile("build", ".xml", antworkdir);
374         builder.setAntWorkingDir(antworkdir.getAbsolutePath());
375         builder.setBuildFile(file.getName());
376         
377         builder.validateBuildFileExists();
378         
379         builder.setBuildFile(file.getAbsolutePath());
380         builder.validateBuildFileExists();
381
382         file.delete();
383         try {
384             builder.validateBuildFileExists();
385             fail();
386         } catch (CruiseControlException expected) {
387         }
388
389         builder.setBuildFile(file.getName());
390         try {
391             builder.validateBuildFileExists();
392             fail();
393         } catch (CruiseControlException expected) {
394         }
395     }
396 }
397
Popular Tags