1 17 18 19 package org.apache.commons.digester.substitution; 20 21 import org.apache.commons.digester.CallMethodRule; 22 import org.apache.commons.digester.Digester; 23 import org.apache.commons.digester.SimpleTestBean; 24 25 import java.io.IOException ; 26 import java.io.StringReader ; 27 import java.util.HashMap ; 28 import java.util.LinkedList ; 29 30 import junit.framework.Test; 31 import junit.framework.TestCase; 32 import junit.framework.TestSuite; 33 34 import org.xml.sax.SAXException ; 35 36 42 43 public class VariableExpansionTestCase extends TestCase { 44 45 47 52 public VariableExpansionTestCase(String name) { 53 54 super(name); 55 56 } 57 58 60 63 public void setUp() { 64 } 65 66 67 70 public static Test suite() { 71 return (new TestSuite(VariableExpansionTestCase.class)); 72 } 73 74 75 78 public void tearDown() { 79 } 80 81 private LinkedList simpleTestBeans = new LinkedList (); 83 public void addSimpleTestBean(SimpleTestBean bean) { 84 simpleTestBeans.add(bean); 85 } 86 87 private HashMap mutableSource=new HashMap (); 90 91 96 public void addProperty(String key, String value) { 97 mutableSource.put(key, value); 98 } 99 100 105 private Digester createDigesterThatCanDoAnt() { 106 Digester digester = new Digester(); 107 108 MultiVariableExpander expander = new MultiVariableExpander(); 109 expander.addSource("$", mutableSource); 110 digester.setSubstitutor(new VariableSubstitutor(expander)); 111 112 int useRootObj = -1; 113 Class [] callerArgTypes = new Class [] {String .class, String .class}; 114 CallMethodRule caller = new CallMethodRule(useRootObj, "addProperty", 115 callerArgTypes.length, callerArgTypes); 116 digester.addRule("root/property", caller); 117 digester.addCallParam("root/property", 0, "name"); 118 digester.addCallParam("root/property", 1, "value"); 119 120 digester.addObjectCreate("root/bean", SimpleTestBean.class); 121 digester.addSetProperties("root/bean"); 122 digester.addSetNext("root/bean", "addSimpleTestBean"); 123 return digester; 124 } 125 126 128 131 public void testNoExpansion() throws SAXException , IOException { 132 133 String xml = "<root alpha='${attr1}' beta='var{attr2}'/>"; 134 StringReader input = new StringReader (xml); 135 Digester digester = new Digester(); 136 137 digester.addObjectCreate("root", SimpleTestBean.class); 139 digester.addSetProperties("root"); 140 141 Object root = digester.parse(input); 143 144 assertNotNull("Digester returned no object", root); 145 SimpleTestBean bean = (SimpleTestBean) root; 146 147 assertEquals("${attr1}", bean.getAlpha()); 148 assertEquals("var{attr2}", bean.getBeta()); 149 } 150 151 154 public void testExpansionWithNoSource() throws SAXException , IOException { 155 156 String xml = "<root alpha='${attr1}' beta='var{attr2}'/>"; 157 StringReader input = new StringReader (xml); 158 Digester digester = new Digester(); 159 160 MultiVariableExpander expander = new MultiVariableExpander(); 162 digester.setSubstitutor(new VariableSubstitutor(expander)); 163 digester.addObjectCreate("root", SimpleTestBean.class); 164 digester.addSetProperties("root"); 165 166 Object root = digester.parse(input); 168 169 assertNotNull("Digester returned no object", root); 170 SimpleTestBean bean = (SimpleTestBean) root; 171 172 assertEquals("${attr1}", bean.getAlpha()); 173 assertEquals("var{attr2}", bean.getBeta()); 174 } 175 176 181 public void testExpansionWithMultipleSources() throws SAXException , IOException { 182 183 String xml = 184 "<root>" + 185 "<bean alpha='${attr1}' beta='var{attr1}'/>" + 186 "<bean alpha='${attr2}' beta='var{attr2}'/>" + 187 "</root>"; 188 189 StringReader input = new StringReader (xml); 190 Digester digester = new Digester(); 191 192 HashMap source1 = new HashMap (); 194 source1.put("attr1", "source1.attr1"); 195 source1.put("attr2", "source1.attr2"); 197 HashMap source2 = new HashMap (); 198 source2.put("attr1", "source2.attr1"); source2.put("attr2", "source2.attr2"); 200 201 202 MultiVariableExpander expander = new MultiVariableExpander(); 203 expander.addSource("$", source1); 204 expander.addSource("var", source2); 205 206 digester.setSubstitutor(new VariableSubstitutor(expander)); 207 digester.addObjectCreate("root/bean", SimpleTestBean.class); 208 digester.addSetProperties("root/bean"); 209 digester.addSetNext("root/bean", "addSimpleTestBean"); 210 211 this.simpleTestBeans.clear(); 213 digester.push(this); 214 digester.parse(input); 215 216 assertEquals(2, this.simpleTestBeans.size()); 217 218 { 219 SimpleTestBean bean = (SimpleTestBean) this.simpleTestBeans.get(0); 220 assertEquals("source1.attr1", bean.getAlpha()); 221 assertEquals("source2.attr1", bean.getBeta()); 222 } 223 224 { 225 SimpleTestBean bean = (SimpleTestBean) this.simpleTestBeans.get(1); 226 assertEquals("source1.attr2", bean.getAlpha()); 227 assertEquals("source2.attr2", bean.getBeta()); 228 } 229 } 230 231 234 public void testBodyExpansion() throws SAXException , IOException { 235 236 String xml = 237 "<root>" + 238 "Twas noun{1} and the noun{2}" + 239 " did verb{1} and verb{2} in the noun{3}" + 240 "</root>"; 241 242 StringReader input = new StringReader (xml); 243 Digester digester = new Digester(); 244 245 HashMap nouns = new HashMap (); 247 nouns.put("1", "brillig"); 248 nouns.put("2", "slithy toves"); 249 nouns.put("3", "wabe"); 250 251 HashMap verbs = new HashMap (); 252 verbs.put("1", "gyre"); 253 verbs.put("2", "gimble"); 254 255 MultiVariableExpander expander = new MultiVariableExpander(); 256 expander.addSource("noun", nouns); 257 expander.addSource("verb", verbs); 258 digester.setSubstitutor(new VariableSubstitutor(expander)); 259 260 digester.addObjectCreate("root", SimpleTestBean.class); 261 digester.addCallMethod("root", "setAlpha", 0); 262 263 Object root = digester.parse(input); 265 266 assertNotNull("Digester returned no object", root); 267 SimpleTestBean bean = (SimpleTestBean) root; 268 269 assertEquals( 270 "Twas brillig and the slithy toves" + 271 " did gyre and gimble in the wabe", 272 bean.getAlpha()); 273 } 274 275 278 public void testExpansionException() throws IOException { 279 280 String xml = "<root alpha='${attr1}'/>"; 281 StringReader input = new StringReader (xml); 282 Digester digester = new Digester(); 283 284 MultiVariableExpander expander = new MultiVariableExpander(); 286 expander.addSource("$", new HashMap ()); 287 digester.setSubstitutor(new VariableSubstitutor(expander)); 288 289 digester.addObjectCreate("root", SimpleTestBean.class); 290 digester.addSetProperties("root"); 291 292 try { 294 digester.parse(input); 295 fail("Exception expected due to unknown variable."); 296 } catch(SAXException e) { 297 } 299 } 300 301 317 public void testExpansionWithMutableSource() throws SAXException , IOException { 318 String xml = 319 "<root>" + 320 "<property name='attr' value='prop.value'/>" + 321 "<bean alpha='${attr}'/>" + 322 "</root>"; 323 StringReader input = new StringReader (xml); 324 Digester digester = createDigesterThatCanDoAnt(); 325 326 simpleTestBeans.clear(); 327 digester.push(this); 328 digester.parse(input); 329 330 assertEquals(1, simpleTestBeans.size()); 331 SimpleTestBean bean = (SimpleTestBean) simpleTestBeans.get(0); 332 assertEquals("prop.value", bean.getAlpha()); 333 } 334 335 349 public void testExpansionOfPropertyInProperty() throws SAXException , IOException { 350 String xml = 351 "<root>" + 352 "<property name='attr1' value='prop.value1'/>" + 353 "<property name='attr2' value='substituted-${attr1}'/>" + 354 "<bean alpha='${attr2}'/>" + 355 "</root>"; 356 StringReader input = new StringReader (xml); 357 Digester digester = createDigesterThatCanDoAnt(); 358 359 simpleTestBeans.clear(); 360 digester.push(this); 361 digester.parse(input); 362 363 assertEquals(1, simpleTestBeans.size()); 364 SimpleTestBean bean = (SimpleTestBean) simpleTestBeans.get(0); 365 assertEquals("substituted-prop.value1", bean.getAlpha()); 366 } 367 368 } 369 | Popular Tags |