1 17 18 19 package org.apache.commons.digester; 20 21 22 import java.io.IOException ; 23 import java.io.Reader ; 24 import java.io.StringReader ; 25 import java.lang.reflect.InvocationTargetException ; 26 import java.util.ArrayList ; 27 import java.util.List ; 28 29 import junit.framework.Test; 30 import junit.framework.TestCase; 31 import junit.framework.TestSuite; 32 33 import org.xml.sax.SAXException ; 34 35 36 41 public class BeanPropertySetterRuleTestCase extends TestCase { 42 43 44 46 49 protected final static String TEST_XML = 50 "<?xml version='1.0'?>" + 51 "<root>ROOT BODY" + 52 "<alpha>ALPHA BODY</alpha>" + 53 "<beta>BETA BODY</beta>" + 54 "<gamma>GAMMA BODY</gamma>" + 55 "<delta>DELTA BODY</delta>" + 56 "</root>"; 57 58 59 62 protected Digester digester = null; 63 64 65 67 68 73 public BeanPropertySetterRuleTestCase(String name) { 74 75 super(name); 76 77 } 78 79 80 82 83 86 public void setUp() { 87 88 digester = new Digester(); 89 90 } 91 92 93 96 public static Test suite() { 97 98 return (new TestSuite(BeanPropertySetterRuleTestCase.class)); 99 100 } 101 102 103 106 public void tearDown() { 107 108 digester = null; 109 110 } 111 112 113 114 116 117 121 public void testDigesterRuleCallOrder() throws SAXException , IOException { 122 123 List callOrder = new ArrayList (); 124 125 digester.setRules(new RulesBase()); 127 128 TestRule firstRule = new TestRule("first"); 130 firstRule.setOrder(callOrder); 131 digester.addRule("root/alpha", firstRule); 132 133 TestRule secondRule = new TestRule("second"); 135 secondRule.setOrder(callOrder); 136 digester.addRule("root/alpha", secondRule); 137 138 TestRule thirdRule = new TestRule("third"); 140 thirdRule.setOrder(callOrder); 141 digester.addRule("root/alpha", thirdRule); 142 143 144 digester.parse(xmlTestReader()); 145 146 148 assertEquals( 149 "Nine calls should have been made.", 150 9, 151 callOrder.size()); 152 153 assertEquals( 155 "First rule begin not called first.", 156 "first", 157 ((TestRule) callOrder.get(0)).getIdentifier()); 158 159 assertEquals( 160 "Second rule begin not called second.", 161 "second", 162 ((TestRule) callOrder.get(1)).getIdentifier()); 163 164 assertEquals( 165 "Third rule begin not called third.", 166 "third", 167 ((TestRule) callOrder.get(2)).getIdentifier()); 168 169 assertEquals( 171 "First rule body text not called first.", 172 "first", 173 ((TestRule) callOrder.get(3)).getIdentifier()); 174 175 assertEquals( 176 "Second rule body text not called second.", 177 "second", 178 ((TestRule) callOrder.get(4)).getIdentifier()); 179 180 assertEquals( 181 "Third rule body text not called third.", 182 "third", 183 ((TestRule) callOrder.get(5)).getIdentifier()); 184 185 assertEquals( 187 "Third rule end not called first.", 188 "third", 189 ((TestRule) callOrder.get(6)).getIdentifier()); 190 191 assertEquals( 192 "Second rule end not called second.", 193 "second", 194 ((TestRule) callOrder.get(7)).getIdentifier()); 195 196 assertEquals( 197 "First rule end not called third.", 198 "first", 199 ((TestRule) callOrder.get(8)).getIdentifier()); 200 201 202 } 203 204 205 209 public void testDigesterBodyTextStack() throws SAXException , IOException { 210 211 digester.setRules(new RulesBase()); 213 214 TestRule rootRule = new TestRule("root"); 216 digester.addRule("root", rootRule); 217 218 TestRule alphaRule = new TestRule("root/alpha"); 220 digester.addRule("root/alpha", alphaRule); 221 222 TestRule betaRule = new TestRule("root/beta"); 224 digester.addRule("root/beta", betaRule); 225 226 TestRule gammaRule = new TestRule("root/gamma"); 228 digester.addRule("root/gamma", gammaRule); 229 230 digester.parse(xmlTestReader()); 231 232 assertEquals( 233 "Root body text not set correct.", 234 "ROOT BODY", 235 rootRule.getBodyText()); 236 237 assertEquals( 238 "Alpha body text not set correct.", 239 "ALPHA BODY", 240 alphaRule.getBodyText()); 241 242 assertEquals( 243 "Beta body text not set correct.", 244 "BETA BODY", 245 betaRule.getBodyText()); 246 247 assertEquals( 248 "Gamma body text not set correct.", 249 "GAMMA BODY", 250 gammaRule.getBodyText()); 251 252 } 253 254 255 258 public void testSetGivenProperty() throws SAXException , IOException { 259 260 digester.setRules(new RulesBase()); 262 263 digester.addObjectCreate("root", 265 "org.apache.commons.digester.SimpleTestBean"); 266 267 digester.addRule("root", new BeanPropertySetterRule("alpha")); 269 270 digester.addRule("root/alpha", new BeanPropertySetterRule("beta")); 272 273 275 digester.addRule("root/delta", new BeanPropertySetterRule("delta")); 277 278 SimpleTestBean bean = (SimpleTestBean) digester.parse(xmlTestReader()); 279 280 assertEquals( 282 "Property alpha not set correctly", 283 "ROOT BODY", 284 bean.getAlpha()); 285 286 assertEquals( 287 "Property beta not set correctly", 288 "ALPHA BODY", 289 bean.getBeta()); 290 291 assertTrue( 292 "Property gamma not set correctly", 293 bean.getGamma() == null); 294 295 assertEquals("Property delta not set correctly", 296 "DELTA BODY", 297 bean.getDeltaValue()); 298 299 300 } 301 302 303 306 public void testSetUnknownProperty() { 307 308 digester.addObjectCreate("root", 310 "org.apache.commons.digester.SimpleTestBean"); 311 312 digester.addRule("root/alpha", 314 new BeanPropertySetterRule("unknown")); 315 316 try { 318 SimpleTestBean bean = (SimpleTestBean) 319 digester.parse(xmlTestReader()); 320 fail("Should have thrown NoSuchMethodException"); 321 } catch (Exception e) { 322 if (e instanceof InvocationTargetException ) { 323 Throwable t = 324 ((InvocationTargetException ) e).getTargetException(); 325 if (t instanceof NoSuchMethodException ) { 326 ; } else { 328 fail("Should have thrown NoSuchMethodException, threw " + t); 329 } 330 } 331 } 332 333 } 334 335 336 339 public void testAutomaticallySetProperties() 340 throws SAXException , IOException { 341 342 digester.setRules(new ExtendedBaseRules()); 344 345 digester.addObjectCreate("root", 347 "org.apache.commons.digester.SimpleTestBean"); 348 349 digester.addRule("root/?", new BeanPropertySetterRule()); 351 352 SimpleTestBean bean = (SimpleTestBean) digester.parse(xmlTestReader()); 353 354 assertEquals( 356 "Property alpha not set correctly", 357 "ALPHA BODY", 358 bean.getAlpha()); 359 360 assertEquals( 361 "Property beta not set correctly", 362 "BETA BODY", 363 bean.getBeta()); 364 365 assertEquals( 366 "Property gamma not set correctly", 367 "GAMMA BODY", 368 bean.getGamma()); 369 370 371 } 372 373 376 private Reader xmlTestReader() throws IOException { 377 return new StringReader (TEST_XML); 378 } 379 380 } 381 382 383 | Popular Tags |