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 TestXMLParserCache extends TestCase {
038:
039: Jelly jelly = null;
040: JellyContext context = null;
041: XMLOutput xmlOutput = null;
042:
043: public TestXMLParserCache(String name) {
044: super (name);
045: }
046:
047: public static TestSuite suite() throws Exception {
048: return new TestSuite(TestXMLParserCache.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 testParserCache1() throws Exception {
068: // without validation, should
069: // not fail because validation is disabled
070: setUp("invalidScript1.jelly");
071: jelly.setValidateXML(false);
072: Script script = jelly.compileScript();
073: script.run(context, xmlOutput);
074: assertTrue("should have set 'foo' variable to 'bar'", context
075: .getVariable("foo").equals("bar"));
076:
077: // if I enable xml validation, the script should fail
078: // despite the cache
079: jelly.setValidateXML(true);
080: try {
081: script = jelly.compileScript();
082: fail("Invalid scripts should throw JellyException on parse, despite the cache");
083: } catch (JellyException e) {
084: }
085: }
086:
087: public void testParserCache2() throws Exception {
088: // no default namespace
089: setUp("nsFilterTest.jelly");
090: Script script = jelly.compileScript();
091: script.run(context, xmlOutput);
092: assertTrue(
093: "should have no var when default namspace is not set",
094: context.getVariable("usedDefaultNamespace") == null);
095:
096: // now we have a default namespace, so we
097: // should see a variable, despite the XMLParser cache
098: jelly.setDefaultNamespaceURI("jelly:core");
099: script = jelly.compileScript();
100: script.run(context, xmlOutput);
101: assertTrue("should have var when default namspace is set",
102: context.getVariable("usedDefaultNamespace").equals(
103: "true"));
104: }
105: }
|