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 26 import junit.framework.Test; 27 import junit.framework.TestCase; 28 import junit.framework.TestSuite; 29 30 import org.xml.sax.SAXException ; 31 32 33 38 public class SetNestedPropertiesRuleTestCase extends TestCase { 39 40 41 43 46 protected final static String TEST_XML = 47 "<?xml version='1.0'?>" + 48 "<root>ROOT BODY" + 49 "<alpha>ALPHA BODY</alpha>" + 50 "<beta>BETA BODY</beta>" + 51 "<gamma>GAMMA BODY</gamma>" + 52 "<delta>DELTA BODY</delta>" + 53 "</root>"; 54 55 56 59 protected Digester digester = null; 60 61 62 64 65 70 public SetNestedPropertiesRuleTestCase(String name) { 71 72 super(name); 73 74 } 75 76 77 79 80 83 public void setUp() { 84 85 digester = new Digester(); 86 87 } 88 89 90 93 public static Test suite() { 94 95 return (new TestSuite(SetNestedPropertiesRuleTestCase.class)); 96 97 } 98 99 100 103 public void tearDown() { 104 105 digester = null; 106 107 } 108 109 110 111 113 114 117 public void testAutomaticallySetProperties() 118 throws SAXException , IOException { 119 120 digester.addObjectCreate("root", 122 "org.apache.commons.digester.SimpleTestBean"); 123 124 digester.addRule("root", new SetNestedPropertiesRule()); 126 127 SimpleTestBean bean = (SimpleTestBean) digester.parse(xmlTestReader()); 128 129 assertEquals( 131 "Property alpha not set correctly", 132 "ALPHA BODY", 133 bean.getAlpha()); 134 135 assertEquals( 136 "Property beta not set correctly", 137 "BETA BODY", 138 bean.getBeta()); 139 140 assertEquals( 141 "Property gamma not set correctly", 142 "GAMMA BODY", 143 bean.getGamma()); 144 145 assertEquals( 146 "Property delta not set correctly", 147 "DELTA BODY", 148 bean.getDeltaValue()); 149 } 150 151 155 public void testMandatoryProperties() 156 throws SAXException , IOException { 157 158 String TEST_XML = 159 "<?xml version='1.0'?>" + 160 "<root>ROOT BODY" + 161 "<badprop>ALPHA BODY</badprop>" + 162 "</root>"; 163 164 digester.addObjectCreate("root", 166 "org.apache.commons.digester.SimpleTestBean"); 167 168 digester.addRule("root", new SetNestedPropertiesRule()); 170 171 try { 172 SimpleTestBean bean = (SimpleTestBean) digester.parse( 173 new StringReader (TEST_XML)); 174 175 fail("No exception thrown by parse when unknown child element found."); 177 } catch(org.xml.sax.SAXParseException e) { 178 String msg = e.getMessage(); 179 if (msg.indexOf("badprop") >= 0) { 180 } else { 183 fail("Unexpected parse exception:" + e.getMessage()); 184 } 185 } 186 } 187 188 192 public void testCustomisedProperties1() 193 throws SAXException , IOException { 194 195 String TEST_XML = 196 "<?xml version='1.0'?>" + 197 "<root>ROOT BODY" + 198 "<alpha>ALPHA BODY</alpha>" + 199 "<beta>BETA BODY</beta>" + 200 "<gamma-alt>GAMMA BODY</gamma-alt>" + 201 "<delta>DELTA BODY</delta>" + 202 "</root>"; 203 204 digester.addObjectCreate("root", 206 "org.apache.commons.digester.SimpleTestBean"); 207 208 213 Rule rule = new SetNestedPropertiesRule( 214 new String []{"alpha", "gamma-alt", "delta"}, 215 new String []{null, "gamma"}); 216 217 digester.addRule("root", rule); 218 219 SimpleTestBean bean = (SimpleTestBean) digester.parse( 220 new StringReader (TEST_XML)); 221 222 assertEquals( 224 "Property alpha was not ignored (it should be)", 225 null, 226 bean.getAlpha()); 227 228 assertEquals( 229 "Property beta not set correctly", 230 "BETA BODY", 231 bean.getBeta()); 232 233 assertEquals( 234 "Property gamma not set correctly", 235 "GAMMA BODY", 236 bean.getGamma()); 237 238 assertEquals( 239 "Property delta was not ignored (it should be)", 240 null, 241 bean.getDeltaValue()); 242 243 assertEquals( 245 "Digester rules object not reset.", 246 RulesBase.class, digester.getRules().getClass()); 247 } 248 249 253 public void testCustomisedProperties2a() 254 throws SAXException , IOException { 255 256 String TEST_XML = 257 "<?xml version='1.0'?>" + 258 "<root>ROOT BODY" + 259 "<alpha>ALPHA BODY</alpha>" + 260 "<beta>BETA BODY</beta>" + 261 "<gamma>GAMMA BODY</gamma>" + 262 "<delta>DELTA BODY</delta>" + 263 "</root>"; 264 265 digester.addObjectCreate("root", 267 "org.apache.commons.digester.SimpleTestBean"); 268 269 Rule rule = new SetNestedPropertiesRule("alpha", null); 271 digester.addRule("root", rule); 272 273 SimpleTestBean bean = (SimpleTestBean) digester.parse( 274 new StringReader (TEST_XML)); 275 276 assertEquals( 278 "Property alpha was not ignored (it should be)", 279 null, 280 bean.getAlpha()); 281 282 assertEquals( 283 "Property beta not set correctly", 284 "BETA BODY", 285 bean.getBeta()); 286 287 assertEquals( 288 "Property gamma not set correctly", 289 "GAMMA BODY", 290 bean.getGamma()); 291 292 assertEquals( 293 "Property delta not set correctly", 294 "DELTA BODY", 295 bean.getDeltaValue()); 296 297 assertEquals( 299 "Digester rules object not reset.", 300 RulesBase.class, digester.getRules().getClass()); 301 } 302 303 307 public void testCustomisedProperties2b() 308 throws SAXException , IOException { 309 310 String TEST_XML = 311 "<?xml version='1.0'?>" + 312 "<root>ROOT BODY" + 313 "<alpha-alt>ALPHA BODY</alpha-alt>" + 314 "<beta>BETA BODY</beta>" + 315 "<gamma>GAMMA BODY</gamma>" + 316 "<delta>DELTA BODY</delta>" + 317 "</root>"; 318 319 digester.addObjectCreate("root", 321 "org.apache.commons.digester.SimpleTestBean"); 322 323 Rule rule = new SetNestedPropertiesRule("alpha-alt", "alpha"); 326 digester.addRule("root", rule); 327 328 SimpleTestBean bean = (SimpleTestBean) digester.parse( 329 new StringReader (TEST_XML)); 330 331 assertEquals( 333 "Property alpha not set correctly", 334 "ALPHA BODY", 335 bean.getAlpha()); 336 337 assertEquals( 338 "Property beta not set correctly", 339 "BETA BODY", 340 bean.getBeta()); 341 342 assertEquals( 343 "Property gamma not set correctly", 344 "GAMMA BODY", 345 bean.getGamma()); 346 347 assertEquals( 348 "Property delta not set correctly", 349 "DELTA BODY", 350 bean.getDeltaValue()); 351 352 assertEquals( 354 "Digester rules object not reset.", 355 RulesBase.class, digester.getRules().getClass()); 356 } 357 358 367 public void testMultiRuleMatch() 368 throws SAXException , IOException { 369 370 String testXml = 371 "<?xml version='1.0'?>" + 372 "<root>" + 373 "<testbean alpha='alpha-attr'>ROOT BODY" + 374 "<beta>BETA BODY</beta>" + 375 "<gamma>GAMMA " + 376 "<prop name='delta' value='delta-prop'/>" + 377 "BODY" + 378 "</gamma>" + 379 "</testbean>" + 380 "</root>"; 381 382 Reader reader = new StringReader (testXml); 383 384 digester.addObjectCreate("root/testbean", 386 "org.apache.commons.digester.SimpleTestBean"); 387 388 digester.addRule("root/testbean", new SetNestedPropertiesRule()); 389 digester.addSetProperties("root/testbean"); 390 digester.addSetProperty("root/testbean/gamma/prop", "name", "value"); 391 392 SimpleTestBean bean = (SimpleTestBean) digester.parse(reader); 393 394 assertNotNull("No object created", bean); 395 396 assertEquals( 398 "Property alpha not set correctly", 399 "alpha-attr", 400 bean.getAlpha()); 401 402 assertEquals( 403 "Property beta not set correctly", 404 "BETA BODY", 405 bean.getBeta()); 406 407 assertEquals( 408 "Property gamma not set correctly", 409 "GAMMA BODY", 410 bean.getGamma()); 411 412 assertEquals( 413 "Property delta not set correctly", 414 "delta-prop", 415 bean.getDeltaValue()); 416 417 assertEquals( 419 "Digester rules object not reset.", 420 RulesBase.class, digester.getRules().getClass()); 421 } 422 423 426 public void testUnknownChildrenCausesException() 427 throws SAXException , IOException { 428 429 String testXml = 430 "<?xml version='1.0'?>" + 431 "<root>" + 432 "<testbean>" + 433 "<beta>BETA BODY</beta>" + 434 "<foo>GAMMA</foo>" + 435 "</testbean>" + 436 "</root>"; 437 438 Reader reader = new StringReader (testXml); 439 440 digester.addObjectCreate("root", 442 "org.apache.commons.digester.SimpleTestBean"); 443 444 Rule rule = new SetNestedPropertiesRule(); 445 digester.addRule("root", rule); 446 447 try { 448 SimpleTestBean bean = (SimpleTestBean) digester.parse(reader); 449 fail("Expected to generate an exception."); 450 } catch(SAXException e) { 451 Exception nested = e.getException(); 452 if ((nested==null) || !(nested instanceof NoSuchMethodException )) { 453 throw e; 455 } 456 } 457 } 458 459 463 public void testUnknownChildrenExceptionOverride() 464 throws SAXException , IOException { 465 466 String testXml = 467 "<?xml version='1.0'?>" + 468 "<root>" + 469 "<testbean>" + 470 "<beta>BETA BODY</beta>" + 471 "<foo>GAMMA</foo>" + 472 "</testbean>" + 473 "</root>"; 474 475 Reader reader = new StringReader (testXml); 476 477 digester.addObjectCreate("root", 479 "org.apache.commons.digester.SimpleTestBean"); 480 481 SetNestedPropertiesRule rule = new SetNestedPropertiesRule(); 482 rule.setAllowUnknownChildElements(true); 483 digester.addRule("root", rule); 484 485 SimpleTestBean bean = (SimpleTestBean) digester.parse(reader); 486 assertNotNull(bean); 487 } 488 489 495 public void testRecursiveNestedProperties() 496 throws SAXException , IOException { 497 498 String testXml = 499 "<?xml version='1.0'?>" + 500 "<testbean>" + 501 "<beta>BETA BODY</beta>" + 502 "<testbean>" + 503 "<beta>BETA BODY</beta>" + 504 "</testbean>" + 505 "</testbean>"; 506 507 Reader reader = new StringReader (testXml); 508 509 digester.addObjectCreate("*/testbean", 511 "org.apache.commons.digester.SimpleTestBean"); 512 513 SetNestedPropertiesRule rule = new SetNestedPropertiesRule(); 514 rule.setAllowUnknownChildElements(true); 515 digester.addRule("*/testbean", rule); 516 517 SimpleTestBean bean = (SimpleTestBean) digester.parse(reader); 518 assertNotNull(bean); 519 } 520 521 522 525 private Reader xmlTestReader() throws IOException { 526 return new StringReader (TEST_XML); 527 } 528 529 } 530 531 532 533 | Popular Tags |