001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jelly.test.xml;
017:
018: import java.io.StringWriter;
019: import java.net.URL;
020:
021: import junit.framework.TestCase;
022: import junit.framework.TestSuite;
023:
024: import org.apache.commons.jelly.Jelly;
025: import org.apache.commons.jelly.JellyContext;
026: import org.apache.commons.jelly.JellyException;
027: import org.apache.commons.jelly.Script;
028: import org.apache.commons.jelly.XMLOutput;
029:
030: /**
031: * A test to confirm that invalid documents are
032: * reject iff jelly.setValidateXML(true)
033: *
034: * @author Morgan Delagrange
035: * @version $Revision: 155420 $
036: */
037: public class TestXMLValidation extends TestCase {
038:
039: Jelly jelly = null;
040: JellyContext context = null;
041: XMLOutput xmlOutput = null;
042:
043: public TestXMLValidation(String name) {
044: super (name);
045: }
046:
047: public static TestSuite suite() throws Exception {
048: return new TestSuite(TestXMLValidation.class);
049: }
050:
051: public void setUp(String scriptName) throws Exception {
052: context = new JellyContext();
053: xmlOutput = XMLOutput.createXMLOutput(new StringWriter());
054:
055: jelly = new Jelly();
056:
057: String script = scriptName;
058: URL url = this .getClass().getResource(script);
059: if (url == null) {
060: throw new Exception("Could not find Jelly script: "
061: + script + " in package of class: "
062: + this .getClass().getName());
063: }
064: jelly.setUrl(url);
065: }
066:
067: public void testInvalidXML1NoValidation() throws Exception {
068: // without validation
069: setUp("invalidScript1.jelly");
070: Script script = jelly.compileScript();
071: script.run(context, xmlOutput);
072: assertTrue("should have set 'foo' variable to 'bar'", context
073: .getVariable("foo").equals("bar"));
074:
075: // do it again, explicitly setting the validateXML variable
076: setUp("invalidScript1.jelly");
077: jelly.setValidateXML(false);
078: script = jelly.compileScript();
079: script.run(context, xmlOutput);
080: assertTrue("should have set 'foo' variable to 'bar'", context
081: .getVariable("foo").equals("bar"));
082: }
083:
084: public void testInvalidXML1Validation() throws Exception {
085: // with validation
086: setUp("invalidScript1.jelly");
087: jelly.setValidateXML(true);
088: try {
089: Script script = jelly.compileScript();
090: fail("Invalid scripts should throw JellyException on parse");
091: } catch (JellyException e) {
092: }
093: }
094:
095: public void testValidXML1Validation() throws Exception {
096: // with validation
097: setUp("validScript1.jelly");
098: jelly.setValidateXML(true);
099: Script script = jelly.compileScript();
100: script.run(context, xmlOutput);
101: assertTrue("should have set 'foo' variable to 'bar'", context
102: .getVariable("foo").equals("bar"));
103: }
104:
105: }
|