1 37 package net.sourceforge.cruisecontrol.builders; 38 39 import java.io.File ; 40 import java.io.BufferedWriter ; 41 import java.io.FileWriter ; 42 import java.io.IOException ; 43 import java.util.Hashtable ; 44 import junit.framework.TestCase; 45 import java.util.List ; 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 56 public class ExecBuilderTest extends TestCase { 57 58 private static final String MOCK_SUCCESS = "good exec"; 59 private static final String MOCK_EXIT_FAILURE = "exit failure"; 60 private static final String MOCK_OUTPUT_FAILURE = "output failure"; 61 private File goodTestScript = null; 63 private File exitTestScript = null; 64 private File outputTestScript = null; 65 66 69 public ExecBuilderTest(String name) { 70 super(name); 71 } 73 76 protected void setUp() throws Exception { 77 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 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 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 } 136 139 public void testValidate() { 140 ExecBuilder ebt = new ExecBuilder(); 141 142 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 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 } 160 163 public void testBuild_BuildSuccess() throws IOException { 164 ExecBuilder eb = new ExecBuilder(); 165 internalTestBuild(MOCK_SUCCESS, eb, goodTestScript.toString()); 166 } 168 171 public void testBuild_BuildFailure() throws IOException { 172 ExecBuilder eb = new ExecBuilder(); 173 internalTestBuild(MOCK_EXIT_FAILURE, eb, exitTestScript.toString()); 174 } 176 179 public void testBuild_OutputFailure() throws IOException { 180 ExecBuilder eb = new ExecBuilder(); 181 internalTestBuild(MOCK_OUTPUT_FAILURE, eb, outputTestScript.toString()); 182 } 184 187 protected void internalTestBuild(String statusType, ExecBuilder eb, String script) throws IOException { 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 ()); 196 } catch (CruiseControlException e) { 197 e.printStackTrace(); 198 fail("ExecBuilder should not throw exceptions when build()-ing."); 199 } 200 201 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 assertNotNull(statusType, logElement); 213 List 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 220 List taskTags = te.getChildren("task"); 221 Element tk = (Element) taskTags.get(0); 222 assertEquals(statusType, script, tk.getAttribute("name").getValue()); 223 225 } 233 236 private void makeTestFile(File testFile, String content, boolean onWindows) { 237 try { 238 BufferedWriter bwr = new BufferedWriter (new FileWriter (testFile)); 239 bwr.write(content); 240 bwr.flush(); 241 bwr.close(); 242 } catch (IOException 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 p = cmdline.execute(); 252 p.waitFor(); 253 assertEquals(0, p.exitValue()); 254 } catch (Exception e) { 255 e.printStackTrace(); 256 fail("exception changing permissions on test file " + testFile.getAbsolutePath()); 257 } 258 } 259 } 261 } | Popular Tags |