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