    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.xml;
    17
    18  import java.io.StringWriter;
    19  import java.net.URL;
    20
    21  import junit.framework.TestCase;
    22  import junit.framework.TestSuite;
    23
    24  import org.apache.commons.jelly.Jelly;
    25  import org.apache.commons.jelly.JellyContext;
    26  import org.apache.commons.jelly.Script;
    27  import org.apache.commons.jelly.XMLOutput;
    28
    29  /***
    30   * A helper class to run jelly test cases as part of Ant's JUnit tests
    31   *
    32   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
    33   * @version $Revision: 155420 $
    34   */
    35  public class TestDefaultNamespaceFilter extends TestCase {
    36
    37      Jelly jelly = null;
    38      JellyContext context = null;
    39      XMLOutput xmlOutput = null;
    40
    41      public TestDefaultNamespaceFilter(String name) {
    42          super(name);
    43      }
    44
    45      public static TestSuite suite() throws Exception {
    46          return new TestSuite(TestDefaultNamespaceFilter.class);
    47      }
    48
    49      public void setUp() throws Exception {
    50          context = new JellyContext();
    51          xmlOutput = XMLOutput.createXMLOutput(new StringWriter());
    52
    53          jelly = new Jelly();
    54
    55          String script = "nsFilterTest.jelly";
    56          URL url = this.getClass().getResource(script);
    57          if ( url == null ) {
    58              throw new Exception(
    59                  "Could not find Jelly script: " + script
    60                  + " in package of class: " + this.getClass().getName()
    61              );
    62          }
    63          jelly.setUrl(url);
    64      }
    65
    66      public void testNamespaceDefined() throws Exception {
    67          jelly.setDefaultNamespaceURI("jelly:core");
    68          Script script = jelly.compileScript();
    69          script.run(context,xmlOutput);
    70          assertTrue("should have set 'usedDefaultNamespace' variable",
    71                     context.getVariable("usedDefaultNamespace") != null);
    72      }
    73
    74      public void testNamespaceNotDefined() throws Exception {
    75          Script script = jelly.compileScript();
    76          script.run(context,xmlOutput);
    77          assertTrue("should not have set 'usedDefaultNamespace' variable",
    78                     context.getVariable("usedDefaultNamespace") == null);
    79      }
    80  }