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 36 37 public class ConfigParser2TestCase extends TestCase { 38 39 40 private static final String DEFAULT_XML = 41 "/org/apache/commons/chain/config/test-config-2.xml"; 42 43 44 46 47 52 public ConfigParser2TestCase(String name) { 53 super(name); 54 } 55 56 57 59 60 63 protected Catalog catalog = null; 64 65 66 69 protected Context context = null; 70 71 72 75 protected ConfigParser parser = null; 76 77 78 80 81 84 public void setUp() { 85 catalog = new CatalogBase(); 86 context = new ContextBase(); 87 parser = new ConfigParser(); 88 } 89 90 91 94 public static Test suite() { 95 return (new TestSuite(ConfigParser2TestCase.class)); 96 } 97 98 99 102 public void tearDown() { 103 parser = null; 104 context = null; 105 catalog = null; 106 } 107 108 109 111 112 public void testDefaut() throws Exception { 114 115 load(DEFAULT_XML); 117 checkCommandCount(17); 118 119 Command command = null; 121 122 command = catalog.getCommand("AddingCommand"); 123 assertNotNull(command); 124 assertTrue(command instanceof AddingCommand); 125 126 command = catalog.getCommand("DelegatingCommand"); 127 assertNotNull(command); 128 assertTrue(command instanceof DelegatingCommand); 129 130 command = catalog.getCommand("DelegatingFilter"); 131 assertNotNull(command); 132 assertTrue(command instanceof DelegatingFilter); 133 134 command = catalog.getCommand("ExceptionCommand"); 135 assertNotNull(command); 136 assertTrue(command instanceof ExceptionCommand); 137 138 command = catalog.getCommand("ExceptionFilter"); 139 assertNotNull(command); 140 assertTrue(command instanceof ExceptionFilter); 141 142 command = catalog.getCommand("NonDelegatingCommand"); 143 assertNotNull(command); 144 assertTrue(command instanceof NonDelegatingCommand); 145 146 command = catalog.getCommand("NonDelegatingFilter"); 147 assertNotNull(command); 148 assertTrue(command instanceof NonDelegatingFilter); 149 150 command = catalog.getCommand("ChainBase"); 151 assertNotNull(command); 152 assertTrue(command instanceof ChainBase); 153 assertTrue(command instanceof TestChain); 154 155 TestCommand tcommand = (TestCommand) catalog.getCommand("Configurable"); 157 assertNotNull(tcommand); 158 assertEquals("Foo Value", tcommand.getFoo()); 159 assertEquals("Bar Value", tcommand.getBar()); 160 161 } 162 163 164 public void testExecute2a() throws Exception { 166 167 load(DEFAULT_XML); 168 assertTrue("Chain returned true", 169 catalog.getCommand("Execute2a").execute(context)); 170 checkExecuteLog("1/2/3"); 171 172 } 173 174 175 public void testExecute2b() throws Exception { 177 178 load(DEFAULT_XML); 179 assertTrue("Chain returned false", 180 !catalog.getCommand("Execute2b").execute(context)); 181 checkExecuteLog("1/2/3"); 182 183 } 184 185 186 public void testExecute2c() throws Exception { 188 189 load(DEFAULT_XML); 190 try { 191 catalog.getCommand("Execute2c").execute(context); 192 } catch (ArithmeticException e) { 193 assertEquals("Correct exception id", 194 "3", e.getMessage()); 195 } 196 checkExecuteLog("1/2/3"); 197 198 } 199 200 201 public void testExecute2d() throws Exception { 203 204 load(DEFAULT_XML); 205 try { 206 catalog.getCommand("Execute2d").execute(context); 207 } catch (ArithmeticException e) { 208 assertEquals("Correct exception id", 209 "2", e.getMessage()); 210 } 211 checkExecuteLog("1/2"); 212 213 } 214 215 216 public void testExecute4a() throws Exception { 218 219 load(DEFAULT_XML); 220 assertTrue("Chain returned true", 221 catalog.getCommand("Execute4a").execute(context)); 222 checkExecuteLog("1/2/3/c/a"); 223 224 } 225 226 227 public void testExecute4b() throws Exception { 229 230 load(DEFAULT_XML); 231 assertTrue("Chain returned false", 232 !catalog.getCommand("Execute4b").execute(context)); 233 checkExecuteLog("1/2/3/b"); 234 235 } 236 237 238 public void testExecute4c() throws Exception { 240 241 load(DEFAULT_XML); 242 try { 243 catalog.getCommand("Execute4c").execute(context); 244 } catch (ArithmeticException e) { 245 assertEquals("Correct exception id", 246 "3", e.getMessage()); 247 } 248 checkExecuteLog("1/2/3/c/b/a"); 249 250 } 251 252 253 public void testExecute4d() throws Exception { 255 256 load(DEFAULT_XML); 257 try { 258 catalog.getCommand("Execute4d").execute(context); 259 } catch (ArithmeticException e) { 260 assertEquals("Correct exception id", 261 "2", e.getMessage()); 262 } 263 checkExecuteLog("1/2/b/a"); 264 265 } 266 267 268 public void testPristine() { 270 271 Digester digester = parser.getDigester(); 273 assertNotNull("Returned a Digester instance", digester); 274 assertTrue("Default namespaceAware", 275 !digester.getNamespaceAware()); 276 assertTrue("Default useContextClassLoader", 277 digester.getUseContextClassLoader()); 278 assertTrue("Default validating", 279 !digester.getValidating()); 280 281 ConfigRuleSet ruleSet = (ConfigRuleSet) parser.getRuleSet(); 283 assertNotNull("Returned a RuleSet instance", ruleSet); 284 assertEquals("Default chainElement", 285 "chain", ruleSet.getChainElement()); 286 assertEquals("Default classAttribute", 287 "className", ruleSet.getClassAttribute()); 288 assertEquals("Default commandElement", 289 "command", ruleSet.getCommandElement()); 290 assertEquals("Default nameAttribute", 291 "name", ruleSet.getNameAttribute()); 292 assertNull("Default namespaceURI", 293 ruleSet.getNamespaceURI()); 294 295 assertTrue("Defaults to use context class loader", 297 parser.getUseContextClassLoader()); 298 299 checkCommandCount(0); 301 302 } 303 304 305 307 308 protected void checkCommandCount(int expected) { 310 int n = 0; 311 Iterator names = catalog.getNames(); 312 while (names.hasNext()) { 313 String name = (String ) names.next(); 314 n++; 315 assertNotNull(name + " exists", catalog.getCommand(name)); 316 } 317 assertEquals("Correct command count", expected, n); 318 } 319 320 321 protected void checkExecuteLog(String expected) { 323 StringBuffer log = (StringBuffer ) context.get("log"); 324 assertNotNull("Context returned log"); 325 assertEquals("Context returned correct log", 326 expected, log.toString()); 327 } 328 329 330 protected void load(String path) throws Exception { 332 parser.parse(catalog, this.getClass().getResource(path)); 333 catalog = CatalogFactoryBase.getInstance().getCatalog("foo"); 334 } 335 336 337 } 338 | Popular Tags |