KickJava   Java API By Example, From Geeks To Geeks.

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


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
41 import junit.framework.TestCase;
42 import net.sourceforge.cruisecontrol.CruiseControlException;
43 import java.io.IOException JavaDoc;
44 import java.io.StringReader JavaDoc;
45 import javax.xml.transform.Source JavaDoc;
46 import javax.xml.transform.TransformerException JavaDoc;
47 import javax.xml.transform.TransformerFactory JavaDoc;
48 import javax.xml.transform.stream.StreamSource JavaDoc;
49 import net.sourceforge.cruisecontrol.builders.Property;
50
51 public class HTMLEmailPublisherTest extends TestCase {
52
53     private HTMLEmailPublisher publisher;
54     private File JavaDoc tmpFile;
55
56     public void setUp() {
57         publisher = new HTMLEmailPublisher();
58     }
59
60     private class BrokenTestPublisher extends HTMLEmailPublisher {
61         private String JavaDoc[] newXslFileNames = null;
62
63         BrokenTestPublisher() {
64             setXSLFileNames(newXslFileNames);
65         }
66     }
67
68     /**
69      * test is set up this way because I expect setXSLFileNames to be used
70      * from derived clases, so the test mirrors the expected use.
71      */

72     public void testSetXSLFileNames() {
73         try {
74             new BrokenTestPublisher();
75             fail("setXSLFileNames should fail when called with null");
76         } catch (IllegalArgumentException JavaDoc e) {
77             // should fail
78
}
79     }
80
81     public void testSetLogDir() {
82         try {
83             publisher.setLogDir(null);
84             fail("setLogDir should fail when called with null");
85         } catch (IllegalArgumentException JavaDoc e) {
86             // should fail
87
}
88     }
89
90     private void checkXslFileNamesExistInDir(String JavaDoc dir, String JavaDoc[] fileNames) {
91         assertNotNull("getXslFileNames() returned null", fileNames);
92
93         for (int i = 0; i < fileNames.length; i++) {
94             String JavaDoc fileName = fileNames[i];
95             File JavaDoc file = new File JavaDoc(dir, fileName);
96             assertTrue("file should exist: " + file.getAbsolutePath(),
97                             file.exists());
98             assertTrue("value shouldn't be a directory: " + file.getAbsolutePath(),
99                             file.isFile());
100         }
101     }
102
103     public void testSetXSLFileList() throws IOException JavaDoc {
104         try {
105             publisher.setXSLFileList(null);
106             fail("setXSLFileList should fail when called with null");
107         } catch (IllegalArgumentException JavaDoc expected) {
108         }
109
110         try {
111             publisher.setXSLFileList("");
112             fail("setXSLFileList should fail if specified xslFileList file is empty");
113         } catch (IllegalArgumentException JavaDoc expected) {
114         }
115         
116         tmpFile = File.createTempFile("HTMLEmailPublisherTest", null);
117         tmpFile.deleteOnExit();
118         String JavaDoc xsldir = tmpFile.getParent();
119         publisher.setXSLDir(xsldir);
120         publisher.setXSLFileList(tmpFile.getName());
121         String JavaDoc[] newFileNames = publisher.getXslFileNames();
122         checkXslFileNamesExistInDir(xsldir, newFileNames);
123         assertEquals(1, newFileNames.length);
124
125         // should work, regardless of spaces & comma between filenames
126
publisher.setXSLFileList(" ,, "
127                                  + tmpFile.getName()
128                                  + " ,,,"
129                                  + tmpFile.getName());
130         newFileNames = publisher.getXslFileNames();
131         checkXslFileNamesExistInDir(xsldir, newFileNames);
132         assertEquals(2, newFileNames.length);
133
134         // append should work
135
publisher.setXSLFileList("+" + tmpFile.getName());
136         newFileNames = publisher.getXslFileNames();
137         checkXslFileNamesExistInDir(xsldir, newFileNames);
138         assertEquals(3, newFileNames.length);
139
140         // should work, if leading spaces
141
publisher.setXSLFileList(" +" + tmpFile.getName());
142         newFileNames = publisher.getXslFileNames();
143         checkXslFileNamesExistInDir(xsldir, newFileNames);
144         assertEquals(4, newFileNames.length);
145     }
146
147     public void testCreateLinkLine() {
148         String JavaDoc serverURL = "http://myserver/context/servlet";
149         publisher.setBuildResultsURL(serverURL);
150         String JavaDoc path = "logs" + File.separator;
151         String JavaDoc date = "20020607115519";
152         String JavaDoc label = "mylabel.100";
153
154         String JavaDoc successFilePrefix = "log" + date + "L" + label;
155         String JavaDoc successURL = serverURL + "?log=" + successFilePrefix;
156         String JavaDoc successLink = "View results here -> <a HREF=\"" + successURL + "\">" + successURL + "</a>";
157         String JavaDoc successFile = successFilePrefix + ".xml";
158         String JavaDoc successLogFileName = path + successFile;
159         assertEquals(successLink, publisher.createLinkLine(successLogFileName));
160
161         publisher.setBuildResultsURL(null);
162         assertEquals("", publisher.createLinkLine(successLogFileName));
163
164         publisher.setBuildResultsURL(serverURL);
165         String JavaDoc failFilePrefix = "log" + date;
166         String JavaDoc failURL = serverURL + "?log=" + failFilePrefix;
167         String JavaDoc failLink = "View results here -> <a HREF=\"" + failURL + "\">" + failURL + "</a>";
168         String JavaDoc failFile = failFilePrefix + ".xml";
169         String JavaDoc failLogFileName = path + failFile;
170         assertEquals(failLink, publisher.createLinkLine(failLogFileName));
171     }
172
173     public void testQuestionMarkInBuildResultsURL() {
174         String JavaDoc serverURL = "http://myserver/context/servlet?key=value";
175         publisher.setBuildResultsURL(serverURL);
176         String JavaDoc path = "logs" + File.separator;
177         String JavaDoc date = "20020607115519";
178         String JavaDoc label = "mylabel.100";
179
180         String JavaDoc successFilePrefix = "log" + date + "L" + label;
181         String JavaDoc successURL = serverURL + "&log=" + successFilePrefix;
182         String JavaDoc successLink = "View results here -> <a HREF=\"" + successURL + "\">" + successURL + "</a>";
183         String JavaDoc successFile = successFilePrefix + ".xml";
184         String JavaDoc successLogFileName = path + successFile;
185
186         assertEquals(successLink, publisher.createLinkLine(successLogFileName));
187     }
188
189     public void testValidate() {
190         setEmailPublisherVariables(publisher);
191
192         String JavaDoc[] origFileNames = publisher.getXslFileNames();
193         try {
194             publisher.validate();
195             fail("should fail if xslFileNames is null");
196         } catch (CruiseControlException ex) {
197             // should fail
198
}
199
200         publisher.setXSLFileNames(origFileNames);
201         publisher.setXSLFile("this file doesn't exist");
202         try {
203             publisher.validate();
204             fail("should fail if the specified xslFile doesn't exist");
205         } catch (CruiseControlException ex) {
206             // should fail
207
}
208
209         publisher.setXSLFileNames(origFileNames);
210         publisher.setXSLFileList("this-file-doesn't-exist");
211         try {
212             publisher.validate();
213             fail("should fail if specified xslFileList file doesn't exist");
214         } catch (CruiseControlException ex) {
215             // should fail
216
}
217
218         publisher.setXSLFileNames(origFileNames);
219         publisher.setXSLFileList("+this-file-doesn't-exist");
220         try {
221             publisher.validate();
222             fail("should fail if sepcified xslFileList file to append doesn't exist");
223         } catch (CruiseControlException ex) {
224             // should fail
225
}
226
227         publisher.setXSLFileNames(origFileNames);
228         publisher.setXSLFileList(".");
229         try {
230             publisher.validate();
231             fail("should fail if xslFileList file is a directory");
232         } catch (CruiseControlException ex) {
233             // should fail
234
}
235     }
236     
237     public void testGetContentType() {
238         String JavaDoc defaultType = "text/html";
239         assertEquals(defaultType, publisher.getContentType());
240         publisher.setCharset("ISO-8859-1");
241         String JavaDoc withCharset = "text/html; charset=\"ISO-8859-1\"";
242         assertEquals(withCharset, publisher.getContentType());
243     }
244
245     public void testTransformWithParameter() throws IOException JavaDoc, TransformerException JavaDoc {
246         String JavaDoc expected = "with.a.value";
247         Property param = publisher.createParameter();
248         param.setName("some.parameter");
249         param.setValue(expected);
250         Source JavaDoc xsl = new StreamSource JavaDoc(new StringReader JavaDoc(
251                 "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">\n"
252                 + "<xsl:output method='text'/>\n"
253                 + "<xsl:param name='some.parameter'/>\n"
254                 + "<xsl:template match='/'>\n"
255                 + " <xsl:value-of select='$some.parameter' />\n"
256                 + "</xsl:template>"
257                 + "</xsl:stylesheet>"));
258         Source JavaDoc xml = new StreamSource JavaDoc(new StringReader JavaDoc("<cruisecontrol/>"));
259         String JavaDoc message = publisher.transformFile(xml, TransformerFactory.newInstance(), xsl);
260         assertEquals(expected, message);
261     }
262
263     private void setEmailPublisherVariables(HTMLEmailPublisher htmlemailpublisher) {
264         htmlemailpublisher.setBuildResultsURL("url");
265         htmlemailpublisher.setMailHost("host");
266         htmlemailpublisher.setReturnAddress("address");
267         htmlemailpublisher.setLogDir(".");
268         htmlemailpublisher.setXSLDir(".");
269     }
270
271 }
272
Popular Tags