KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > taglib > XSLTagTest


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001, 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.taglib;
38
39 import java.io.ByteArrayOutputStream JavaDoc;
40 import java.io.CharArrayWriter JavaDoc;
41 import java.io.File JavaDoc;
42 import java.io.FileWriter JavaDoc;
43 import java.io.OutputStream JavaDoc;
44 import java.io.StringWriter JavaDoc;
45 import java.io.Writer JavaDoc;
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 JavaDoc logDir;
55     private File JavaDoc log1;
56     private File JavaDoc log2;
57     private File JavaDoc log3;
58
59     public XSLTagTest(String JavaDoc name) {
60         super(name);
61     }
62
63     protected void setUp() throws Exception JavaDoc {
64         logDir = new File JavaDoc("testresults/");
65         if (!logDir.exists()) {
66             assertTrue("Failed to create test result dir", logDir.mkdir());
67         }
68         log1 = new File JavaDoc(logDir, "log20040903010203.xml");
69         log2 = new File JavaDoc(logDir, "log20040905010203.xml");
70         log3 = new File JavaDoc(logDir, "log20051021103500.xml");
71     }
72
73     protected void tearDown() throws Exception JavaDoc {
74         log1.delete();
75         log2.delete();
76         log3.delete();
77         logDir.delete();
78     }
79
80     public void testTransform() throws Exception JavaDoc {
81         final String JavaDoc 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 JavaDoc out = new ByteArrayOutputStream JavaDoc();
95
96         XSLTag tag = createXSLTag();
97
98         // JDK1.4 use File.toURI().toURL() instead
99
tag.transform(new LogFile(log3), log1.toURL(), out);
100         assertEquals("test=3.1", out.toString());
101     }
102
103     public void testTransformNested() throws Exception JavaDoc {
104         final String JavaDoc 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 JavaDoc 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 JavaDoc out = new ByteArrayOutputStream JavaDoc();
124
125         XSLTag tag = createXSLTag();
126
127         // JDK1.4 use File.toURI().toURL() instead
128
tag.transform(new LogFile(log3), log2.toURL(), out);
129         assertEquals("test=3.1", out.toString());
130     }
131     
132     public void testTransformUTF8() throws Exception JavaDoc {
133          final String JavaDoc 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=\"'&#198;&#216;&#197;'\"/>"
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 JavaDoc writer = new CharArrayWriter JavaDoc();
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 JavaDoc expectedValue = "log20020221120000-cruisecontrol.html";
156         assertEquals(expectedValue, tag.getCachedCopyFileName(new File JavaDoc("log20020221120000.xml")));
157     }
158
159     public void testIsNoCacheCurrent() throws Exception JavaDoc {
160         writeFile(log1, "");
161
162         XSLTag tag = createXSLTag();
163         assertFalse(tag.isCacheFileCurrent(log1, log2));
164     }
165
166     public void testIsEmptyCacheCurrent() throws Exception JavaDoc {
167         writeFile(log1, "");
168         writeFile(log2, "");
169
170         XSLTag tag = createXSLTag();
171         assertFalse(tag.isCacheFileCurrent(log1, log2));
172     }
173
174     // NOTE: Testing if a the XSL file is newer than the cache or XML log file
175
// cannot use newly created files because of the timestamp granularity of
176
// the filesystem which can be as high as 2 seconds
177

178     public void testServeCachedCopy() throws Exception JavaDoc {
179         writeFile(log3, "<test></test>");
180         StringWriter JavaDoc out = new StringWriter JavaDoc();
181         XSLTag tag = createXSLTag();
182
183         tag.serveCachedCopy(log3, out);
184         assertEquals("<test></test>", out.toString());
185     }
186
187     public void testGetXSLTParameters() throws Exception JavaDoc {
188         final String JavaDoc 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 JavaDoc writer = new CharArrayWriter JavaDoc();
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 JavaDoc file, String JavaDoc body) throws Exception JavaDoc {
223         FileWriter JavaDoc writer = new FileWriter JavaDoc(file);
224         writer.write(body);
225         writer.close();
226     }
227 }
228
Popular Tags