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  
21  import org.apache.commons.jelly.impl.Embedded;
22  
23  import junit.framework.Test;
24  import junit.framework.TestCase;
25  import junit.framework.TestSuite;
26  import junit.textui.TestRunner;
27  
28  /***
29   *  Unit case  of Embedded
30   *
31   * @author <a href="mailto:vinayc@apache.org">Vinay Chandran</a>
32   */
33  public class TestEmbedded extends TestCase
34  {
35  
36      public static void main(String[] args)
37      {
38          TestRunner.run(suite());
39      }
40  
41      public static Test suite()
42      {
43          return new TestSuite(TestEmbedded.class);
44      }
45  
46      public TestEmbedded(String testName)
47      {
48          super(testName);
49      }
50  
51      /***
52       *  test Script input as a java.lang.String object
53       */
54      public void testStringAsScript()
55      {
56          Embedded embedded = new Embedded();
57          String jellyScript =
58              "<?xml version=\"1.0\"?>"
59                  + " <j:jelly xmlns:j=\"jelly:core\">"
60                  + "jelly-test-case"
61                  + " </j:jelly>";
62          embedded.setScript(jellyScript);
63          ByteArrayOutputStream baos = new ByteArrayOutputStream();
64          embedded.setOutputStream(baos);
65          boolean status = embedded.execute();
66          //executed properly without script errors
67          assertEquals(status, true);
68          //check that the output  confirms the exepected
69          assertEquals("jelly-test-case", new String(baos.toByteArray()));
70          //test generation of error
71          embedded.setScript(jellyScript + "obnoxious-part");
72          status = embedded.execute();
73          //test failure of execution
74          assertEquals(false, status);
75          //Asserting the parser generated a errorMsg
76          assertNotNull(embedded.getErrorMsg());
77      }
78  
79      /***
80       *  test Script input as a InputStream
81       */
82      public void testInputStreamAsScript()
83      {
84          Embedded embedded = new Embedded();
85          String jellyScript =
86              "<?xml version=\"1.0\"?>"
87                  + " <j:jelly xmlns:j=\"jelly:core\">"
88                  + "jelly-test-case"
89                  + " </j:jelly>";
90          embedded.setScript(new ByteArrayInputStream(jellyScript.getBytes()));
91          ByteArrayOutputStream baos = new ByteArrayOutputStream();
92          embedded.setOutputStream(baos);
93          boolean status = embedded.execute();
94          //executed properly without script errors
95          assertEquals(status, true);
96          //check that the output  confirms the exepected
97          assertEquals("jelly-test-case", new String(baos.toByteArray()));
98  
99      }
100 }