KickJava   Java API By Example, From Geeks To Geeks.

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


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 Maven2BuilderTest 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 Exception JavaDoc {
63         Maven2Builder mb = new Maven2Builder();
64         try {
65             mb.validate();
66             fail("Maven2Builder should throw exceptions when required fields are not set.");
67         } catch (CruiseControlException e) {
68             assertEquals("'mvnhome' is required for Maven2Builder", e.getMessage());
69         }
70
71         // these files must also exist for Maven2Builder to be happy.
72
File JavaDoc testScript = File.createTempFile("Maven2BuilderTest.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("Maven2BuilderTest.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.setMvnScript(testScript.getAbsolutePath());
82
83         try {
84             mb.validate();
85             fail("Maven2Builder should throw exceptions when required fields are not set.");
86         } catch (CruiseControlException e) {
87             assertEquals("'pomfile' is required for Maven2Builder", e.getMessage());
88         }
89
90         mb.setPomFile(testProject.getAbsolutePath());
91
92         try {
93             mb.validate();
94             fail("Maven2Builder should throw exceptions when required fields are not set.");
95         } catch (CruiseControlException e) {
96             assertEquals("'goal' is required for Maven2Builder", e.getMessage());
97         }
98
99         mb.setGoal("");
100
101         try {
102             mb.validate();
103             fail("Maven2Builder should throw exceptions when required fields are not set.");
104         } catch (CruiseControlException e) {
105             assertEquals("'goal' is required for Maven2Builder", e.getMessage());
106         }
107
108         mb.setGoal("mygoal");
109
110         mb.validate();
111     }
112
113     public void testBuild_Success() throws IOException JavaDoc {
114       Maven2Builder mb = new Maven2Builder();
115       internalTestBuild(MOCK_SUCCESS, mb);
116     }
117
118     public void testBuild_BuildFailure() throws IOException JavaDoc {
119         Maven2Builder mb = new Maven2Builder();
120         internalTestBuild(MOCK_BUILD_FAILURE, mb);
121     }
122     
123     public void testBuild_DownloadFailure() throws IOException JavaDoc {
124         Maven2Builder mb = new Maven2Builder();
125         internalTestBuild(MOCK_DOWNLOAD_FAILURE, mb);
126     }
127
128     /**
129      * Element build(Map). mockFailure == (Mock a failure?).
130      *
131      * @param statusType The exit status to be tested
132      */

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

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

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

304        
305     }
306
307     /**
308      * Text for build status
309      *
310      * @param statusCode The exit status to be tested
311      */

312     private String JavaDoc getStatusText(String JavaDoc statusCode) {
313         if (statusCode.equals(MOCK_SUCCESS)) {
314             return "BUILD SUCCESSFUL";
315         } else if (statusCode.equals(MOCK_BUILD_FAILURE)) {
316             return "BUILD FAILED";
317         } else if (statusCode.equals(MOCK_DOWNLOAD_FAILURE)) {
318             return "The build cannot continue because of the following unsatisfied dependency";
319         }
320         throw new IllegalArgumentException JavaDoc("please use one of the constants");
321     }
322 }
Popular Tags