KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: SetPropertyRuleTestCase.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
27 import junit.framework.Test;
28 import junit.framework.TestCase;
29 import junit.framework.TestSuite;
30
31 import org.xml.sax.SAXException JavaDoc;
32
33
34 /**
35  * <p> Test case for <code>SetPropertyRule</code>.</p>
36  */

37 public class SetPropertyRuleTestCase extends TestCase {
38
39
40     // ----------------------------------------------------- Instance Variables
41

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

45     protected final static String JavaDoc TEST_XML_1 =
46         "<?xml version='1.0'?><root>" +
47         "<set name='alpha' value='ALPHA VALUE'/>" +
48         "<set name='beta' value='BETA VALUE'/>" +
49         "<set name='delta' value='DELTA VALUE'/>" +
50         "</root>";
51
52     /**
53      * Simple test xml document used in the tests.
54      */

55     protected final static String JavaDoc TEST_XML_2 =
56         "<?xml version='1.0'?><root>" +
57         "<set name='unknown' value='UNKNOWN VALUE'/>" +
58         "</root>";
59
60
61     /**
62      * The digester instance we will be processing.
63      */

64     protected Digester digester = null;
65
66
67     // ----------------------------------------------------------- Constructors
68

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

75     public SetPropertyRuleTestCase(String JavaDoc name) {
76
77         super(name);
78
79     }
80
81
82     // --------------------------------------------------- Overall Test Methods
83

84
85     /**
86      * Set up instance variables required by this test case.
87      */

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

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

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

118
119     /**
120      * Positive test for SetPropertyRule.
121      */

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

152     public void testNegative() {
153
154         // Set up the rules we need
155
digester.addObjectCreate("root",
156                                  "org.apache.commons.digester.SimpleTestBean");
157         digester.addSetProperty("root/set", "name", "value");
158
159         // Parse the input (should fail)
160
try {
161             SimpleTestBean bean =
162                 (SimpleTestBean) digester.parse(xmlTestReader(TEST_XML_2));
163             fail("Should have thrown NoSuchMethodException");
164         } catch (Exception JavaDoc e) {
165             if (e instanceof NoSuchMethodException JavaDoc) {
166                 ; // Expected result
167
} else if (e instanceof InvocationTargetException JavaDoc) {
168                 Throwable JavaDoc t =
169                     ((InvocationTargetException JavaDoc) e).getTargetException();
170                 if (t instanceof NoSuchMethodException JavaDoc) {
171                     ; // Expected result
172
} else {
173                     fail("Should have thrown ITE->NoSuchMethodException, threw " + t);
174                 }
175             } else if (e instanceof SAXException JavaDoc) {
176                 Exception JavaDoc ee = ((SAXException JavaDoc) e).getException();
177                 if (ee != null) {
178                     if (ee instanceof NoSuchMethodException JavaDoc) {
179                         ; // Expected result
180
} else {
181                         fail("Should have thrown SE->NoSuchMethodException, threw " + ee);
182                     }
183                 } else {
184                     fail("Should have thrown NoSuchMethodException, threw " +
185                          e.getClass().getName());
186                 }
187             } else {
188                 fail("Should have thrown NoSuchMethodException, threw " + e);
189             }
190         }
191
192     }
193
194
195     /**
196      * Get input stream from specified String containing XML data.
197      */

198     private Reader JavaDoc xmlTestReader(String JavaDoc xml) throws IOException JavaDoc {
199         return new StringReader JavaDoc(xml);
200     }
201
202 }
203
204
205
Popular Tags