1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.digester3;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.fail;
23
24 import java.io.StringReader;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.List;
28
29 import org.apache.commons.digester3.AbstractObjectCreationFactory;
30 import org.apache.commons.digester3.Digester;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.junit.runners.Parameterized;
34 import org.junit.runners.Parameterized.Parameters;
35 import org.xml.sax.Attributes;
36
37
38
39
40
41
42 @RunWith(value = Parameterized.class)
43 public class TestFactoryCreate
44 {
45
46 private final boolean ignoreCreateExceptions;
47
48 public TestFactoryCreate( boolean ignoreCreateExceptions )
49 {
50 this.ignoreCreateExceptions = ignoreCreateExceptions;
51 }
52
53 @Parameters
54 public static Collection<Object[]> data()
55 {
56 Collection<Object[]> data = new ArrayList<Object[]>(2);
57
58 data.add( new Object[] { true } );
59 data.add( new Object[] { false } );
60
61 return data;
62 }
63
64
65
66 @Test
67 public void testPropagateException()
68 throws Exception
69 {
70
71
72 class ThrowExceptionCreateRule
73 extends AbstractObjectCreationFactory<Object>
74 {
75 @Override
76 public Object createObject( Attributes attributes )
77 throws Exception
78 {
79 throw new RuntimeException();
80 }
81 }
82
83
84 String xml = "<?xml version='1.0' ?><root><element/></root>";
85
86
87 Digester digester = new Digester();
88 digester.addFactoryCreate( "root", new ThrowExceptionCreateRule(), ignoreCreateExceptions );
89 try
90 {
91
92 digester.parse( new StringReader( xml ) );
93 if ( !ignoreCreateExceptions )
94 {
95 fail( "Exception should be propagated from create rule" );
96 }
97
98 }
99 catch ( Exception e )
100 {
101 if ( ignoreCreateExceptions )
102 {
103 fail( "Exception should not be propagated" );
104 }
105 }
106 }
107
108 @Test
109 public void testFactoryCreateRule()
110 throws Exception
111 {
112
113
114 Digester digester = new Digester();
115 ObjectCreationFactoryTestImpl factory = new ObjectCreationFactoryTestImpl();
116 digester.addFactoryCreate( "root", factory, ignoreCreateExceptions );
117 String xml = new String( "<?xml version='1.0' ?><root one='good' two='bad' three='ugly'><element/></root>" );
118 digester.parse( new StringReader( xml ) );
119
120 assertEquals( "Object create not called(1)[" + ignoreCreateExceptions + "]", factory.called, true );
121 assertEquals( "Attribute not passed (1)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "one" ),
122 "good" );
123 assertEquals( "Attribute not passed (2)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "two" ),
124 "bad" );
125 assertEquals( "Attribute not passed (3)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "three" ),
126 "ugly" );
127
128 digester = new Digester();
129 digester.addFactoryCreate( "root", "org.apache.commons.digester3.ObjectCreationFactoryTestImpl",
130 ignoreCreateExceptions );
131 digester.addSetNext( "root", "add" );
132 xml = new String( "<?xml version='1.0' ?><root one='good' two='bad' three='ugly'><element/></root>" );
133 List<ObjectCreationFactoryTestImpl> list = new ArrayList<ObjectCreationFactoryTestImpl>();
134 digester.push( list );
135 digester.parse( new StringReader( xml ) );
136
137 assertEquals( "List should contain only the factory object", list.size(), 1 );
138 factory = list.get( 0 );
139 assertEquals( "Object create not called(2)[" + ignoreCreateExceptions + "]", factory.called, true );
140 assertEquals( "Attribute not passed (4)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "one" ),
141 "good" );
142 assertEquals( "Attribute not passed (5)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "two" ),
143 "bad" );
144 assertEquals( "Attribute not passed (6)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "three" ),
145 "ugly" );
146
147 digester = new Digester();
148 digester.addFactoryCreate( "root", "org.apache.commons.digester3.ObjectCreationFactoryTestImpl", "override",
149 ignoreCreateExceptions );
150 digester.addSetNext( "root", "add" );
151 xml = new String( "<?xml version='1.0' ?><root one='good' two='bad' three='ugly'><element/></root>" );
152 list = new ArrayList<ObjectCreationFactoryTestImpl>();
153 digester.push( list );
154 digester.parse( new StringReader( xml ) );
155
156 assertEquals( "List should contain only the factory object", list.size(), 1 );
157 factory = list.get( 0 );
158 assertEquals( "Object create not called(3)[" + ignoreCreateExceptions + "]", factory.called, true );
159 assertEquals( "Attribute not passed (7)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "one" ),
160 "good" );
161 assertEquals( "Attribute not passed (8)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "two" ),
162 "bad" );
163 assertEquals( "Attribute not passed (8)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "three" ),
164 "ugly" );
165
166 digester = new Digester();
167 digester.addFactoryCreate( "root", "org.apache.commons.digester3.ObjectCreationFactoryTestImpl", "override",
168 ignoreCreateExceptions );
169 digester.addSetNext( "root", "add" );
170 xml =
171 new String( "<?xml version='1.0' ?><root one='good' two='bad' three='ugly' "
172 + " override='org.apache.commons.digester3.OtherTestObjectCreationFactory' >" + "<element/></root>" );
173 list = new ArrayList<ObjectCreationFactoryTestImpl>();
174 digester.push( list );
175 digester.parse( new StringReader( xml ) );
176
177 assertEquals( "List should contain only the factory object", list.size(), 1 );
178 factory = list.get( 0 );
179 assertEquals( "Attribute Override Failed (1)", factory.getClass().getName(),
180 "org.apache.commons.digester3.OtherTestObjectCreationFactory" );
181 assertEquals( "Object create not called(4)[" + ignoreCreateExceptions + "]", factory.called, true );
182 assertEquals( "Attribute not passed (10)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "one" ),
183 "good" );
184 assertEquals( "Attribute not passed (11)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "two" ),
185 "bad" );
186 assertEquals( "Attribute not passed (12)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "three" ),
187 "ugly" );
188
189 digester = new Digester();
190 digester.addFactoryCreate( "root", ObjectCreationFactoryTestImpl.class, "override", ignoreCreateExceptions );
191 digester.addSetNext( "root", "add" );
192 xml = new String( "<?xml version='1.0' ?><root one='good' two='bad' three='ugly'><element/></root>" );
193 list = new ArrayList<ObjectCreationFactoryTestImpl>();
194 digester.push( list );
195 digester.parse( new StringReader( xml ) );
196
197 assertEquals( "List should contain only the factory object", list.size(), 1 );
198 factory = list.get( 0 );
199 assertEquals( "Object create not called(5)[" + ignoreCreateExceptions + "]", factory.called, true );
200 assertEquals( "Attribute not passed (13)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "one" ),
201 "good" );
202 assertEquals( "Attribute not passed (14)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "two" ),
203 "bad" );
204 assertEquals( "Attribute not passed (15)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "three" ),
205 "ugly" );
206
207 digester = new Digester();
208 digester.addFactoryCreate( "root", ObjectCreationFactoryTestImpl.class, "override", ignoreCreateExceptions );
209 digester.addSetNext( "root", "add" );
210 xml =
211 new String( "<?xml version='1.0' ?><root one='good' two='bad' three='ugly' "
212 + " override='org.apache.commons.digester3.OtherTestObjectCreationFactory' >" + "<element/></root>" );
213 list = new ArrayList<ObjectCreationFactoryTestImpl>();
214 digester.push( list );
215 digester.parse( new StringReader( xml ) );
216
217 assertEquals( "List should contain only the factory object", list.size(), 1 );
218 factory = list.get( 0 );
219 assertEquals( "Attribute Override Failed (2)", factory.getClass().getName(),
220 "org.apache.commons.digester3.OtherTestObjectCreationFactory" );
221 assertEquals( "Object create not called(6)[" + ignoreCreateExceptions + "]", factory.called, true );
222 assertEquals( "Attribute not passed (16)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "one" ),
223 "good" );
224 assertEquals( "Attribute not passed (17)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "two" ),
225 "bad" );
226 assertEquals( "Attribute not passed (18)[" + ignoreCreateExceptions + "]", factory.attributes.getValue( "three" ),
227 "ugly" );
228 }
229 }