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