KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: SetPropertiesRuleTestCase.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>SetPropertiesRule</code>.</p>
35  */

36 public class SetPropertiesRuleTestCase extends TestCase {
37
38
39     // ----------------------------------------------------- Instance Variables
40

41     /**
42      * Simple test xml document used in the tests.
43      */

44     protected final static String JavaDoc TEST_XML_1 =
45         "<?xml version='1.0'?><root alpha='ALPHA VALUE' beta='BETA VALUE' delta='DELTA VALUE'/>";
46
47     /**
48      * Simple test xml document used in the tests.
49      */

50     protected final static String JavaDoc TEST_XML_2 =
51         "<?xml version='1.0'?><root alpa='ALPA VALUE' beta='BETA VALUE' delta='DELTA VALUE'/>";
52
53     /**
54      * Simple test xml document used in the tests.
55      */

56     protected final static String JavaDoc TEST_XML_3 =
57         "<?xml version='1.0'?><root alpha='ALPHA VALUE' beta='BETA VALUE' delta='DELTA VALUE' ignore='ignore value'/>";
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 SetPropertiesRuleTestCase(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(SetPropertiesRuleTestCase.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      * Positive test for SetPropertiesRule.
119      */

120     public void testPositive() throws Exception JavaDoc {
121
122         // Set up the rules we need
123
digester.addObjectCreate("root",
124                                  "org.apache.commons.digester.SimpleTestBean");
125         digester.addSetProperties("root");
126
127         // Parse the input
128
SimpleTestBean bean =
129             (SimpleTestBean) digester.parse(xmlTestReader(TEST_XML_1));
130
131         // Check that the properties were set correctly
132
assertEquals("alpha property set",
133                      "ALPHA VALUE",
134                      bean.getAlpha());
135         assertEquals("beta property set",
136                      "BETA VALUE",
137                      bean.getBeta());
138         assertNull("gamma property not set",
139                    bean.getGamma());
140         assertEquals("delta property set",
141                      "DELTA VALUE",
142                      bean.getDeltaValue());
143
144     }
145
146     /**
147      * Positive test for SetPropertyRule ignoring missing properties.
148      */

149     public void testIgnoreMissing() throws Exception JavaDoc {
150
151         // Set up the rules we need
152
digester.addObjectCreate("root",
153                                  "org.apache.commons.digester.SimpleTestBean");
154         digester.addSetProperties("root");
155
156         // Parse the input
157
SimpleTestBean bean =
158             (SimpleTestBean) digester.parse(xmlTestReader(TEST_XML_2));
159
160         // Check that the properties were set correctly
161
assertNull("alpha property not set",
162                      bean.getAlpha());
163         assertEquals("beta property set",
164                      "BETA VALUE",
165                      bean.getBeta());
166         assertNull("gamma property not set",
167                    bean.getGamma());
168         assertEquals("delta property set",
169                      "DELTA VALUE",
170                      bean.getDeltaValue());
171
172     }
173
174     /**
175      * Negative test for SetPropertyRule ignoring missing properties.
176      */

177     public void testNegativeNotIgnoreMissing() throws Exception JavaDoc {
178
179         // Set up the rules we need
180
digester.addObjectCreate("root",
181                                  "org.apache.commons.digester.SimpleTestBean");
182         SetPropertiesRule rule = new SetPropertiesRule();
183         rule.setIgnoreMissingProperty(false);
184         digester.addRule("root", rule);
185
186         try {
187             // Parse the input
188
SimpleTestBean bean =
189                 (SimpleTestBean) digester.parse(xmlTestReader(TEST_XML_2));
190             fail("Should have thrown NoSuchMethodException");
191         } catch (Exception JavaDoc e) {
192             if (e instanceof NoSuchMethodException JavaDoc) {
193                 // Expected;
194
} else if (e instanceof SAXException JavaDoc) {
195                 Exception JavaDoc ee = ((SAXException JavaDoc) e).getException();
196                 if (ee != null) {
197                     if (ee instanceof NoSuchMethodException JavaDoc) {
198                         ; // Expected result
199
} else {
200                         fail("Should have thrown SE->NoSuchMethodException, threw " + ee);
201                     }
202                 } else {
203                     fail("Should have thrown NoSuchMethodException, threw " +
204                          e.getClass().getName());
205                 }
206             } else {
207                 fail("Should have thrown NoSuchMethodException, threw " + e);
208             }
209         }
210     }
211
212     /**
213      * Negative test for SetPropertyRule ignoring missing properties.
214      */

215     public void testPositiveNotIgnoreMissingWithIgnoreAttributes() throws Exception JavaDoc {
216
217         // Set up the rules we need
218
digester.addObjectCreate("root",
219                                  "org.apache.commons.digester.SimpleTestBean");
220         SetPropertiesRule rule = new SetPropertiesRule(new String JavaDoc[] {"ignore"}, new String JavaDoc[] {});
221         rule.setIgnoreMissingProperty(false);
222         digester.addRule("root", rule);
223
224         // Parse the input
225
SimpleTestBean bean =
226             (SimpleTestBean) digester.parse(xmlTestReader(TEST_XML_3));
227         
228         // Check that the properties were set correctly
229
assertEquals("alpha property set",
230                      "ALPHA VALUE",
231                      bean.getAlpha());
232         assertEquals("beta property set",
233                      "BETA VALUE",
234                      bean.getBeta());
235         assertNull("gamma property not set",
236                    bean.getGamma());
237         assertEquals("delta property set",
238                      "DELTA VALUE",
239                      bean.getDeltaValue());
240     }
241
242
243     /**
244      * Get input stream from specified String containing XML data.
245      */

246     private Reader JavaDoc xmlTestReader(String JavaDoc xml) throws IOException JavaDoc {
247         return new StringReader JavaDoc(xml);
248     }
249
250 }
251
252
253
Popular Tags