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.core;
017:
018: import java.net.URL;
019:
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022:
023: import org.apache.commons.jelly.Jelly;
024: import org.apache.commons.jelly.JellyContext;
025: import org.apache.commons.jelly.Script;
026: import org.apache.commons.jelly.TagLibrary;
027: import org.apache.commons.jelly.XMLOutput;
028:
029: /**
030: * Makes sure that nested includes work correctly
031: *
032: * @author Morgan Delagrange
033: * @version $Revision: 155420 $
034: */
035: public class TestIncludeTag extends TestCase {
036:
037: Jelly jelly = null;
038: JellyContext context = null;
039: XMLOutput xmlOutput = null;
040:
041: public TestIncludeTag(String name) {
042: super (name);
043: }
044:
045: public static TestSuite suite() throws Exception {
046: return new TestSuite(TestIncludeTag.class);
047: }
048:
049: public void setUp(String scriptName) throws Exception {
050: URL url = this .getClass().getResource(scriptName);
051: if (url == null) {
052: throw new Exception("Could not find Jelly script: "
053: + scriptName + " in package of class: "
054: + this .getClass().getName());
055: }
056: setUpFromURL(url);
057: }
058:
059: public void setUpFromURL(URL url) throws Exception {
060: context = new CoreTaglibOnlyContext();
061: xmlOutput = XMLOutput.createDummyXMLOutput();
062:
063: jelly = new Jelly();
064:
065: jelly.setUrl(url);
066:
067: String exturl = url.toExternalForm();
068: int lastSlash = exturl.lastIndexOf("/");
069: String extBase = exturl.substring(0, lastSlash + 1);
070: URL baseurl = new URL(extBase);
071: context.setCurrentURL(baseurl);
072: }
073:
074: public void testInnermost() throws Exception {
075: // performs no includes
076: setUp("c.jelly");
077: Script script = jelly.compileScript();
078: script.run(context, xmlOutput);
079: assertTrue("should have set 'c' variable to 'true'", context
080: .getVariable("c").equals("true"));
081: }
082:
083: public void testMiddle() throws Exception {
084: // performs one include
085: setUp("b.jelly");
086: Script script = jelly.compileScript();
087: script.run(context, xmlOutput);
088: assertTrue("should have set 'c' variable to 'true'", context
089: .getVariable("c").equals("true"));
090: assertTrue("should have set 'b' variable to 'true'", context
091: .getVariable("b").equals("true"));
092: }
093:
094: public void testOutermost() throws Exception {
095: // performs one nested include
096: setUp("a.jelly");
097: Script script = jelly.compileScript();
098: script.run(context, xmlOutput);
099: assertTrue("should have set 'c' variable to 'true'", context
100: .getVariable("c").equals("true"));
101: assertTrue("should have set 'b' variable to 'true'", context
102: .getVariable("b").equals("true"));
103: assertTrue("should have set 'a' variable to 'true'", context
104: .getVariable("a").equals("true"));
105: }
106:
107: /**
108: * Insure that includes happen correctly when Jelly scripts
109: * are referenced as a file (rather than as a classpath
110: * element). Specifically checks to make sure includes succeed
111: * when the initial script is not in the user.dir directory.
112: */
113: public void testFileInclude() throws Exception {
114: // testing outermost
115: setUpFromURL(new URL(
116: "file:src/test/org/apache/commons/jelly/core/a.jelly"));
117: Script script = jelly.compileScript();
118: script.run(context, xmlOutput);
119: assertTrue("should have set 'c' variable to 'true'", context
120: .getVariable("c").equals("true"));
121: assertTrue("should have set 'b' variable to 'true'", context
122: .getVariable("b").equals("true"));
123: assertTrue("should have set 'a' variable to 'true'", context
124: .getVariable("a").equals("true"));
125: }
126:
127: private class CoreTaglibOnlyContext extends JellyContext {
128:
129: /**
130: * This implementations makes sure that only "jelly:core"
131: * taglib is instantiated, insuring that "optional" dependencies
132: * are not inadvertantly called. Specifically addresses a bug
133: * in older Jelly dev versions where a nested include
134: * would trigger instantiation of all tag libraries.
135: *
136: * @param namespaceURI
137: * @return
138: */
139: public TagLibrary getTagLibrary(String namespaceURI) {
140: if (namespaceURI.equals("jelly:core")) {
141: return super .getTagLibrary(namespaceURI);
142: } else {
143: throw new NoClassDefFoundError(
144: "Unexpected tag library uri: " + namespaceURI);
145: }
146: }
147:
148: }
149:
150: }
|