1 16 17 package org.apache.commons.chain.config; 18 19 import junit.framework.Test; 20 import junit.framework.TestCase; 21 import junit.framework.TestSuite; 22 import org.apache.commons.chain.Catalog; 23 import org.apache.commons.chain.Command; 24 import org.apache.commons.chain.Context; 25 import org.apache.commons.chain.impl.*; 26 import org.apache.commons.digester.Digester; 27 28 import java.util.Iterator ; 29 30 31 34 35 public class ConfigParserTestCase extends TestCase { 36 37 38 private static final String DEFAULT_XML = 39 "/org/apache/commons/chain/config/test-config.xml"; 40 41 42 44 45 50 public ConfigParserTestCase(String name) { 51 super(name); 52 } 53 54 55 57 58 61 protected Catalog catalog = null; 62 63 64 67 protected Context context = null; 68 69 70 73 protected ConfigParser parser = null; 74 75 76 78 79 82 public void setUp() { 83 catalog = new CatalogBase(); 84 context = new ContextBase(); 85 parser = new ConfigParser(); 86 } 87 88 89 92 public static Test suite() { 93 return (new TestSuite(ConfigParserTestCase.class)); 94 } 95 96 97 100 public void tearDown() { 101 parser = null; 102 context = null; 103 catalog = null; 104 } 105 106 107 109 110 public void testDefaut() throws Exception { 112 113 load(DEFAULT_XML); 115 checkCommandCount(17); 116 117 Command command = null; 119 120 command = catalog.getCommand("AddingCommand"); 121 assertNotNull(command); 122 assertTrue(command instanceof AddingCommand); 123 124 command = catalog.getCommand("DelegatingCommand"); 125 assertNotNull(command); 126 assertTrue(command instanceof DelegatingCommand); 127 128 command = catalog.getCommand("DelegatingFilter"); 129 assertNotNull(command); 130 assertTrue(command instanceof DelegatingFilter); 131 132 command = catalog.getCommand("ExceptionCommand"); 133 assertNotNull(command); 134 assertTrue(command instanceof ExceptionCommand); 135 136 command = catalog.getCommand("ExceptionFilter"); 137 assertNotNull(command); 138 assertTrue(command instanceof ExceptionFilter); 139 140 command = catalog.getCommand("NonDelegatingCommand"); 141 assertNotNull(command); 142 assertTrue(command instanceof NonDelegatingCommand); 143 144 command = catalog.getCommand("NonDelegatingFilter"); 145 assertNotNull(command); 146 assertTrue(command instanceof NonDelegatingFilter); 147 148 command = catalog.getCommand("ChainBase"); 149 assertNotNull(command); 150 assertTrue(command instanceof ChainBase); 151 assertTrue(command instanceof TestChain); 152 153 TestCommand tcommand = (TestCommand) catalog.getCommand("Configurable"); 155 assertNotNull(tcommand); 156 assertEquals("Foo Value", tcommand.getFoo()); 157 assertEquals("Bar Value", tcommand.getBar()); 158 159 } 160 161 162 public void testExecute2a() throws Exception { 164 165 load(DEFAULT_XML); 166 assertTrue("Chain returned true", 167 catalog.getCommand("Execute2a").execute(context)); 168 checkExecuteLog("1/2/3"); 169 170 } 171 172 173 public void testExecute2b() throws Exception { 175 176 load(DEFAULT_XML); 177 assertTrue("Chain returned false", 178 !catalog.getCommand("Execute2b").execute(context)); 179 checkExecuteLog("1/2/3"); 180 181 } 182 183 184 public void testExecute2c() throws Exception { 186 187 load(DEFAULT_XML); 188 try { 189 catalog.getCommand("Execute2c").execute(context); 190 } catch (ArithmeticException e) { 191 assertEquals("Correct exception id", 192 "3", e.getMessage()); 193 } 194 checkExecuteLog("1/2/3"); 195 196 } 197 198 199 public void testExecute2d() throws Exception { 201 202 load(DEFAULT_XML); 203 try { 204 catalog.getCommand("Execute2d").execute(context); 205 } catch (ArithmeticException e) { 206 assertEquals("Correct exception id", 207 "2", e.getMessage()); 208 } 209 checkExecuteLog("1/2"); 210 211 } 212 213 214 public void testExecute4a() throws Exception { 216 217 load(DEFAULT_XML); 218 assertTrue("Chain returned true", 219 catalog.getCommand("Execute4a").execute(context)); 220 checkExecuteLog("1/2/3/c/a"); 221 222 } 223 224 225 public void testExecute4b() throws Exception { 227 228 load(DEFAULT_XML); 229 assertTrue("Chain returned false", 230 !catalog.getCommand("Execute4b").execute(context)); 231 checkExecuteLog("1/2/3/b"); 232 233 } 234 235 236 public void testExecute4c() throws Exception { 238 239 load(DEFAULT_XML); 240 try { 241 catalog.getCommand("Execute4c").execute(context); 242 } catch (ArithmeticException e) { 243 assertEquals("Correct exception id", 244 "3", e.getMessage()); 245 } 246 checkExecuteLog("1/2/3/c/b/a"); 247 248 } 249 250 251 public void testExecute4d() throws Exception { 253 254 load(DEFAULT_XML); 255 try { 256 catalog.getCommand("Execute4d").execute(context); 257 } catch (ArithmeticException e) { 258 assertEquals("Correct exception id", 259 "2", e.getMessage()); 260 } 261 checkExecuteLog("1/2/b/a"); 262 263 } 264 265 266 public void testPristine() { 268 269 Digester digester = parser.getDigester(); 271 assertNotNull("Returned a Digester instance", digester); 272 assertTrue("Default namespaceAware", 273 !digester.getNamespaceAware()); 274 assertTrue("Default useContextClassLoader", 275 digester.getUseContextClassLoader()); 276 assertTrue("Default validating", 277 !digester.getValidating()); 278 279 ConfigRuleSet ruleSet = (ConfigRuleSet) parser.getRuleSet(); 281 assertNotNull("Returned a RuleSet instance", ruleSet); 282 assertEquals("Default chainElement", 283 "chain", ruleSet.getChainElement()); 284 assertEquals("Default classAttribute", 285 "className", ruleSet.getClassAttribute()); 286 assertEquals("Default commandElement", 287 "command", ruleSet.getCommandElement()); 288 assertEquals("Default nameAttribute", 289 "name", ruleSet.getNameAttribute()); 290 assertNull("Default namespaceURI", 291 ruleSet.getNamespaceURI()); 292 293 assertTrue("Defaults to use context class loader", 295 parser.getUseContextClassLoader()); 296 297 checkCommandCount(0); 299 300 } 301 302 303 305 306 protected void checkCommandCount(int expected) { 308 int n = 0; 309 Iterator names = catalog.getNames(); 310 while (names.hasNext()) { 311 String name = (String ) names.next(); 312 n++; 313 assertNotNull(name + " exists", catalog.getCommand(name)); 314 } 315 assertEquals("Correct command count", expected, n); 316 } 317 318 319 protected void checkExecuteLog(String expected) { 321 StringBuffer log = (StringBuffer ) context.get("log"); 322 assertNotNull("Context returned log"); 323 assertEquals("Context returned correct log", 324 expected, log.toString()); 325 } 326 327 328 protected void load(String path) throws Exception { 330 parser.parse(this.getClass().getResource(path)); 331 catalog = CatalogFactoryBase.getInstance().getCatalog(); 332 } 333 334 335 } 336 | Popular Tags |