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.core;
17  
18  import java.net.URL;
19  
20  import junit.framework.TestCase;
21  import junit.framework.TestSuite;
22  
23  import org.apache.commons.jelly.Jelly;
24  import org.apache.commons.jelly.JellyContext;
25  import org.apache.commons.jelly.Script;
26  import org.apache.commons.jelly.TagLibrary;
27  import org.apache.commons.jelly.XMLOutput;
28  
29  /***
30   * Makes sure that nested includes work correctly
31   *
32   * @author Morgan Delagrange
33   * @version $Revision: 201959 $
34   */
35  public class TestIncludeTag extends TestCase {
36  
37      Jelly jelly = null;
38      JellyContext context = null;
39      XMLOutput xmlOutput = null;
40  
41      public TestIncludeTag(String name) {
42          super(name);
43      }
44  
45      public static TestSuite suite() throws Exception {
46          return new TestSuite(TestIncludeTag.class);
47      }
48  
49      public void setUp(String scriptName) throws Exception {
50          URL url = this.getClass().getResource(scriptName);
51          if ( url == null ) {
52              throw new Exception(
53                  "Could not find Jelly script: " + scriptName
54                  + " in package of class: " + this.getClass().getName()
55              );
56          }
57          setUpFromURL(url);
58      }
59  
60      public void setUpFromURL(URL url) throws Exception {
61          context = new CoreTaglibOnlyContext();
62          xmlOutput = XMLOutput.createDummyXMLOutput();
63  
64          jelly = new Jelly();
65  
66          jelly.setUrl(url);
67  
68          String exturl = url.toExternalForm();
69          int lastSlash = exturl.lastIndexOf("/");
70          String extBase = exturl.substring(0,lastSlash+1);
71          URL baseurl = new URL(extBase);
72          context.setCurrentURL(baseurl);
73      }
74  
75      public void testInnermost() throws Exception {
76          // performs no includes
77          setUp("c.jelly");
78          Script script = jelly.compileScript();
79          script.run(context,xmlOutput);
80          assertTrue("should have set 'c' variable to 'true'",
81                     context.getVariable("c").equals("true"));
82      }
83  
84      public void testMiddle() throws Exception {
85          // performs one include
86          setUp("b.jelly");
87          Script script = jelly.compileScript();
88          script.run(context,xmlOutput);
89          assertTrue("should have set 'c' variable to 'true'",
90                     context.getVariable("c").equals("true"));
91          assertTrue("should have set 'b' variable to 'true'",
92                     context.getVariable("b").equals("true"));
93      }
94  
95      public void testOutermost() throws Exception {
96          // performs one nested include
97          setUp("a.jelly");
98          Script script = jelly.compileScript();
99          script.run(context,xmlOutput);
100         assertTrue("should have set 'c' variable to 'true'",
101                    context.getVariable("c").equals("true"));
102         assertTrue("should have set 'b' variable to 'true'",
103                    context.getVariable("b").equals("true"));
104         assertTrue("should have set 'a' variable to 'true'",
105                    context.getVariable("a").equals("true"));
106     }
107 
108     /***
109      * Insure that includes happen correctly when Jelly scripts
110      * are referenced as a file (rather than as a classpath
111      * element).  Specifically checks to make sure includes succeed
112      * when the initial script is not in the user.dir directory.
113      */
114     public void testFileInclude() throws Exception {
115         // testing outermost
116         setUpFromURL(new URL("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'",
120                    context.getVariable("c").equals("true"));
121         assertTrue("should have set 'b' variable to 'true'",
122                    context.getVariable("b").equals("true"));
123         assertTrue("should have set 'a' variable to 'true'",
124                    context.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             }
143             throw new NoClassDefFoundError("Unexpected tag library uri: " +
144                                                namespaceURI);
145         }
146 
147     }
148 
149 }