1 37 package net.sourceforge.cruisecontrol.publishers; 38 39 import java.io.File ; 40 import java.io.FileWriter ; 41 import java.io.IOException ; 42 import java.io.Writer ; 43 import java.util.ArrayList ; 44 import java.util.List ; 45 46 import javax.xml.transform.TransformerFactory ; 47 48 import net.sourceforge.cruisecontrol.CruiseControlException; 49 import net.sourceforge.cruisecontrol.util.XMLLogHelper; 50 51 import junit.framework.TestCase; 52 53 59 public class WeblogPublisherTest extends TestCase { 60 61 private static int counter = 1; 62 63 private File xslDir; 64 65 private String [] xslFiles = { "header.xsl", "maven.xsl", "checkstyle.xsl", 66 "compile.xsl", "javadoc.xsl", "unittests.xsl", "modifications.xsl", 67 "distributables.xsl" }; 68 69 private WeblogPublisher publisher; 70 71 protected void setUp() throws Exception { 72 publisher = new WeblogPublisher(); 73 publisher.setBuildResultsURL("http://localhost:8080/cc"); 74 publisher.setLogDir(createTempDir().getAbsolutePath()); 75 publisher.setUsername("user123"); 76 publisher.setPassword("topsecret"); 77 publisher.setBlogId("myblog"); 78 publisher.setBlogUrl("http://foobar.com/blog/xmlrpc"); 79 80 xslDir = createTempDir(); 81 for (int i = 0; i < xslFiles.length; i++) { 82 createTempFile(xslDir, xslFiles[i]); 83 } 84 85 publisher.setXSLDir(xslDir.getAbsolutePath()); 86 publisher.setCSS(createTempFile(xslDir, "cc.css").getAbsolutePath()); 87 } 88 89 public void testUsernameIsRequired() throws Exception { 90 publisher.validate(); 91 try { 92 publisher.setUsername(null); 93 publisher.validate(); 94 fail("Validation should fail when the username is not set"); 95 } catch (CruiseControlException expected) { 96 assertTrue(expected.getMessage().indexOf("username") != -1); 97 } 98 } 99 100 public void testPasswordIsRequired() throws Exception { 101 publisher.validate(); 102 try { 103 publisher.setPassword(null); 104 publisher.validate(); 105 fail("Validation should fail when password is not set"); 106 } catch (CruiseControlException expected) { 107 assertTrue("The error should mention the missing attribute", 108 expected.getMessage().indexOf("password") != -1); 109 } 110 } 111 112 public void testBlogIdIsRequired() throws Exception { 113 publisher.validate(); 114 try { 115 publisher.setBlogId(null); 116 publisher.validate(); 117 fail("Validation should fail when blogid is not set"); 118 } catch (CruiseControlException expected) { 119 assertTrue("The error should mention the missing attribute", 120 expected.getMessage().indexOf("blogid") != -1); 121 } 122 } 123 124 public void testBlogUrlIsRequired() throws Exception { 125 publisher.validate(); 126 try { 127 publisher.setBlogUrl(null); 128 publisher.validate(); 129 fail("Validation should fail when blogurl is not set"); 130 } catch (CruiseControlException expected) { 131 assertTrue("The error should mention the missing attribute", 132 expected.getMessage().indexOf("blogurl") != -1); 133 } 134 } 135 136 public void testBlogUrlMustBeValidUrl() throws Exception { 137 publisher.validate(); 138 String invalidUrl = "htt//thisisnotavalid.url"; 139 try { 140 publisher.setBlogUrl(invalidUrl); 141 publisher.validate(); 142 fail("Validation should fail when blogurl is not a valid URL"); 143 } catch (CruiseControlException expected) { 144 assertTrue("The error should mention the invalid URL", expected 145 .getMessage().indexOf(invalidUrl) != -1); 146 } 147 } 148 149 public void testBuildResultsUrlIsNotRequired() throws Exception { 150 publisher.validate(); 151 try { 152 publisher.setBuildResultsURL(null); 153 publisher.validate(); 154 } catch (CruiseControlException expected) { 155 fail("Validation should fail if buildresultsurl is not set" 156 + "since it's not a required attribute."); 157 } 158 } 159 160 public void testBuildResultsUrlMustBeValidUrl() throws Exception { 161 publisher.validate(); 162 String invalidUrl = "htt//thisisnotavalid.url"; 163 try { 164 publisher.setBuildResultsURL(invalidUrl); 165 publisher.validate(); 166 fail("Validation should fail when buildresultsurl is not a valid URL"); 167 } catch (CruiseControlException expected) { 168 assertTrue("The error should mention the invalid URL", expected 169 .getMessage().indexOf(invalidUrl) != -1); 170 } 171 } 172 173 public void testXslDirIsRequired() throws Exception { 174 publisher.validate(); 175 try { 176 publisher.setXSLDir("doesnotexist"); 177 publisher.validate(); 178 fail("Validation should fail when the XSL directory doesn't exist"); 179 } catch (CruiseControlException expected) { 180 assertTrue("The error should mention the missing directory's name", 181 expected.getMessage().indexOf("doesnotexist") != -1); 182 } 183 } 184 185 public void testXslFileMustExist() throws Exception { 186 publisher.validate(); 187 try { 188 publisher.setXSLFile("doesnotexist"); 189 publisher.validate(); 190 fail("Validation should fail when the XSL file doesn't exist"); 191 } catch (CruiseControlException expected) { 192 assertTrue("The error should mention the missing file's name", 193 expected.getMessage().indexOf("doesnotexist") != -1); 194 } 195 } 196 197 public void testCssFileIsRequired() throws Exception { 198 publisher.validate(); 199 try { 200 publisher.setCSS("doesnotexist"); 201 publisher.validate(); 202 fail("Validation should fail when the CSS file doesn't exist"); 203 } catch (CruiseControlException expected) { 204 assertTrue("The error should mention the missing file's name", 205 expected.getMessage().indexOf("doesnotexist") != -1); 206 } 207 } 208 209 public void testXslFilesAreRequired() throws Exception { 210 for (int i = 0; i < xslFiles.length; i++) { 211 String xslFileName = xslFiles[i]; 212 publisher.validate(); 213 try { 214 new File (xslDir, xslFileName).delete(); 215 publisher.validate(); 216 fail("Validation should fail if " + xslFileName + " is missing"); 217 } catch (CruiseControlException expected) { 218 assertTrue("The error message should include the " 219 + "name of the missing file", expected.getMessage() 220 .indexOf(xslFileName) != -1); 221 } 222 createTempFile(xslDir, xslFileName); 223 } 224 } 225 226 public void testBuildResultsUrlIsConstructedCorrectly() throws Exception { 227 String serverURL = "http://localhost:8080/cc"; 228 String expected = serverURL + "?log=LOGFILE"; 229 publisher.setBuildResultsURL(serverURL); 230 assertEquals(expected, publisher.createBuildResultsUrl("LOGFILE.XML")); 231 } 232 233 public void testBuildResultsUrlIsConstructedCorrectlyWithQuestionMark() { 234 String serverURL = "http://myserver/context/servlet?key=value"; 235 String expected = serverURL + "&log=LOGFILE"; 236 publisher.setBuildResultsURL(serverURL); 237 assertEquals(expected, publisher.createBuildResultsUrl("LOGFILE.XML")); 238 } 239 240 public void testBuildResultsLinkIsConstructedCorrectly() throws Exception { 241 String url = publisher.createBuildResultsUrl("TEST.XML"); 242 String link = "<a HREF=\"" + url + "\">" + url + "</a>"; 243 assertTrue(publisher.createLinkLine("TEST.XML").indexOf(link) != -1); 244 } 245 246 public void testDefaultLogDirectory() throws Exception { 247 String expected = "logs" + File.separator + "myproject"; 248 assertEquals(expected, publisher.getDefaultLogDir("myproject")); 249 } 250 251 public void testCreateSubjectForSuccessfulBuild() throws Exception { 252 boolean isSuccessful = true; 253 boolean isFix = false; 254 String subject = publisher.createSubject("myproject", "mylabel", 255 isSuccessful, isFix); 256 assertEquals("myproject mylabel - Build Successful", subject); 257 } 258 259 public void testCreateSubjectForFailedBuild() throws Exception { 260 boolean isSuccessful = false; 261 boolean isFix = false; 262 String subject = publisher.createSubject("myproject", "mylabel", 263 isSuccessful, isFix); 264 assertEquals("myproject - Build Failed", subject); 265 } 266 267 public void testCreateSubjectForFixedBuild() throws Exception { 268 boolean isSuccessful = true; 269 boolean isFix = true; 270 String subject = publisher.createSubject("myproject", "mylabel", 271 isSuccessful, isFix); 272 assertEquals("myproject mylabel - Build Fixed", subject); 273 } 274 275 public void testCreateSubjectUsesPrefixIfSpecified() throws Exception { 276 boolean isSuccessful = true; 277 boolean isFix = false; 278 publisher.setSubjectPrefix("[CC]"); 279 String subject = publisher.createSubject("myproject", "mylabel", 280 isSuccessful, isFix); 281 assertEquals("[CC] myproject mylabel - Build Successful", subject); 282 } 283 284 private static class MockXMLLogHelper extends XMLLogHelper { 285 private boolean isBuildSuccessful = true; 287 private boolean wasPreviousBuildSuccessful = true; 289 private boolean isBuildNecessary = true; 291 public MockXMLLogHelper() { 292 super(null); 293 } 294 295 public boolean isBuildSuccessful() { 296 return isBuildSuccessful; 297 } 298 299 public void setBuildSuccessful(boolean b) { 300 this.isBuildSuccessful = b; 301 } 302 303 public boolean wasPreviousBuildSuccessful() { 304 return wasPreviousBuildSuccessful; 305 } 306 307 public void setPreviousBuildSuccessful(boolean b) { 308 this.wasPreviousBuildSuccessful = b; 309 } 310 311 public boolean isBuildNecessary() { 312 return isBuildNecessary; 313 } 314 315 public void setBuildNecessary(boolean b) { 316 this.isBuildNecessary = b; 317 } 318 } 319 320 public void testShouldSendWhenBuildIsSuccessful() throws Exception { 321 MockXMLLogHelper logHelper = new MockXMLLogHelper(); 322 logHelper.setPreviousBuildSuccessful(true); 323 logHelper.setBuildSuccessful(true); 324 325 publisher.setReportSuccess("always"); 326 assertTrue(publisher.shouldSend(logHelper)); 327 328 publisher.setReportSuccess("fixes"); 329 assertFalse(publisher.shouldSend(logHelper)); 330 331 publisher.setReportSuccess("never"); 332 assertFalse(publisher.shouldSend(logHelper)); 333 } 334 335 public void testShouldSendWhenBuildIsFixed() throws Exception { 336 MockXMLLogHelper logHelper = new MockXMLLogHelper(); 337 logHelper.setPreviousBuildSuccessful(false); 338 logHelper.setBuildSuccessful(true); 339 340 publisher.setReportSuccess("always"); 341 assertTrue(publisher.shouldSend(logHelper)); 342 343 publisher.setReportSuccess("fixes"); 344 assertTrue(publisher.shouldSend(logHelper)); 345 346 publisher.setReportSuccess("never"); 347 assertFalse(publisher.shouldSend(logHelper)); 348 } 349 350 public void testShouldSendWhenBuildFailed() throws Exception { 351 MockXMLLogHelper logHelper = new MockXMLLogHelper(); 352 logHelper.setPreviousBuildSuccessful(true); 353 logHelper.setBuildSuccessful(false); 354 355 assertTrue(publisher.shouldSend(logHelper)); 356 } 357 358 public void testShouldSendWhenBuildFailedManyTimes() throws Exception { 359 MockXMLLogHelper logHelper = new MockXMLLogHelper(); 360 logHelper.setPreviousBuildSuccessful(false); 361 logHelper.setBuildSuccessful(false); 362 363 publisher.setSpamWhileBroken(true); 364 assertTrue(publisher.shouldSend(logHelper)); 365 366 publisher.setSpamWhileBroken(false); 367 assertFalse(publisher.shouldSend(logHelper)); 368 } 369 370 public void testTransformDelegatesCorrectlyWithSingleStylesheetSpecified() 371 throws Exception { 372 final boolean[] delegatedToTheCorrectMethod = { false }; 373 publisher = new WeblogPublisher() { 374 void transformWithSingleStylesheet(File xml, StringBuffer buf) { 375 delegatedToTheCorrectMethod[0] = true; 376 } 377 }; 378 publisher.setXSLFile("foo.xsl"); 379 publisher.transform(new File ("foo.xml")); 380 assertTrue(delegatedToTheCorrectMethod[0]); 381 } 382 383 public void testTransformDelegatesCorrectlyWithoutStylesheetSpecified() 384 throws Exception { 385 final boolean[] delegatedToTheCorrectMethod = { false }; 386 publisher = new WeblogPublisher() { 387 void transformWithMultipleStylesheets(File xml, StringBuffer buf) { 388 delegatedToTheCorrectMethod[0] = true; 389 } 390 }; 391 publisher.transform(new File ("foo.xml")); 392 assertTrue(delegatedToTheCorrectMethod[0]); 393 } 394 395 public void testAllStylesheetsAreUsedInTransformation() throws Exception { 397 final List xslFilesUsed = new ArrayList (); 398 publisher = new WeblogPublisher() { 399 void appendTransform(File xml, File xsl, StringBuffer buf, 400 TransformerFactory tf) { 401 xslFilesUsed.add(xsl.getName()); 402 } 403 }; 404 publisher.setXSLDir("foo"); 405 publisher.transform(new File ("foo.xml")); 406 for (int i = 0; i < publisher.getXslFileNames().length; i++) { 407 assertTrue("File " + publisher.getXslFileNames()[i] 408 + " was not used for transformation", xslFilesUsed 409 .contains(publisher.getXslFileNames()[i])); 410 } 411 } 412 413 public void testTheAppendTransformMethodWorksInGeneral() throws Exception { 414 File xml = createTempXmlFile(); 415 File xsl = createTempXslFile(); 416 StringBuffer buf = new StringBuffer (); 417 TransformerFactory tfactory = TransformerFactory.newInstance(); 418 publisher.appendTransform(xml, xsl, buf, tfactory); 419 assertEquals("Testing", buf.toString()); 420 } 421 422 private File createTempXslFile() throws IOException { 423 File f = createTempFile(); 424 StringBuffer buf = new StringBuffer (); 425 buf.append("<?xml version='1.0'?>").append('\n'); 426 buf.append("<xsl:stylesheet"); 427 buf.append(" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\""); 428 buf.append(" version=\"1.0\">").append('\n'); 429 buf.append("<xsl:output method=\"text\"/>").append('\n'); 430 buf.append("<xsl:template match=\"/\">"); 431 buf.append("<xsl:value-of select=\"just\"/>"); 432 buf.append("</xsl:template>").append('\n'); 433 buf.append("</xsl:stylesheet>"); 434 435 Writer out = new FileWriter (f); 436 out.write(buf.toString()); 437 out.flush(); 438 out.close(); 439 return f; 440 } 441 442 private File createTempXmlFile() throws IOException { 443 File f = createTempFile(); 444 Writer out = new FileWriter (f); 445 out.write("<?xml version='1.0'?><just>Testing</just>"); 446 out.flush(); 447 out.close(); 448 return f; 449 } 450 451 private File createTempFile() throws IOException { 452 File tempFile = File.createTempFile("WeblogPublisherTest", ".tmp"); 453 tempFile.deleteOnExit(); 454 return tempFile; 455 } 456 457 private File createTempFile(File parent, String name) throws IOException { 458 File tempFile = new File (parent, name); 459 tempFile.deleteOnExit(); 460 FileWriter fw = new FileWriter (tempFile); 461 fw.write(""); 462 fw.close(); 463 return tempFile; 464 } 465 466 private File createTempDir() throws IOException { 467 File tempDir = new File (System.getProperty("java.io.tmpdir")); 468 tempDir = new File (tempDir, "tempdir_" + (counter++)); 469 tempDir.mkdirs(); 470 tempDir.deleteOnExit(); 471 return tempDir; 472 } 473 474 } 475 | Popular Tags |