1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.test.impl;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.ByteArrayOutputStream;
20 import java.io.StringReader;
21
22 import junit.framework.Test;
23 import junit.framework.TestCase;
24 import junit.framework.TestSuite;
25 import junit.textui.TestRunner;
26
27 import org.apache.commons.jelly.JellyContext;
28 import org.apache.commons.jelly.XMLOutput;
29 import org.apache.commons.jelly.impl.Embedded;
30 import org.xml.sax.InputSource;
31
32 /***
33 * Unit case of Embedded
34 *
35 * @author <a href="mailto:vinayc@apache.org">Vinay Chandran</a>
36 */
37 public class TestEmbedded extends TestCase
38 {
39
40 public static void main(String[] args)
41 {
42 TestRunner.run(suite());
43 }
44
45 public static Test suite()
46 {
47 return new TestSuite(TestEmbedded.class);
48 }
49
50 public TestEmbedded(String testName)
51 {
52 super(testName);
53 }
54
55 /***
56 * test Script input as a java.lang.String object
57 */
58 public void testStringAsScript()
59 {
60 Embedded embedded = new Embedded();
61 String jellyScript =
62 "<?xml version=\"1.0\"?>"
63 + " <j:jelly xmlns:j=\"jelly:core\">"
64 + "jelly-test-case"
65 + " </j:jelly>";
66 embedded.setScript(jellyScript);
67 ByteArrayOutputStream baos = new ByteArrayOutputStream();
68 embedded.setOutputStream(baos);
69 boolean status = embedded.execute();
70
71 assertTrue("Emebedded execution failed", status);
72
73 assertEquals("jelly-test-case", new String(baos.toByteArray()));
74
75 embedded.setScript(jellyScript + "obnoxious-part");
76 status = embedded.execute();
77
78 assertFalse("A script with bad XML was executed successfully", status);
79
80 assertNotNull("A script with bad XML didn't generate an error message", embedded.getErrorMsg());
81 }
82
83 /***
84 * test Script input as a InputStream
85 */
86 public void testInputStreamAsScript()
87 {
88 Embedded embedded = new Embedded();
89 String jellyScript =
90 "<?xml version=\"1.0\"?>"
91 + " <j:jelly xmlns:j=\"jelly:core\">"
92 + "jelly-test-case"
93 + " </j:jelly>";
94 embedded.setScript(new ByteArrayInputStream(jellyScript.getBytes()));
95 ByteArrayOutputStream baos = new ByteArrayOutputStream();
96 embedded.setOutputStream(baos);
97 boolean status = embedded.execute();
98
99 assertEquals(status, true);
100
101 assertEquals("jelly-test-case", new String(baos.toByteArray()));
102 }
103
104 /***
105 * Test simple 'raw' execution of a string. See JELLY-189.
106 */
107 public void testRawExecuteAsString() throws Exception
108 {
109 String message =
110 "<?xml version=\"1.0\"?>"
111 + " <j:jelly xmlns:j=\"jelly:core\">"
112 + "jelly-test-case"
113 + " </j:jelly>";
114 ByteArrayOutputStream output = new ByteArrayOutputStream();
115 XMLOutput xmlOutput = XMLOutput.createXMLOutput(output);
116 InputSource script = new InputSource( new StringReader(message.toString()) );
117 JellyContext context = new JellyContext();
118 context.runScript( script, xmlOutput);
119 output.close();
120
121 assertEquals("jelly-test-case", new String(output.toByteArray()));
122 }
123 }