KickJava   Java API By Example, From Geeks To Geeks.

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


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2003, 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.Hashtable JavaDoc;
44 import java.util.List JavaDoc;
45
46 import junit.framework.TestCase;
47 import net.sourceforge.cruisecontrol.CruiseControlException;
48 import net.sourceforge.cruisecontrol.util.Commandline;
49 import net.sourceforge.cruisecontrol.util.Util;
50
51 import org.jdom.Element;
52
53 public class MavenBuilderTest extends TestCase {
54
55     private static final String JavaDoc MOCK_SUCCESS = "successful build";
56     private static final String JavaDoc MOCK_BUILD_FAILURE = "failed build";
57     private static final String JavaDoc MOCK_DOWNLOAD_FAILURE = "download failure";
58
59     /**
60      * void validate()
61      */

62     public void testValidate() throws IOException JavaDoc {
63         MavenBuilder mb = new MavenBuilder();
64         try {
65             mb.validate();
66             fail("MavenBuilder should throw exceptions when required fields are not set.");
67         } catch (CruiseControlException e) {
68             assertTrue(true);
69         }
70
71         // these files must also exist for MavenBuilder to be happy.
72
File JavaDoc testScript = File.createTempFile("MavenBuilderTest.testValidate", "_testmaven.bat");
73         testScript.deleteOnExit();
74         makeTestFile(testScript, "@echo This is a fake maven.bat\n", true);
75
76         File JavaDoc testProject = File.createTempFile("MavenBuilderTest.testValidate", "_testproject.xml");
77         testProject.deleteOnExit();
78         makeTestFile(testProject,
79             "<project><!-- This is a fake Maven project file --></project>\n", true);
80         mb.setMultiple(1);
81         mb.setMavenScript(testScript.getAbsolutePath());
82
83         try {
84             mb.validate();
85             fail("MavenBuilder should throw exceptions when required fields are not set.");
86         } catch (CruiseControlException e) {
87             assertTrue(true);
88         }
89
90         mb.setProjectFile(testProject.getAbsolutePath());
91
92         try {
93             mb.validate();
94             fail("MavenBuilder should throw exceptions when required fields are not set.");
95         } catch (CruiseControlException e) {
96             assertTrue(true);
97         }
98
99         mb.setGoal("");
100
101         try {
102             mb.validate();
103             fail("MavenBuilder should throw exceptions when required fields are not set.");
104         } catch (CruiseControlException e) {
105             assertTrue(true);
106         }
107
108         mb.setGoal("mygoal");
109
110         try {
111             mb.validate();
112             assertTrue(true);
113         } catch (CruiseControlException e) {
114             fail("MavenBuilder should not throw exceptions when required fields are set. Exception ["
115                     + e.getMessage() + "].");
116         }
117     }
118
119     public void testBuild_Success() throws IOException JavaDoc {
120       MavenBuilder mb = new MavenBuilder();
121       internalTestBuild(MOCK_SUCCESS, mb);
122     }
123
124     public void testBuild_BuildFailure() throws IOException JavaDoc {
125         MavenBuilder mb = new MavenBuilder();
126         internalTestBuild(MOCK_BUILD_FAILURE, mb);
127     }
128     
129     public void testBuild_DownloadFailure() throws IOException JavaDoc {
130         MavenBuilder mb = new MavenBuilder();
131         internalTestBuild(MOCK_DOWNLOAD_FAILURE, mb);
132     }
133
134     /**
135      * Element build(Map). mockFailure == (Mock a failure?).
136      *
137      * @param statusType The exit status to be tested
138      */

139     private void internalTestBuild(String JavaDoc statusType, MavenBuilder mb) throws IOException JavaDoc {
140         
141         File JavaDoc testScript = null;
142         boolean buildSuccessful = statusType.equals(MOCK_SUCCESS);
143         String JavaDoc statusText = getStatusText(statusType);
144         try {
145             // Prepare mock files.
146
if (Util.isWindows()) {
147                 testScript = File.createTempFile("MavenBuilderTest.internalTestBuild", "_testmaven.bat");
148                 testScript.deleteOnExit();
149                 makeTestFile(
150                     testScript,
151                     "@rem This is a fake maven.bat\n"
152                         + "@echo java:compile:\n"
153                         + "@echo Bla-bla-compile\n"
154                         + "@echo test:test:\n"
155                         + "@echo "
156                         + statusText
157                         + "\n",
158                     true);
159             } else {
160                 testScript = File.createTempFile("MavenBuilderTest.internalTestBuild", "_testmaven.sh");
161                 testScript.deleteOnExit();
162                 makeTestFile(
163                     testScript,
164                     "#!/bin/sh\n"
165                         + "\n"
166                         + "# This is a fake maven.sh\n"
167                         + "echo java:compile:\n"
168                         + "echo Bla-bla-compile\n"
169                         + "echo test:test:\n"
170                         + "echo "
171                         + statusText
172                         + "\n",
173                     false);
174             }
175             mb.setMavenScript(testScript.getAbsolutePath());
176             mb.setProjectFile("don-t-care.xml");
177
178             try {
179                 Element we;
180                 List JavaDoc goalTags;
181
182                 // some fake goal is still needed to start working (no '|' here!)
183
mb.setGoal("fakegoal");
184                 // this should "succeed"
185
Element logElement = mb.build(new Hashtable JavaDoc());
186                 assertNotNull(statusType, logElement);
187                 goalTags = logElement.getChildren("mavengoal");
188                 assertNotNull(statusType, goalTags);
189                 assertEquals(statusType, 2, goalTags.size());
190                 we = (Element) goalTags.get(0);
191                 assertEquals(statusType, "java:compile", we.getAttribute("name").getValue());
192                 we = (Element) goalTags.get(1);
193                 assertEquals(statusType, "test:test", we.getAttribute("name").getValue());
194                 if (!buildSuccessful) {
195                     assertNotNull("error attribute not found when " + statusType, logElement.getAttribute("error"));
196                 } else {
197                     assertNull(statusType, logElement.getAttribute("error"));
198                 }
199
200                 // this time let's test multiple runs
201
mb.setGoal("fakegoal|otherfakegoal");
202                 // this should "double succeed"
203
logElement = mb.build(new Hashtable JavaDoc());
204                 assertNotNull(statusType, logElement);
205                 goalTags = logElement.getChildren("mavengoal");
206                 assertNotNull(statusType, goalTags);
207                 we = (Element) goalTags.get(0);
208                 assertEquals(statusType, "java:compile", we.getAttribute("name").getValue());
209                 we = (Element) goalTags.get(1);
210                 assertEquals(statusType, "test:test", we.getAttribute("name").getValue());
211                 if (!buildSuccessful) {
212                     assertNotNull(statusType, logElement.getAttribute("error"));
213                     // if we mocked a failure, the second run should never happen
214
assertEquals(statusType, 2, goalTags.size());
215                 } else {
216                     assertNull(statusType, logElement.getAttribute("error"));
217                     assertEquals(statusType, 4, goalTags.size());
218                     we = (Element) goalTags.get(2);
219                     assertEquals(statusType, "java:compile", we.getAttribute("name").getValue());
220                     we = (Element) goalTags.get(3);
221                     assertEquals(statusType, "test:test", we.getAttribute("name").getValue());
222                 }
223
224             } catch (CruiseControlException e) {
225                 e.printStackTrace();
226                 fail("MavenBuilder should not throw exceptions when build()-ing.");
227             }
228         } finally {
229             if (testScript != null) {
230                 //PJ: May 08, 2005: The following statement is breaking the build on the cclive box.
231
//(new File(testScriptName)).delete();
232
return;
233             }
234         }
235     }
236
237     /**
238      * List getGoalSets()
239      */

240     public void testGetGoalSets() {
241         MavenBuilder mb = new MavenBuilder();
242         List JavaDoc gsList;
243         mb.setGoal(null);
244         gsList = mb.getGoalSets();
245         assertNotNull(gsList);
246         assertEquals("No goal produces non-empty list", 0, gsList.size());
247
248         mb.setGoal("clean "); // I want the space there..
249
gsList = mb.getGoalSets();
250         assertNotNull(gsList);
251         assertEquals("One goal should produce one item", 1, gsList.size());
252         // but notice, no space below
253
assertEquals("One goal produces bad list content", "clean", (String JavaDoc) gsList.get(0));
254
255         mb.setGoal(" clean|update "); // Notice the spaces here
256
gsList = mb.getGoalSets();
257         assertNotNull(gsList);
258         assertEquals("Two goals should produce two items", 2, gsList.size());
259         // but not here
260
assertEquals("First run produces bad goal", "clean", (String JavaDoc) gsList.get(0));
261         assertEquals("Second run produces bad goal", "update", (String JavaDoc) gsList.get(1));
262
263         // full-featured test
264
mb.setGoal("clean update|\ttest||"); // Notice the spaces here
265
gsList = mb.getGoalSets();
266         assertNotNull(gsList);
267         assertEquals("Complex goal should produce two goalsets", 2, gsList.size());
268         // but not here
269
assertEquals("First cplx run produces bad goal", "clean update", (String JavaDoc) gsList.get(0));
270         assertEquals("Second cplx run produces bad goal", "test", (String JavaDoc) gsList.get(1));
271     }
272
273     /**
274      * Make a test file with specified content. Assumes the file does not exist.
275      */

276     private void makeTestFile(File JavaDoc testFile, String JavaDoc content, boolean onWindows) {
277         try {
278             BufferedWriter JavaDoc bwr = new BufferedWriter JavaDoc(new FileWriter JavaDoc(testFile));
279             bwr.write(content);
280             bwr.flush();
281             bwr.close();
282         } catch (IOException JavaDoc ioex) {
283             fail("Unexpected IOException while preparing " + testFile.getAbsolutePath() + " test file");
284         }
285         if (!onWindows) {
286             Commandline cmdline = new Commandline();
287             cmdline.setExecutable("chmod");
288             cmdline.createArgument().setValue("755");
289             cmdline.createArgument().setValue(testFile.getAbsolutePath());
290             try {
291                 Process JavaDoc p = cmdline.execute();
292                 p.waitFor();
293             } catch (Exception JavaDoc e) {
294                 e.printStackTrace();
295                 fail("exception changing permissions on test file " + testFile.getAbsolutePath());
296             }
297         }
298     }
299     public void testBuildTimeout() throws Exception JavaDoc {
300
301         MavenBuilder builder = new MavenBuilder();
302         builder.setTimeout(5);
303         long startTime = System.currentTimeMillis();
304
305         internalTestBuild(MOCK_BUILD_FAILURE, builder);
306
307         assertTrue((System.currentTimeMillis() - startTime) < 9 * 1000L);
308        // assertTrue(buildElement.getAttributeValue("error").indexOf("timeout") >= 0);
309

310        
311     }
312
313     /**
314      * Text for build status
315      *
316      * @param statusCode The exit status to be tested
317      */

318     private String JavaDoc getStatusText(String JavaDoc statusCode) {
319         if (statusCode.equals(MOCK_SUCCESS)) {
320             return "BUILD SUCCESSFUL";
321         } else if (statusCode.equals(MOCK_BUILD_FAILURE)) {
322             return "BUILD FAILED";
323         } else if (statusCode.equals(MOCK_DOWNLOAD_FAILURE)) {
324             return "The build cannot continue because of the following unsatisfied dependency";
325         }
326         throw new IllegalArgumentException JavaDoc("please use one of the constants");
327     }
328 }
Popular Tags