    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.JellyException;
    27  import org.apache.commons.jelly.Script;
    28  import org.apache.commons.jelly.XMLOutput;
    29
    30  /***
    31   * A test to confirm that invalid documents are
    32   * reject iff jelly.setValidateXML(true)
    33   *
    34   * @author Morgan Delagrange
    35   * @version $Revision: 155420 $
    36   */
    37  public class TestXMLValidation extends TestCase {
    38
    39      Jelly jelly = null;
    40      JellyContext context = null;
    41      XMLOutput xmlOutput = null;
    42
    43      public TestXMLValidation(String name) {
    44          super(name);
    45      }
    46
    47      public static TestSuite suite() throws Exception {
    48          return new TestSuite(TestXMLValidation.class);
    49      }
    50
    51      public void setUp(String scriptName) throws Exception {
    52          context = new JellyContext();
    53          xmlOutput = XMLOutput.createXMLOutput(new StringWriter());
    54
    55          jelly = new Jelly();
    56
    57          String script = scriptName;
    58          URL url = this.getClass().getResource(script);
    59          if ( url == null ) {
    60              throw new Exception(
    61                  "Could not find Jelly script: " + script
    62                  + " in package of class: " + this.getClass().getName()
    63              );
    64          }
    65          jelly.setUrl(url);
    66      }
    67
    68      public void testInvalidXML1NoValidation() throws Exception {
    69          // without validation
    70          setUp("invalidScript1.jelly");
    71          Script script = jelly.compileScript();
    72          script.run(context,xmlOutput);
    73          assertTrue("should have set 'foo' variable to 'bar'",
    74                     context.getVariable("foo").equals("bar"));
    75
    76          // do it again, explicitly setting the validateXML variable
    77          setUp("invalidScript1.jelly");
    78          jelly.setValidateXML(false);
    79          script = jelly.compileScript();
    80          script.run(context,xmlOutput);
    81          assertTrue("should have set 'foo' variable to 'bar'",
    82                     context.getVariable("foo").equals("bar"));
    83      }
    84
    85      public void testInvalidXML1Validation() throws Exception {
    86          // with validation
    87          setUp("invalidScript1.jelly");
    88          jelly.setValidateXML(true);
    89          try {
    90              Script script = jelly.compileScript();
    91              fail("Invalid scripts should throw JellyException on parse");
    92          } catch (JellyException e) {
    93          }
    94      }
    95
    96      public void testValidXML1Validation()throws Exception {
    97          // with validation
    98          setUp("validScript1.jelly");
    99          jelly.setValidateXML(true);
    100         Script script = jelly.compileScript();
    101         script.run(context,xmlOutput);
    102         assertTrue("should have set 'foo' variable to 'bar'",
    103                    context.getVariable("foo").equals("bar"));
    104     }
    105
    106 }