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;
17  
18  import java.io.FileInputStream;
19  import java.io.InputStream;
20  import java.io.StringWriter;
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.impl.TextScript;
28  import org.apache.commons.jelly.parser.XMLParser;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  /*** Tests the core tags
33    *
34    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
35    * @version $Revision: 219726 $
36    */
37  public class TestCoreTags extends TestCase {
38  
39      /*** The Log to which logging calls will be made. */
40      private static final Log log = LogFactory.getLog(TestCoreTags.class);
41  
42      public static void main(String[] args) {
43          TestRunner.run(suite());
44      }
45  
46      public static Test suite() {
47          return new TestSuite(TestCoreTags.class);
48      }
49  
50      public TestCoreTags(String testName) {
51          super(testName);
52      }
53  
54      public void testArgs() throws Exception {
55          InputStream in = new FileInputStream("src/test/org/apache/commons/jelly/test_args.jelly");
56          XMLParser parser = new XMLParser();
57          Script script = parser.parse(in);
58          script = script.compile();
59          log.debug("Found: " + script);
60          String[] args = { "one", "two", "three" };
61          JellyContext context = new JellyContext();
62          context.setVariable("args", args);
63          StringWriter buffer = new StringWriter();
64          script.run(context, XMLOutput.createXMLOutput(buffer));
65          String text = buffer.toString().trim();
66          if (log.isDebugEnabled()) {
67              log.debug("Evaluated script as...");
68              log.debug(text);
69          }
70          assertEquals("Produces the correct output", "one two three", text);
71      }
72  
73      public void testTrimEndWhitespace() throws Exception {
74          TextScript textScript = new TextScript(" ");
75          textScript.trimEndWhitespace();
76          assertEquals("", textScript.getText());
77  
78          textScript = new TextScript("");
79          textScript.trimEndWhitespace();
80          assertEquals("", textScript.getText());
81  
82          textScript = new TextScript(" foo ");
83          textScript.trimEndWhitespace();
84          assertEquals(" foo", textScript.getText());
85  
86          textScript = new TextScript("foo");
87          textScript.trimEndWhitespace();
88          assertEquals("foo", textScript.getText());
89      }
90  
91      public void testTrimStartWhitespace() throws Exception {
92          TextScript textScript = new TextScript(" ");
93          textScript.trimStartWhitespace();
94          assertEquals("", textScript.getText());
95  
96          textScript = new TextScript("");
97          textScript.trimStartWhitespace();
98          assertEquals("", textScript.getText());
99  
100         textScript = new TextScript(" foo ");
101         textScript.trimStartWhitespace();
102         assertEquals("foo ", textScript.getText());
103 
104         textScript = new TextScript("foo");
105         textScript.trimStartWhitespace();
106         assertEquals("foo", textScript.getText());
107     }
108 
109 
110     public void testStaticNamespacedAttributes() throws Exception {
111         InputStream in = new FileInputStream("src/test/org/apache/commons/jelly/testStaticNamespacedAttributes.jelly");
112         XMLParser parser = new XMLParser();
113         Script script = parser.parse(in);
114         script = script.compile();
115         log.debug("Found: " + script);
116         JellyContext context = new JellyContext();
117         StringWriter buffer = new StringWriter();
118         script.run(context, XMLOutput.createXMLOutput(buffer));
119         String text = buffer.toString().trim();
120         if (log.isDebugEnabled()) {
121             log.debug("Evaluated script as...");
122             log.debug(text);
123         }
124         assertEquals("Should produces the correct output",
125                 "<blip xmlns:blop=\"blop\" blop:x=\"blip\"></blip>",
126                 text);
127     }
128 }