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;
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          //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(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 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          //executed properly without script errors
99          assertEquals(status, true);
100         //check that the output confirms the expected
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        //check that the output confirms the expected
121        assertEquals("jelly-test-case", new String(output.toByteArray()));
122     }
123 }