KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: BeanPropertySetterRuleTestCase.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.Reader JavaDoc;
24 import java.io.StringReader JavaDoc;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.List 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
36 /**
37  * <p> Test case for <code>BeanPropertySetterRule</code>.
38  * This contains tests for the main applications of the rule
39  * and two more general tests of digester functionality used by this rule.
40  */

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

46     /**
47      * Simple test xml document used in the tests.
48      */

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

62     protected Digester digester = null;
63
64
65     // ----------------------------------------------------------- Constructors
66

67
68     /**
69      * Construct a new instance of this test case.
70      *
71      * @param name Name of the test case
72      */

73     public BeanPropertySetterRuleTestCase(String JavaDoc name) {
74
75         super(name);
76
77     }
78
79
80     // --------------------------------------------------- Overall Test Methods
81

82
83     /**
84      * Set up instance variables required by this test case.
85      */

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

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

106     public void tearDown() {
107
108         digester = null;
109
110     }
111
112
113
114     // ------------------------------------------------ Individual Test Methods
115

116
117     /**
118      * This is a general digester test but it fits into here pretty well.
119      * This tests that the rule calling order is properly enforced.
120      */

121     public void testDigesterRuleCallOrder() throws SAXException JavaDoc, IOException JavaDoc {
122
123         List JavaDoc callOrder = new ArrayList JavaDoc();
124
125         // use the standard rules
126
digester.setRules(new RulesBase());
127
128         // add first test rule
129
TestRule firstRule = new TestRule("first");
130         firstRule.setOrder(callOrder);
131         digester.addRule("root/alpha", firstRule);
132
133         // add second test rule
134
TestRule secondRule = new TestRule("second");
135         secondRule.setOrder(callOrder);
136         digester.addRule("root/alpha", secondRule);
137
138         // add third test rule
139
TestRule thirdRule = new TestRule("third");
140         thirdRule.setOrder(callOrder);
141         digester.addRule("root/alpha", thirdRule);
142
143
144         digester.parse(xmlTestReader());
145
146         // we should have nine entries in our list of calls
147

148         assertEquals(
149                 "Nine calls should have been made.",
150                 9,
151                 callOrder.size());
152
153         // begin should be called in the order added
154
assertEquals(
155                 "First rule begin not called first.",
156                 "first",
157                 ((TestRule) callOrder.get(0)).getIdentifier());
158
159         assertEquals(
160                 "Second rule begin not called second.",
161                 "second",
162                 ((TestRule) callOrder.get(1)).getIdentifier());
163
164         assertEquals(
165                 "Third rule begin not called third.",
166                 "third",
167                 ((TestRule) callOrder.get(2)).getIdentifier());
168
169         // body text should be called in the order added
170
assertEquals(
171                 "First rule body text not called first.",
172                 "first",
173                 ((TestRule) callOrder.get(3)).getIdentifier());
174
175         assertEquals(
176                 "Second rule body text not called second.",
177                 "second",
178                 ((TestRule) callOrder.get(4)).getIdentifier());
179
180         assertEquals(
181                 "Third rule body text not called third.",
182                 "third",
183                 ((TestRule) callOrder.get(5)).getIdentifier());
184
185         // end should be called in reverse order
186
assertEquals(
187                 "Third rule end not called first.",
188                 "third",
189                 ((TestRule) callOrder.get(6)).getIdentifier());
190
191         assertEquals(
192                 "Second rule end not called second.",
193                 "second",
194                 ((TestRule) callOrder.get(7)).getIdentifier());
195
196         assertEquals(
197                 "First rule end not called third.",
198                 "first",
199                 ((TestRule) callOrder.get(8)).getIdentifier());
200
201
202     }
203
204
205     /**
206      * This is a general digester test but it fits into here pretty well.
207      * This tests that the body text stack is functioning correctly.
208      */

209     public void testDigesterBodyTextStack() throws SAXException JavaDoc, IOException JavaDoc {
210
211         // use the standard rules
212
digester.setRules(new RulesBase());
213
214         // add test rule to catch body text
215
TestRule rootRule = new TestRule("root");
216         digester.addRule("root", rootRule);
217
218         // add test rule to catch body text
219
TestRule alphaRule = new TestRule("root/alpha");
220         digester.addRule("root/alpha", alphaRule);
221
222         // add test rule to catch body text
223
TestRule betaRule = new TestRule("root/beta");
224         digester.addRule("root/beta", betaRule);
225
226         // add test rule to catch body text
227
TestRule gammaRule = new TestRule("root/gamma");
228         digester.addRule("root/gamma", gammaRule);
229
230         digester.parse(xmlTestReader());
231
232         assertEquals(
233                 "Root body text not set correct.",
234                 "ROOT BODY",
235                 rootRule.getBodyText());
236
237         assertEquals(
238                 "Alpha body text not set correct.",
239                 "ALPHA BODY",
240                 alphaRule.getBodyText());
241
242         assertEquals(
243                 "Beta body text not set correct.",
244                 "BETA BODY",
245                 betaRule.getBodyText());
246
247         assertEquals(
248                 "Gamma body text not set correct.",
249                 "GAMMA BODY",
250                 gammaRule.getBodyText());
251
252     }
253
254
255     /**
256      * Test that you can successfully set a given property
257      */

258     public void testSetGivenProperty() throws SAXException JavaDoc, IOException JavaDoc {
259
260         // use the standard rules
261
digester.setRules(new RulesBase());
262
263         // going to be setting properties on a SimpleTestBean
264
digester.addObjectCreate("root",
265                                  "org.apache.commons.digester.SimpleTestBean");
266
267         // we'll set property alpha with the body text of root
268
digester.addRule("root", new BeanPropertySetterRule("alpha"));
269
270         // we'll set property beta with the body text of child element alpha
271
digester.addRule("root/alpha", new BeanPropertySetterRule("beta"));
272
273         // we'll leave property gamma alone
274

275         // we'll set property delta (a write-only property) also
276
digester.addRule("root/delta", new BeanPropertySetterRule("delta"));
277
278         SimpleTestBean bean = (SimpleTestBean) digester.parse(xmlTestReader());
279
280         // check properties are set correctly
281
assertEquals(
282                 "Property alpha not set correctly",
283                 "ROOT BODY",
284                 bean.getAlpha());
285
286         assertEquals(
287                 "Property beta not set correctly",
288                 "ALPHA BODY",
289                 bean.getBeta());
290
291         assertTrue(
292                 "Property gamma not set correctly",
293                 bean.getGamma() == null);
294
295         assertEquals("Property delta not set correctly",
296                      "DELTA BODY",
297                      bean.getDeltaValue());
298                     
299
300     }
301
302
303     /**
304      * Test that trying to set an unknown property throws an exception.
305      */

306     public void testSetUnknownProperty() {
307
308         // going to be setting properties on a SimpleTestBean
309
digester.addObjectCreate("root",
310                                  "org.apache.commons.digester.SimpleTestBean");
311
312         // attempt to set an unknown property name
313
digester.addRule("root/alpha",
314                          new BeanPropertySetterRule("unknown"));
315
316         // Attempt to parse the input
317
try {
318             SimpleTestBean bean = (SimpleTestBean)
319                 digester.parse(xmlTestReader());
320             fail("Should have thrown NoSuchMethodException");
321         } catch (Exception JavaDoc e) {
322             if (e instanceof InvocationTargetException JavaDoc) {
323                 Throwable JavaDoc t =
324                     ((InvocationTargetException JavaDoc) e).getTargetException();
325                 if (t instanceof NoSuchMethodException JavaDoc) {
326                     ; // Expected result
327
} else {
328                     fail("Should have thrown NoSuchMethodException, threw " + t);
329                 }
330             }
331         }
332
333     }
334
335
336     /**
337      * Test that you can successfully automatically set properties.
338      */

339     public void testAutomaticallySetProperties()
340         throws SAXException JavaDoc, IOException JavaDoc {
341
342         // need the extended rules
343
digester.setRules(new ExtendedBaseRules());
344
345         // going to be setting properties on a SimpleTestBean
346
digester.addObjectCreate("root",
347                                  "org.apache.commons.digester.SimpleTestBean");
348
349         // match all children of root with this rule
350
digester.addRule("root/?", new BeanPropertySetterRule());
351
352         SimpleTestBean bean = (SimpleTestBean) digester.parse(xmlTestReader());
353
354         // check properties are set correctly
355
assertEquals(
356                 "Property alpha not set correctly",
357                 "ALPHA BODY",
358                 bean.getAlpha());
359
360         assertEquals(
361                 "Property beta not set correctly",
362                 "BETA BODY",
363                 bean.getBeta());
364
365         assertEquals(
366                 "Property gamma not set correctly",
367                 "GAMMA BODY",
368                 bean.getGamma());
369
370
371     }
372
373     /**
374      * Get input stream from {@link #TEST_XML}.
375      */

376     private Reader JavaDoc xmlTestReader() throws IOException JavaDoc {
377         return new StringReader JavaDoc(TEST_XML);
378     }
379
380 }
381
382
383
Popular Tags