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.IOException;
19 import java.io.StringWriter;
20 import java.io.UnsupportedEncodingException;
21 import java.net.URL;
22
23 import org.apache.commons.jelly.JellyContext;
24 import org.apache.commons.jelly.JellyException;
25 import org.apache.commons.jelly.Script;
26 import org.apache.commons.jelly.XMLOutput;
27 import org.apache.commons.jelly.parser.XMLParser;
28 import org.xml.sax.SAXException;
29
30 import junit.framework.TestCase;
31
32 /***
33 * Test that compiled scripts can access resources
34 */
35 public class TestImport extends TestCase {
36
37 private String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
38 + "<html xmlns=\"http://www.w3.org/TR/xhtml1/strict\" "
39 + "xmlns=\"http://www.w3.org/TR/xhtml1/strict\">"
40 + "<head><title>Expense Report Summary</title></head>"
41 + "<body><p>Total Amount: 12</p></body></html>";
42 public TestImport(String name) {
43 super(name);
44 }
45
46 public void testImportResources() throws JellyException, UnsupportedEncodingException, IOException {
47 JellyContext context = new JellyContext();
48 URL url = TestImport.class.getResource("/resources/import.jelly");
49 StringWriter writer = new StringWriter();
50 XMLOutput out = XMLOutput.createXMLOutput(writer);
51
52
53 context.runScript(url, out);
54 out.close();
55 assertEquals(expected, writer.toString());
56 }
57
58 public void testImportResourcesCompiled() throws JellyException, UnsupportedEncodingException, IOException {
59 JellyContext context = new JellyContext();
60 URL url = TestImport.class.getResource("/resources/import.jelly");
61 StringWriter writer = new StringWriter();
62 XMLOutput out = XMLOutput.createXMLOutput(writer);
63 Script script = context.compileScript(url);
64 script.run(context, out);
65 out.close();
66 assertEquals(expected, writer.toString());
67 }
68
69 public void testImportResourcesFromUncompiledScript() throws JellyException, UnsupportedEncodingException, IOException, SAXException {
70 JellyContext context = new JellyContext();
71 URL url = TestImport.class.getResource("/resources/import.jelly");
72 StringWriter writer = new StringWriter();
73 XMLOutput out = XMLOutput.createXMLOutput(writer);
74 Script script = new XMLParser().parse(url);
75 script.run(context, out);
76 out.close();
77 assertEquals(expected, writer.toString());
78 }
79
80 }