KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: TestFactoryCreate.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 import java.io.StringReader JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24
25 import junit.framework.Test;
26 import junit.framework.TestCase;
27 import junit.framework.TestSuite;
28
29 import org.xml.sax.Attributes JavaDoc;
30
31 /**
32  * Test case for factory create rules.
33  *
34  * @author Robert Burrell Donkin
35  */

36
37 public class TestFactoryCreate extends TestCase {
38     /** Standard constructor */
39     public TestFactoryCreate(String JavaDoc name) {
40         super(name);
41     }
42
43     /** Set up instance variables required by this test case. */
44     public void setUp() {}
45
46     /** Return the tests included in this test suite. */
47     public static Test suite() {
48
49         return (new TestSuite(TestFactoryCreate.class));
50
51     }
52
53     /** Tear down instance variables required by this test case.*/
54     public void tearDown() {}
55         
56     // --------------------------------------------------------------- Test cases
57

58        
59     
60     public void testPropagateException() throws Exception JavaDoc {
61     
62         // only used with this method
63
class ThrowExceptionCreateRule extends AbstractObjectCreationFactory {
64             public Object JavaDoc createObject(Attributes JavaDoc attributes) throws Exception JavaDoc {
65                 throw new RuntimeException JavaDoc();
66             }
67         }
68         
69         
70         // now for the tests
71
String JavaDoc xml = "<?xml version='1.0' ?><root><element/></root>";
72         
73         // test default - which is to propagate the exception
74
Digester digester = new Digester();
75         digester.addFactoryCreate("root", new ThrowExceptionCreateRule());
76         try {
77         
78             digester.parse(new StringReader JavaDoc(xml));
79             fail("Exception not propagated from create rule (1)");
80         
81         } catch (Exception JavaDoc e) {
82             /* This is what's expected */
83         }
84         
85         // test propagate exception
86
digester = new Digester();
87         digester.addFactoryCreate("root", new ThrowExceptionCreateRule(), false);
88         try {
89         
90             digester.parse(new StringReader JavaDoc(xml));
91             fail("Exception not propagated from create rule (1)");
92         
93         } catch (Exception JavaDoc e) {
94             /* This is what's expected */
95         }
96         
97         // test don't propagate exception
98
digester = new Digester();
99         digester.addFactoryCreate("root", new ThrowExceptionCreateRule(), true);
100         try {
101         
102             digester.parse(new StringReader JavaDoc(xml));
103         
104         } catch (Exception JavaDoc e) {
105             // this shouldn't happen
106
fail("Exception should not be propagated");
107         }
108     }
109     
110     public void testFactoryCreateRule() throws Exception JavaDoc {
111         tryVariations(true);
112         tryVariations(false);
113     }
114     
115     private void tryVariations(boolean propagateExceptions) throws Exception JavaDoc {
116         
117         
118         // test passing object create
119
Digester digester = new Digester();
120         TestObjectCreationFactory factory = new TestObjectCreationFactory();
121         digester.addFactoryCreate("root", factory, propagateExceptions);
122         String JavaDoc xml = new String JavaDoc (
123             "<?xml version='1.0' ?><root one='good' two='bad' three='ugly'><element/></root>");
124         digester.parse(new StringReader JavaDoc(xml));
125         
126         assertEquals("Object create not called(1)[" + propagateExceptions + "]", factory.called , true);
127         assertEquals(
128                     "Attribute not passed (1)[" + propagateExceptions + "]",
129                     factory.attributes.getValue("one"),
130                     "good");
131         assertEquals(
132                     "Attribute not passed (2)[" + propagateExceptions + "]",
133                     factory.attributes.getValue("two"),
134                     "bad");
135         assertEquals(
136                     "Attribute not passed (3)[" + propagateExceptions + "]",
137                     factory.attributes.getValue("three"),
138                     "ugly");
139                     
140         digester = new Digester();
141         digester.addFactoryCreate(
142                                 "root",
143                                 "org.apache.commons.digester.TestObjectCreationFactory",
144                                 propagateExceptions);
145         digester.addSetNext("root", "add");
146         xml = new String JavaDoc (
147             "<?xml version='1.0' ?><root one='good' two='bad' three='ugly'><element/></root>");
148         List JavaDoc list = new ArrayList JavaDoc();
149         digester.push(list);
150         digester.parse(new StringReader JavaDoc(xml));
151         
152         assertEquals("List should contain only the factory object", list.size() , 1);
153         factory = (TestObjectCreationFactory) list.get(0);
154         assertEquals("Object create not called(2)[" + propagateExceptions + "]", factory.called , true);
155         assertEquals(
156                     "Attribute not passed (4)[" + propagateExceptions + "]",
157                     factory.attributes.getValue("one"),
158                     "good");
159         assertEquals(
160                     "Attribute not passed (5)[" + propagateExceptions + "]",
161                     factory.attributes.getValue("two"),
162                     "bad");
163         assertEquals(
164                     "Attribute not passed (6)[" + propagateExceptions + "]",
165                     factory.attributes.getValue("three"),
166                     "ugly");
167                     
168     
169         digester = new Digester();
170         digester.addFactoryCreate(
171                                 "root",
172                                 "org.apache.commons.digester.TestObjectCreationFactory",
173                                 "override",
174                                 propagateExceptions);
175         digester.addSetNext("root", "add");
176         xml = new String JavaDoc (
177             "<?xml version='1.0' ?><root one='good' two='bad' three='ugly'><element/></root>");
178         list = new ArrayList JavaDoc();
179         digester.push(list);
180         digester.parse(new StringReader JavaDoc(xml));
181         
182         assertEquals("List should contain only the factory object", list.size() , 1);
183         factory = (TestObjectCreationFactory) list.get(0);
184         assertEquals("Object create not called(3)[" + propagateExceptions + "]", factory.called , true);
185         assertEquals(
186                     "Attribute not passed (7)[" + propagateExceptions + "]",
187                     factory.attributes.getValue("one"),
188                     "good");
189         assertEquals(
190                     "Attribute not passed (8)[" + propagateExceptions + "]",
191                     factory.attributes.getValue("two"),
192                     "bad");
193         assertEquals(
194                     "Attribute not passed (8)[" + propagateExceptions + "]",
195                     factory.attributes.getValue("three"),
196                     "ugly");
197         
198         digester = new Digester();
199         digester.addFactoryCreate(
200                                 "root",
201                                 "org.apache.commons.digester.TestObjectCreationFactory",
202                                 "override",
203                                 propagateExceptions);
204         digester.addSetNext("root", "add");
205         xml = new String JavaDoc (
206             "<?xml version='1.0' ?><root one='good' two='bad' three='ugly' "
207             + " override='org.apache.commons.digester.OtherTestObjectCreationFactory' >"
208             + "<element/></root>");
209         list = new ArrayList JavaDoc();
210         digester.push(list);
211         digester.parse(new StringReader JavaDoc(xml));
212         
213         assertEquals("List should contain only the factory object", list.size() , 1);
214         factory = (TestObjectCreationFactory) list.get(0);
215         assertEquals(
216                     "Attribute Override Failed (1)",
217                     factory.getClass().getName() ,
218                     "org.apache.commons.digester.OtherTestObjectCreationFactory");
219         assertEquals("Object create not called(4)[" + propagateExceptions + "]", factory.called , true);
220         assertEquals(
221                     "Attribute not passed (10)[" + propagateExceptions + "]",
222                     factory.attributes.getValue("one"),
223                     "good");
224         assertEquals(
225                     "Attribute not passed (11)[" + propagateExceptions + "]",
226                     factory.attributes.getValue("two"),
227                     "bad");
228         assertEquals(
229                     "Attribute not passed (12)[" + propagateExceptions + "]",
230                     factory.attributes.getValue("three"),
231                     "ugly");
232     
233         digester = new Digester();
234         digester.addFactoryCreate(
235                                 "root",
236                                 TestObjectCreationFactory.class,
237                                 "override",
238                                 propagateExceptions);
239         digester.addSetNext("root", "add");
240         xml = new String JavaDoc (
241             "<?xml version='1.0' ?><root one='good' two='bad' three='ugly'><element/></root>");
242         list = new ArrayList JavaDoc();
243         digester.push(list);
244         digester.parse(new StringReader JavaDoc(xml));
245         
246         assertEquals("List should contain only the factory object", list.size() , 1);
247         factory = (TestObjectCreationFactory) list.get(0);
248         assertEquals("Object create not called(5)[" + propagateExceptions + "]", factory.called , true);
249         assertEquals(
250                     "Attribute not passed (13)[" + propagateExceptions + "]",
251                     factory.attributes.getValue("one"),
252                     "good");
253         assertEquals(
254                     "Attribute not passed (14)[" + propagateExceptions + "]",
255                     factory.attributes.getValue("two"),
256                     "bad");
257         assertEquals(
258                     "Attribute not passed (15)[" + propagateExceptions + "]",
259                     factory.attributes.getValue("three"),
260                     "ugly");
261         
262         digester = new Digester();
263         digester.addFactoryCreate(
264                                 "root",
265                                 TestObjectCreationFactory.class,
266                                 "override",
267                                 propagateExceptions);
268         digester.addSetNext("root", "add");
269         xml = new String JavaDoc (
270             "<?xml version='1.0' ?><root one='good' two='bad' three='ugly' "
271             + " override='org.apache.commons.digester.OtherTestObjectCreationFactory' >"
272             + "<element/></root>");
273         list = new ArrayList JavaDoc();
274         digester.push(list);
275         digester.parse(new StringReader JavaDoc(xml));
276         
277         assertEquals("List should contain only the factory object", list.size() , 1);
278         factory = (TestObjectCreationFactory) list.get(0);
279         assertEquals(
280                     "Attribute Override Failed (2)",
281                     factory.getClass().getName() ,
282                     "org.apache.commons.digester.OtherTestObjectCreationFactory");
283         assertEquals("Object create not called(6)[" + propagateExceptions + "]", factory.called , true);
284         assertEquals(
285                     "Attribute not passed (16)[" + propagateExceptions + "]",
286                     factory.attributes.getValue("one"),
287                     "good");
288         assertEquals(
289                     "Attribute not passed (17)[" + propagateExceptions + "]",
290                     factory.attributes.getValue("two"),
291                     "bad");
292         assertEquals(
293                     "Attribute not passed (18)[" + propagateExceptions + "]",
294                     factory.attributes.getValue("three"),
295                     "ugly");
296     }
297 }
298
Popular Tags