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.ArrayList ; 44 import java.util.HashMap ; 45 import java.util.Hashtable ; 46 import java.util.Iterator ; 47 import java.util.List ; 48 49 import junit.framework.TestCase; 50 import net.sourceforge.cruisecontrol.CruiseControlException; 51 52 import org.jdom.Element; 53 54 public class AntBuilderTest extends TestCase { 55 private final List filesToClear = new ArrayList (); 56 private AntBuilder builder; 57 private AntBuilder unixBuilder; 58 private AntBuilder windowsBuilder; 59 private Hashtable properties; 60 private static final String UNIX_PATH = "/usr/java/jdk1.5.0/lib/tools.jar:" 61 + "/home/joris/java/cruisecontrol-2.2/main/dist/cruisecontrol.jar:" 62 + "/home/joris/java/cruisecontrol-2.2/main/lib/log4j.jar:" 63 + "/home/joris/java/cruisecontrol-2.2/main/lib/jdom.jar:" 64 + "/home/joris/java/cruisecontrol-2.2/main/lib/ant:" 65 + "/home/joris/java/cruisecontrol-2.2/main/lib/ant/ant.jar:" 66 + "/home/joris/java/cruisecontrol-2.2/main/lib/ant/ant-launcher.jar:" 67 + "/home/joris/java/cruisecontrol-2.2/main/lib/xerces.jar:" 68 + "/home/joris/java/cruisecontrol-2.2/main/lib/xalan.jar:" 69 + "/home/joris/java/cruisecontrol-2.2/main/lib/jakarta-oro-2.0.3.jar:" 70 + "/home/joris/java/cruisecontrol-2.2/main/lib/mail.jar:" 71 + "/home/joris/java/cruisecontrol-2.2/main/lib/junit.jar:" 72 + "/home/joris/java/cruisecontrol-2.2/main/lib/activation.jar:" 73 + "/home/joris/java/cruisecontrol-2.2/main/lib/commons-net-1.1.0.jar:" 74 + "/home/joris/java/cruisecontrol-2.2/main/lib/starteam-sdk.jar:" 75 + "/home/joris/java/cruisecontrol-2.2/main/lib/mx4j.jar:" 76 + "/home/joris/java/cruisecontrol-2.2/main/lib/mx4j-tools.jar:" 77 + "/home/joris/java/cruisecontrol-2.2/main/lib/mx4j-remote.jar:" 78 + "/home/joris/java/cruisecontrol-2.2/main/lib/smack.jar:."; 79 private static final String WINDOWS_PATH = "C:\\Progra~1\\IBM\\WSAD\\tools.jar;" 80 + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\dist\\cruisecontrol.jar;" 81 + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\log4j.jar;" 82 + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\jdom.jar;" 83 + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\ant;" 84 + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\ant\\ant.jar;" 85 + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\ant\\ant-launcher.jar;" 86 + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\xerces.jar;" 87 + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\xalan.jar;" 88 + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\jakarta-oro-2.0.3.jar;" 89 + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\mail.jar;" 90 + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\junit.jar;" 91 + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\activation.jar;" 92 + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\commons-net-1.1.0.jar;" 93 + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\starteam-sdk.jar;" 94 + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\mx4j.jar;" 95 + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\mx4j-tools.jar;" 96 + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\mx4j-remote.jar;" 97 + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\smack.jar;" 98 + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\comm.jar;" 99 + "C:\\Java\\cruisecontrol-2.2\\main\\bin\\\\..\\lib\\x10.jar;."; 100 101 protected void setUp() throws Exception { 102 builder = new AntBuilder(); 103 builder.setTarget("target"); 104 builder.setBuildFile("buildfile"); 105 106 properties = new Hashtable (); 107 properties.put("label", "200.1.23"); 108 109 unixBuilder = new AntBuilder() { 110 protected String getSystemClassPath() { 111 return UNIX_PATH; 112 } 113 }; 114 unixBuilder.setTarget("target"); 115 unixBuilder.setBuildFile("buildfile"); 116 117 windowsBuilder = new AntBuilder() { 118 protected String getSystemClassPath() { 119 return WINDOWS_PATH; 120 } 121 }; 122 windowsBuilder.setTarget("target"); 123 windowsBuilder.setBuildFile("buildfile"); 124 } 125 126 public void tearDown() { 127 for (Iterator iterator = filesToClear.iterator(); iterator.hasNext();) { 128 File file = (File ) iterator.next(); 129 if (file.exists()) { 130 file.delete(); 131 } 132 } 133 134 builder = null; 135 unixBuilder = null; 136 windowsBuilder = null; 137 properties = null; 138 } 139 140 public void testValidate() { 141 builder = new AntBuilder(); 142 143 try { 144 builder.validate(); 145 } catch (CruiseControlException e) { 146 fail("antbuilder has no required attributes"); 147 } 148 149 builder.setTime("0100"); 150 builder.setBuildFile("buildfile"); 151 builder.setTarget("target"); 152 153 try { 154 builder.validate(); 155 } catch (CruiseControlException e) { 156 fail("validate should not throw exceptions when options are set."); 157 } 158 } 159 160 public void testValidateShouldThrowExceptionWhenSaveLogDirDoesntExist() { 161 builder.setSaveLogDir("I/hope/this/dir/does/not/exist/"); 162 try { 163 builder.validate(); 164 fail(); 165 } catch (CruiseControlException expected) { 166 } 167 } 168 169 public void testValidateShouldThrowExceptionWhenMultipleAndTimeAreBothSet() { 170 builder.setTime("0100"); 171 builder.setMultiple(2); 172 173 try { 174 builder.validate(); 175 fail(); 176 } catch (CruiseControlException expected) { 177 } 178 } 179 180 public void testValidateAntHomeNotExist() { 181 builder = new AntBuilder(); 182 builder.setAntHome("/this/directory/doesnt/exist"); 183 try { 184 builder.validate(); 185 fail("validate should throw exceptions when the specified anthome doesn't exist"); 186 } catch (CruiseControlException e) { 187 assertTrue("wrong exception caught [" + e.getMessage() + "]", e 188 .getMessage().indexOf( 189 "'antHome' must exist and be a directory") >= 0); 190 } 191 } 192 193 public void testValidateAntHomeExistButNoAntScript() { 194 builder = new AntBuilder(); 195 builder.setAntHome("/"); 196 try { 197 builder.validate(); 198 fail("validate should throw exceptions when the specified anthome doesn't contain the antscript"); 199 } catch (CruiseControlException e) { 200 assertTrue( 201 "wrong exception caught [" + e.getMessage() + "]", 202 e.getMessage().indexOf( 203 "'antHome' must contain an ant execution script") >= 0); 204 } 205 } 206 207 public void testValidateAntHomeAndAntscriptSet() { 208 builder = new AntBuilder(); 209 builder.setAntHome("/"); 210 builder.setAntScript("foo.bat"); 211 try { 212 builder.validate(); 213 fail("validate should throw exceptions when anthome and antscript are both set"); 214 } catch (CruiseControlException e) { 215 assertTrue( 216 "wrong exception caught [" + e.getMessage() + "]", 217 e.getMessage().indexOf( 218 "'antHome' and 'antscript' cannot both be set") >= 0); 219 } 220 } 221 222 public void testGetCommandLineArgs_DebugAndQuiet() { 223 builder.setUseDebug(true); 224 builder.setUseQuiet(true); 225 try { 226 builder.validate(); 227 fail("validate() should throw CruiseControlException when both useDebug and useQuiet are true"); 228 } catch (CruiseControlException expected) { 229 } 230 } 231 232 public void testGetAntLogAsElement() throws IOException , CruiseControlException { 233 Element buildLogElement = new Element("build"); 234 File logFile = new File ("_tempAntLog.xml"); 235 filesToClear.add(logFile); 236 BufferedWriter bw2 = new BufferedWriter (new FileWriter (logFile)); 237 bw2.write( 238 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<?xml-stylesheet " 239 + "type=\"text/xsl\" HREF=\"log.xsl\"?>\n<build></build>"); 240 bw2.flush(); 241 bw2.close(); 242 243 assertEquals( 244 buildLogElement.toString(), 245 AntBuilder.getAntLogAsElement(logFile).toString()); 246 } 247 248 public void testGetAntLogAsElement_NoLogFile() { 249 File doesNotExist = new File ("blah blah blah does not exist"); 250 try { 251 AntBuilder.getAntLogAsElement(doesNotExist); 252 fail(); 253 } catch (CruiseControlException expected) { 254 assertEquals("ant logfile " + doesNotExist.getAbsolutePath() + " does not exist.", expected.getMessage()); 255 } 256 } 257 258 public void testBuild() throws Exception { 259 assertTrue("testbuild.xml expected in working dir", new File ("testbuild.xml").exists()); 260 builder.setBuildFile("testbuild.xml"); 261 builder.setTempFile("notLog.xml"); 262 builder.setTarget("init"); 263 builder.validate(); 264 HashMap buildProperties = new HashMap (); 265 Element buildElement = builder.build(buildProperties); 266 int initCount = getInitCount(buildElement); 267 assertEquals(1, initCount); 268 269 builder.setTarget("init init"); 270 buildElement = builder.build(buildProperties); 271 initCount = getInitCount(buildElement); 272 assertEquals(2, initCount); 273 } 274 275 public int getInitCount(Element buildElement) { 276 int initFoundCount = 0; 277 Iterator targetIterator = buildElement.getChildren("target").iterator(); 278 String name; 279 while (targetIterator.hasNext()) { 280 name = ((Element) targetIterator.next()).getAttributeValue("name"); 281 if (name.equals("init")) { 282 initFoundCount++; 283 } 284 } 285 return initFoundCount; 286 } 287 288 public void testBuildTimeout() throws Exception { 289 assertTrue("testbuild.xml expected in working dir", new File ("testbuild.xml").exists()); 290 builder.setBuildFile("testbuild.xml"); 291 builder.setTarget("timeout-test-target"); 292 builder.setTimeout(5); 293 builder.setUseDebug(true); 294 builder.setUseLogger(true); 295 builder.validate(); 296 297 HashMap buildProperties = new HashMap (); 298 long startTime = System.currentTimeMillis(); 299 Element buildElement = builder.build(buildProperties); 300 assertTrue((System.currentTimeMillis() - startTime) < 9 * 1000L); 301 assertTrue(buildElement.getAttributeValue("error").indexOf("timeout") >= 0); 302 303 builder.setTimeout(1); 305 builder.setUseDebug(false); 306 builder.setUseLogger(false); 307 builder.setTempFile("shouldNot.xml"); 308 buildElement = builder.build(buildProperties); 309 assertTrue(buildElement.getAttributeValue("error").indexOf("timeout") >= 0); 310 } 311 312 public void testSaveAntLog() throws IOException { 313 String originalDirName = "target"; 314 String logName = "log.xml"; 315 String saveDirName = "target/reports/ant"; 316 317 builder.setSaveLogDir(saveDirName); 318 builder.setTempFile(logName); 319 320 File originalDir = new File (originalDirName); 321 File originalLog = new File (originalDir, logName); 322 originalDir.mkdirs(); 323 originalLog.createNewFile(); 324 325 File saveDir = new File (saveDirName); 326 File savedLog = new File (saveDir, logName); 327 saveDir.mkdirs(); 328 savedLog.delete(); 329 330 builder.saveAntLog(originalLog); 331 assertTrue(savedLog.exists()); 332 333 savedLog.delete(); 334 335 builder.setSaveLogDir(""); 336 builder.saveAntLog(originalLog); 337 assertFalse(savedLog.exists()); 338 339 builder.setSaveLogDir(null); 340 builder.saveAntLog(originalLog); 341 assertFalse(savedLog.exists()); 342 } 343 344 public void testFindAntScriptNonWindows() throws CruiseControlException { 345 builder.setAntHome("/foo/bar"); 346 assertEquals("/foo/bar/bin/ant", builder.findAntScript(false)); 347 } 348 349 public void testFindAntScriptWindows() throws CruiseControlException { 350 builder.setAntHome("c:\\foo\\bar"); 351 assertEquals("c:\\foo\\bar\\bin\\ant.bat", builder.findAntScript(true)); 352 } 353 354 public void testFindAntScriptWindowsNoAntHome() { 355 try { 356 builder.findAntScript(false); 357 fail("expected exception"); 358 } catch (CruiseControlException expected) { 359 } 361 362 try { 363 builder.findAntScript(true); 364 fail("expected exception"); 365 } catch (CruiseControlException expected) { 366 } 368 } 369 370 public void testValidateBuildFileWorksForNonDefaultDirectory() throws IOException , CruiseControlException { 371 File antworkdir = new File ("antworkdir"); 372 antworkdir.mkdir(); 373 File file = File.createTempFile("build", ".xml", antworkdir); 374 builder.setAntWorkingDir(antworkdir.getAbsolutePath()); 375 builder.setBuildFile(file.getName()); 376 377 builder.validateBuildFileExists(); 378 379 builder.setBuildFile(file.getAbsolutePath()); 380 builder.validateBuildFileExists(); 381 382 file.delete(); 383 try { 384 builder.validateBuildFileExists(); 385 fail(); 386 } catch (CruiseControlException expected) { 387 } 388 389 builder.setBuildFile(file.getName()); 390 try { 391 builder.validateBuildFileExists(); 392 fail(); 393 } catch (CruiseControlException expected) { 394 } 395 } 396 } 397 | Popular Tags |