001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.commons.chain.config; 019 020 import junit.framework.Test; 021 import junit.framework.TestCase; 022 import junit.framework.TestSuite; 023 import org.apache.commons.chain.Catalog; 024 import org.apache.commons.chain.Command; 025 import org.apache.commons.chain.Context; 026 import org.apache.commons.chain.impl.*; 027 import org.apache.commons.digester.Digester; 028 029 import java.util.Iterator; 030 031 032 /** 033 * <p>Test Case for <code>org.apache.commons.chain.config.ConfigParser</code>.</p> 034 */ 035 036 public class ConfigParserTestCase extends TestCase { 037 038 039 private static final String DEFAULT_XML = 040 "/org/apache/commons/chain/config/test-config.xml"; 041 042 043 // ------------------------------------------------------------ Constructors 044 045 046 /** 047 * Construct a new instance of this test case. 048 * 049 * @param name Name of the test case 050 */ 051 public ConfigParserTestCase(String name) { 052 super(name); 053 } 054 055 056 // ------------------------------------------------------ Instance Variables 057 058 059 /** 060 * <p>The <code>Catalog</code> to contain our configured commands.</p> 061 */ 062 protected Catalog catalog = null; 063 064 065 /** 066 * <p>The <code>Context</code> to use for execution tests.</p> 067 */ 068 protected Context context = null; 069 070 071 /** 072 * <p>The <code>ConfigParser</code> instance under test.</p> 073 */ 074 protected ConfigParser parser = null; 075 076 077 // ---------------------------------------------------- Overall Test Methods 078 079 080 /** 081 * Set up instance variables required by this test case. 082 */ 083 public void setUp() { 084 catalog = new CatalogBase(); 085 context = new ContextBase(); 086 parser = new ConfigParser(); 087 } 088 089 090 /** 091 * Return the tests included in this test suite. 092 */ 093 public static Test suite() { 094 return (new TestSuite(ConfigParserTestCase.class)); 095 } 096 097 098 /** 099 * Tear down instance variables required by this test case. 100 */ 101 public void tearDown() { 102 parser = null; 103 context = null; 104 catalog = null; 105 } 106 107 108 // ------------------------------------------------ Individual Test Methods 109 110 111 // Load the default test-config.xml file and examine the results 112 public void testDefaut() throws Exception { 113 114 // Check overall command count 115 load(DEFAULT_XML); 116 checkCommandCount(17); 117 118 // Check individual single command instances 119 Command command = null; 120 121 command = catalog.getCommand("AddingCommand"); 122 assertNotNull(command); 123 assertTrue(command instanceof AddingCommand); 124 125 command = catalog.getCommand("DelegatingCommand"); 126 assertNotNull(command); 127 assertTrue(command instanceof DelegatingCommand); 128 129 command = catalog.getCommand("DelegatingFilter"); 130 assertNotNull(command); 131 assertTrue(command instanceof DelegatingFilter); 132 133 command = catalog.getCommand("ExceptionCommand"); 134 assertNotNull(command); 135 assertTrue(command instanceof ExceptionCommand); 136 137 command = catalog.getCommand("ExceptionFilter"); 138 assertNotNull(command); 139 assertTrue(command instanceof ExceptionFilter); 140 141 command = catalog.getCommand("NonDelegatingCommand"); 142 assertNotNull(command); 143 assertTrue(command instanceof NonDelegatingCommand); 144 145 command = catalog.getCommand("NonDelegatingFilter"); 146 assertNotNull(command); 147 assertTrue(command instanceof NonDelegatingFilter); 148 149 command = catalog.getCommand("ChainBase"); 150 assertNotNull(command); 151 assertTrue(command instanceof ChainBase); 152 assertTrue(command instanceof TestChain); 153 154 // Check configurable properties instance 155 TestCommand tcommand = (TestCommand) catalog.getCommand("Configurable"); 156 assertNotNull(tcommand); 157 assertEquals("Foo Value", tcommand.getFoo()); 158 assertEquals("Bar Value", tcommand.getBar()); 159 160 } 161 162 163 // Test execution of chain "Execute2a" 164 public void testExecute2a() throws Exception { 165 166 load(DEFAULT_XML); 167 assertTrue("Chain returned true", 168 catalog.getCommand("Execute2a").execute(context)); 169 checkExecuteLog("1/2/3"); 170 171 } 172 173 174 // Test execution of chain "Execute2b" 175 public void testExecute2b() throws Exception { 176 177 load(DEFAULT_XML); 178 assertTrue("Chain returned false", 179 !catalog.getCommand("Execute2b").execute(context)); 180 checkExecuteLog("1/2/3"); 181 182 } 183 184 185 // Test execution of chain "Execute2c" 186 public void testExecute2c() throws Exception { 187 188 load(DEFAULT_XML); 189 try { 190 catalog.getCommand("Execute2c").execute(context); 191 } catch (ArithmeticException e) { 192 assertEquals("Correct exception id", 193 "3", e.getMessage()); 194 } 195 checkExecuteLog("1/2/3"); 196 197 } 198 199 200 // Test execution of chain "Execute2d" 201 public void testExecute2d() throws Exception { 202 203 load(DEFAULT_XML); 204 try { 205 catalog.getCommand("Execute2d").execute(context); 206 } catch (ArithmeticException e) { 207 assertEquals("Correct exception id", 208 "2", e.getMessage()); 209 } 210 checkExecuteLog("1/2"); 211 212 } 213 214 215 // Test execution of chain "Execute4a" 216 public void testExecute4a() throws Exception { 217 218 load(DEFAULT_XML); 219 assertTrue("Chain returned true", 220 catalog.getCommand("Execute4a").execute(context)); 221 checkExecuteLog("1/2/3/c/a"); 222 223 } 224 225 226 // Test execution of chain "Execute2b" 227 public void testExecute4b() throws Exception { 228 229 load(DEFAULT_XML); 230 assertTrue("Chain returned false", 231 !catalog.getCommand("Execute4b").execute(context)); 232 checkExecuteLog("1/2/3/b"); 233 234 } 235 236 237 // Test execution of chain "Execute4c" 238 public void testExecute4c() throws Exception { 239 240 load(DEFAULT_XML); 241 try { 242 catalog.getCommand("Execute4c").execute(context); 243 } catch (ArithmeticException e) { 244 assertEquals("Correct exception id", 245 "3", e.getMessage()); 246 } 247 checkExecuteLog("1/2/3/c/b/a"); 248 249 } 250 251 252 // Test execution of chain "Execute4d" 253 public void testExecute4d() throws Exception { 254 255 load(DEFAULT_XML); 256 try { 257 catalog.getCommand("Execute4d").execute(context); 258 } catch (ArithmeticException e) { 259 assertEquals("Correct exception id", 260 "2", e.getMessage()); 261 } 262 checkExecuteLog("1/2/b/a"); 263 264 } 265 266 267 // Test a pristine ConfigParser instance 268 public void testPristine() { 269 270 // Validate the "digester" property 271 Digester digester = parser.getDigester(); 272 assertNotNull("Returned a Digester instance", digester); 273 assertTrue("Default namespaceAware", 274 !digester.getNamespaceAware()); 275 assertTrue("Default useContextClassLoader", 276 digester.getUseContextClassLoader()); 277 assertTrue("Default validating", 278 !digester.getValidating()); 279 280 // Validate the "ruleSet" property 281 ConfigRuleSet ruleSet = (ConfigRuleSet) parser.getRuleSet(); 282 assertNotNull("Returned a RuleSet instance", ruleSet); 283 assertEquals("Default chainElement", 284 "chain", ruleSet.getChainElement()); 285 assertEquals("Default classAttribute", 286 "className", ruleSet.getClassAttribute()); 287 assertEquals("Default commandElement", 288 "command", ruleSet.getCommandElement()); 289 assertEquals("Default nameAttribute", 290 "name", ruleSet.getNameAttribute()); 291 assertNull("Default namespaceURI", 292 ruleSet.getNamespaceURI()); 293 294 // Validate the "useContextClassLoader" property 295 assertTrue("Defaults to use context class loader", 296 parser.getUseContextClassLoader()); 297 298 // Ensure that there are no preconfigured commands in the catalog 299 checkCommandCount(0); 300 301 } 302 303 304 // --------------------------------------------------------- Private Methods 305 306 307 // Verify the number of configured commands 308 protected void checkCommandCount(int expected) { 309 int n = 0; 310 Iterator names = catalog.getNames(); 311 while (names.hasNext()) { 312 String name = (String) names.next(); 313 n++; 314 assertNotNull(name + " exists", catalog.getCommand(name)); 315 } 316 assertEquals("Correct command count", expected, n); 317 } 318 319 320 // Verify the contents of the execution log 321 protected void checkExecuteLog(String expected) { 322 StringBuffer log = (StringBuffer) context.get("log"); 323 assertNotNull("Context returned log"); 324 assertEquals("Context returned correct log", 325 expected, log.toString()); 326 } 327 328 329 // Load the specified catalog from the specified resource path 330 protected void load(String path) throws Exception { 331 parser.parse(this.getClass().getResource(path)); 332 catalog = CatalogFactoryBase.getInstance().getCatalog(); 333 } 334 335 336 }