KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
40 import java.io.BufferedWriter JavaDoc;
41 import java.io.FileWriter JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.util.Hashtable JavaDoc;
44 import junit.framework.TestCase;
45 import java.util.List JavaDoc;
46 import net.sourceforge.cruisecontrol.CruiseControlException;
47 import net.sourceforge.cruisecontrol.util.Commandline;
48 import net.sourceforge.cruisecontrol.util.Util;
49 import org.jdom.Element;
50
51 /**
52  * Exec builder test class.
53  *
54  * @author <a HREF="mailto:kevin.lee@buildmeister.com">Kevin Lee</a>
55  */

56 public class ExecBuilderTest extends TestCase {
57
58     private static final String JavaDoc MOCK_SUCCESS = "good exec";
59     private static final String JavaDoc MOCK_EXIT_FAILURE = "exit failure";
60     private static final String JavaDoc MOCK_OUTPUT_FAILURE = "output failure";
61     // private static final String MOCK_TIMEOUT_FAILURE = "timeout failure";
62
private File JavaDoc goodTestScript = null;
63     private File JavaDoc exitTestScript = null;
64     private File JavaDoc outputTestScript = null;
65     
66     /*
67      * default constructor
68      */

69     public ExecBuilderTest(String JavaDoc name) {
70         super(name);
71     } // ExecBuilderTest
72

73     /*
74      * setup test environment
75      */

76     protected void setUp() throws Exception JavaDoc {
77          // prepare "good" mock files
78
if (Util.isWindows()) {
79              goodTestScript = File.createTempFile("ExecBuilderTest.internalTestBuild", "_goodexec.bat");
80              goodTestScript.deleteOnExit();
81              makeTestFile(
82                  goodTestScript,
83                  "@rem This is a good exec.bat\n"
84                      + "@echo output from good exec\n",
85                  true);
86          } else {
87              goodTestScript = File.createTempFile("ExecBuilderTest.internalTestBuild", "_goodexec.sh");
88              goodTestScript.deleteOnExit();
89              makeTestFile(
90                  goodTestScript,
91                  "#!/bin/sh\n"
92                      + "\n"
93                      + "echo good exec\n",
94                  false);
95          }
96          // prepare "bad" mock files - with exit value > 0
97
if (Util.isWindows()) {
98              exitTestScript = File.createTempFile("ExecBuilderTest.internalTestBuild", "_exitexec.bat");
99              exitTestScript.deleteOnExit();
100              makeTestFile(
101                  exitTestScript,
102                  "@rem This is a bad exec.bat\n"
103                      + "exit 1\n",
104                  true);
105          } else {
106              exitTestScript = File.createTempFile("ExecBuilderTest.internalTestBuild", "_exitexec.sh");
107              exitTestScript.deleteOnExit();
108              makeTestFile(
109                  exitTestScript,
110                  "#!/bin/sh\n"
111                      + "\n"
112                      + "exit 1\n",
113                  false);
114          }
115          // prepare "bad" mock files - containing error string
116
if (Util.isWindows()) {
117              outputTestScript = File.createTempFile("ExecBuilderTest.internalTestBuild", "_outputexec.bat");
118              outputTestScript.deleteOnExit();
119              makeTestFile(
120                  outputTestScript,
121                  "@rem This is a bad exec.bat\n"
122                      + "@echo some input and then an " + MOCK_OUTPUT_FAILURE + "\n",
123                  true);
124          } else {
125              outputTestScript = File.createTempFile("ExecBuilderTest.internalTestBuild", "_outputexec.sh");
126              outputTestScript.deleteOnExit();
127              makeTestFile(
128                  outputTestScript,
129                  "#!/bin/sh\n"
130                      + "\n"
131                      + "echo some input and then an " + MOCK_OUTPUT_FAILURE + "\n",
132                  false);
133          }
134     } // setUp
135

136     /*
137      * test validation of required attributes
138      */

139     public void testValidate() {
140         ExecBuilder ebt = new ExecBuilder();
141
142         // test missing "command" attribute
143
try {
144             ebt.validate();
145             fail("ExecBuilder should throw an exception when the required attributes are not set.");
146         } catch (CruiseControlException e) {
147             assertEquals("exception message when required attributes not set",
148                     "'command' is required for ExecBuilder", e.getMessage());
149         }
150         
151         // test no error with all required attributes
152
ebt.setCommand("dir");
153         try {
154             ebt.validate();
155         } catch (CruiseControlException e) {
156             fail("ExecBuilder should not throw an exception when the required attributes are set.");
157         }
158     } // testValidate
159

160     /*
161      * test a succesful build
162      */

163     public void testBuild_BuildSuccess() throws IOException JavaDoc {
164         ExecBuilder eb = new ExecBuilder();
165         internalTestBuild(MOCK_SUCCESS, eb, goodTestScript.toString());
166     } // testBuild_BuildSuccess
167

168     /*
169      * test a buid failure - exit
170      */

171     public void testBuild_BuildFailure() throws IOException JavaDoc {
172         ExecBuilder eb = new ExecBuilder();
173         internalTestBuild(MOCK_EXIT_FAILURE, eb, exitTestScript.toString());
174     } // testBuild_BuildFailure
175

176     /*
177      * test a build failure - error in output
178      */

179     public void testBuild_OutputFailure() throws IOException JavaDoc {
180         ExecBuilder eb = new ExecBuilder();
181         internalTestBuild(MOCK_OUTPUT_FAILURE, eb, outputTestScript.toString());
182     } // testBuild_OutputFailure
183

184     /*
185      * execute the build and check results
186      */

187     protected void internalTestBuild(String JavaDoc statusType, ExecBuilder eb, String JavaDoc script) throws IOException JavaDoc {
188         Element logElement = null;
189         try {
190             eb.setCommand(script);
191             if (statusType.equals(MOCK_OUTPUT_FAILURE)) {
192                 eb.setErrorStr(MOCK_OUTPUT_FAILURE);
193             }
194             eb.validate();
195             logElement = eb.build(new Hashtable JavaDoc());
196         } catch (CruiseControlException e) {
197             e.printStackTrace();
198             fail("ExecBuilder should not throw exceptions when build()-ing.");
199         }
200         
201         // check whether there was a build error
202
//System.out.println("error output = " + eb.getBuildError());
203
if (statusType.equals(MOCK_SUCCESS)) {
204             assertEquals(statusType, "none", eb.getBuildError());
205         } else if (statusType.equals(MOCK_EXIT_FAILURE)) {
206             assertEquals(statusType, "return code is 1", eb.getBuildError());
207         } else if (statusType.equals(MOCK_OUTPUT_FAILURE)) {
208             assertEquals(statusType, "error string found", eb.getBuildError());
209         }
210         
211         // check the format of the produced log
212
assertNotNull(statusType, logElement);
213         List JavaDoc targetTags = logElement.getChildren("target");
214         assertNotNull(statusType, targetTags);
215         assertEquals(statusType, 1, targetTags.size());
216         Element te = (Element) targetTags.get(0);
217         assertEquals(statusType, "exec", te.getAttribute("name").getValue());
218         //System.out.println("target name = " + te.getAttribute("name").getValue());
219

220         List JavaDoc taskTags = te.getChildren("task");
221         Element tk = (Element) taskTags.get(0);
222         assertEquals(statusType, script, tk.getAttribute("name").getValue());
223         //System.out.println("task name = " + tk.getAttribute("name").getValue());
224

225         //TODO: check for contents of messages
226
//Iterator msgIterator = tk.getChildren("message").iterator();
227
//while (msgIterator.hasNext()) {
228
// Element msg = (Element) msgIterator.next();
229
// System.out.println("message priority = " + msg.getAttribute("priority").getValue());
230
//}
231
} // internalTestBuild
232

233       /*
234        * Make a test file with specified content. Assumes the file does not exist.
235        */

236       private void makeTestFile(File JavaDoc testFile, String JavaDoc content, boolean onWindows) {
237           try {
238               BufferedWriter JavaDoc bwr = new BufferedWriter JavaDoc(new FileWriter JavaDoc(testFile));
239               bwr.write(content);
240               bwr.flush();
241               bwr.close();
242           } catch (IOException JavaDoc ioex) {
243               fail("Unexpected IOException while preparing " + testFile.getAbsolutePath() + " test file");
244           }
245           if (!onWindows) {
246               Commandline cmdline = new Commandline();
247               cmdline.setExecutable("chmod");
248               cmdline.createArgument().setValue("755");
249               cmdline.createArgument().setValue(testFile.getAbsolutePath());
250               try {
251                   Process JavaDoc p = cmdline.execute();
252                   p.waitFor();
253                   assertEquals(0, p.exitValue());
254               } catch (Exception JavaDoc e) {
255                   e.printStackTrace();
256                   fail("exception changing permissions on test file " + testFile.getAbsolutePath());
257               }
258           }
259       } // makeTestFile
260

261 } // ExecBuilderTest
262
Popular Tags