KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: DigesterLoaderRulesTest.java 156347 2005-03-06 22:42:30Z 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 package org.apache.commons.digester.xmlrules;
18
19 import java.io.StringReader JavaDoc;
20
21 import junit.framework.TestCase;
22
23 import org.xml.sax.InputSource JavaDoc;
24
25 import org.apache.commons.digester.Digester;
26
27 /**
28  * Tests for digester loader rule implementations,
29  *
30  * @author Robert Burrell Donkin
31  */

32
33 public class DigesterLoaderRulesTest extends TestCase {
34
35     public DigesterLoaderRulesTest(java.lang.String JavaDoc testName) {
36         super(testName);
37     }
38     
39     /** Basic test for object param rule */
40     public void testObjectParamRule() throws Exception JavaDoc {
41         String JavaDoc xmlRules =
42             "<?xml version='1.0'?>" +
43             "<digester-rules>" +
44             " <pattern value='root'>" +
45             " <pattern value='foo'>" +
46             " <call-method-rule " +
47             " methodname='duo' " +
48             " paramcount='2' " +
49             " paramtypes='java.lang.String,java.lang.String'/>" +
50             " <pattern value='bar'>" +
51             " <object-param-rule paramnumber='0' type='java.lang.String' />" +
52             " </pattern>" +
53             " <pattern value='rab'>" +
54             " <object-param-rule paramnumber='1' type='java.lang.String' value='tester.test' />" +
55             " </pattern>" +
56             " </pattern>" +
57             " </pattern>" +
58             "</digester-rules>";
59         
60         String JavaDoc xml =
61             "<?xml version='1.0'?>" +
62             "<root>" +
63             " <foo>" +
64             " <bar/>" +
65             " <rab/>" +
66             " </foo>" +
67             "</root>";
68             
69         CallParamTestObject testObject = new CallParamTestObject();
70         Digester digester = DigesterLoader.createDigester(new InputSource JavaDoc(new StringReader JavaDoc(xmlRules)));
71         digester.push(testObject);
72         digester.parse(new InputSource JavaDoc(new StringReader JavaDoc(xml)));
73         assertEquals("First param", "", testObject.getLeft());
74         assertEquals("Param with default set", "tester.test", testObject.getRight());
75     }
76     
77     /** Test for call method rule with target offset */
78     public void testCallMethodRuleTargetOffset() throws Exception JavaDoc {
79         String JavaDoc xmlRules =
80             "<?xml version='1.0'?>" +
81             "<digester-rules>" +
82             " <pattern value='root'>" +
83             " <pattern value='call-top-object'>" +
84             " <call-method-rule " +
85             " methodname='setMiddle' " +
86             " targetoffset='0' " +
87             " paramcount='1' " +
88             " paramtypes='java.lang.String' />" +
89             " <call-param-rule paramnumber='0' />" +
90             " </pattern>" +
91             " <pattern value='call-parent-object'>" +
92             " <call-method-rule " +
93             " methodname='setLeft' " +
94             " targetoffset='1' " +
95             " paramcount='1' " +
96             " paramtypes='java.lang.String' />" +
97             " <call-param-rule paramnumber='0' />" +
98             " </pattern>" +
99             " <pattern value='call-root-object'>" +
100             " <call-method-rule " +
101             " methodname='setRight' " +
102             " targetoffset='-1' " +
103             " paramcount='1' " +
104             " paramtypes='java.lang.String' />" +
105             " <call-param-rule paramnumber='0' />" +
106             " </pattern>" +
107             " </pattern>" +
108             "</digester-rules>";
109         
110         String JavaDoc xml =
111             "<?xml version='1.0'?>" +
112             "<root>" +
113             " <call-top-object>DataForTheTopObject</call-top-object>" +
114             " <call-parent-object>DataForTheParentObject</call-parent-object>" +
115             " <call-root-object>DataForTheRootObject</call-root-object>" +
116             "</root>";
117             
118         CallParamTestObject testObjectA = new CallParamTestObject();
119         CallParamTestObject testObjectB = new CallParamTestObject();
120         CallParamTestObject testObjectC = new CallParamTestObject();
121         Digester digester = DigesterLoader.createDigester(new InputSource JavaDoc(new StringReader JavaDoc(xmlRules)));
122         digester.push( testObjectA );
123         digester.push( testObjectB );
124         digester.push( testObjectC );
125         digester.parse(new InputSource JavaDoc(new StringReader JavaDoc(xml)));
126         
127         assertEquals("Top object invoked", "DataForTheTopObject", testObjectC.getMiddle());
128         assertEquals("Parent object invoked", "DataForTheParentObject", testObjectB.getLeft());
129         assertEquals("Root object invoked", "DataForTheRootObject", testObjectA.getRight());
130
131     }
132 }
133
Popular Tags