KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jelly > test > impl > TestEmbedded


1 /*
2  * Copyright 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.jelly.test.impl;
17
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.StringReader JavaDoc;
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 JavaDoc;
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 JavaDoc[] 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 JavaDoc 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 JavaDoc 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 JavaDoc baos = new ByteArrayOutputStream JavaDoc();
68         embedded.setOutputStream(baos);
69         boolean status = embedded.execute();
70         //executed properly without script errors
71
assertTrue("Emebedded execution failed", status);
72         //check that the output confirms the exepected
73
assertEquals("jelly-test-case", new String JavaDoc(baos.toByteArray()));
74         //test generation of error
75
embedded.setScript(jellyScript + "obnoxious-part");
76         status = embedded.execute();
77         //test failure of execution
78
assertFalse("A script with bad XML was executed successfully", status);
79         //Asserting the parser generated a errorMsg
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 JavaDoc 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 JavaDoc(jellyScript.getBytes()));
95         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
96         embedded.setOutputStream(baos);
97         boolean status = embedded.execute();
98         //executed properly without script errors
99
assertEquals(status, true);
100         //check that the output confirms the expected
101
assertEquals("jelly-test-case", new String JavaDoc(baos.toByteArray()));
102     }
103     
104     /**
105      * Test simple 'raw' execution of a string. See JELLY-189.
106      */

107     public void testRawExecuteAsString() throws Exception JavaDoc
108     {
109         String JavaDoc message =
110             "<?xml version=\"1.0\"?>"
111                 + " <j:jelly xmlns:j=\"jelly:core\">"
112                 + "jelly-test-case"
113                 + " </j:jelly>";
114        ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc();
115        XMLOutput xmlOutput = XMLOutput.createXMLOutput(output);
116        InputSource JavaDoc script = new InputSource JavaDoc( new StringReader JavaDoc(message.toString()) );
117        JellyContext context = new JellyContext();
118        context.runScript( script, xmlOutput);
119        output.close();
120        //check that the output confirms the expected
121
assertEquals("jelly-test-case", new String JavaDoc(output.toByteArray()));
122     }
123 }
124
Popular Tags