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 Maven2BuilderTest 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 Exception { 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 File 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 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 { 114 Maven2Builder mb = new Maven2Builder(); 115 internalTestBuild(MOCK_SUCCESS, mb); 116 } 117 118 public void testBuild_BuildFailure() throws IOException { 119 Maven2Builder mb = new Maven2Builder(); 120 internalTestBuild(MOCK_BUILD_FAILURE, mb); 121 } 122 123 public void testBuild_DownloadFailure() throws IOException { 124 Maven2Builder mb = new Maven2Builder(); 125 internalTestBuild(MOCK_DOWNLOAD_FAILURE, mb); 126 } 127 128 133 private void internalTestBuild(String statusType, Maven2Builder mb) throws IOException { 134 135 File testScript = null; 136 boolean buildSuccessful = statusType.equals(MOCK_SUCCESS); 137 String statusText = getStatusText(statusType); 138 try { 139 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 goalTags; 175 176 mb.setGoal("fakegoal"); 178 Element logElement = mb.build(new Hashtable ()); 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 mb.setGoal("fakegoal|otherfakegoal"); 196 logElement = mb.build(new Hashtable ()); 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 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 return; 227 } 228 } 229 } 230 231 234 public void testGetGoalSets() { 235 Maven2Builder mb = new Maven2Builder(); 236 List 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 "); gsList = mb.getGoalSets(); 244 assertNotNull(gsList); 245 assertEquals("One goal should produce one item", 1, gsList.size()); 246 assertEquals("One goal produces bad list content", "clean", (String ) gsList.get(0)); 248 249 mb.setGoal(" clean|update "); gsList = mb.getGoalSets(); 251 assertNotNull(gsList); 252 assertEquals("Two goals should produce two items", 2, gsList.size()); 253 assertEquals("First run produces bad goal", "clean", (String ) gsList.get(0)); 255 assertEquals("Second run produces bad goal", "update", (String ) gsList.get(1)); 256 257 mb.setGoal("clean update|\ttest||"); gsList = mb.getGoalSets(); 260 assertNotNull(gsList); 261 assertEquals("Complex goal should produce two goalsets", 2, gsList.size()); 262 assertEquals("First cplx run produces bad goal", "clean update", (String ) gsList.get(0)); 264 assertEquals("Second cplx run produces bad goal", "test", (String ) gsList.get(1)); 265 } 266 267 270 private void makeTestFile(File testFile, String content, boolean onWindows) { 271 try { 272 BufferedWriter bwr = new BufferedWriter (new FileWriter (testFile)); 273 bwr.write(content); 274 bwr.flush(); 275 bwr.close(); 276 } catch (IOException 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 p = cmdline.execute(); 286 p.waitFor(); 287 } catch (Exception e) { 288 e.printStackTrace(); 289 fail("exception changing permissions on test file " + testFile.getAbsolutePath()); 290 } 291 } 292 } 293 public void testBuildTimeout() throws Exception { 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 304 305 } 306 307 312 private String getStatusText(String 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 ("please use one of the constants"); 321 } 322 } | Popular Tags |