KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > xmlrules > DigesterLoaderTest


1 /* $Id: DigesterLoaderTest.java 180258 2005-06-06 08:25:55Z skitching $
2  *
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.commons.digester.xmlrules;
20
21
22 import java.io.InputStream JavaDoc;
23 import java.io.StringReader JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26
27 import junit.framework.TestCase;
28 import junit.framework.TestSuite;
29
30 import org.apache.commons.digester.Address;
31 import org.apache.commons.digester.Digester;
32 import org.apache.commons.digester.TestObjectCreationFactory;
33
34 import org.xml.sax.InputSource JavaDoc;
35
36 /**
37  * Tests loading Digester rules from an XML file.
38  *
39  * @author David H. Martin - Initial Contribution
40  * @author Scott Sanders - Added ASL, removed external dependencies
41  */

42
43 public class DigesterLoaderTest extends TestCase {
44
45     public DigesterLoaderTest(java.lang.String JavaDoc testName) {
46         super(testName);
47     }
48
49     public static void main(java.lang.String JavaDoc[] args) {
50         junit.textui.TestRunner.run(suite());
51     }
52
53     public static junit.framework.Test suite() {
54         TestSuite suite = new TestSuite(DigesterLoaderTest.class);
55
56         return suite;
57     }
58
59     /**
60      * Tests the DigesterLoader.createDigester(), with multiple
61      * included rule sources: testrules.xml includes another rules xml
62      * file, and also includes programmatically created rules.
63      */

64     public void testCreateDigester() throws Exception JavaDoc {
65         URL JavaDoc rules = ClassLoader.getSystemResource("org/apache/commons/digester/xmlrules/testrules.xml");
66         URL JavaDoc input = ClassLoader.getSystemResource("org/apache/commons/digester/xmlrules/test.xml");
67         assertNotNull("The test could not locate testrules.xml", rules);
68         assertNotNull("The test could not locate test.xml", input);
69         Digester digester = DigesterLoader.createDigester(rules);
70         digester.push(new ArrayList JavaDoc());
71         Object JavaDoc root = digester.parse(input.openStream());
72         assertEquals("[foo1 baz1 foo2, foo3 foo4]",root.toString());
73     }
74
75     /**
76      * Tests the DigesterLoader.load(), with multiple included rule
77      * sources: testrules.xml includes another rules xml file, and
78      * also includes programmatically created rules.
79      */

80     public void testLoad1() throws Exception JavaDoc {
81         ClassLoader JavaDoc classLoader = getClass().getClassLoader();
82         URL JavaDoc rules = classLoader.getResource("org/apache/commons/digester/xmlrules/testrules.xml");
83         URL JavaDoc input = classLoader.getResource("org/apache/commons/digester/xmlrules/test.xml");
84         assertNotNull("The test could not locate testrules.xml", rules);
85         assertNotNull("The test could not locate test.xml", input);
86         Object JavaDoc root = DigesterLoader.load(rules, classLoader, input, new ArrayList JavaDoc());
87         if (!(root instanceof ArrayList JavaDoc)) {
88             fail("Unexpected object returned from DigesterLoader. Expected ArrayList; got " + root.getClass().getName());
89         }
90         assertEquals( "[foo1 baz1 foo2, foo3 foo4]",root.toString());
91
92         ArrayList JavaDoc al = (ArrayList JavaDoc)root;
93         Object JavaDoc obj = al.get(0);
94         if (! (obj instanceof TestObject)) {
95             fail("Unexpected object returned from DigesterLoader. Expected TestObject; got " + obj.getClass().getName());
96         }
97         TestObject to = (TestObject)obj;
98         assertEquals(new Long JavaDoc(555),to.getLongValue());
99         assertEquals( "foo", to.getMapValue( "test1" ) );
100         assertEquals( "bar", to.getMapValue( "test2" ) );
101     }
102
103     /**
104      * The same as testLoad1, exception the input file is passed to
105      * DigesterLoader as an InputStream instead of a URL.
106      */

107     public void testLoad2() throws Exception JavaDoc {
108         URL JavaDoc rules = ClassLoader.getSystemResource("org/apache/commons/digester/xmlrules/testrules.xml");
109         InputStream JavaDoc input = ClassLoader.getSystemResource("org/apache/commons/digester/xmlrules/test.xml").openStream();
110         Object JavaDoc root = DigesterLoader.load(rules, getClass().getClassLoader(), input, new ArrayList JavaDoc());
111         if (!(root instanceof ArrayList JavaDoc)) {
112             fail("Unexpected object returned from DigesterLoader. Expected ArrayList; got " + root.getClass().getName());
113         }
114         ArrayList JavaDoc list = (ArrayList JavaDoc) root;
115         assertEquals(root.toString(), "[foo1 baz1 foo2, foo3 foo4]");
116         assertEquals("Wrong number of classes created", 2 , list.size());
117         assertEquals("Pushed first", true , ((TestObject)list.get(0)).isPushed());
118         assertEquals("Didn't push second", false , ((TestObject)list.get(1)).isPushed());
119         assertTrue("Property was set properly", ((TestObject)list.get(0)).getProperty().equals("I am a property!") );
120     }
121
122
123     /**
124      * Validates that circular includes are detected and result in an exception
125      */

126     public void testCircularInclude1() {
127         URL JavaDoc rules = ClassLoader.getSystemResource("org/apache/commons/digester/xmlrules/testCircularRules.xml");
128         try {
129             Digester digester = DigesterLoader.createDigester(rules);
130         } catch (Exception JavaDoc ex) {
131             return;
132         }
133         fail("Creating a digester with circular rules should have thrown CircularIncludeException.");
134     }
135
136
137     /**
138      */

139     public void testSetCustomProperties() throws Exception JavaDoc {
140         URL JavaDoc rules = ClassLoader.getSystemResource
141             ("org/apache/commons/digester/xmlrules/testPropertyAliasRules.xml");
142         InputStream JavaDoc input = ClassLoader.getSystemResource
143             ("org/apache/commons/digester/Test7.xml").openStream();
144             
145         Object JavaDoc obj = DigesterLoader.load(
146                                         rules,
147                                         getClass().getClassLoader(),
148                                         input,
149                                         new ArrayList JavaDoc());
150                                         
151         if (!(obj instanceof ArrayList JavaDoc)) {
152             fail(
153                 "Unexpected object returned from DigesterLoader. Expected ArrayList; got "
154                 + obj.getClass().getName());
155         }
156         
157         ArrayList JavaDoc root = (ArrayList JavaDoc) obj;
158         
159         assertEquals("Wrong array size", 4, root.size());
160         
161         // note that the array is in popped order (rather than pushed)
162

163         obj = root.get(0);
164         assertTrue("(1) Should be an Address ", obj instanceof Address);
165         Address addressOne = (Address) obj;
166         assertEquals("(1) Street attribute", "New Street", addressOne.getStreet());
167         assertEquals("(1) City attribute", "Las Vegas", addressOne.getCity());
168         assertEquals("(1) State attribute", "Nevada", addressOne.getState());
169         
170         obj = root.get(1);
171         assertTrue("(2) Should be an Address ", obj instanceof Address);
172         Address addressTwo = (Address) obj;
173         assertEquals("(2) Street attribute", "Old Street", addressTwo.getStreet());
174         assertEquals("(2) City attribute", "Portland", addressTwo.getCity());
175         assertEquals("(2) State attribute", "Oregon", addressTwo.getState());
176         
177         obj = root.get(2);
178         assertTrue("(3) Should be an Address ", obj instanceof Address);
179         Address addressThree = (Address) obj;
180         assertEquals("(3) Street attribute", "4th Street", addressThree.getStreet());
181         assertEquals("(3) City attribute", "Dayton", addressThree.getCity());
182         assertEquals("(3) State attribute", "US" , addressThree.getState());
183        
184         obj = root.get(3);
185         assertTrue("(4) Should be an Address ", obj instanceof Address);
186         Address addressFour = (Address) obj;
187         assertEquals("(4) Street attribute", "6th Street", addressFour.getStreet());
188         assertEquals("(4) City attribute", "Cleveland", addressFour.getCity());
189         assertEquals("(4) State attribute", "Ohio", addressFour.getState());
190         
191     }
192     
193    public void testFactoryCreateRule() throws Exception JavaDoc {
194         URL JavaDoc rules = ClassLoader.getSystemResource
195             ("org/apache/commons/digester/xmlrules/testfactory.xml");
196             
197         String JavaDoc xml = "<?xml version='1.0' ?><root one='good' two='bad' three='ugly'><foo/></root>";
198         Object JavaDoc obj = DigesterLoader.load(
199                                         rules,
200                                         getClass().getClassLoader(),
201                                         new StringReader JavaDoc(xml),
202                                         new ArrayList JavaDoc());
203                                         
204         if (!(obj instanceof ArrayList JavaDoc)) {
205             fail(
206                 "Unexpected object returned from DigesterLoader. Expected ArrayList; got "
207                 + obj.getClass().getName());
208         }
209         
210         ArrayList JavaDoc list = (ArrayList JavaDoc) obj;
211          
212         assertEquals("List should contain only the factory object", list.size() , 1);
213         TestObjectCreationFactory factory = (TestObjectCreationFactory) list.get(0);
214         assertEquals("Object create not called(1)", factory.called , true);
215         assertEquals(
216                     "Attribute not passed (1)",
217                     factory.attributes.getValue("one"),
218                     "good");
219         assertEquals(
220                     "Attribute not passed (2)",
221                     factory.attributes.getValue("two"),
222                     "bad");
223         assertEquals(
224                     "Attribute not passed (3)",
225                     factory.attributes.getValue("three"),
226                     "ugly");
227
228         
229         rules = ClassLoader.getSystemResource
230             ("org/apache/commons/digester/xmlrules/testfactoryignore.xml");
231             
232         xml = "<?xml version='1.0' ?><root one='good' two='bad' three='ugly'><foo/></root>";
233         try {
234             DigesterLoader.load(
235                                     rules,
236                                     getClass().getClassLoader(),
237                                     new StringReader JavaDoc(xml));
238         } catch (Exception JavaDoc e) {
239             fail("This exception should have been ignored: " + e.getClass().getName());
240         }
241         
242         rules = ClassLoader.getSystemResource
243             ("org/apache/commons/digester/xmlrules/testfactorynoignore.xml");
244             
245         xml = "<?xml version='1.0' ?><root one='good' two='bad' three='ugly'><foo/></root>";
246         try {
247             DigesterLoader.load(
248                                     rules,
249                                     getClass().getClassLoader(),
250                                     new StringReader JavaDoc(xml));
251             fail("Exception should have been propagated from create method.");
252         } catch (Exception JavaDoc e) {
253             /* What we expected */
254             assertEquals(org.xml.sax.SAXParseException JavaDoc.class, e.getClass());
255         }
256     }
257
258     public void testCallParamRule() throws Exception JavaDoc {
259     
260         URL JavaDoc rules = ClassLoader.getSystemResource
261             ("org/apache/commons/digester/xmlrules/test-call-param-rules.xml");
262         
263         String JavaDoc xml = "<?xml version='1.0' ?>"
264                      + "<root><foo attr='long'><bar>short</bar><foobar><ping>tosh</ping></foobar></foo></root>";
265         
266         CallParamTestObject testObject = new CallParamTestObject();
267         
268         DigesterLoader.load(
269                                     rules,
270                                     getClass().getClassLoader(),
271                                     new StringReader JavaDoc(xml),
272                                     testObject);
273                                                                         
274         assertEquals("Incorrect left value", "long", testObject.getLeft());
275         assertEquals("Incorrect middle value", "short", testObject.getMiddle());
276         assertEquals("Incorrect right value", "", testObject.getRight());
277     }
278     
279     public void testInputSourceLoader() throws Exception JavaDoc {
280         String JavaDoc rulesXml = "<?xml version='1.0'?>"
281                 + "<digester-rules>"
282                 + " <pattern value='root'>"
283                 + " <pattern value='foo'>"
284                 + " <call-method-rule methodname='triple' paramcount='3'"
285                 + " paramtypes='java.lang.String,java.lang.String,java.lang.String'/>"
286                 + " <call-param-rule paramnumber='0' attrname='attr'/>"
287                 + " <pattern value='bar'>"
288                 + " <call-param-rule paramnumber='1' from-stack='false'/>"
289                 + " </pattern>"
290                 + " <pattern value='foobar'>"
291                 + " <object-create-rule classname='java.lang.String'/>"
292                 + " <pattern value='ping'>"
293                 + " <call-param-rule paramnumber='2' from-stack='true'/>"
294                 + " </pattern>"
295                 + " </pattern>"
296                 + " </pattern>"
297                 + " </pattern>"
298                 + "</digester-rules>";
299                 
300         String JavaDoc xml = "<?xml version='1.0' ?>"
301                      + "<root><foo attr='long'><bar>short</bar><foobar><ping>tosh</ping></foobar></foo></root>";
302         
303         CallParamTestObject testObject = new CallParamTestObject();
304         
305         Digester digester = DigesterLoader.createDigester(new InputSource JavaDoc(new StringReader JavaDoc(rulesXml)));
306         digester.push(testObject);
307         digester.parse(new StringReader JavaDoc(xml));
308                                                                         
309         assertEquals("Incorrect left value", "long", testObject.getLeft());
310         assertEquals("Incorrect middle value", "short", testObject.getMiddle());
311         assertEquals("Incorrect right value", "", testObject.getRight());
312     }
313 }
314
Popular Tags