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 36 public class SetPropertiesRuleTestCase extends TestCase { 37 38 39 41 44 protected final static String TEST_XML_1 = 45 "<?xml version='1.0'?><root alpha='ALPHA VALUE' beta='BETA VALUE' delta='DELTA VALUE'/>"; 46 47 50 protected final static String TEST_XML_2 = 51 "<?xml version='1.0'?><root alpa='ALPA VALUE' beta='BETA VALUE' delta='DELTA VALUE'/>"; 52 53 56 protected final static String TEST_XML_3 = 57 "<?xml version='1.0'?><root alpha='ALPHA VALUE' beta='BETA VALUE' delta='DELTA VALUE' ignore='ignore value'/>"; 58 59 62 protected Digester digester = null; 63 64 65 67 68 73 public SetPropertiesRuleTestCase(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(SetPropertiesRuleTestCase.class)); 99 100 } 101 102 103 106 public void tearDown() { 107 108 digester = null; 109 110 } 111 112 113 114 116 117 120 public void testPositive() throws Exception { 121 122 digester.addObjectCreate("root", 124 "org.apache.commons.digester.SimpleTestBean"); 125 digester.addSetProperties("root"); 126 127 SimpleTestBean bean = 129 (SimpleTestBean) digester.parse(xmlTestReader(TEST_XML_1)); 130 131 assertEquals("alpha property set", 133 "ALPHA VALUE", 134 bean.getAlpha()); 135 assertEquals("beta property set", 136 "BETA VALUE", 137 bean.getBeta()); 138 assertNull("gamma property not set", 139 bean.getGamma()); 140 assertEquals("delta property set", 141 "DELTA VALUE", 142 bean.getDeltaValue()); 143 144 } 145 146 149 public void testIgnoreMissing() throws Exception { 150 151 digester.addObjectCreate("root", 153 "org.apache.commons.digester.SimpleTestBean"); 154 digester.addSetProperties("root"); 155 156 SimpleTestBean bean = 158 (SimpleTestBean) digester.parse(xmlTestReader(TEST_XML_2)); 159 160 assertNull("alpha property not set", 162 bean.getAlpha()); 163 assertEquals("beta property set", 164 "BETA VALUE", 165 bean.getBeta()); 166 assertNull("gamma property not set", 167 bean.getGamma()); 168 assertEquals("delta property set", 169 "DELTA VALUE", 170 bean.getDeltaValue()); 171 172 } 173 174 177 public void testNegativeNotIgnoreMissing() throws Exception { 178 179 digester.addObjectCreate("root", 181 "org.apache.commons.digester.SimpleTestBean"); 182 SetPropertiesRule rule = new SetPropertiesRule(); 183 rule.setIgnoreMissingProperty(false); 184 digester.addRule("root", rule); 185 186 try { 187 SimpleTestBean bean = 189 (SimpleTestBean) digester.parse(xmlTestReader(TEST_XML_2)); 190 fail("Should have thrown NoSuchMethodException"); 191 } catch (Exception e) { 192 if (e instanceof NoSuchMethodException ) { 193 } else if (e instanceof SAXException ) { 195 Exception ee = ((SAXException ) e).getException(); 196 if (ee != null) { 197 if (ee instanceof NoSuchMethodException ) { 198 ; } else { 200 fail("Should have thrown SE->NoSuchMethodException, threw " + ee); 201 } 202 } else { 203 fail("Should have thrown NoSuchMethodException, threw " + 204 e.getClass().getName()); 205 } 206 } else { 207 fail("Should have thrown NoSuchMethodException, threw " + e); 208 } 209 } 210 } 211 212 215 public void testPositiveNotIgnoreMissingWithIgnoreAttributes() throws Exception { 216 217 digester.addObjectCreate("root", 219 "org.apache.commons.digester.SimpleTestBean"); 220 SetPropertiesRule rule = new SetPropertiesRule(new String [] {"ignore"}, new String [] {}); 221 rule.setIgnoreMissingProperty(false); 222 digester.addRule("root", rule); 223 224 SimpleTestBean bean = 226 (SimpleTestBean) digester.parse(xmlTestReader(TEST_XML_3)); 227 228 assertEquals("alpha property set", 230 "ALPHA VALUE", 231 bean.getAlpha()); 232 assertEquals("beta property set", 233 "BETA VALUE", 234 bean.getBeta()); 235 assertNull("gamma property not set", 236 bean.getGamma()); 237 assertEquals("delta property set", 238 "DELTA VALUE", 239 bean.getDeltaValue()); 240 } 241 242 243 246 private Reader xmlTestReader(String xml) throws IOException { 247 return new StringReader (xml); 248 } 249 250 } 251 252 253 | Popular Tags |