1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.chain.config;
19  
20  import junit.framework.Test;
21  import junit.framework.TestCase;
22  import junit.framework.TestSuite;
23  import org.apache.commons.chain.Catalog;
24  import org.apache.commons.chain.Command;
25  import org.apache.commons.chain.Context;
26  import org.apache.commons.chain.impl.*;
27  import org.apache.commons.digester.Digester;
28  
29  import java.util.Iterator;
30  
31  
32  /**
33   * <p>Test Case for <code>org.apache.commons.chain.config.ConfigParser</code>.</p>
34   */
35  
36  public class ConfigParserTestCase extends TestCase {
37  
38  
39      private static final String DEFAULT_XML =
40          "/org/apache/commons/chain/config/test-config.xml";
41  
42  
43      // ------------------------------------------------------------ Constructors
44  
45  
46      /**
47       * Construct a new instance of this test case.
48       *
49       * @param name Name of the test case
50       */
51      public ConfigParserTestCase(String name) {
52          super(name);
53      }
54  
55  
56      // ------------------------------------------------------ Instance Variables
57  
58  
59      /**
60       * <p>The <code>Catalog</code> to contain our configured commands.</p>
61       */
62      protected Catalog catalog = null;
63  
64  
65      /**
66       * <p>The <code>Context</code> to use for execution tests.</p>
67       */
68      protected Context context = null;
69  
70  
71      /**
72       * <p>The <code>ConfigParser</code> instance under test.</p>
73       */
74      protected ConfigParser parser = null;
75  
76  
77      // ---------------------------------------------------- Overall Test Methods
78  
79  
80      /**
81       * Set up instance variables required by this test case.
82       */
83      public void setUp() {
84          catalog = new CatalogBase();
85          context = new ContextBase();
86          parser = new ConfigParser();
87      }
88  
89  
90      /**
91       * Return the tests included in this test suite.
92       */
93      public static Test suite() {
94          return (new TestSuite(ConfigParserTestCase.class));
95      }
96  
97  
98      /**
99       * 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 }