1 37 package net.sourceforge.cruisecontrol.publishers; 38 39 import java.io.File ; 40 41 import junit.framework.TestCase; 42 import net.sourceforge.cruisecontrol.CruiseControlException; 43 import java.io.IOException ; 44 import java.io.StringReader ; 45 import javax.xml.transform.Source ; 46 import javax.xml.transform.TransformerException ; 47 import javax.xml.transform.TransformerFactory ; 48 import javax.xml.transform.stream.StreamSource ; 49 import net.sourceforge.cruisecontrol.builders.Property; 50 51 public class HTMLEmailPublisherTest extends TestCase { 52 53 private HTMLEmailPublisher publisher; 54 private File tmpFile; 55 56 public void setUp() { 57 publisher = new HTMLEmailPublisher(); 58 } 59 60 private class BrokenTestPublisher extends HTMLEmailPublisher { 61 private String [] newXslFileNames = null; 62 63 BrokenTestPublisher() { 64 setXSLFileNames(newXslFileNames); 65 } 66 } 67 68 72 public void testSetXSLFileNames() { 73 try { 74 new BrokenTestPublisher(); 75 fail("setXSLFileNames should fail when called with null"); 76 } catch (IllegalArgumentException e) { 77 } 79 } 80 81 public void testSetLogDir() { 82 try { 83 publisher.setLogDir(null); 84 fail("setLogDir should fail when called with null"); 85 } catch (IllegalArgumentException e) { 86 } 88 } 89 90 private void checkXslFileNamesExistInDir(String dir, String [] fileNames) { 91 assertNotNull("getXslFileNames() returned null", fileNames); 92 93 for (int i = 0; i < fileNames.length; i++) { 94 String fileName = fileNames[i]; 95 File file = new File (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 { 104 try { 105 publisher.setXSLFileList(null); 106 fail("setXSLFileList should fail when called with null"); 107 } catch (IllegalArgumentException expected) { 108 } 109 110 try { 111 publisher.setXSLFileList(""); 112 fail("setXSLFileList should fail if specified xslFileList file is empty"); 113 } catch (IllegalArgumentException expected) { 114 } 115 116 tmpFile = File.createTempFile("HTMLEmailPublisherTest", null); 117 tmpFile.deleteOnExit(); 118 String xsldir = tmpFile.getParent(); 119 publisher.setXSLDir(xsldir); 120 publisher.setXSLFileList(tmpFile.getName()); 121 String [] newFileNames = publisher.getXslFileNames(); 122 checkXslFileNamesExistInDir(xsldir, newFileNames); 123 assertEquals(1, newFileNames.length); 124 125 publisher.setXSLFileList(" ,, " 127 + tmpFile.getName() 128 + " ,,," 129 + tmpFile.getName()); 130 newFileNames = publisher.getXslFileNames(); 131 checkXslFileNamesExistInDir(xsldir, newFileNames); 132 assertEquals(2, newFileNames.length); 133 134 publisher.setXSLFileList("+" + tmpFile.getName()); 136 newFileNames = publisher.getXslFileNames(); 137 checkXslFileNamesExistInDir(xsldir, newFileNames); 138 assertEquals(3, newFileNames.length); 139 140 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 serverURL = "http://myserver/context/servlet"; 149 publisher.setBuildResultsURL(serverURL); 150 String path = "logs" + File.separator; 151 String date = "20020607115519"; 152 String label = "mylabel.100"; 153 154 String successFilePrefix = "log" + date + "L" + label; 155 String successURL = serverURL + "?log=" + successFilePrefix; 156 String successLink = "View results here -> <a HREF=\"" + successURL + "\">" + successURL + "</a>"; 157 String successFile = successFilePrefix + ".xml"; 158 String 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 failFilePrefix = "log" + date; 166 String failURL = serverURL + "?log=" + failFilePrefix; 167 String failLink = "View results here -> <a HREF=\"" + failURL + "\">" + failURL + "</a>"; 168 String failFile = failFilePrefix + ".xml"; 169 String failLogFileName = path + failFile; 170 assertEquals(failLink, publisher.createLinkLine(failLogFileName)); 171 } 172 173 public void testQuestionMarkInBuildResultsURL() { 174 String serverURL = "http://myserver/context/servlet?key=value"; 175 publisher.setBuildResultsURL(serverURL); 176 String path = "logs" + File.separator; 177 String date = "20020607115519"; 178 String label = "mylabel.100"; 179 180 String successFilePrefix = "log" + date + "L" + label; 181 String successURL = serverURL + "&log=" + successFilePrefix; 182 String successLink = "View results here -> <a HREF=\"" + successURL + "\">" + successURL + "</a>"; 183 String successFile = successFilePrefix + ".xml"; 184 String successLogFileName = path + successFile; 185 186 assertEquals(successLink, publisher.createLinkLine(successLogFileName)); 187 } 188 189 public void testValidate() { 190 setEmailPublisherVariables(publisher); 191 192 String [] origFileNames = publisher.getXslFileNames(); 193 try { 194 publisher.validate(); 195 fail("should fail if xslFileNames is null"); 196 } catch (CruiseControlException ex) { 197 } 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 } 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 } 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 } 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 } 235 } 236 237 public void testGetContentType() { 238 String defaultType = "text/html"; 239 assertEquals(defaultType, publisher.getContentType()); 240 publisher.setCharset("ISO-8859-1"); 241 String withCharset = "text/html; charset=\"ISO-8859-1\""; 242 assertEquals(withCharset, publisher.getContentType()); 243 } 244 245 public void testTransformWithParameter() throws IOException , TransformerException { 246 String expected = "with.a.value"; 247 Property param = publisher.createParameter(); 248 param.setName("some.parameter"); 249 param.setValue(expected); 250 Source xsl = new StreamSource (new StringReader ( 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 xml = new StreamSource (new StringReader ("<cruisecontrol/>")); 259 String 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
|