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 identical to {@link ConfigParserTestCase} except
34   * that it uses the <code>define</code> rule to define aliases
35   * for the commands and chains used in the test.</p>
36   */
37  
38  public class ConfigParser2TestCase extends TestCase {
39  
40  
41      private static final String DEFAULT_XML =
42          "/org/apache/commons/chain/config/test-config-2.xml";
43  
44  
45      // ------------------------------------------------------------ Constructors
46  
47  
48      /**
49       * Construct a new instance of this test case.
50       *
51       * @param name Name of the test case
52       */
53      public ConfigParser2TestCase(String name) {
54          super(name);
55      }
56  
57  
58      // ------------------------------------------------------ Instance Variables
59  
60  
61      /**
62       * <p>The <code>Catalog</code> to contain our configured commands.</p>
63       */
64      protected Catalog catalog = null;
65  
66  
67      /**
68       * <p>The <code>Context</code> to use for execution tests.</p>
69       */
70      protected Context context = null;
71  
72  
73      /**
74       * <p>The <code>ConfigParser</code> instance under test.</p>
75       */
76      protected ConfigParser parser = null;
77  
78  
79      // ---------------------------------------------------- Overall Test Methods
80  
81  
82      /**
83       * Set up instance variables required by this test case.
84       */
85      public void setUp() {
86          catalog = new CatalogBase();
87          context = new ContextBase();
88          parser = new ConfigParser();
89      }
90  
91  
92      /**
93       * Return the tests included in this test suite.
94       */
95      public static Test suite() {
96          return (new TestSuite(ConfigParser2TestCase.class));
97      }
98  
99  
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 }