KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: RuleTestCase.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.util.ArrayList JavaDoc;
25
26 import junit.framework.Test;
27 import junit.framework.TestCase;
28 import junit.framework.TestSuite;
29
30 import org.xml.sax.SAXException JavaDoc;
31
32 /**
33  * <p>Test Case for the Digester class. These tests perform parsing of
34  * XML documents to exercise the built-in rules.</p>
35  *
36  * @author Craig R. McClanahan
37  * @author Janek Bogucki
38  * @version $Revision$ $Date: 2005-02-26 04:58:36 -0800 (Sat, 26 Feb 2005) $
39  */

40
41 public class RuleTestCase extends TestCase {
42
43
44     // ----------------------------------------------------- Instance Variables
45

46
47     /**
48      * The digester instance we will be processing.
49      */

50     protected Digester digester = null;
51
52
53     // ----------------------------------------------------------- Constructors
54

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

61     public RuleTestCase(String JavaDoc name) {
62
63         super(name);
64
65     }
66
67
68     // --------------------------------------------------- Overall Test Methods
69

70
71     /**
72      * Set up instance variables required by this test case.
73      */

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

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

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

104
105     /**
106      * Test object creation (and associated property setting) with nothing on
107      * the stack, which should cause an appropriate Employee object to be
108      * returned.
109      */

110     public void testObjectCreate1() throws SAXException JavaDoc, IOException JavaDoc {
111
112         // Configure the digester as required
113
digester.addObjectCreate("employee",
114                 "org.apache.commons.digester.Employee");
115         digester.addSetProperties("employee");
116
117         // Parse our test input.
118
Object JavaDoc root = null;
119         root = digester.parse(getInputStream("Test1.xml"));
120
121         assertNotNull("Digester returned an object", root);
122         assertTrue("Digester returned an Employee",
123                 root instanceof Employee);
124         Employee employee = (Employee) root;
125         assertEquals("First name is correct",
126                 "First Name",
127                 employee.getFirstName());
128         assertEquals("Last name is correct",
129                 "Last Name",
130                 employee.getLastName());
131
132
133     }
134
135
136     /**
137      * Test object creation (and associated property setting) with nothing on
138      * the stack, which should cause an appropriate Employee object to be
139      * returned. The processing rules will process the nested Address elements
140      * as well, but will not attempt to add them to the Employee.
141      */

142     public void testObjectCreate2() throws SAXException JavaDoc, IOException JavaDoc {
143
144         // Configure the digester as required
145
digester.addObjectCreate("employee", Employee.class);
146         digester.addSetProperties("employee");
147         digester.addObjectCreate("employee/address",
148                 "org.apache.commons.digester.Address");
149         digester.addSetProperties("employee/address");
150
151         // Parse our test input.
152
Object JavaDoc root = null;
153         root = digester.parse(getInputStream("Test1.xml"));
154
155         assertNotNull("Digester returned an object", root);
156         assertTrue("Digester returned an Employee",
157                 root instanceof Employee);
158         Employee employee = (Employee) root;
159         assertEquals("First name is correct",
160                 "First Name",
161                 employee.getFirstName());
162         assertEquals("Last name is correct",
163                 "Last Name",
164                 employee.getLastName());
165
166
167     }
168
169
170     /**
171      * Test object creation (and associated property setting) with nothing on
172      * the stack, which should cause an appropriate Employee object to be
173      * returned. The processing rules will process the nested Address elements
174      * as well, and will add them to the owning Employee.
175      */

176     public void testObjectCreate3() throws SAXException JavaDoc, IOException JavaDoc {
177
178         // Configure the digester as required
179
digester.addObjectCreate("employee", Employee.class);
180         digester.addSetProperties("employee");
181         digester.addObjectCreate("employee/address",
182                 "org.apache.commons.digester.Address");
183         digester.addSetProperties("employee/address");
184         digester.addSetNext("employee/address",
185                 "addAddress");
186
187         // Parse our test input once
188
Object JavaDoc root = null;
189         root = digester.parse(getInputStream("Test1.xml"));
190
191         validateObjectCreate3(root);
192
193         // Parse the same input again
194
try {
195             root = digester.parse(getInputStream("Test1.xml"));
196         } catch (Throwable JavaDoc t) {
197             fail("Digester threw IOException: " + t);
198         }
199         validateObjectCreate3(root);
200
201     }
202
203
204     /**
205      * Same as testObjectCreate1(), except use individual call method rules
206      * to set the properties of the Employee.
207      */

208     public void testObjectCreate4() throws SAXException JavaDoc, IOException JavaDoc {
209
210         // Configure the digester as required
211
digester.addObjectCreate("employee", Employee.class);
212         digester.addCallMethod("employee",
213                 "setFirstName", 1);
214         digester.addCallParam("employee", 0, "firstName");
215         digester.addCallMethod("employee",
216                 "setLastName", 1);
217         digester.addCallParam("employee", 0, "lastName");
218
219
220         // Parse our test input.
221
Object JavaDoc root = null;
222         root = digester.parse(getInputStream("Test1.xml"));
223
224         assertNotNull("Digester returned an object", root);
225         assertTrue("Digester returned an Employee",
226                 root instanceof Employee);
227         Employee employee = (Employee) root;
228         assertEquals("First name is correct",
229                 "First Name",
230                 employee.getFirstName());
231         assertEquals("Last name is correct",
232                 "Last Name",
233                 employee.getLastName());
234
235     }
236
237
238     /**
239      * Same as testObjectCreate1(), except use individual call method rules
240      * to set the properties of the Employee. Bean data are defined using
241      * elements instead of attributes. The purpose is to test CallMethod with
242      * a paramCount=0 (ie the body of the element is the argument of the
243      * method).
244      */

245     public void testObjectCreate5() throws SAXException JavaDoc, IOException JavaDoc {
246
247         // Configure the digester as required
248
digester.addObjectCreate("employee", Employee.class);
249         digester.addCallMethod("employee/firstName", "setFirstName", 0);
250         digester.addCallMethod("employee/lastName", "setLastName", 0);
251
252
253         // Parse our test input.
254
Object JavaDoc root = null;
255         root = digester.parse(getInputStream("Test5.xml"));
256
257         assertNotNull("Digester returned an object", root);
258         assertTrue("Digester returned an Employee",
259                 root instanceof Employee);
260         Employee employee = (Employee) root;
261         assertEquals("First name is correct",
262                 "First Name",
263                 employee.getFirstName());
264         assertEquals("Last name is correct",
265                 "Last Name",
266                 employee.getLastName());
267
268     }
269
270
271     /**
272      * It should be possible to parse the same input twice, and get trees
273      * of objects that are isomorphic but not be identical object instances.
274      */

275     public void testRepeatedParse() throws SAXException JavaDoc, IOException JavaDoc {
276
277         // Configure the digester as required
278
digester.addObjectCreate("employee", Employee.class);
279         digester.addSetProperties("employee");
280         digester.addObjectCreate("employee/address",
281                 "org.apache.commons.digester.Address");
282         digester.addSetProperties("employee/address");
283         digester.addSetNext("employee/address",
284                 "addAddress");
285
286         // Parse our test input the first time
287
Object JavaDoc root1 = null;
288         root1 = digester.parse(getInputStream("Test1.xml"));
289
290         validateObjectCreate3(root1);
291
292         // Parse our test input the second time
293
Object JavaDoc root2 = null;
294         root2 = digester.parse(getInputStream("Test1.xml"));
295
296         validateObjectCreate3(root2);
297
298         // Make sure that it was a different root
299
assertTrue("Different tree instances were returned",
300                 root1 != root2);
301
302     }
303
304
305     /**
306      * Test object creation (and associated property setting) with nothing on
307      * the stack, which should cause an appropriate Employee object to be
308      * returned. The processing rules will process the nested Address elements
309      * as well, but will not attempt to add them to the Employee.
310      */

311     public void testRuleSet1() throws SAXException JavaDoc, IOException JavaDoc {
312
313         // Configure the digester as required
314
RuleSet rs = new TestRuleSet();
315         digester.addRuleSet(rs);
316
317         // Parse our test input.
318
Object JavaDoc root = null;
319         root = digester.parse(getInputStream("Test1.xml"));
320
321         assertNotNull("Digester returned an object", root);
322         assertTrue("Digester returned an Employee",
323                 root instanceof Employee);
324         Employee employee = (Employee) root;
325         assertEquals("First name is correct",
326                 "First Name",
327                 employee.getFirstName());
328         assertEquals("Last name is correct",
329                 "Last Name",
330                 employee.getLastName());
331         assertNotNull("Can retrieve home address",
332                 employee.getAddress("home"));
333         assertNotNull("Can retrieve office address",
334                 employee.getAddress("office"));
335
336     }
337
338
339     /**
340      * Same as <code>testRuleSet1</code> except using a single namespace.
341      */

342     public void testRuleSet2() throws SAXException JavaDoc, IOException JavaDoc {
343
344         // Configure the digester as required
345
digester.setNamespaceAware(true);
346         RuleSet rs = new TestRuleSet(null,
347                 "http://jakarta.apache.org/digester/Foo");
348         digester.addRuleSet(rs);
349
350         // Parse our test input.
351
Object JavaDoc root = null;
352         root = digester.parse(getInputStream("Test2.xml"));
353
354         assertNotNull("Digester returned an object", root);
355         assertTrue("Digester returned an Employee",
356                 root instanceof Employee);
357         Employee employee = (Employee) root;
358         assertEquals("First name is correct",
359                 "First Name",
360                 employee.getFirstName());
361         assertEquals("Last name is correct",
362                 "Last Name",
363                 employee.getLastName());
364         assertNotNull("Can retrieve home address",
365                 employee.getAddress("home"));
366         assertNotNull("Can retrieve office address",
367                 employee.getAddress("office"));
368
369     }
370
371
372     /**
373      * Same as <code>testRuleSet2</code> except using a namespace
374      * for employee that we should recognize, and a namespace for
375      * address that we should skip.
376      */

377     public void testRuleSet3() throws SAXException JavaDoc, IOException JavaDoc {
378
379         // Configure the digester as required
380
digester.setNamespaceAware(true);
381         RuleSet rs = new TestRuleSet(null,
382                 "http://jakarta.apache.org/digester/Foo");
383         digester.addRuleSet(rs);
384
385         // Parse our test input.
386
Object JavaDoc root = null;
387         root = digester.parse(getInputStream("Test3.xml"));
388
389         assertNotNull("Digester returned an object", root);
390         assertTrue("Digester returned an Employee",
391                 root instanceof Employee);
392         Employee employee = (Employee) root;
393         assertEquals("First name is correct",
394                 "First Name",
395                 employee.getFirstName());
396         assertEquals("Last name is correct",
397                 "Last Name",
398                 employee.getLastName());
399         assertNull("Can not retrieve home address",
400                 employee.getAddress("home"));
401         assertNull("Can not retrieve office address",
402                 employee.getAddress("office"));
403
404     }
405
406
407     /**
408      * Test the two argument version of the SetTopRule rule. This test is
409      * based on testObjectCreate3 and should result in the same tree of
410      * objects. Instead of using the SetNextRule rule which results in
411      * a method invocation on the (top-1) (parent) object with the top
412      * object (child) as an argument, this test uses the SetTopRule rule
413      * which results in a method invocation on the top object (child)
414      * with the top-1 (parent) object as an argument. The three argument
415      * form is tested in <code>testSetTopRule2</code>.
416      */

417     public void testSetTopRule1() throws SAXException JavaDoc, IOException JavaDoc {
418
419         // Configure the digester as required
420
digester.addObjectCreate("employee",
421                 "org.apache.commons.digester.Employee");
422         digester.addSetProperties("employee");
423         digester.addObjectCreate("employee/address",
424                 "org.apache.commons.digester.Address");
425         digester.addSetProperties("employee/address");
426         digester.addSetTop("employee/address", "setEmployee");
427
428         // Parse our test input.
429
Object JavaDoc root = null;
430         root = digester.parse(getInputStream("Test1.xml"));
431         validateObjectCreate3(root);
432
433     }
434
435
436     /**
437      * Same as <code>testSetTopRule1</code> except using the three argument
438      * form of the SetTopRule rule.
439      */

440     public void testSetTopRule2() throws SAXException JavaDoc, IOException JavaDoc {
441
442         // Configure the digester as required
443
digester.addObjectCreate("employee",
444                 "org.apache.commons.digester.Employee");
445         digester.addSetProperties("employee");
446         digester.addObjectCreate("employee/address",
447                 "org.apache.commons.digester.Address");
448         digester.addSetProperties("employee/address");
449         digester.addSetTop("employee/address", "setEmployee",
450                 "org.apache.commons.digester.Employee");
451
452         // Parse our test input.
453
Object JavaDoc root = null;
454         root = digester.parse(getInputStream("Test1.xml"));
455
456         validateObjectCreate3(root);
457
458     }
459
460     /**
461      * Test rule addition - this boils down to making sure that
462      * digester is set properly on rule addition.
463      */

464     public void testAddRule() {
465         Digester digester = new Digester();
466         TestRule rule = new TestRule("Test");
467         digester.addRule("/root", rule);
468         
469         assertEquals("Digester is not properly on rule addition.",
470                         digester, rule.getDigester());
471
472     }
473     
474
475     public void testSetNext() throws SAXException JavaDoc, IOException JavaDoc {
476         Digester digester = new Digester();
477         digester.setRules(new ExtendedBaseRules());
478         digester.setValidating(false);
479         
480         
481         digester.addObjectCreate("!*/b", BetaBean.class);
482         digester.addObjectCreate("!*/a", AlphaBean.class);
483         digester.addObjectCreate("root", ArrayList JavaDoc.class);
484         digester.addSetProperties("!*");
485         digester.addSetNext("!*/b/?", "setChild");
486         digester.addSetNext("!*/a/?", "setChild");
487         digester.addSetNext("!root/?", "add");
488         ArrayList JavaDoc root =
489             (ArrayList JavaDoc) digester.parse(getInputStream("Test4.xml"));
490         
491         assertEquals("Wrong array size", 2, root.size());
492         AlphaBean one = (AlphaBean) root.get(0);
493         assertTrue(one.getChild() instanceof BetaBean);
494         BetaBean two = (BetaBean) one.getChild();
495         assertEquals("Wrong name (1)", two.getName() , "TWO");
496         assertTrue(two.getChild() instanceof AlphaBean);
497         AlphaBean three = (AlphaBean) two.getChild();
498         assertEquals("Wrong name (2)", three.getName() , "THREE");
499         BetaBean four = (BetaBean) root.get(1);
500         assertEquals("Wrong name (3)", four.getName() , "FOUR");
501         assertTrue(four.getChild() instanceof BetaBean);
502         BetaBean five = (BetaBean) four.getChild();
503         assertEquals("Wrong name (4)", five.getName() , "FIVE");
504         
505     }
506     
507     
508     public void testSetTop() throws SAXException JavaDoc, IOException JavaDoc {
509         Digester digester = new Digester();
510         digester.setRules(new ExtendedBaseRules());
511         digester.setValidating(false);
512         
513         
514         digester.addObjectCreate("!*/b", BetaBean.class);
515         digester.addObjectCreate("!*/a", AlphaBean.class);
516         digester.addObjectCreate("root", ArrayList JavaDoc.class);
517         digester.addSetProperties("!*");
518         digester.addSetTop("!*/b/?", "setParent");
519         digester.addSetTop("!*/a/?", "setParent");
520         digester.addSetRoot("!*/a", "add");
521         digester.addSetRoot("!*/b", "add");
522         ArrayList JavaDoc root =
523             (ArrayList JavaDoc) digester.parse(getInputStream("Test4.xml"));
524         
525         assertEquals("Wrong array size", 5, root.size());
526         
527         // note that the array is in popped order (rather than pushed)
528

529         Object JavaDoc obj = root.get(1);
530         assertTrue("TWO should be a BetaBean", obj instanceof BetaBean);
531         BetaBean two = (BetaBean) obj;
532         assertNotNull("Two's parent should not be null", two.getParent());
533         assertEquals("Wrong name (1)", "TWO", two.getName());
534         assertEquals("Wrong name (2)", "ONE", two.getParent().getName() );
535         
536         obj = root.get(0);
537         assertTrue("THREE should be an AlphaBean", obj instanceof AlphaBean);
538         AlphaBean three = (AlphaBean) obj;
539         assertNotNull("Three's parent should not be null", three.getParent());
540         assertEquals("Wrong name (3)", "THREE", three.getName());
541         assertEquals("Wrong name (4)", "TWO", three.getParent().getName());
542         
543         obj = root.get(3);
544         assertTrue("FIVE should be a BetaBean", obj instanceof BetaBean);
545         BetaBean five = (BetaBean) obj;
546         assertNotNull("Five's parent should not be null", five.getParent());
547         assertEquals("Wrong name (5)", "FIVE", five.getName());
548         assertEquals("Wrong name (6)", "FOUR", five.getParent().getName());
549
550     }
551
552
553     /**
554      */

555     public void testSetCustomProperties() throws SAXException JavaDoc, IOException JavaDoc {
556         
557         Digester digester = new Digester();
558         
559         digester.setValidating(false);
560         
561         digester.addObjectCreate("toplevel", ArrayList JavaDoc.class);
562         digester.addObjectCreate("toplevel/one", Address.class);
563         digester.addSetNext("toplevel/one", "add");
564         digester.addObjectCreate("toplevel/two", Address.class);
565         digester.addSetNext("toplevel/two", "add");
566         digester.addObjectCreate("toplevel/three", Address.class);
567         digester.addSetNext("toplevel/three", "add");
568         digester.addObjectCreate("toplevel/four", Address.class);
569         digester.addSetNext("toplevel/four", "add");
570         digester.addSetProperties("toplevel/one");
571         digester.addSetProperties(
572                     "toplevel/two",
573                     new String JavaDoc[] {"alt-street", "alt-city", "alt-state"},
574                     new String JavaDoc[] {"street", "city", "state"});
575         digester.addSetProperties(
576                     "toplevel/three",
577                     new String JavaDoc[] {"aCity", "state"},
578                     new String JavaDoc[] {"city"});
579         digester.addSetProperties("toplevel/four", "alt-city", "city");
580         
581
582         ArrayList JavaDoc root =
583             (ArrayList JavaDoc) digester.parse(getInputStream("Test7.xml"));
584         
585         assertEquals("Wrong array size", 4, root.size());
586         
587         // note that the array is in popped order (rather than pushed)
588

589         Object JavaDoc
590         obj = root.get(0);
591         assertTrue("(1) Should be an Address ", obj instanceof Address);
592         Address addressOne = (Address) obj;
593         assertEquals("(1) Street attribute", "New Street",
594                     addressOne.getStreet());
595         assertEquals("(1) City attribute", "Las Vegas", addressOne.getCity());
596         assertEquals("(1) State attribute", "Nevada", addressOne.getState());
597         
598         obj = root.get(1);
599         assertTrue("(2) Should be an Address ", obj instanceof Address);
600         Address addressTwo = (Address) obj;
601         assertEquals("(2) Street attribute", "Old Street",
602                     addressTwo.getStreet());
603         assertEquals("(2) City attribute", "Portland", addressTwo.getCity());
604         assertEquals("(2) State attribute", "Oregon", addressTwo.getState());
605         
606         obj = root.get(2);
607         assertTrue("(3) Should be an Address ", obj instanceof Address);
608         Address addressThree = (Address) obj;
609         assertEquals("(3) Street attribute", "4th Street",
610                     addressThree.getStreet());
611         assertEquals("(3) City attribute", "Dayton", addressThree.getCity());
612         assertEquals("(3) State attribute", "US" , addressThree.getState());
613        
614         obj = root.get(3);
615         assertTrue("(4) Should be an Address ", obj instanceof Address);
616         Address addressFour = (Address) obj;
617         assertEquals("(4) Street attribute", "6th Street",
618                     addressFour.getStreet());
619         assertEquals("(4) City attribute", "Cleveland", addressFour.getCity());
620         assertEquals("(4) State attribute", "Ohio", addressFour.getState());
621         
622     }
623     
624     // ------------------------------------------------ Utility Support Methods
625

626
627     /**
628      * Return an appropriate InputStream for the specified test file (which
629      * must be inside our current package.
630      *
631      * @param name Name of the test file we want
632      *
633      * @exception IOException if an input/output error occurs
634      */

635     protected InputStream JavaDoc getInputStream(String JavaDoc name) throws IOException JavaDoc {
636
637         return (this.getClass().getResourceAsStream
638                 ("/org/apache/commons/digester/" + name));
639
640     }
641
642
643     /**
644      * Validate the assertions for ObjectCreateRule3.
645      *
646      * @param object Root object returned by <code>digester.parse()</code>
647      */

648     protected void validateObjectCreate3(Object JavaDoc root) {
649
650         // Validate the retrieved Employee
651
assertNotNull("Digester returned an object", root);
652         assertTrue("Digester returned an Employee",
653                 root instanceof Employee);
654         Employee employee = (Employee) root;
655         assertEquals("First name is correct",
656                 "First Name",
657                 employee.getFirstName());
658         assertEquals("Last name is correct",
659                 "Last Name",
660                 employee.getLastName());
661
662         // Validate the corresponding "home" Address
663
Address home = employee.getAddress("home");
664         assertNotNull("Retrieved home address", home);
665         assertEquals("Home street", "Home Street",
666                 home.getStreet());
667         assertEquals("Home city", "Home City",
668                 home.getCity());
669         assertEquals("Home state", "HS",
670                 home.getState());
671         assertEquals("Home zip", "HmZip",
672                 home.getZipCode());
673
674         // Validate the corresponding "office" Address
675
Address office = employee.getAddress("office");
676         assertNotNull("Retrieved office address", office);
677         assertEquals("Office street", "Office Street",
678                 office.getStreet());
679         assertEquals("Office city", "Office City",
680                 office.getCity());
681         assertEquals("Office state", "OS",
682                 office.getState());
683         assertEquals("Office zip", "OfZip",
684                 office.getZipCode());
685
686     }
687     
688
689 }
690
Popular Tags