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 27 import junit.framework.Test; 28 import junit.framework.TestCase; 29 import junit.framework.TestSuite; 30 31 import org.xml.sax.SAXException ; 32 33 34 37 public class SetPropertyRuleTestCase extends TestCase { 38 39 40 42 45 protected final static String TEST_XML_1 = 46 "<?xml version='1.0'?><root>" + 47 "<set name='alpha' value='ALPHA VALUE'/>" + 48 "<set name='beta' value='BETA VALUE'/>" + 49 "<set name='delta' value='DELTA VALUE'/>" + 50 "</root>"; 51 52 55 protected final static String TEST_XML_2 = 56 "<?xml version='1.0'?><root>" + 57 "<set name='unknown' value='UNKNOWN VALUE'/>" + 58 "</root>"; 59 60 61 64 protected Digester digester = null; 65 66 67 69 70 75 public SetPropertyRuleTestCase(String name) { 76 77 super(name); 78 79 } 80 81 82 84 85 88 public void setUp() { 89 90 digester = new Digester(); 91 92 } 93 94 95 98 public static Test suite() { 99 100 return (new TestSuite(SetPropertyRuleTestCase.class)); 101 102 } 103 104 105 108 public void tearDown() { 109 110 digester = null; 111 112 } 113 114 115 116 118 119 122 public void testPositive() throws Exception { 123 124 digester.addObjectCreate("root", 126 "org.apache.commons.digester.SimpleTestBean"); 127 digester.addSetProperty("root/set", "name", "value"); 128 129 SimpleTestBean bean = 131 (SimpleTestBean) digester.parse(xmlTestReader(TEST_XML_1)); 132 133 assertEquals("alpha property set", 135 "ALPHA VALUE", 136 bean.getAlpha()); 137 assertEquals("beta property set", 138 "BETA VALUE", 139 bean.getBeta()); 140 assertNull("gamma property not set", 141 bean.getGamma()); 142 assertEquals("delta property set", 143 "DELTA VALUE", 144 bean.getDeltaValue()); 145 146 } 147 148 149 152 public void testNegative() { 153 154 digester.addObjectCreate("root", 156 "org.apache.commons.digester.SimpleTestBean"); 157 digester.addSetProperty("root/set", "name", "value"); 158 159 try { 161 SimpleTestBean bean = 162 (SimpleTestBean) digester.parse(xmlTestReader(TEST_XML_2)); 163 fail("Should have thrown NoSuchMethodException"); 164 } catch (Exception e) { 165 if (e instanceof NoSuchMethodException ) { 166 ; } else if (e instanceof InvocationTargetException ) { 168 Throwable t = 169 ((InvocationTargetException ) e).getTargetException(); 170 if (t instanceof NoSuchMethodException ) { 171 ; } else { 173 fail("Should have thrown ITE->NoSuchMethodException, threw " + t); 174 } 175 } else if (e instanceof SAXException ) { 176 Exception ee = ((SAXException ) e).getException(); 177 if (ee != null) { 178 if (ee instanceof NoSuchMethodException ) { 179 ; } else { 181 fail("Should have thrown SE->NoSuchMethodException, threw " + ee); 182 } 183 } else { 184 fail("Should have thrown NoSuchMethodException, threw " + 185 e.getClass().getName()); 186 } 187 } else { 188 fail("Should have thrown NoSuchMethodException, threw " + e); 189 } 190 } 191 192 } 193 194 195 198 private Reader xmlTestReader(String xml) throws IOException { 199 return new StringReader (xml); 200 } 201 202 } 203 204 205 | Popular Tags |