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.tags.xml;
17  
18  import java.io.IOException;
19  import java.io.StringWriter;
20  import java.io.UnsupportedEncodingException;
21  import java.net.URL;
22  
23  import org.apache.commons.jelly.JellyContext;
24  import org.apache.commons.jelly.JellyException;
25  import org.apache.commons.jelly.Script;
26  import org.apache.commons.jelly.XMLOutput;
27  import org.apache.commons.jelly.parser.XMLParser;
28  import org.xml.sax.SAXException;
29  
30  import junit.framework.TestCase;
31  
32  /***
33   * Test that compiled scripts can access resources
34   */
35  public class TestImport extends TestCase {
36      
37      private String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
38          + "<html xmlns=\"http://www.w3.org/TR/xhtml1/strict\" "
39          + "xmlns=\"http://www.w3.org/TR/xhtml1/strict\">"
40          + "<head><title>Expense Report Summary</title></head>"
41          + "<body><p>Total Amount: 12</p></body></html>";
42      public TestImport(String name) {
43          super(name);
44      }
45      
46      public void testImportResources() throws JellyException, UnsupportedEncodingException, IOException {
47          JellyContext context = new JellyContext();
48          URL url = TestImport.class.getResource("/resources/import.jelly");
49          StringWriter writer = new StringWriter();
50          XMLOutput out = XMLOutput.createXMLOutput(writer);
51  //         this works because of the created child context that has knowledge
52  //         of the URL
53          context.runScript(url, out);
54          out.close();
55          assertEquals(expected, writer.toString());
56      }
57  
58      public void testImportResourcesCompiled() throws JellyException, UnsupportedEncodingException, IOException {
59          JellyContext context = new JellyContext();
60          URL url = TestImport.class.getResource("/resources/import.jelly");
61          StringWriter writer = new StringWriter();
62          XMLOutput out = XMLOutput.createXMLOutput(writer);
63          Script script = context.compileScript(url);
64          script.run(context, out);
65          out.close();
66          assertEquals(expected, writer.toString());
67      }
68  
69      public void testImportResourcesFromUncompiledScript() throws JellyException, UnsupportedEncodingException, IOException, SAXException {
70          JellyContext context = new JellyContext();
71          URL url = TestImport.class.getResource("/resources/import.jelly");
72          StringWriter writer = new StringWriter();
73          XMLOutput out = XMLOutput.createXMLOutput(writer);
74          Script script = new XMLParser().parse(url);
75          script.run(context, out);
76          out.close();
77          assertEquals(expected, writer.toString());
78      }
79  
80  }