1 37 package net.sourceforge.cruisecontrol.builders; 38 39 import java.io.BufferedWriter ; 40 import java.io.File ; 41 import java.io.FileWriter ; 42 import java.io.IOException ; 43 import java.util.Hashtable ; 44 import java.util.List ; 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 MOCK_SUCCESS = "successful build"; 56 private static final String MOCK_BUILD_FAILURE = "failed build"; 57 private static final String MOCK_DOWNLOAD_FAILURE = "download failure"; 58 59 62 public void testValidate() throws IOException { 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 File 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 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 { 120 MavenBuilder mb = new MavenBuilder(); 121 internalTestBuild(MOCK_SUCCESS, mb); 122 } 123 124 public void testBuild_BuildFailure() throws IOException { 125 MavenBuilder mb = new MavenBuilder(); 126 internalTestBuild(MOCK_BUILD_FAILURE, mb); 127 } 128 129 public void testBuild_DownloadFailure() throws IOException { 130 MavenBuilder mb = new MavenBuilder(); 131 internalTestBuild(MOCK_DOWNLOAD_FAILURE, mb); 132 } 133 134 139 private void internalTestBuild(String statusType, MavenBuilder mb) throws IOException { 140 141 File testScript = null; 142 boolean buildSuccessful = statusType.equals(MOCK_SUCCESS); 143 String statusText = getStatusText(statusType); 144 try { 145 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 goalTags; 181 182 mb.setGoal("fakegoal"); 184 Element logElement = mb.build(new Hashtable ()); 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 mb.setGoal("fakegoal|otherfakegoal"); 202 logElement = mb.build(new Hashtable ()); 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 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 return; 233 } 234 } 235 } 236 237 240 public void testGetGoalSets() { 241 MavenBuilder mb = new MavenBuilder(); 242 List 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 "); gsList = mb.getGoalSets(); 250 assertNotNull(gsList); 251 assertEquals("One goal should produce one item", 1, gsList.size()); 252 assertEquals("One goal produces bad list content", "clean", (String ) gsList.get(0)); 254 255 mb.setGoal(" clean|update "); gsList = mb.getGoalSets(); 257 assertNotNull(gsList); 258 assertEquals("Two goals should produce two items", 2, gsList.size()); 259 assertEquals("First run produces bad goal", "clean", (String ) gsList.get(0)); 261 assertEquals("Second run produces bad goal", "update", (String ) gsList.get(1)); 262 263 mb.setGoal("clean update|\ttest||"); gsList = mb.getGoalSets(); 266 assertNotNull(gsList); 267 assertEquals("Complex goal should produce two goalsets", 2, gsList.size()); 268 assertEquals("First cplx run produces bad goal", "clean update", (String ) gsList.get(0)); 270 assertEquals("Second cplx run produces bad goal", "test", (String ) gsList.get(1)); 271 } 272 273 276 private void makeTestFile(File testFile, String content, boolean onWindows) { 277 try { 278 BufferedWriter bwr = new BufferedWriter (new FileWriter (testFile)); 279 bwr.write(content); 280 bwr.flush(); 281 bwr.close(); 282 } catch (IOException 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 p = cmdline.execute(); 292 p.waitFor(); 293 } catch (Exception e) { 294 e.printStackTrace(); 295 fail("exception changing permissions on test file " + testFile.getAbsolutePath()); 296 } 297 } 298 } 299 public void testBuildTimeout() throws Exception { 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 310 311 } 312 313 318 private String getStatusText(String 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 ("please use one of the constants"); 327 } 328 } | Popular Tags |