1 37 package net.sourceforge.cruisecontrol.publishers; 38 39 import java.io.File ; 40 import java.io.FileOutputStream ; 41 import java.io.StringReader ; 42 import java.util.Iterator ; 43 import java.util.Properties ; 44 import java.util.Set ; 45 import java.util.TreeSet ; 46 47 import javax.mail.internet.AddressException ; 48 import javax.mail.internet.InternetAddress ; 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 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 { 80 PropertiesMapper propertiesMapper = new PropertiesMapper(); 81 Properties props = new Properties (); 83 tmpFile = File.createTempFile("cruise", "Test"); 84 tmpFile.deleteOnExit(); 85 props.setProperty("always1", "always1"); 86 FileOutputStream fos = new FileOutputStream (tmpFile); 87 props.store(fos, null); 88 fos.close(); 89 90 String 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 xml) 108 throws Exception { 109 110 SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser"); 111 112 Element emailPublisherElement = builder.build(new StringReader (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 generateXML(boolean includeAlerts) { 129 StringBuffer xml = new StringBuffer (); 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 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 { 172 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 { 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 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 { 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 { 285 String returnAddress = "me@you.com"; 286 String returnName = "Me you Me"; 287 emailPublisher.setReturnAddress(returnAddress); 288 emailPublisher.setReturnName(returnName); 289 InternetAddress fromAddress = emailPublisher.getFromAddress(); 290 assertEquals(returnAddress, fromAddress.getAddress()); 291 assertEquals(returnName, fromAddress.getPersonal()); 292 } 293 294 public void testSendMail() throws Exception { 295 assertFalse(emailPublisher.sendMail(null, "subject", "message", false)); 296 assertFalse(emailPublisher.sendMail(" ", "subject", "message", false)); 297 } 298 299 300 public void testCreateUserSet() throws Exception { 301 emailPublisher.setReportSuccess("success"); 302 Set 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 319 public void testCreateModsElement() throws Exception { 320 Set modSet = successLogHelper.getModifications(); 321 Modification mod = null; 322 Iterator 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 { 336 emailPublisher.validate(); 337 Set alertUsers = emailPublisher.createAlertUserSet(successLogHelper); 338 assertTrue(alertUsers.contains("filename1@host.com")); 340 assertTrue(alertUsers.contains("filename3@host.com")); 341 assertFalse(alertUsers.contains("")); 342 assertEquals(2, alertUsers.size()); 345 346 alertUsers = noAlertsEmailPublisher.createAlertUserSet(failureLogHelper); 347 assertEquals(0, alertUsers.size()); 348 } 349 350 public void testCreateEmailString() { 351 Set emailSet = new TreeSet (); 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 |