KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > publishers > WeblogPublisherTest


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001-2003, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.publishers;
38
39 import java.io.File JavaDoc;
40 import java.io.FileWriter JavaDoc;
41 import java.io.IOException JavaDoc;
42 import java.io.Writer JavaDoc;
43 import java.util.ArrayList JavaDoc;
44 import java.util.List JavaDoc;
45
46 import javax.xml.transform.TransformerFactory JavaDoc;
47
48 import net.sourceforge.cruisecontrol.CruiseControlException;
49 import net.sourceforge.cruisecontrol.util.XMLLogHelper;
50
51 import junit.framework.TestCase;
52
53 /**
54  * Unit tests for
55  * {@link net.sourceforge.cruisecontrol.publishers.WeblogPublisher WeblogPublisher}.
56  *
57  * @author Lasse Koskela
58  */

59 public class WeblogPublisherTest extends TestCase {
60
61     private static int counter = 1;
62
63     private File JavaDoc xslDir;
64
65     private String JavaDoc[] 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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
137         publisher.validate();
138         String JavaDoc 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 JavaDoc {
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 JavaDoc {
161         publisher.validate();
162         String JavaDoc 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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
210         for (int i = 0; i < xslFiles.length; i++) {
211             String JavaDoc xslFileName = xslFiles[i];
212             publisher.validate();
213             try {
214                 new File JavaDoc(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 JavaDoc {
227         String JavaDoc serverURL = "http://localhost:8080/cc";
228         String JavaDoc expected = serverURL + "?log=LOGFILE";
229         publisher.setBuildResultsURL(serverURL);
230         assertEquals(expected, publisher.createBuildResultsUrl("LOGFILE.XML"));
231     }
232
233     public void testBuildResultsUrlIsConstructedCorrectlyWithQuestionMark() {
234         String JavaDoc serverURL = "http://myserver/context/servlet?key=value";
235         String JavaDoc expected = serverURL + "&log=LOGFILE";
236         publisher.setBuildResultsURL(serverURL);
237         assertEquals(expected, publisher.createBuildResultsUrl("LOGFILE.XML"));
238     }
239
240     public void testBuildResultsLinkIsConstructedCorrectly() throws Exception JavaDoc {
241         String JavaDoc url = publisher.createBuildResultsUrl("TEST.XML");
242         String JavaDoc link = "<a HREF=\"" + url + "\">" + url + "</a>";
243         assertTrue(publisher.createLinkLine("TEST.XML").indexOf(link) != -1);
244     }
245
246     public void testDefaultLogDirectory() throws Exception JavaDoc {
247         String JavaDoc expected = "logs" + File.separator + "myproject";
248         assertEquals(expected, publisher.getDefaultLogDir("myproject"));
249     }
250
251     public void testCreateSubjectForSuccessfulBuild() throws Exception JavaDoc {
252         boolean isSuccessful = true;
253         boolean isFix = false;
254         String JavaDoc subject = publisher.createSubject("myproject", "mylabel",
255                 isSuccessful, isFix);
256         assertEquals("myproject mylabel - Build Successful", subject);
257     }
258
259     public void testCreateSubjectForFailedBuild() throws Exception JavaDoc {
260         boolean isSuccessful = false;
261         boolean isFix = false;
262         String JavaDoc subject = publisher.createSubject("myproject", "mylabel",
263                 isSuccessful, isFix);
264         assertEquals("myproject - Build Failed", subject);
265     }
266
267     public void testCreateSubjectForFixedBuild() throws Exception JavaDoc {
268         boolean isSuccessful = true;
269         boolean isFix = true;
270         String JavaDoc subject = publisher.createSubject("myproject", "mylabel",
271                 isSuccessful, isFix);
272         assertEquals("myproject mylabel - Build Fixed", subject);
273     }
274
275     public void testCreateSubjectUsesPrefixIfSpecified() throws Exception JavaDoc {
276         boolean isSuccessful = true;
277         boolean isFix = false;
278         publisher.setSubjectPrefix("[CC]");
279         String JavaDoc 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; // 'true' by default
286

287         private boolean wasPreviousBuildSuccessful = true; // 'true' by default
288

289         private boolean isBuildNecessary = true; // 'true' by default
290

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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
372         final boolean[] delegatedToTheCorrectMethod = { false };
373         publisher = new WeblogPublisher() {
374             void transformWithSingleStylesheet(File JavaDoc xml, StringBuffer JavaDoc buf) {
375                 delegatedToTheCorrectMethod[0] = true;
376             }
377         };
378         publisher.setXSLFile("foo.xsl");
379         publisher.transform(new File JavaDoc("foo.xml"));
380         assertTrue(delegatedToTheCorrectMethod[0]);
381     }
382
383     public void testTransformDelegatesCorrectlyWithoutStylesheetSpecified()
384             throws Exception JavaDoc {
385         final boolean[] delegatedToTheCorrectMethod = { false };
386         publisher = new WeblogPublisher() {
387             void transformWithMultipleStylesheets(File JavaDoc xml, StringBuffer JavaDoc buf) {
388                 delegatedToTheCorrectMethod[0] = true;
389             }
390         };
391         publisher.transform(new File JavaDoc("foo.xml"));
392         assertTrue(delegatedToTheCorrectMethod[0]);
393     }
394
395     //appendTransform(inFile, xsl, messageBuffer, tFactory);
396
public void testAllStylesheetsAreUsedInTransformation() throws Exception JavaDoc {
397         final List JavaDoc xslFilesUsed = new ArrayList JavaDoc();
398         publisher = new WeblogPublisher() {
399             void appendTransform(File JavaDoc xml, File JavaDoc xsl, StringBuffer JavaDoc buf,
400                     TransformerFactory JavaDoc tf) {
401                 xslFilesUsed.add(xsl.getName());
402             }
403         };
404         publisher.setXSLDir("foo");
405         publisher.transform(new File JavaDoc("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 JavaDoc {
414         File JavaDoc xml = createTempXmlFile();
415         File JavaDoc xsl = createTempXslFile();
416         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
417         TransformerFactory JavaDoc tfactory = TransformerFactory.newInstance();
418         publisher.appendTransform(xml, xsl, buf, tfactory);
419         assertEquals("Testing", buf.toString());
420     }
421
422     private File JavaDoc createTempXslFile() throws IOException JavaDoc {
423         File JavaDoc f = createTempFile();
424         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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 JavaDoc out = new FileWriter JavaDoc(f);
436         out.write(buf.toString());
437         out.flush();
438         out.close();
439         return f;
440     }
441
442     private File JavaDoc createTempXmlFile() throws IOException JavaDoc {
443         File JavaDoc f = createTempFile();
444         Writer JavaDoc out = new FileWriter JavaDoc(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 JavaDoc createTempFile() throws IOException JavaDoc {
452         File JavaDoc tempFile = File.createTempFile("WeblogPublisherTest", ".tmp");
453         tempFile.deleteOnExit();
454         return tempFile;
455     }
456
457     private File JavaDoc createTempFile(File JavaDoc parent, String JavaDoc name) throws IOException JavaDoc {
458         File JavaDoc tempFile = new File JavaDoc(parent, name);
459         tempFile.deleteOnExit();
460         FileWriter JavaDoc fw = new FileWriter JavaDoc(tempFile);
461         fw.write("");
462         fw.close();
463         return tempFile;
464     }
465
466     private File JavaDoc createTempDir() throws IOException JavaDoc {
467         File JavaDoc tempDir = new File JavaDoc(System.getProperty("java.io.tmpdir"));
468         tempDir = new File JavaDoc(tempDir, "tempdir_" + (counter++));
469         tempDir.mkdirs();
470         tempDir.deleteOnExit();
471         return tempDir;
472     }
473
474 }
475
Popular Tags