KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > CallMethodRuleTestCase


1 /* $Id: CallMethodRuleTestCase.java 155412 2005-02-26 12:58:36Z dirkv $
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;
20
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.StringReader JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import junit.framework.Test;
30 import junit.framework.TestCase;
31 import junit.framework.TestSuite;
32
33 import org.xml.sax.SAXException JavaDoc;
34
35 //import org.apache.commons.logging.impl.SimpleLog;
36

37 /**
38  * <p>Tests for the <code>CallMethodRule</code> and associated
39  * <code>CallParamRule</code>.
40  *
41  * @author Christopher Lenz
42  */

43 public class CallMethodRuleTestCase extends TestCase {
44
45
46     // ----------------------------------------------------- Instance Variables
47

48
49     /**
50      * The digester instance we will be processing.
51      */

52     protected Digester digester = null;
53
54
55     // ----------------------------------------------------------- Constructors
56

57
58     /**
59      * Construct a new instance of this test case.
60      *
61      * @param name Name of the test case
62      */

63     public CallMethodRuleTestCase(String JavaDoc name) {
64
65         super(name);
66
67     }
68
69
70     // --------------------------------------------------- Overall Test Methods
71

72
73     /**
74      * Set up instance variables required by this test case.
75      */

76     public void setUp() {
77
78         digester = new Digester();
79
80     }
81
82
83     /**
84      * Return the tests included in this test suite.
85      */

86     public static Test suite() {
87
88         return (new TestSuite(CallMethodRuleTestCase.class));
89
90     }
91
92
93     /**
94      * Tear down instance variables required by this test case.
95      */

96     public void tearDown() {
97
98         digester = null;
99
100     }
101
102
103
104     // ------------------------------------------------ Individual Test Methods
105

106
107     /**
108      * Test method calls with the CallMethodRule rule. It should be possible
109      * to call a method with no arguments using several rule syntaxes.
110      */

111     public void testBasic() throws SAXException JavaDoc, IOException JavaDoc {
112         
113         // Configure the digester as required
114
digester.addObjectCreate("employee", Employee.class);
115         // try all syntax permutations
116
digester.addCallMethod("employee", "toString", 0, (Class JavaDoc[])null);
117         digester.addCallMethod("employee", "toString", 0, (String JavaDoc[])null);
118         digester.addCallMethod("employee", "toString", 0, new Class JavaDoc[] {});
119         digester.addCallMethod("employee", "toString", 0, new String JavaDoc[] {});
120         digester.addCallMethod("employee", "toString");
121
122         // Parse our test input
123
Object JavaDoc root1 = null;
124         // an exception will be thrown if the method can't be found
125
root1 = digester.parse(getInputStream("Test5.xml"));
126
127     }
128
129
130     /**
131      * Test method calls with the CallMethodRule reading from the element
132      * body, with no CallParamMethod rules added.
133      */

134     public void testCallMethodOnly() throws Exception JavaDoc {
135
136         // Configure the digester as required
137
digester.addObjectCreate("employee", Employee.class);
138         digester.addCallMethod("employee/firstName", "setFirstName", 0);
139         digester.addCallMethod("employee/lastName", "setLastName", 0);
140
141         // Parse our test input
142
Employee employee = (Employee)
143             digester.parse(getInputStream("Test9.xml"));
144         assertNotNull("parsed an employee", employee);
145
146         // Validate that the property setters were called
147
assertEquals("Set first name", "First Name", employee.getFirstName());
148         assertEquals("Set last name", "Last Name", employee.getLastName());
149     }
150
151
152     /**
153      * Test CallMethodRule variants which specify the classes of the
154      * parameters to target methods. String, int, boolean, float should all
155      * be acceptable as parameter types.
156      */

157     public void testSettingProperties() throws SAXException JavaDoc, IOException JavaDoc {
158             
159         // Configure the digester as required
160
digester.addObjectCreate("employee", Employee.class);
161         // try all syntax permutations
162
digester.addCallMethod("employee", "setLastName", 1,
163                                 new String JavaDoc[] {"java.lang.String"});
164         digester.addCallParam("employee/lastName", 0);
165                 
166         // Parse our test input
167
Object JavaDoc root1 = null;
168         
169         // an exception will be thrown if the method can't be found
170
root1 = digester.parse(getInputStream("Test5.xml"));
171         Employee employee = (Employee) root1;
172         assertEquals("Failed to call Employee.setLastName",
173                     "Last Name", employee.getLastName());
174         
175
176         digester = new Digester();
177         // Configure the digester as required
178
digester.addObjectCreate("employee", Employee.class);
179         // try out primitive convertion
180
digester.addCallMethod("employee", "setAge", 1,
181                                 new Class JavaDoc[] {int.class});
182         digester.addCallParam("employee/age", 0);
183                 
184         // Parse our test input
185
root1 = null;
186         
187         // an exception will be thrown if the method can't be found
188
root1 = digester.parse(getInputStream("Test5.xml"));
189         employee = (Employee) root1;
190         assertEquals("Failed to call Employee.setAge", 21, employee.getAge());
191         
192         digester = new Digester();
193         // Configure the digester as required
194
digester.addObjectCreate("employee", Employee.class);
195         digester.addCallMethod("employee", "setActive", 1,
196                                 new Class JavaDoc[] {boolean.class});
197         digester.addCallParam("employee/active", 0);
198                 
199         // Parse our test input
200
root1 = null;
201
202         // an exception will be thrown if the method can't be found
203
root1 = digester.parse(getInputStream("Test5.xml"));
204         employee = (Employee) root1;
205         assertEquals("Failed to call Employee.setActive",
206                         true, employee.isActive());
207         
208         digester = new Digester();
209         // Configure the digester as required
210
digester.addObjectCreate("employee", Employee.class);
211         digester.addCallMethod("employee", "setSalary", 1,
212                                 new Class JavaDoc[] {float.class});
213         digester.addCallParam("employee/salary", 0);
214                 
215         // Parse our test input
216
root1 = null;
217         // an exception will be thrown if the method can't be found
218
root1 = digester.parse(getInputStream("Test5.xml"));
219         employee = (Employee) root1;
220         assertEquals("Failed to call Employee.setSalary",
221                         1000000.0f, employee.getSalary(), 0.1f);
222     }
223
224
225     /**
226      * This tests the call methods params enhancement that provides
227      * for more complex stack-based calls.
228      */

229     public void testParamsFromStack() throws SAXException JavaDoc, IOException JavaDoc {
230
231         StringBuffer JavaDoc xml = new StringBuffer JavaDoc().
232             append("<?xml version='1.0'?>").
233             append("<map>").
234             append(" <key name='The key'/>").
235             append(" <value name='The value'/>").
236             append("</map>");
237
238         digester.addObjectCreate("map", HashMap JavaDoc.class);
239         digester.addCallMethod("map", "put", 2);
240         digester.addObjectCreate("map/key", AlphaBean.class);
241         digester.addSetProperties("map/key");
242         digester.addCallParam("map/key", 0, true);
243         digester.addObjectCreate("map/value", BetaBean.class);
244         digester.addSetProperties("map/value");
245         digester.addCallParam("map/value", 1, true);
246
247         Map JavaDoc map = (Map JavaDoc) digester.parse(new StringReader JavaDoc(xml.toString()));
248
249         assertNotNull(map);
250         assertEquals(1, map.size());
251         assertEquals("The key",
252                      ((AlphaBean)map.keySet().iterator().next()).getName());
253         assertEquals("The value",
254                      ((BetaBean)map.values().iterator().next()).getName());
255     }
256
257
258     /**
259      * Test that the target object for a CallMethodRule is the object that was
260      * on top of the object stack when the CallMethodRule fired, even when other
261      * rules fire between the CallMethodRule and its associated CallParamRules.
262      * <p>
263      * The current implementation of CallMethodRule ensures this works by
264      * firing only at the end of the tag that CallMethodRule triggered on.
265      */

266     public void testOrderNestedPartA() throws Exception JavaDoc {
267
268         // Configure the digester as required
269

270         // Here, we use the "grandchild element name" as a parameter to
271
// the created element, to ensure that all the params aren't
272
// avaiable to the CallMethodRule until some other rules have fired,
273
// in particular an ObjectCreateRule. The CallMethodRule should still
274
// function correctly in this scenario.
275
digester.addObjectCreate("toplevel/element", NamedBean.class);
276         digester.addCallMethod("toplevel/element", "setName", 1);
277         digester.addCallParam("toplevel/element/element/element", 0, "name");
278         
279         digester.addObjectCreate("toplevel/element/element", NamedBean.class);
280
281         // Parse our test input
282
NamedBean root1 = null;
283         try {
284             // an exception will be thrown if the method can't be found
285
root1 = (NamedBean) digester.parse(getInputStream("Test8.xml"));
286             
287         } catch (Throwable JavaDoc t) {
288             // this means that the method can't be found and so the test fails
289
fail("Digester threw Exception: " + t);
290         }
291         
292         // if the CallMethodRule were to incorrectly invoke the method call
293
// on the second-created NamedBean instance, then the root one would
294
// have a null name. If it works correctly, the target element will
295
// be the first-created (root) one, despite the fact that a second
296
// object instance was created between the firing of the
297
// CallMethodRule and its associated CallParamRule.
298
assertEquals("Wrong method call order", "C", root1.getName());
299     }
300
301     /**
302      * Test nested CallMethod rules.
303      * <p>
304      * The current implementation of CallMethodRule, in which the method is
305      * invoked in its end() method, causes behaviour which some users find
306      * non-intuitive. In this test it can be seen to "reverse" the order of
307      * data processed. However this is the way CallMethodRule has always
308      * behaved, and it is expected that apps out there rely on this call order
309      * so this test is present to ensure that no-one changes this behaviour.
310      */

311     public void testOrderNestedPartB() throws Exception JavaDoc {
312         
313         // Configure the digester as required
314
StringBuffer JavaDoc word = new StringBuffer JavaDoc();
315         digester.push(word);
316         digester.addCallMethod("*/element", "append", 1);
317         digester.addCallParam("*/element", 0, "name");
318         
319         // Parse our test input
320
Object JavaDoc root1 = null;
321         try {
322             // an exception will be thrown if the method can't be found
323
root1 = digester.parse(getInputStream("Test8.xml"));
324             
325         } catch (Throwable JavaDoc t) {
326             // this means that the method can't be found and so the test fails
327
fail("Digester threw Exception: " + t);
328         }
329         
330         assertEquals("Wrong method call order", "CBA", word.toString());
331     }
332
333     public void testPrimitiveReading() throws Exception JavaDoc {
334         StringReader JavaDoc reader = new StringReader JavaDoc(
335             "<?xml version='1.0' ?><root><bean good='true'/><bean good='false'/><bean/>"
336             + "<beanie bad='Fee Fie Foe Fum' good='true'/><beanie bad='Fee Fie Foe Fum' good='false'/>"
337             + "<beanie bad='Fee Fie Foe Fum'/></root>");
338             
339         Digester digester = new Digester();
340         
341         //SimpleLog log = new SimpleLog("[testPrimitiveReading:Digester]");
342
//log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
343
//digester.setLogger(log);
344

345         digester.addObjectCreate("root/bean", PrimitiveBean.class);
346         digester.addSetNext("root/bean", "add");
347         Class JavaDoc [] params = { Boolean.TYPE };
348         digester.addCallMethod("root/bean", "setBoolean", 1, params);
349         digester.addCallParam("root/bean", 0, "good");
350         
351         digester.addObjectCreate("root/beanie", PrimitiveBean.class);
352         digester.addSetNext("root/beanie", "add");
353         Class JavaDoc [] beanieParams = { String JavaDoc.class, Boolean.TYPE };
354         digester.addCallMethod("root/beanie", "testSetBoolean", 2, beanieParams);
355         digester.addCallParam("root/beanie", 0, "bad");
356         digester.addCallParam("root/beanie", 1, "good");
357         
358         ArrayList JavaDoc list = new ArrayList JavaDoc();
359         digester.push(list);
360         digester.parse(reader);
361         
362         assertEquals("Wrong number of beans in list", 6, list.size());
363         PrimitiveBean bean = (PrimitiveBean) list.get(0);
364         assertTrue("Bean 0 property not called", bean.getSetBooleanCalled());
365         assertEquals("Bean 0 property incorrect", true, bean.getBoolean());
366         bean = (PrimitiveBean) list.get(1);
367         assertTrue("Bean 1 property not called", bean.getSetBooleanCalled());
368         assertEquals("Bean 1 property incorrect", false, bean.getBoolean());
369         bean = (PrimitiveBean) list.get(2);
370         // no attibute, no call is what's expected
371
assertTrue("Bean 2 property called", !bean.getSetBooleanCalled());
372         bean = (PrimitiveBean) list.get(3);
373         assertTrue("Bean 3 property not called", bean.getSetBooleanCalled());
374         assertEquals("Bean 3 property incorrect", true, bean.getBoolean());
375         bean = (PrimitiveBean) list.get(4);
376         assertTrue("Bean 4 property not called", bean.getSetBooleanCalled());
377         assertEquals("Bean 4 property incorrect", false, bean.getBoolean());
378         bean = (PrimitiveBean) list.get(5);
379         assertTrue("Bean 5 property not called", bean.getSetBooleanCalled());
380         assertEquals("Bean 5 property incorrect", false, bean.getBoolean());
381     }
382     
383     public void testFromStack() throws Exception JavaDoc {
384     
385         StringReader JavaDoc reader = new StringReader JavaDoc(
386             "<?xml version='1.0' ?><root><one/><two/><three/><four/><five/></root>");
387             
388         Digester digester = new Digester();
389         
390         Class JavaDoc [] params = { String JavaDoc.class };
391         
392         digester.addObjectCreate("root/one", NamedBean.class);
393         digester.addSetNext("root/one", "add");
394         digester.addCallMethod("root/one", "setName", 1, params);
395         digester.addCallParam("root/one", 0, 2);
396         
397         digester.addObjectCreate("root/two", NamedBean.class);
398         digester.addSetNext("root/two", "add");
399         digester.addCallMethod("root/two", "setName", 1, params);
400         digester.addCallParam("root/two", 0, 3);
401         
402         digester.addObjectCreate("root/three", NamedBean.class);
403         digester.addSetNext("root/three", "add");
404         digester.addCallMethod("root/three", "setName", 1, params);
405         digester.addCallParam("root/three", 0, 4);
406         
407         digester.addObjectCreate("root/four", NamedBean.class);
408         digester.addSetNext("root/four", "add");
409         digester.addCallMethod("root/four", "setName", 1, params);
410         digester.addCallParam("root/four", 0, 5);
411         
412         digester.addObjectCreate("root/five", NamedBean.class);
413         digester.addSetNext("root/five", "add");
414         Class JavaDoc [] newParams = { String JavaDoc.class, String JavaDoc.class };
415         digester.addCallMethod("root/five", "test", 2, newParams);
416         digester.addCallParam("root/five", 0, 10);
417         digester.addCallParam("root/five", 1, 3);
418         
419         // prepare stack
420
digester.push("That lamb was sure to go.");
421         digester.push("And everywhere that Mary went,");
422         digester.push("It's fleece was white as snow.");
423         digester.push("Mary had a little lamb,");
424         
425         ArrayList JavaDoc list = new ArrayList JavaDoc();
426         digester.push(list);
427         digester.parse(reader);
428         
429         assertEquals("Wrong number of beans in list", 5, list.size());
430         NamedBean bean = (NamedBean) list.get(0);
431         assertEquals("Parameter not set from stack (1)", "Mary had a little lamb,", bean.getName());
432         bean = (NamedBean) list.get(1);
433         assertEquals("Parameter not set from stack (2)", "It's fleece was white as snow.", bean.getName());
434         bean = (NamedBean) list.get(2);
435         assertEquals("Parameter not set from stack (3)", "And everywhere that Mary went,", bean.getName());
436         bean = (NamedBean) list.get(3);
437         assertEquals("Parameter not set from stack (4)", "That lamb was sure to go.", bean.getName());
438         bean = (NamedBean) list.get(4);
439         assertEquals("Out of stack not set to null", null , bean.getName());
440     }
441     
442     public void testTwoCalls() throws Exception JavaDoc {
443         
444     
445         StringReader JavaDoc reader = new StringReader JavaDoc(
446             "<?xml version='1.0' ?><root>"
447             + "<param class='int' coolness='true'>25</param>"
448             + "<param class='long'>50</param>"
449             + "<param class='float' coolness='false'>90</param></root>");
450             
451         Digester digester = new Digester();
452         //SimpleLog log = new SimpleLog("{testTwoCalls:Digester]");
453
//log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
454
//digester.setLogger(log);
455

456         digester.addObjectCreate( "root/param", ParamBean.class );
457         digester.addSetNext( "root/param", "add" );
458         digester.addCallMethod( "root/param", "setThisAndThat", 2 );
459         digester.addCallParam( "root/param", 0, "class" );
460         digester.addCallParam( "root/param", 1 );
461         digester.addCallMethod( "root/param", "setCool", 1, new Class JavaDoc[] {boolean.class } );
462         digester.addCallParam( "root/param", 0, "coolness" );
463         
464         ArrayList JavaDoc list = new ArrayList JavaDoc();
465         digester.push(list);
466         digester.parse(reader);
467     
468         assertEquals("Wrong number of objects created", 3, list.size());
469         ParamBean bean = (ParamBean) list.get(0);
470         assertEquals("Coolness wrong (1)", true, bean.isCool());
471         assertEquals("This wrong (1)", "int", bean.getThis());
472         assertEquals("That wrong (1)", "25", bean.getThat());
473         bean = (ParamBean) list.get(1);
474         assertEquals("Coolness wrong (2)", false, bean.isCool());
475         assertEquals("This wrong (2)", "long", bean.getThis());
476         assertEquals("That wrong (2)", "50", bean.getThat());
477         bean = (ParamBean) list.get(2);
478         assertEquals("Coolness wrong (3)", false, bean.isCool());
479         assertEquals("This wrong (3)", "float", bean.getThis());
480         assertEquals("That wrong (3)", "90", bean.getThat());
481     }
482
483     public void testNestedBody() throws Exception JavaDoc {
484         
485         StringReader JavaDoc reader = new StringReader JavaDoc(
486             "<?xml version='1.0' ?><root>"
487             + "<spam>Simple</spam>"
488             + "<spam>Complex<spam>Deep<spam>Deeper<spam>Deepest</spam></spam></spam></spam>"
489             + "</root>");
490             
491         Digester digester = new Digester();
492
493         //SimpleLog log = new SimpleLog("[testPrimitiveReading:Digester]");
494
//log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
495
//digester.setLogger(log);
496

497         
498         digester.addObjectCreate("root/spam", NamedBean.class);
499         digester.addSetRoot("root/spam", "add");
500         digester.addCallMethod( "root/spam", "setName", 1 );
501         digester.addCallParam( "root/spam", 0);
502         
503         digester.addObjectCreate("root/spam/spam", NamedBean.class);
504         digester.addSetRoot("root/spam/spam", "add");
505         digester.addCallMethod( "root/spam/spam", "setName", 1 );
506         digester.addCallParam( "root/spam/spam", 0);
507         
508         digester.addObjectCreate("root/spam/spam/spam", NamedBean.class);
509         digester.addSetRoot("root/spam/spam/spam", "add");
510         digester.addCallMethod( "root/spam/spam/spam", "setName", 1 );
511         digester.addCallParam( "root/spam/spam/spam", 0);
512
513         
514         digester.addObjectCreate("root/spam/spam/spam/spam", NamedBean.class);
515         digester.addSetRoot("root/spam/spam/spam/spam", "add");
516         digester.addCallMethod( "root/spam/spam/spam/spam", "setName", 1 );
517         digester.addCallParam( "root/spam/spam/spam/spam", 0);
518         
519         ArrayList JavaDoc list = new ArrayList JavaDoc();
520         digester.push(list);
521         digester.parse(reader);
522         
523         NamedBean bean = (NamedBean) list.get(0);
524         assertEquals("Wrong name (1)", "Simple", bean.getName());
525         // these are added in deepest first order by the addRootRule
526
bean = (NamedBean) list.get(4);
527         assertEquals("Wrong name (2)", "Complex", bean.getName());
528         bean = (NamedBean) list.get(3);
529         assertEquals("Wrong name (3)", "Deep", bean.getName());
530         bean = (NamedBean) list.get(2);
531         assertEquals("Wrong name (4)", "Deeper", bean.getName());
532         bean = (NamedBean) list.get(1);
533         assertEquals("Wrong name (5)", "Deepest", bean.getName());
534     }
535     
536     public void testProcessingHook() throws Exception JavaDoc {
537         
538         class TestCallMethodRule extends CallMethodRule {
539             Object JavaDoc result;
540             TestCallMethodRule(String JavaDoc methodName, int paramCount)
541             {
542                 super(methodName, paramCount);
543             }
544             protected void processMethodCallResult(Object JavaDoc result) {
545                 this.result = result;
546             }
547         }
548     
549         StringReader JavaDoc reader = new StringReader JavaDoc(
550             "<?xml version='1.0' ?><root>"
551             + "<param class='float' coolness='false'>90</param></root>");
552         
553             
554         Digester digester = new Digester();
555         //SimpleLog log = new SimpleLog("{testTwoCalls:Digester]");
556
//log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
557
//digester.setLogger(log);
558

559         digester.addObjectCreate( "root/param", ParamBean.class );
560         digester.addSetNext( "root/param", "add" );
561         TestCallMethodRule rule = new TestCallMethodRule( "setThisAndThat" , 2 );
562         digester.addRule( "root/param", rule );
563         digester.addCallParam( "root/param", 0, "class" );
564         digester.addCallParam( "root/param", 1, "coolness" );
565         
566         ArrayList JavaDoc list = new ArrayList JavaDoc();
567         digester.push(list);
568         digester.parse(reader);
569     
570         assertEquals("Wrong number of objects created", 1, list.size());
571         assertEquals("Result not passed into hook", "The Other", rule.result);
572     }
573
574     /** Test for the PathCallParamRule */
575     public void testPathCallParam() throws Exception JavaDoc {
576         String JavaDoc xml = "<?xml version='1.0'?><main>"
577             + "<alpha><beta>Ignore this</beta></alpha>"
578             + "<beta><epsilon><gamma>Ignore that</gamma></epsilon></beta>"
579             + "</main>";
580     
581         SimpleTestBean bean = new SimpleTestBean();
582         bean.setAlphaBeta("[UNSET]", "[UNSET]");
583         
584         StringReader JavaDoc in = new StringReader JavaDoc(xml);
585         Digester digester = new Digester();
586         digester.setRules(new ExtendedBaseRules());
587         digester.addCallParamPath("*/alpha/?", 0);
588         digester.addCallParamPath("*/epsilon/?", 1);
589         digester.addCallMethod("main", "setAlphaBeta", 2);
590         
591         digester.push(bean);
592         
593         digester.parse(in);
594         
595         assertEquals("Test alpha property setting", "main/alpha/beta" , bean.getAlpha());
596         assertEquals("Test beta property setting", "main/beta/epsilon/gamma" , bean.getBeta());
597     }
598
599
600     /**
601      * Test invoking an object which does not exist on the stack.
602      */

603     public void testCallInvalidTarget() throws Exception JavaDoc {
604     
605         Digester digester = new Digester();
606         digester.addObjectCreate("employee", HashMap JavaDoc.class);
607
608         // there should be only one object on the stack (index zero),
609
// so selecting a target object with index 1 on the object stack
610
// should result in an exception.
611
CallMethodRule r = new CallMethodRule(1, "put", 0);
612         digester.addRule("employee", r);
613         
614         try {
615             digester.parse(getInputStream("Test5.xml"));
616             fail("Exception should be thrown for invalid target offset");
617         }
618         catch(SAXException JavaDoc e) {
619             // ok, exception expected
620
}
621     }
622
623     /**
624      * Test invoking an object which is at top-1 on the stack, like
625      * SetNextRule does...
626      */

627     public void testCallNext() throws Exception JavaDoc {
628     
629         Digester digester = new Digester();
630         digester.addObjectCreate("employee", HashMap JavaDoc.class);
631
632         digester.addObjectCreate("employee/address", Address.class);
633         digester.addSetNestedProperties("employee/address");
634         CallMethodRule r = new CallMethodRule(1, "put", 2);
635         digester.addRule("employee/address", r);
636         digester.addCallParam("employee/address/type", 0);
637         digester.addCallParam("employee/address", 1, 0);
638         
639         HashMap JavaDoc map = (HashMap JavaDoc) digester.parse(getInputStream("Test5.xml"));
640         
641         assertNotNull(map);
642         java.util.Set JavaDoc keys = map.keySet();
643         assertEquals(2, keys.size());
644         Address home = (Address) map.get("home");
645         assertNotNull(home);
646         assertEquals("HmZip", home.getZipCode());
647         Address office = (Address) map.get("office");
648         assertNotNull(office);
649         assertEquals("OfZip", office.getZipCode());
650     }
651
652     /**
653      * Test invoking an object which is at the root of the stack, like
654      * SetRoot does...
655      */

656     public void testCallRoot() throws Exception JavaDoc {
657     
658         Digester digester = new Digester();
659         digester.addObjectCreate("employee", HashMap JavaDoc.class);
660
661         digester.addObjectCreate("employee/address", Address.class);
662         digester.addSetNestedProperties("employee/address");
663         CallMethodRule r = new CallMethodRule(-1, "put", 2);
664         digester.addRule("employee/address", r);
665         digester.addCallParam("employee/address/type", 0);
666         digester.addCallParam("employee/address", 1, 0);
667         
668         HashMap JavaDoc map = (HashMap JavaDoc) digester.parse(getInputStream("Test5.xml"));
669         
670         assertNotNull(map);
671         java.util.Set JavaDoc keys = map.keySet();
672         assertEquals(2, keys.size());
673         Address home = (Address) map.get("home");
674         assertNotNull(home);
675         assertEquals("HmZip", home.getZipCode());
676         Address office = (Address) map.get("office");
677         assertNotNull(office);
678         assertEquals("OfZip", office.getZipCode());
679     }
680
681     // ------------------------------------------------ Utility Support Methods
682

683
684     /**
685      * Return an appropriate InputStream for the specified test file (which
686      * must be inside our current package.
687      *
688      * @param name Name of the test file we want
689      *
690      * @exception IOException if an input/output error occurs
691      */

692     protected InputStream JavaDoc getInputStream(String JavaDoc name) throws IOException JavaDoc {
693
694         return (this.getClass().getResourceAsStream
695                 ("/org/apache/commons/digester/" + name));
696
697     }
698
699
700 }
701
702
Popular Tags