KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jelly > test > xml > TestXMLParserCache


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.test.xml;
17
18 import java.io.StringWriter JavaDoc;
19 import java.net.URL JavaDoc;
20
21 import junit.framework.TestCase;
22 import junit.framework.TestSuite;
23
24 import org.apache.commons.jelly.Jelly;
25 import org.apache.commons.jelly.JellyContext;
26 import org.apache.commons.jelly.JellyException;
27 import org.apache.commons.jelly.Script;
28 import org.apache.commons.jelly.XMLOutput;
29
30 /**
31  * A test to confirm that invalid documents are
32  * reject iff jelly.setValidateXML(true)
33  *
34  * @author Morgan Delagrange
35  * @version $Revision: 155420 $
36  */

37 public class TestXMLParserCache extends TestCase {
38
39     Jelly jelly = null;
40     JellyContext context = null;
41     XMLOutput xmlOutput = null;
42
43     public TestXMLParserCache(String JavaDoc name) {
44         super(name);
45     }
46
47     public static TestSuite suite() throws Exception JavaDoc {
48         return new TestSuite(TestXMLParserCache.class);
49     }
50
51     public void setUp(String JavaDoc scriptName) throws Exception JavaDoc {
52         context = new JellyContext();
53         xmlOutput = XMLOutput.createXMLOutput(new StringWriter JavaDoc());
54
55         jelly = new Jelly();
56
57         String JavaDoc script = scriptName;
58         URL JavaDoc url = this.getClass().getResource(script);
59         if ( url == null ) {
60             throw new Exception JavaDoc(
61                 "Could not find Jelly script: " + script
62                 + " in package of class: " + this.getClass().getName()
63             );
64         }
65         jelly.setUrl(url);
66     }
67
68     public void testParserCache1() throws Exception JavaDoc {
69         // without validation, should
70
// not fail because validation is disabled
71
setUp("invalidScript1.jelly");
72         jelly.setValidateXML(false);
73         Script script = jelly.compileScript();
74         script.run(context,xmlOutput);
75         assertTrue("should have set 'foo' variable to 'bar'",
76                    context.getVariable("foo").equals("bar"));
77
78         // if I enable xml validation, the script should fail
79
// despite the cache
80
jelly.setValidateXML(true);
81         try {
82             script = jelly.compileScript();
83             fail("Invalid scripts should throw JellyException on parse, despite the cache");
84         } catch (JellyException e) {
85         }
86     }
87
88     public void testParserCache2() throws Exception JavaDoc {
89         // no default namespace
90
setUp("nsFilterTest.jelly");
91         Script script = jelly.compileScript();
92         script.run(context,xmlOutput);
93         assertTrue("should have no var when default namspace is not set",
94                    context.getVariable("usedDefaultNamespace") == null);
95
96         // now we have a default namespace, so we
97
// should see a variable, despite the XMLParser cache
98
jelly.setDefaultNamespaceURI("jelly:core");
99         script = jelly.compileScript();
100         script.run(context,xmlOutput);
101         assertTrue("should have var when default namspace is set",
102                    context.getVariable("usedDefaultNamespace").equals("true"));
103     }
104 }
105
Popular Tags