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.apache.commons.digester3.binder.DigesterLoader.newLoader;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.fail;
26
27 import java.io.Reader;
28 import java.io.StringReader;
29
30 import org.apache.commons.digester3.binder.AbstractRulesModule;
31 import org.junit.Test;
32 import org.xml.sax.SAXException;
33
34
35
36
37
38
39 public class SetPropertiesRuleTestCase
40 {
41
42
43
44
45
46
47 protected final static String TEST_XML_1 =
48 "<?xml version='1.0'?><root alpha='ALPHA VALUE' beta='BETA VALUE' delta='DELTA VALUE'/>";
49
50
51
52
53 protected final static String TEST_XML_2 =
54 "<?xml version='1.0'?><root alpa='ALPA VALUE' beta='BETA VALUE' delta='DELTA VALUE'/>";
55
56
57
58
59 protected final static String TEST_XML_3 =
60 "<?xml version='1.0'?><root alpha='ALPHA VALUE' beta='BETA VALUE' delta='DELTA VALUE' ignore='ignore value'/>";
61
62
63
64
65
66
67 @Test
68 public void testPositive()
69 throws Exception
70 {
71 Digester digester = newLoader( new AbstractRulesModule()
72 {
73
74 @Override
75 protected void configure()
76 {
77 forPattern( "root" ).createObject().ofType( "org.apache.commons.digester3.SimpleTestBean" )
78 .then()
79 .setProperties();
80 }
81
82 }).newDigester();
83
84
85 SimpleTestBean bean = digester.parse( xmlTestReader( TEST_XML_1 ) );
86
87
88 assertEquals( "alpha property set", "ALPHA VALUE", bean.getAlpha() );
89 assertEquals( "beta property set", "BETA VALUE", bean.getBeta() );
90 assertNull( "gamma property not set", bean.getGamma() );
91 assertEquals( "delta property set", "DELTA VALUE", bean.getDeltaValue() );
92
93 }
94
95
96
97
98 @Test
99 public void testIgnoreMissing()
100 throws Exception
101 {
102 Digester digester = newLoader( new AbstractRulesModule()
103 {
104
105 @Override
106 protected void configure()
107 {
108 forPattern( "root" ).createObject().ofType( "org.apache.commons.digester3.SimpleTestBean" )
109 .then()
110 .setProperties();
111 }
112
113 }).newDigester();
114
115
116 SimpleTestBean bean = digester.parse( xmlTestReader( TEST_XML_2 ) );
117
118
119 assertNull( "alpha property not set", bean.getAlpha() );
120 assertEquals( "beta property set", "BETA VALUE", bean.getBeta() );
121 assertNull( "gamma property not set", bean.getGamma() );
122 assertEquals( "delta property set", "DELTA VALUE", bean.getDeltaValue() );
123
124 }
125
126
127
128
129 @Test
130 public void testNegativeNotIgnoreMissing()
131 throws Exception
132 {
133 Digester digester = newLoader( new AbstractRulesModule()
134 {
135
136 @Override
137 protected void configure()
138 {
139 forPattern( "root" ).createObject().ofType( "org.apache.commons.digester3.SimpleTestBean" )
140 .then()
141 .setProperties().ignoreMissingProperty( false );
142 }
143
144 }).newDigester();
145
146 try
147 {
148
149 SimpleTestBean bean = digester.parse( xmlTestReader( TEST_XML_2 ) );
150 fail( "Should have thrown NoSuchMethodException" );
151 assertNotNull( bean );
152 }
153 catch ( Exception e )
154 {
155 if ( e instanceof NoSuchMethodException )
156 {
157
158 }
159 else if ( e instanceof SAXException )
160 {
161 Exception ee = ( (SAXException) e ).getException();
162 if ( ee != null )
163 {
164 if ( ee instanceof NoSuchMethodException )
165 {
166
167 }
168 else
169 {
170 fail( "Should have thrown SE->NoSuchMethodException, threw " + ee );
171 }
172 }
173 else
174 {
175 fail( "Should have thrown NoSuchMethodException, threw " + e.getClass().getName() );
176 }
177 }
178 else
179 {
180 fail( "Should have thrown NoSuchMethodException, threw " + e );
181 }
182 }
183 }
184
185
186
187
188 @Test
189 public void testPositiveNotIgnoreMissingWithIgnoreAttributes()
190 throws Exception
191 {
192 Digester digester = newLoader( new AbstractRulesModule()
193 {
194
195 @Override
196 protected void configure()
197 {
198 forPattern( "root" ).createObject().ofType( "org.apache.commons.digester3.SimpleTestBean" )
199 .then()
200 .setProperties()
201 .addAlias( "ignore" ).forProperty( null )
202 .ignoreMissingProperty( false );
203 }
204
205 }).newDigester();
206
207
208 SimpleTestBean bean = digester.parse( xmlTestReader( TEST_XML_3 ) );
209
210
211 assertEquals( "alpha property set", "ALPHA VALUE", bean.getAlpha() );
212 assertEquals( "beta property set", "BETA VALUE", bean.getBeta() );
213 assertNull( "gamma property not set", bean.getGamma() );
214 assertEquals( "delta property set", "DELTA VALUE", bean.getDeltaValue() );
215 }
216
217
218
219
220 private Reader xmlTestReader( String xml )
221 {
222 return new StringReader( xml );
223 }
224
225 }