KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: SetNestedPropertiesRuleTestCase.java 179714 2005-06-03 03:53:39Z 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;
20
21
22 import java.io.IOException JavaDoc;
23 import java.io.Reader JavaDoc;
24 import java.io.StringReader 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 /**
34  * <p> Test case for <code>SetNestedPropertiesRule</code>.
35  * This contains tests for the main applications of the rule
36  * and two more general tests of digester functionality used by this rule.
37  */

38 public class SetNestedPropertiesRuleTestCase extends TestCase {
39
40
41     // ----------------------------------------------------- Instance Variables
42

43     /**
44      * Simple test xml document used in the tests.
45      */

46     protected final static String JavaDoc TEST_XML =
47         "<?xml version='1.0'?>" +
48         "<root>ROOT BODY" +
49         "<alpha>ALPHA BODY</alpha>" +
50         "<beta>BETA BODY</beta>" +
51         "<gamma>GAMMA BODY</gamma>" +
52         "<delta>DELTA BODY</delta>" +
53         "</root>";
54
55
56     /**
57      * The digester instance we will be processing.
58      */

59     protected Digester digester = null;
60
61
62     // ----------------------------------------------------------- Constructors
63

64
65     /**
66      * Construct a new instance of this test case.
67      *
68      * @param name Name of the test case
69      */

70     public SetNestedPropertiesRuleTestCase(String JavaDoc name) {
71
72         super(name);
73
74     }
75
76
77     // --------------------------------------------------- Overall Test Methods
78

79
80     /**
81      * Set up instance variables required by this test case.
82      */

83     public void setUp() {
84
85         digester = new Digester();
86
87     }
88
89
90     /**
91      * Return the tests included in this test suite.
92      */

93     public static Test suite() {
94
95         return (new TestSuite(SetNestedPropertiesRuleTestCase.class));
96
97     }
98
99
100     /**
101      * Tear down instance variables required by this test case.
102      */

103     public void tearDown() {
104
105         digester = null;
106
107     }
108
109
110
111     // ------------------------------------------------ Individual Test Methods
112

113
114     /**
115      * Test that you can successfully automatically set properties.
116      */

117     public void testAutomaticallySetProperties()
118         throws SAXException JavaDoc, IOException JavaDoc {
119
120         // going to be setting properties on a SimpleTestBean
121
digester.addObjectCreate("root",
122                                  "org.apache.commons.digester.SimpleTestBean");
123
124         // match all children of root with this rule
125
digester.addRule("root", new SetNestedPropertiesRule());
126
127         SimpleTestBean bean = (SimpleTestBean) digester.parse(xmlTestReader());
128
129         // check properties are set correctly
130
assertEquals(
131                 "Property alpha not set correctly",
132                 "ALPHA BODY",
133                 bean.getAlpha());
134
135         assertEquals(
136                 "Property beta not set correctly",
137                 "BETA BODY",
138                 bean.getBeta());
139
140         assertEquals(
141                 "Property gamma not set correctly",
142                 "GAMMA BODY",
143                 bean.getGamma());
144
145         assertEquals(
146                 "Property delta not set correctly",
147                 "DELTA BODY",
148                 bean.getDeltaValue());
149     }
150
151     /**
152      * Test that it is an error when a child element exists but no corresponding
153      * java property exists.
154      */

155     public void testMandatoryProperties()
156         throws SAXException JavaDoc, IOException JavaDoc {
157
158         String JavaDoc TEST_XML =
159             "<?xml version='1.0'?>" +
160             "<root>ROOT BODY" +
161             "<badprop>ALPHA BODY</badprop>" +
162             "</root>";
163
164         // going to be setting properties on a SimpleTestBean
165
digester.addObjectCreate("root",
166                                  "org.apache.commons.digester.SimpleTestBean");
167
168         // match all children of root with this rule
169
digester.addRule("root", new SetNestedPropertiesRule());
170
171         try {
172             SimpleTestBean bean = (SimpleTestBean) digester.parse(
173                 new StringReader JavaDoc(TEST_XML));
174
175             // we should never get here...
176
fail("No exception thrown by parse when unknown child element found.");
177         } catch(org.xml.sax.SAXParseException JavaDoc e) {
178             String JavaDoc msg = e.getMessage();
179             if (msg.indexOf("badprop") >= 0) {
180                 // ok, this is expected; there is no "setBadprop" method on the
181
// SimpleTestBean class...
182
} else {
183                 fail("Unexpected parse exception:" + e.getMessage());
184             }
185         }
186     }
187
188     /**
189      * Test that you can customise the property mappings using the
190      * constructor which takes arrays-of-strings.
191      */

192     public void testCustomisedProperties1()
193         throws SAXException JavaDoc, IOException JavaDoc {
194
195         String JavaDoc TEST_XML =
196             "<?xml version='1.0'?>" +
197             "<root>ROOT BODY" +
198             "<alpha>ALPHA BODY</alpha>" +
199             "<beta>BETA BODY</beta>" +
200             "<gamma-alt>GAMMA BODY</gamma-alt>" +
201             "<delta>DELTA BODY</delta>" +
202             "</root>";
203
204         // going to be setting properties on a SimpleTestBean
205
digester.addObjectCreate("root",
206                                  "org.apache.commons.digester.SimpleTestBean");
207
208         // ignore the "alpha" element (target=null)
209
// don't remap the "beta" element
210
// map the gamma-alt element into the gamma property
211
// ignore the delta element (no matching element in array)
212

213         Rule rule = new SetNestedPropertiesRule(
214             new String JavaDoc[]{"alpha", "gamma-alt", "delta"},
215             new String JavaDoc[]{null, "gamma"});
216             
217         digester.addRule("root", rule);
218
219         SimpleTestBean bean = (SimpleTestBean) digester.parse(
220             new StringReader JavaDoc(TEST_XML));
221
222         // check properties are set correctly
223
assertEquals(
224                 "Property alpha was not ignored (it should be)",
225                 null,
226                 bean.getAlpha());
227
228         assertEquals(
229                 "Property beta not set correctly",
230                 "BETA BODY",
231                 bean.getBeta());
232
233         assertEquals(
234                 "Property gamma not set correctly",
235                 "GAMMA BODY",
236                 bean.getGamma());
237
238         assertEquals(
239                 "Property delta was not ignored (it should be)",
240                 null,
241                 bean.getDeltaValue());
242                 
243          // check no bad rules object is left
244
assertEquals(
245             "Digester rules object not reset.",
246             RulesBase.class, digester.getRules().getClass());
247     }
248
249     /**
250      * Test that you can ignore a single input xml element using the
251      * constructor which takes a single remapping.
252      */

253     public void testCustomisedProperties2a()
254         throws SAXException JavaDoc, IOException JavaDoc {
255
256         String JavaDoc TEST_XML =
257             "<?xml version='1.0'?>" +
258             "<root>ROOT BODY" +
259             "<alpha>ALPHA BODY</alpha>" +
260             "<beta>BETA BODY</beta>" +
261             "<gamma>GAMMA BODY</gamma>" +
262             "<delta>DELTA BODY</delta>" +
263             "</root>";
264
265         // going to be setting properties on a SimpleTestBean
266
digester.addObjectCreate("root",
267                                  "org.apache.commons.digester.SimpleTestBean");
268
269         // ignore the "alpha" element (target=null)
270
Rule rule = new SetNestedPropertiesRule("alpha", null);
271         digester.addRule("root", rule);
272
273         SimpleTestBean bean = (SimpleTestBean) digester.parse(
274             new StringReader JavaDoc(TEST_XML));
275
276         // check properties are set correctly
277
assertEquals(
278                 "Property alpha was not ignored (it should be)",
279                 null,
280                 bean.getAlpha());
281
282         assertEquals(
283                 "Property beta not set correctly",
284                 "BETA BODY",
285                 bean.getBeta());
286
287         assertEquals(
288                 "Property gamma not set correctly",
289                 "GAMMA BODY",
290                 bean.getGamma());
291
292         assertEquals(
293                 "Property delta not set correctly",
294                 "DELTA BODY",
295                 bean.getDeltaValue());
296
297         // check no bad rules object is left
298
assertEquals(
299             "Digester rules object not reset.",
300             RulesBase.class, digester.getRules().getClass());
301     }
302
303     /**
304      * Test that you can customise the property mappings using the
305      * constructor which takes a single remapping.
306      */

307     public void testCustomisedProperties2b()
308         throws SAXException JavaDoc, IOException JavaDoc {
309
310         String JavaDoc TEST_XML =
311             "<?xml version='1.0'?>" +
312             "<root>ROOT BODY" +
313             "<alpha-alt>ALPHA BODY</alpha-alt>" +
314             "<beta>BETA BODY</beta>" +
315             "<gamma>GAMMA BODY</gamma>" +
316             "<delta>DELTA BODY</delta>" +
317             "</root>";
318
319         // going to be setting properties on a SimpleTestBean
320
digester.addObjectCreate("root",
321                                  "org.apache.commons.digester.SimpleTestBean");
322
323         // map the contents of the alpha-alt xml child into the
324
// "alpha" java property.
325
Rule rule = new SetNestedPropertiesRule("alpha-alt", "alpha");
326         digester.addRule("root", rule);
327
328         SimpleTestBean bean = (SimpleTestBean) digester.parse(
329             new StringReader JavaDoc(TEST_XML));
330
331         // check properties are set correctly
332
assertEquals(
333                 "Property alpha not set correctly",
334                 "ALPHA BODY",
335                 bean.getAlpha());
336
337         assertEquals(
338                 "Property beta not set correctly",
339                 "BETA BODY",
340                 bean.getBeta());
341
342         assertEquals(
343                 "Property gamma not set correctly",
344                 "GAMMA BODY",
345                 bean.getGamma());
346
347         assertEquals(
348                 "Property delta not set correctly",
349                 "DELTA BODY",
350                 bean.getDeltaValue());
351                 
352          // check no bad rules object is left
353
assertEquals(
354             "Digester rules object not reset.",
355             RulesBase.class, digester.getRules().getClass());
356     }
357
358     /**
359      * Test that:
360      * <ul>
361      * <li> you can have rules matching the same pattern as the
362      * SetNestedPropertiesRule, </li>
363      * <li> you can have rules matching child elements of the rule, </li>
364      * <li> the Rules object is reset nicely. </li>
365      * </ul>
366      */

367     public void testMultiRuleMatch()
368         throws SAXException JavaDoc, IOException JavaDoc {
369
370         String JavaDoc testXml =
371             "<?xml version='1.0'?>" +
372             "<root>" +
373                 "<testbean alpha='alpha-attr'>ROOT BODY" +
374                     "<beta>BETA BODY</beta>" +
375                     "<gamma>GAMMA " +
376                     "<prop name='delta' value='delta-prop'/>" +
377                     "BODY" +
378                     "</gamma>" +
379                 "</testbean>" +
380             "</root>";
381
382         Reader JavaDoc reader = new StringReader JavaDoc(testXml);
383
384         // going to be setting properties on a SimpleTestBean
385
digester.addObjectCreate("root/testbean",
386                                  "org.apache.commons.digester.SimpleTestBean");
387
388         digester.addRule("root/testbean", new SetNestedPropertiesRule());
389         digester.addSetProperties("root/testbean");
390         digester.addSetProperty("root/testbean/gamma/prop", "name", "value");
391
392         SimpleTestBean bean = (SimpleTestBean) digester.parse(reader);
393
394         assertNotNull("No object created", bean);
395         
396         // check properties are set correctly
397
assertEquals(
398                 "Property alpha not set correctly",
399                 "alpha-attr",
400                 bean.getAlpha());
401
402         assertEquals(
403                 "Property beta not set correctly",
404                 "BETA BODY",
405                 bean.getBeta());
406
407         assertEquals(
408                 "Property gamma not set correctly",
409                 "GAMMA BODY",
410                 bean.getGamma());
411
412         assertEquals(
413                 "Property delta not set correctly",
414                 "delta-prop",
415                 bean.getDeltaValue());
416
417          // check no bad rules object is left
418
assertEquals(
419             "Digester rules object not reset.",
420             RulesBase.class, digester.getRules().getClass());
421     }
422
423     /**
424      * Test that unknown child elements trigger an exception.
425      */

426     public void testUnknownChildrenCausesException()
427         throws SAXException JavaDoc, IOException JavaDoc {
428
429         String JavaDoc testXml =
430             "<?xml version='1.0'?>" +
431             "<root>" +
432                 "<testbean>" +
433                     "<beta>BETA BODY</beta>" +
434                     "<foo>GAMMA</foo>" +
435                 "</testbean>" +
436             "</root>";
437
438         Reader JavaDoc reader = new StringReader JavaDoc(testXml);
439
440         // going to be setting properties on a SimpleTestBean
441
digester.addObjectCreate("root",
442                                  "org.apache.commons.digester.SimpleTestBean");
443
444         Rule rule = new SetNestedPropertiesRule();
445         digester.addRule("root", rule);
446
447         try {
448             SimpleTestBean bean = (SimpleTestBean) digester.parse(reader);
449             fail("Expected to generate an exception.");
450         } catch(SAXException JavaDoc e) {
451             Exception JavaDoc nested = e.getException();
452             if ((nested==null) || !(nested instanceof NoSuchMethodException JavaDoc)) {
453                 // nope, not the sort of exception we expected
454
throw e;
455             }
456         }
457     }
458
459     /**
460      * Test that unknown child elements are allowed if the appropriate
461      * flag is set.
462      */

463     public void testUnknownChildrenExceptionOverride()
464         throws SAXException JavaDoc, IOException JavaDoc {
465
466         String JavaDoc testXml =
467             "<?xml version='1.0'?>" +
468             "<root>" +
469                 "<testbean>" +
470                     "<beta>BETA BODY</beta>" +
471                     "<foo>GAMMA</foo>" +
472                 "</testbean>" +
473             "</root>";
474
475         Reader JavaDoc reader = new StringReader JavaDoc(testXml);
476
477         // going to be setting properties on a SimpleTestBean
478
digester.addObjectCreate("root",
479                                  "org.apache.commons.digester.SimpleTestBean");
480
481         SetNestedPropertiesRule rule = new SetNestedPropertiesRule();
482         rule.setAllowUnknownChildElements(true);
483         digester.addRule("root", rule);
484
485         SimpleTestBean bean = (SimpleTestBean) digester.parse(reader);
486         assertNotNull(bean);
487     }
488
489     /**
490      * Test that the rule works in a sane manner when the associated pattern
491      * is a wildcard such that the rule matches one of its own child elements.
492      * <p>
493      * See bugzilla entry 31393.
494      */

495     public void testRecursiveNestedProperties()
496         throws SAXException JavaDoc, IOException JavaDoc {
497
498         String JavaDoc testXml =
499             "<?xml version='1.0'?>" +
500             "<testbean>" +
501                 "<beta>BETA BODY</beta>" +
502                 "<testbean>" +
503                     "<beta>BETA BODY</beta>" +
504                 "</testbean>" +
505             "</testbean>";
506
507         Reader JavaDoc reader = new StringReader JavaDoc(testXml);
508
509         // going to be setting properties on a SimpleTestBean
510
digester.addObjectCreate("*/testbean",
511                                  "org.apache.commons.digester.SimpleTestBean");
512
513         SetNestedPropertiesRule rule = new SetNestedPropertiesRule();
514         rule.setAllowUnknownChildElements(true);
515         digester.addRule("*/testbean", rule);
516
517         SimpleTestBean bean = (SimpleTestBean) digester.parse(reader);
518         assertNotNull(bean);
519     }
520
521
522     /**
523      * Get input stream from {@link #TEST_XML}.
524      */

525     private Reader JavaDoc xmlTestReader() throws IOException JavaDoc {
526         return new StringReader JavaDoc(TEST_XML);
527     }
528
529 }
530
531
532
533
Popular Tags