1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.tags.xml;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.InputStream;
21 import java.io.StringWriter;
22 import java.util.Iterator;
23 import java.util.List;
24
25 import junit.framework.Test;
26 import junit.framework.TestCase;
27 import junit.framework.TestSuite;
28 import junit.textui.TestRunner;
29
30 import org.apache.commons.jelly.JellyContext;
31 import org.apache.commons.jelly.Script;
32 import org.apache.commons.jelly.XMLOutput;
33 import org.apache.commons.jelly.parser.XMLParser;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.dom4j.Document;
37 import org.dom4j.DocumentHelper;
38 import org.dom4j.Node;
39
40 /*** Tests the parser, the engine and the XML tags
41 *
42 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
43 * @version $Revision: 1.4 $
44 */
45 public class TestXMLTags extends TestCase {
46
47 /*** The Log to which logging calls will be made. */
48 private static final Log log = LogFactory.getLog(TestXMLTags.class);
49
50 /*** basedir for test source */
51 private static final String testBaseDir ="src/test/org/apache/commons/jelly/tags/xml";
52
53 public static void main(String[] args) {
54 TestRunner.run(suite());
55 }
56
57 public static Test suite() {
58 return new TestSuite(TestXMLTags.class);
59 }
60
61 public TestXMLTags(String testName) {
62 super(testName);
63 }
64
65 public void testUnitTests() throws Exception {
66 runUnitTest( testBaseDir + "/testForEach.jelly" );
67 }
68
69 public void testExpressions() throws Exception {
70 runUnitTest( testBaseDir + "/testExpressions.jelly");
71 }
72
73 public void testParse() throws Exception {
74 InputStream in = new FileInputStream(testBaseDir + "/example.jelly");
75 XMLParser parser = new XMLParser();
76 Script script = parser.parse(in);
77 script = script.compile();
78 log.debug("Found: " + script);
79 assertTrue("Parsed a Script", script instanceof Script);
80 StringWriter buffer = new StringWriter();
81 script.run(parser.getContext(), XMLOutput.createXMLOutput(buffer));
82 String text = buffer.toString().trim();
83 if (log.isDebugEnabled()) {
84 log.debug("Evaluated script as...");
85 log.debug(text);
86 }
87 assertEquals("Produces the correct output", "It works!", text);
88 }
89
90 public void testTransform() throws Exception {
91 String text = evaluteScriptAsText(testBaseDir + "/transformExample.jelly");
92 assertEquals("Produces the correct output", "It works!", text);
93 }
94
95 public void testTransformAllInLine() throws Exception {
96 String text = evaluteScriptAsText(testBaseDir + "/transformExampleAllInLine.jelly");
97 assertEquals("Produces the correct output", "It works!", text);
98 }
99
100 public void testTransformParams() throws Exception {
101 String text = evaluteScriptAsText(testBaseDir + "/transformParamExample.jelly");
102 assertEquals("Produces the correct output", "It works!", text);
103 }
104
105 public void testTransformParamsInLine() throws Exception {
106
107 String text = evaluteScriptAsText(testBaseDir + "/transformParamExample2.jelly");
108 assertEquals("Produces the correct output", "It works!", text);
109 }
110
111 public void testTransformSAXOutput() throws Exception {
112 String text = evaluteScriptAsText(testBaseDir + "/transformExampleSAXOutput.jelly");
113 assertEquals("Produces the correct output", "It works!", text);
114 }
115
116 public void testTransformSAXOutputNestedTransforms() throws Exception {
117 String text = evaluteScriptAsText(testBaseDir +
118 "/transformExampleSAXOutputNestedTransforms.jelly");
119 assertEquals("Produces the correct output", "It works!", text);
120 }
121
122 public void testTransformSchematron() throws Exception {
123 String text = evaluteScriptAsText(testBaseDir +
124 "/schematron/transformSchematronExample.jelly");
125 assertEquals("Produces the correct output", "Report count=1:assert count=2", text);
126 }
127
128 public void testTransformXmlVar() throws Exception {
129 String text = evaluteScriptAsText(testBaseDir +
130 "/transformExampleXmlVar.jelly");
131 assertEquals("Produces the correct output", "It works!", text);
132 }
133
134 public void testDoctype() throws Exception {
135 String text = evaluteScriptAsText(testBaseDir +
136 "/testDoctype.jelly");
137 assertEquals("Produces the correct output", "<!DOCTYPE foo PUBLIC \"publicID\" \"foo.dtd\">\n<foo></foo>", text);
138 }
139
140 public void runUnitTest(String name) throws Exception {
141 Document document = parseUnitTest(name);
142
143 List failures = document.selectNodes( "/*/fail" );
144 for ( Iterator iter = failures.iterator(); iter.hasNext(); ) {
145 Node node = (Node) iter.next();
146 fail( node.getStringValue() );
147 }
148 }
149
150 public Document parseUnitTest(String name) throws Exception {
151
152 InputStream in = new FileInputStream(name);
153 XMLParser parser = new XMLParser();
154 Script script = parser.parse(in);
155 script = script.compile();
156 assertTrue("Parsed a Script", script instanceof Script);
157 StringWriter buffer = new StringWriter();
158 script.run(parser.getContext(), XMLOutput.createXMLOutput(buffer));
159
160 String text = buffer.toString().trim();
161 if (log.isDebugEnabled()) {
162 log.debug("Evaluated script as...");
163 log.debug(text);
164 }
165
166
167 return DocumentHelper.parseText( text );
168 }
169
170 /***
171 * Evaluates the script by the given file name and
172 * returns the whitespace trimmed output as text
173 */
174 protected String evaluteScriptAsText(String fileName) throws Exception {
175 JellyContext context = new JellyContext();
176
177
178
179 context.setRootURL(new File(".").toURL());
180
181
182 StringWriter buffer = new StringWriter();
183 XMLOutput output = XMLOutput.createXMLOutput(buffer);
184
185 context.runScript( new File(fileName), output );
186 String text = buffer.toString().trim();
187 if (log.isDebugEnabled()) {
188 log.debug("Evaluated script as...");
189 log.debug(text);
190 }
191 return text;
192 }
193 }