KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileOutputStream JavaDoc;
41 import java.io.StringReader JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.Properties JavaDoc;
44 import java.util.Set JavaDoc;
45 import java.util.TreeSet JavaDoc;
46
47 import javax.mail.internet.AddressException JavaDoc;
48 import javax.mail.internet.InternetAddress JavaDoc;
49
50 import junit.framework.TestCase;
51 import net.sourceforge.cruisecontrol.CruiseControlException;
52 import net.sourceforge.cruisecontrol.Modification;
53 import net.sourceforge.cruisecontrol.PluginXMLHelper;
54 import net.sourceforge.cruisecontrol.ProjectXMLHelper;
55 import net.sourceforge.cruisecontrol.publishers.email.DropLetterEmailAddressMapper;
56 import net.sourceforge.cruisecontrol.publishers.email.PropertiesMapper;
57 import net.sourceforge.cruisecontrol.testutil.TestUtil;
58 import net.sourceforge.cruisecontrol.util.XMLLogHelper;
59
60 import org.jdom.Element;
61 import org.jdom.input.SAXBuilder;
62
63 public class EmailPublisherTest extends TestCase {
64
65     private XMLLogHelper successLogHelper;
66     private XMLLogHelper fixedLogHelper;
67     private XMLLogHelper failureLogHelper;
68     private XMLLogHelper firstFailureLogHelper;
69     private EmailPublisher emailPublisher;
70     private EmailPublisher noAlertsEmailPublisher;
71     private File JavaDoc tmpFile;
72
73     protected XMLLogHelper createLogHelper(boolean success, boolean lastBuildSuccess) {
74         Element cruisecontrolElement = TestUtil.createElement(success, lastBuildSuccess);
75
76         return new XMLLogHelper(cruisecontrolElement);
77     }
78
79     public void setUp() throws Exception JavaDoc {
80         PropertiesMapper propertiesMapper = new PropertiesMapper();
81         // create a temp file to test propertiesmapper
82
Properties JavaDoc props = new Properties JavaDoc();
83         tmpFile = File.createTempFile("cruise", "Test");
84         tmpFile.deleteOnExit();
85         props.setProperty("always1", "always1");
86         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(tmpFile);
87         props.store(fos, null);
88         fos.close();
89
90         String JavaDoc xml = generateXML(true);
91         emailPublisher = initPublisher(propertiesMapper, xml);
92         emailPublisher.setMailHost("mailhost");
93         emailPublisher.setReturnAddress("returnaddress");
94
95         xml = generateXML(false);
96         noAlertsEmailPublisher = initPublisher(propertiesMapper, xml);
97         
98         successLogHelper = createLogHelper(true, true);
99         failureLogHelper = createLogHelper(false, false);
100         fixedLogHelper = createLogHelper(true, false);
101         firstFailureLogHelper = createLogHelper(false, true);
102     }
103
104     
105
106     protected EmailPublisher initPublisher(PropertiesMapper propMapper,
107                                   String JavaDoc xml)
108                                   throws Exception JavaDoc {
109         
110         SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser");
111         
112         Element emailPublisherElement = builder.build(new StringReader JavaDoc(xml)).getRootElement();
113         PluginXMLHelper xmlHelper = new PluginXMLHelper(new ProjectXMLHelper());
114
115         EmailPublisher ePublisher =
116             (MockEmailPublisher) xmlHelper.configure(
117                 emailPublisherElement,
118                 Class.forName("net.sourceforge.cruisecontrol.publishers.MockEmailPublisher"),
119                 false);
120
121         ePublisher.add(new DropLetterEmailAddressMapper());
122         propMapper.setFile(tmpFile.getPath());
123         ePublisher.add(propMapper);
124         
125         return ePublisher;
126     }
127     
128     protected String JavaDoc generateXML(boolean includeAlerts) {
129         StringBuffer JavaDoc xml = new StringBuffer JavaDoc();
130         xml.append("<email defaultsuffix='@host.com'>");
131         xml.append("<always address='always1'/>");
132         xml.append("<always address='always1@host.com'/>");
133         xml.append("<always address='always2@host.com'/>");
134         xml.append("<always address='dropletteruser1'/>");
135         xml.append("<failure address='failure1'/>");
136         xml.append("<failure address='failure2@host.com' reportWhenFixed='true'/>");
137         xml.append("<success address='success1' />");
138         xml.append("<success address='success2@host.com' />");
139         xml.append("<map alias='user3' address='user3@host2.com'/>");
140         if (includeAlerts) {
141             //xml.append("<alert file='.*' address='anyFileMod@host.com' />");
142
xml.append("<alert fileRegExpr='filename1' address='filename1@host.com' />");
143             xml.append("<alert fileRegExpr='basedir/subdirectory2/.*' address='subdir2@host.com' />");
144             xml.append("<alert fileRegExpr='basedir/subdirectory3/filename3' address='filename3@host.com' />");
145             xml.append("<alert fileRegExpr='basedir/subdirectory5/.*' address='basedirSubdirectory5@host.com' />");
146             xml.append("<alert fileRegExpr='' address='' />");
147         }
148         xml.append("</email>");
149         
150         return xml.toString();
151     }
152
153     public void testValidate() {
154         EmailPublisher publisher = new MockEmailPublisher();
155         try {
156             publisher.validate();
157             fail("EmailPublisher should throw exceptions when required fields are not set.");
158         } catch (CruiseControlException e) {
159         }
160
161         publisher.setMailHost("mailhost");
162         publisher.setReturnAddress("returnaddress");
163
164         try {
165             publisher.validate();
166         } catch (CruiseControlException e) {
167             fail("EmailPublisher should not throw exceptions when required fields are set.");
168         }
169     }
170
171     public void testShouldSend() throws Exception JavaDoc {
172         //build not necessary, spam while broken=true
173
emailPublisher.setSpamWhileBroken(true);
174         emailPublisher.setReportSuccess("success");
175         assertEquals(true, emailPublisher.shouldSend(successLogHelper));
176         assertEquals(true, emailPublisher.shouldSend(fixedLogHelper));
177         assertEquals(true, emailPublisher.shouldSend(failureLogHelper));
178
179         emailPublisher.setReportSuccess("fixes");
180         assertEquals(false, emailPublisher.shouldSend(successLogHelper));
181         assertEquals(true, emailPublisher.shouldSend(fixedLogHelper));
182         assertEquals(true, emailPublisher.shouldSend(failureLogHelper));
183
184         emailPublisher.setReportSuccess("never");
185         assertEquals(false, emailPublisher.shouldSend(successLogHelper));
186         assertEquals(false, emailPublisher.shouldSend(fixedLogHelper));
187         assertEquals(true, emailPublisher.shouldSend(failureLogHelper));
188
189         emailPublisher.setSpamWhileBroken(false);
190         emailPublisher.setReportSuccess("success");
191         assertEquals(true, emailPublisher.shouldSend(successLogHelper));
192         assertEquals(true, emailPublisher.shouldSend(fixedLogHelper));
193         assertEquals(false, emailPublisher.shouldSend(failureLogHelper));
194         assertEquals(true, emailPublisher.shouldSend(firstFailureLogHelper));
195
196         emailPublisher.setReportSuccess("fixes");
197         assertEquals(false, emailPublisher.shouldSend(successLogHelper));
198         assertEquals(true, emailPublisher.shouldSend(fixedLogHelper));
199         assertEquals(false, emailPublisher.shouldSend(failureLogHelper));
200         assertEquals(true, emailPublisher.shouldSend(firstFailureLogHelper));
201
202         emailPublisher.setReportSuccess("never");
203         assertEquals(false, emailPublisher.shouldSend(successLogHelper));
204         assertEquals(false, emailPublisher.shouldSend(fixedLogHelper));
205         assertEquals(false, emailPublisher.shouldSend(failureLogHelper));
206         assertEquals(true, emailPublisher.shouldSend(firstFailureLogHelper));
207
208     }
209
210     public void testCreateSubject() throws Exception JavaDoc {
211         emailPublisher.setReportSuccess("always");
212         assertEquals(
213             "someproject somelabel Build Successful",
214             emailPublisher.createSubject(successLogHelper));
215         emailPublisher.setReportSuccess("fixes");
216         assertEquals(
217             "someproject somelabel Build Fixed",
218             emailPublisher.createSubject(fixedLogHelper));
219
220         assertEquals("someproject Build Failed",
221                 emailPublisher.createSubject(failureLogHelper));
222
223         emailPublisher.setSubjectPrefix("[CC]");
224         emailPublisher.setReportSuccess("always");
225         assertEquals(
226             "[CC] someproject somelabel Build Successful",
227             emailPublisher.createSubject(successLogHelper));
228         emailPublisher.setReportSuccess("fixes");
229         assertEquals(
230             "[CC] someproject somelabel Build Fixed",
231             emailPublisher.createSubject(fixedLogHelper));
232
233         assertEquals(
234             "[CC] someproject Build Failed",
235             emailPublisher.createSubject(failureLogHelper));
236
237         //Anytime it is a "fixed" build, the subject should read "fixed".
238
emailPublisher.setReportSuccess("always");
239         assertEquals(
240             "[CC] someproject somelabel Build Fixed",
241             emailPublisher.createSubject(fixedLogHelper));
242
243         emailPublisher.setReportSuccess("failures");
244         assertEquals(
245             "[CC] someproject somelabel Build Fixed",
246             emailPublisher.createSubject(fixedLogHelper));
247
248     }
249
250     public void testCreateUserList() throws Exception JavaDoc {
251         assertEquals(
252                 "always1@host.com,always2@host.com,ropletteruser1@host.com,"
253                 + "success1@host.com,success2@host.com,"
254                 + "user1@host.com,user2@host.com,user3@host2.com",
255             emailPublisher.createUserList(successLogHelper));
256         assertEquals(
257             "always1@host.com,always2@host.com,failure1@host.com,"
258                 + "failure2@host.com,ropletteruser1@host.com,user1@host.com,user2@host.com,user3@host2.com",
259             emailPublisher.createUserList(failureLogHelper));
260         assertEquals(
261             "always1@host.com,always2@host.com,"
262                 + "failure2@host.com,ropletteruser1@host.com,"
263                 + "success1@host.com,success2@host.com,"
264                 + "user1@host.com,user2@host.com,user3@host2.com",
265             emailPublisher.createUserList(fixedLogHelper));
266
267         emailPublisher.setSkipUsers(true);
268         assertEquals(
269                 "always1@host.com,always2@host.com,ropletteruser1@host.com,success1@host.com,success2@host.com",
270             emailPublisher.createUserList(successLogHelper));
271         assertEquals(
272                 "always1@host.com,always2@host.com,failure1@host.com,failure2@host.com,ropletteruser1@host.com",
273             emailPublisher.createUserList(failureLogHelper));
274
275         emailPublisher.setSkipUsers(false);
276
277         assertEquals(
278                 "always1@host.com,always2@host.com,failure2@host.com,"
279                 + "ropletteruser1@host.com,success1@host.com,success2@host.com,"
280                 + "user1@host.com,user2@host.com,user3@host2.com",
281             emailPublisher.createUserList(fixedLogHelper));
282     }
283
284     public void testGetFromAddress() throws AddressException JavaDoc {
285         String JavaDoc returnAddress = "me@you.com";
286         String JavaDoc returnName = "Me you Me";
287         emailPublisher.setReturnAddress(returnAddress);
288         emailPublisher.setReturnName(returnName);
289         InternetAddress JavaDoc fromAddress = emailPublisher.getFromAddress();
290         assertEquals(returnAddress, fromAddress.getAddress());
291         assertEquals(returnName, fromAddress.getPersonal());
292     }
293
294     public void testSendMail() throws Exception JavaDoc {
295         assertFalse(emailPublisher.sendMail(null, "subject", "message", false));
296         assertFalse(emailPublisher.sendMail(" ", "subject", "message", false));
297     }
298     
299
300     public void testCreateUserSet() throws Exception JavaDoc {
301         emailPublisher.setReportSuccess("success");
302         Set JavaDoc userSet = emailPublisher.createUserSet(successLogHelper);
303         assertNotNull(userSet);
304         assertTrue(userSet.contains("always1"));
305         assertTrue(userSet.contains("always2@host.com"));
306         assertTrue(userSet.contains("ropletteruser1"));
307         assertTrue(userSet.contains("success1"));
308         assertTrue(userSet.contains("user1"));
309         assertTrue(userSet.contains("user2"));
310         assertTrue(userSet.contains("user3@host2.com"));
311     }
312     
313     /**
314      * The following unit test ensures TestUtil.createModsElement
315      * creates a full XMLLogHelper object
316      *
317      * @throws Exception
318      */

319     public void testCreateModsElement() throws Exception JavaDoc {
320         Set JavaDoc modSet = successLogHelper.getModifications();
321         Modification mod = null;
322         Iterator JavaDoc modIter = modSet.iterator();
323         
324         while (modIter.hasNext()) {
325             mod = (Modification) modIter.next();
326             assertNotNull("getFileName should not return null", mod.getFileName());
327             assertNotNull("getFullPath should not return null", mod.getFullPath());
328             
329             if ("filename1".equalsIgnoreCase(mod.getFileName())) {
330                 assertNull(mod.getFolderName());
331             }
332         }
333     }
334     
335     public void testCreateAlertUserSet() throws Exception JavaDoc {
336         emailPublisher.validate();
337         Set JavaDoc alertUsers = emailPublisher.createAlertUserSet(successLogHelper);
338         //assertTrue(alertUsers.contains("anyFileMod@host.com"));
339
assertTrue(alertUsers.contains("filename1@host.com"));
340         assertTrue(alertUsers.contains("filename3@host.com"));
341         assertFalse(alertUsers.contains(""));
342 // assertTrue(alertUsers.contains("subdir2@host.com"));
343
// assertEquals(3, alertUsers.size());
344
assertEquals(2, alertUsers.size());
345         
346         alertUsers = noAlertsEmailPublisher.createAlertUserSet(failureLogHelper);
347         assertEquals(0, alertUsers.size());
348     }
349     
350     public void testCreateEmailString() {
351         Set JavaDoc emailSet = new TreeSet JavaDoc();
352         emailSet.add("always1@host.com");
353         emailSet.add("always2@host.com");
354         emailSet.add("always1@host.com");
355         emailSet.add("always3@host.com");
356  
357         assertEquals("always1@host.com,always2@host.com,always3@host.com",
358                 emailPublisher.createEmailString(emailSet));
359     }
360 }
361
Popular Tags