    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: 155420 $
    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          assertTrue("Parsed a Script", script instanceof Script);
    61          String[] args = { "one", "two", "three" };
    62          JellyContext context = new JellyContext();
    63          context.setVariable("args", args);
    64          StringWriter buffer = new StringWriter();
    65          script.run(context, XMLOutput.createXMLOutput(buffer));
    66          String text = buffer.toString().trim();
    67          if (log.isDebugEnabled()) {
    68              log.debug("Evaluated script as...");
    69              log.debug(text);
    70          }
    71          assertEquals("Produces the correct output", "one two three", text);
    72      }
    73
    74      public void testTrimEndWhitespace() throws Exception {
    75          TextScript textScript = new TextScript(" ");
    76          textScript.trimEndWhitespace();
    77          assertEquals("", textScript.getText());
    78
    79          textScript = new TextScript("");
    80          textScript.trimEndWhitespace();
    81          assertEquals("", textScript.getText());
    82
    83          textScript = new TextScript(" foo ");
    84          textScript.trimEndWhitespace();
    85          assertEquals(" foo", textScript.getText());
    86
    87          textScript = new TextScript("foo");
    88          textScript.trimEndWhitespace();
    89          assertEquals("foo", textScript.getText());
    90      }
    91
    92      public void testTrimStartWhitespace() throws Exception {
    93          TextScript textScript = new TextScript(" ");
    94          textScript.trimStartWhitespace();
    95          assertEquals("", textScript.getText());
    96
    97          textScript = new TextScript("");
    98          textScript.trimStartWhitespace();
    99          assertEquals("", textScript.getText());
    100
    101         textScript = new TextScript(" foo ");
    102         textScript.trimStartWhitespace();
    103         assertEquals("foo ", textScript.getText());
    104
    105         textScript = new TextScript("foo");
    106         textScript.trimStartWhitespace();
    107         assertEquals("foo", textScript.getText());
    108     }
    109 }