1 37 package net.sourceforge.cruisecontrol.taglib; 38 39 import java.io.ByteArrayOutputStream ; 40 import java.io.CharArrayWriter ; 41 import java.io.File ; 42 import java.io.FileWriter ; 43 import java.io.OutputStream ; 44 import java.io.StringWriter ; 45 import java.io.Writer ; 46 import junit.framework.TestCase; 47 import net.sourceforge.cruisecontrol.LogFile; 48 import net.sourceforge.cruisecontrol.mock.MockPageContext; 49 import net.sourceforge.cruisecontrol.mock.MockServletConfig; 50 import net.sourceforge.cruisecontrol.mock.MockServletContext; 51 52 public class XSLTagTest extends TestCase { 53 54 private File logDir; 55 private File log1; 56 private File log2; 57 private File log3; 58 59 public XSLTagTest(String name) { 60 super(name); 61 } 62 63 protected void setUp() throws Exception { 64 logDir = new File ("testresults/"); 65 if (!logDir.exists()) { 66 assertTrue("Failed to create test result dir", logDir.mkdir()); 67 } 68 log1 = new File (logDir, "log20040903010203.xml"); 69 log2 = new File (logDir, "log20040905010203.xml"); 70 log3 = new File (logDir, "log20051021103500.xml"); 71 } 72 73 protected void tearDown() throws Exception { 74 log1.delete(); 75 log2.delete(); 76 log3.delete(); 77 logDir.delete(); 78 } 79 80 public void testTransform() throws Exception { 81 final String styleSheetText = 82 "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\" " 83 + "xmlns:lxslt=\"http://xml.apache.org/xslt\">" 84 + "<xsl:output method=\"text\"/>" 85 + "<xsl:template match=\"/\">" 86 + "<xsl:apply-templates />" 87 + "</xsl:template>" 88 + "<xsl:template match=\"test\" >" 89 + "test=<xsl:value-of select=\"/\" />.<xsl:value-of select=\"@sub\" />" 90 + "</xsl:template>" 91 + "</xsl:stylesheet>"; 92 writeFile(log1, styleSheetText); 93 writeFile(log3, "<test sub=\"1\">3</test>"); 94 OutputStream out = new ByteArrayOutputStream (); 95 96 XSLTag tag = createXSLTag(); 97 98 tag.transform(new LogFile(log3), log1.toURL(), out); 100 assertEquals("test=3.1", out.toString()); 101 } 102 103 public void testTransformNested() throws Exception { 104 final String innerStyleSheetText = 105 "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\" " 106 + "xmlns:lxslt=\"http://xml.apache.org/xslt\">" 107 + "<xsl:template match=\"test\" >" 108 + "test=<xsl:value-of select=\"/\" />.<xsl:value-of select=\"@sub\" />" 109 + "</xsl:template>" 110 + "</xsl:stylesheet>"; 111 writeFile(log1, innerStyleSheetText); 112 final String outerStyleSheetText = 113 "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\" " 114 + "xmlns:lxslt=\"http://xml.apache.org/xslt\">" 115 + "<xsl:output method=\"text\"/>" 116 + "<xsl:include HREF=\"" + log1.getName() + "\" />" 117 + "<xsl:template match=\"/\">" 118 + "<xsl:apply-templates />" 119 + "</xsl:template>" 120 + "</xsl:stylesheet>"; 121 writeFile(log2, outerStyleSheetText); 122 writeFile(log3, "<test sub=\"1\">3</test>"); 123 OutputStream out = new ByteArrayOutputStream (); 124 125 XSLTag tag = createXSLTag(); 126 127 tag.transform(new LogFile(log3), log2.toURL(), out); 129 assertEquals("test=3.1", out.toString()); 130 } 131 132 public void testTransformUTF8() throws Exception { 133 final String styleSheetText = 134 "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\" " 135 + "xmlns:lxslt=\"http://xml.apache.org/xslt\">" 136 + "<xsl:output method=\"text\"/>" 137 + "<xsl:template match=\"/\">" 138 + "<xsl:value-of disable-output-escaping=\"yes\" select=\"'ÆØÅ'\"/>" 139 + "</xsl:template>" 140 + "</xsl:stylesheet>"; 141 writeFile(log1, styleSheetText); 142 writeFile(log2, "<test sub=\"1\">3</test>"); 143 144 XSLTag tag = createXSLTag(); 145 tag.setXslFile(log1.getName()); 146 tag.updateCacheFile(new LogFile(log2), log3); 147 Writer writer = new CharArrayWriter (); 148 tag.serveCachedCopy(log3, writer); 149 assertEquals("\u00c6\u00d8\u00c5", writer.toString()); 150 } 151 152 public void testGetCachedCopyFileName() { 153 XSLTag tag = createXSLTag(); 154 tag.setXslFile("xsl/cruisecontrol.xsl"); 155 final String expectedValue = "log20020221120000-cruisecontrol.html"; 156 assertEquals(expectedValue, tag.getCachedCopyFileName(new File ("log20020221120000.xml"))); 157 } 158 159 public void testIsNoCacheCurrent() throws Exception { 160 writeFile(log1, ""); 161 162 XSLTag tag = createXSLTag(); 163 assertFalse(tag.isCacheFileCurrent(log1, log2)); 164 } 165 166 public void testIsEmptyCacheCurrent() throws Exception { 167 writeFile(log1, ""); 168 writeFile(log2, ""); 169 170 XSLTag tag = createXSLTag(); 171 assertFalse(tag.isCacheFileCurrent(log1, log2)); 172 } 173 174 178 public void testServeCachedCopy() throws Exception { 179 writeFile(log3, "<test></test>"); 180 StringWriter out = new StringWriter (); 181 XSLTag tag = createXSLTag(); 182 183 tag.serveCachedCopy(log3, out); 184 assertEquals("<test></test>", out.toString()); 185 } 186 187 public void testGetXSLTParameters() throws Exception { 188 final String styleSheetText = 189 "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">" 190 + "<xsl:output method=\"text\"/>" 191 + "<xsl:param name=\"context.parameter\"/>" 192 + "<xsl:param name=\"config.parameter\"/>" 193 + "<xsl:template match=\"/\">" 194 + "<xsl:value-of select=\"$config.parameter\"/>" 195 + "<xsl:value-of select=\"$context.parameter\"/>" 196 + "</xsl:template>" 197 + "</xsl:stylesheet>"; 198 writeFile(log1, styleSheetText); 199 writeFile(log2, "<test/>"); 200 201 XSLTag tag = createXSLTag(); 202 MockServletConfig config = (MockServletConfig) tag.getPageContext().getServletConfig(); 203 config.setInitParameter("xslt.config.parameter", "config.value"); 204 MockServletContext context = (MockServletContext) tag.getPageContext().getServletContext(); 205 context.setInitParameter("xslt.context.parameter", "context.value"); 206 tag.setXslFile(log1.getName()); 207 tag.updateCacheFile(new LogFile(log2), log3); 208 Writer writer = new CharArrayWriter (); 209 tag.serveCachedCopy(log3, writer); 210 assertEquals("config.valuecontext.value", writer.toString()); 211 } 212 213 private XSLTag createXSLTag() { 214 XSLTag tag = new XSLTag(); 215 final MockPageContext pageContext = new MockPageContext(); 216 final MockServletContext servletContext = (MockServletContext) pageContext.getServletContext(); 217 servletContext.setBaseResourceDir(logDir); 218 tag.setPageContext(pageContext); 219 return tag; 220 } 221 222 private void writeFile(File file, String body) throws Exception { 223 FileWriter writer = new FileWriter (file); 224 writer.write(body); 225 writer.close(); 226 } 227 } 228 | Popular Tags |