1 17 18 19 package org.apache.commons.digester; 20 21 import java.io.StringReader ; 22 import java.util.ArrayList ; 23 import java.util.List ; 24 25 import junit.framework.Test; 26 import junit.framework.TestCase; 27 import junit.framework.TestSuite; 28 29 import org.xml.sax.Attributes ; 30 31 36 37 public class TestFactoryCreate extends TestCase { 38 39 public TestFactoryCreate(String name) { 40 super(name); 41 } 42 43 44 public void setUp() {} 45 46 47 public static Test suite() { 48 49 return (new TestSuite(TestFactoryCreate.class)); 50 51 } 52 53 54 public void tearDown() {} 55 56 58 59 60 public void testPropagateException() throws Exception { 61 62 class ThrowExceptionCreateRule extends AbstractObjectCreationFactory { 64 public Object createObject(Attributes attributes) throws Exception { 65 throw new RuntimeException (); 66 } 67 } 68 69 70 String xml = "<?xml version='1.0' ?><root><element/></root>"; 72 73 Digester digester = new Digester(); 75 digester.addFactoryCreate("root", new ThrowExceptionCreateRule()); 76 try { 77 78 digester.parse(new StringReader (xml)); 79 fail("Exception not propagated from create rule (1)"); 80 81 } catch (Exception e) { 82 83 } 84 85 digester = new Digester(); 87 digester.addFactoryCreate("root", new ThrowExceptionCreateRule(), false); 88 try { 89 90 digester.parse(new StringReader (xml)); 91 fail("Exception not propagated from create rule (1)"); 92 93 } catch (Exception e) { 94 95 } 96 97 digester = new Digester(); 99 digester.addFactoryCreate("root", new ThrowExceptionCreateRule(), true); 100 try { 101 102 digester.parse(new StringReader (xml)); 103 104 } catch (Exception e) { 105 fail("Exception should not be propagated"); 107 } 108 } 109 110 public void testFactoryCreateRule() throws Exception { 111 tryVariations(true); 112 tryVariations(false); 113 } 114 115 private void tryVariations(boolean propagateExceptions) throws Exception { 116 117 118 Digester digester = new Digester(); 120 TestObjectCreationFactory factory = new TestObjectCreationFactory(); 121 digester.addFactoryCreate("root", factory, propagateExceptions); 122 String xml = new String ( 123 "<?xml version='1.0' ?><root one='good' two='bad' three='ugly'><element/></root>"); 124 digester.parse(new StringReader (xml)); 125 126 assertEquals("Object create not called(1)[" + propagateExceptions + "]", factory.called , true); 127 assertEquals( 128 "Attribute not passed (1)[" + propagateExceptions + "]", 129 factory.attributes.getValue("one"), 130 "good"); 131 assertEquals( 132 "Attribute not passed (2)[" + propagateExceptions + "]", 133 factory.attributes.getValue("two"), 134 "bad"); 135 assertEquals( 136 "Attribute not passed (3)[" + propagateExceptions + "]", 137 factory.attributes.getValue("three"), 138 "ugly"); 139 140 digester = new Digester(); 141 digester.addFactoryCreate( 142 "root", 143 "org.apache.commons.digester.TestObjectCreationFactory", 144 propagateExceptions); 145 digester.addSetNext("root", "add"); 146 xml = new String ( 147 "<?xml version='1.0' ?><root one='good' two='bad' three='ugly'><element/></root>"); 148 List list = new ArrayList (); 149 digester.push(list); 150 digester.parse(new StringReader (xml)); 151 152 assertEquals("List should contain only the factory object", list.size() , 1); 153 factory = (TestObjectCreationFactory) list.get(0); 154 assertEquals("Object create not called(2)[" + propagateExceptions + "]", factory.called , true); 155 assertEquals( 156 "Attribute not passed (4)[" + propagateExceptions + "]", 157 factory.attributes.getValue("one"), 158 "good"); 159 assertEquals( 160 "Attribute not passed (5)[" + propagateExceptions + "]", 161 factory.attributes.getValue("two"), 162 "bad"); 163 assertEquals( 164 "Attribute not passed (6)[" + propagateExceptions + "]", 165 factory.attributes.getValue("three"), 166 "ugly"); 167 168 169 digester = new Digester(); 170 digester.addFactoryCreate( 171 "root", 172 "org.apache.commons.digester.TestObjectCreationFactory", 173 "override", 174 propagateExceptions); 175 digester.addSetNext("root", "add"); 176 xml = new String ( 177 "<?xml version='1.0' ?><root one='good' two='bad' three='ugly'><element/></root>"); 178 list = new ArrayList (); 179 digester.push(list); 180 digester.parse(new StringReader (xml)); 181 182 assertEquals("List should contain only the factory object", list.size() , 1); 183 factory = (TestObjectCreationFactory) list.get(0); 184 assertEquals("Object create not called(3)[" + propagateExceptions + "]", factory.called , true); 185 assertEquals( 186 "Attribute not passed (7)[" + propagateExceptions + "]", 187 factory.attributes.getValue("one"), 188 "good"); 189 assertEquals( 190 "Attribute not passed (8)[" + propagateExceptions + "]", 191 factory.attributes.getValue("two"), 192 "bad"); 193 assertEquals( 194 "Attribute not passed (8)[" + propagateExceptions + "]", 195 factory.attributes.getValue("three"), 196 "ugly"); 197 198 digester = new Digester(); 199 digester.addFactoryCreate( 200 "root", 201 "org.apache.commons.digester.TestObjectCreationFactory", 202 "override", 203 propagateExceptions); 204 digester.addSetNext("root", "add"); 205 xml = new String ( 206 "<?xml version='1.0' ?><root one='good' two='bad' three='ugly' " 207 + " override='org.apache.commons.digester.OtherTestObjectCreationFactory' >" 208 + "<element/></root>"); 209 list = new ArrayList (); 210 digester.push(list); 211 digester.parse(new StringReader (xml)); 212 213 assertEquals("List should contain only the factory object", list.size() , 1); 214 factory = (TestObjectCreationFactory) list.get(0); 215 assertEquals( 216 "Attribute Override Failed (1)", 217 factory.getClass().getName() , 218 "org.apache.commons.digester.OtherTestObjectCreationFactory"); 219 assertEquals("Object create not called(4)[" + propagateExceptions + "]", factory.called , true); 220 assertEquals( 221 "Attribute not passed (10)[" + propagateExceptions + "]", 222 factory.attributes.getValue("one"), 223 "good"); 224 assertEquals( 225 "Attribute not passed (11)[" + propagateExceptions + "]", 226 factory.attributes.getValue("two"), 227 "bad"); 228 assertEquals( 229 "Attribute not passed (12)[" + propagateExceptions + "]", 230 factory.attributes.getValue("three"), 231 "ugly"); 232 233 digester = new Digester(); 234 digester.addFactoryCreate( 235 "root", 236 TestObjectCreationFactory.class, 237 "override", 238 propagateExceptions); 239 digester.addSetNext("root", "add"); 240 xml = new String ( 241 "<?xml version='1.0' ?><root one='good' two='bad' three='ugly'><element/></root>"); 242 list = new ArrayList (); 243 digester.push(list); 244 digester.parse(new StringReader (xml)); 245 246 assertEquals("List should contain only the factory object", list.size() , 1); 247 factory = (TestObjectCreationFactory) list.get(0); 248 assertEquals("Object create not called(5)[" + propagateExceptions + "]", factory.called , true); 249 assertEquals( 250 "Attribute not passed (13)[" + propagateExceptions + "]", 251 factory.attributes.getValue("one"), 252 "good"); 253 assertEquals( 254 "Attribute not passed (14)[" + propagateExceptions + "]", 255 factory.attributes.getValue("two"), 256 "bad"); 257 assertEquals( 258 "Attribute not passed (15)[" + propagateExceptions + "]", 259 factory.attributes.getValue("three"), 260 "ugly"); 261 262 digester = new Digester(); 263 digester.addFactoryCreate( 264 "root", 265 TestObjectCreationFactory.class, 266 "override", 267 propagateExceptions); 268 digester.addSetNext("root", "add"); 269 xml = new String ( 270 "<?xml version='1.0' ?><root one='good' two='bad' three='ugly' " 271 + " override='org.apache.commons.digester.OtherTestObjectCreationFactory' >" 272 + "<element/></root>"); 273 list = new ArrayList (); 274 digester.push(list); 275 digester.parse(new StringReader (xml)); 276 277 assertEquals("List should contain only the factory object", list.size() , 1); 278 factory = (TestObjectCreationFactory) list.get(0); 279 assertEquals( 280 "Attribute Override Failed (2)", 281 factory.getClass().getName() , 282 "org.apache.commons.digester.OtherTestObjectCreationFactory"); 283 assertEquals("Object create not called(6)[" + propagateExceptions + "]", factory.called , true); 284 assertEquals( 285 "Attribute not passed (16)[" + propagateExceptions + "]", 286 factory.attributes.getValue("one"), 287 "good"); 288 assertEquals( 289 "Attribute not passed (17)[" + propagateExceptions + "]", 290 factory.attributes.getValue("two"), 291 "bad"); 292 assertEquals( 293 "Attribute not passed (18)[" + propagateExceptions + "]", 294 factory.attributes.getValue("three"), 295 "ugly"); 296 } 297 } 298 | Popular Tags |