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  package org.apache.commons.chain.impl;
18  
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  
26  import java.util.Iterator;
27  
28  
29  /**
30   * <p>Test case for the <code>CatalogBase</code> class.</p>
31   *
32   * @author Craig R. McClanahan
33   * @version $Revision: 480477 $ $Date: 2006-11-29 08:34:52 +0000 (Wed, 29 Nov 2006) $
34   */
35  
36  public class CatalogBaseTestCase extends TestCase {
37  
38  
39      // ---------------------------------------------------- Instance Variables
40  
41  
42      /**
43       * The {@link Catalog} instance under test.
44       */
45      protected CatalogBase catalog = null;
46  
47  
48      // ---------------------------------------------------------- Constructors
49  
50      /**
51       * Construct a new instance of this test case.
52       *
53       * @param name Name of the test case
54       */
55      public CatalogBaseTestCase(String name) {
56          super(name);
57      }
58  
59  
60      // -------------------------------------------------- Overall Test Methods
61  
62  
63      /**
64       * Set up instance variables required by this test case.
65       */
66      public void setUp() {
67          catalog = new CatalogBase();
68      }
69  
70  
71      /**
72       * Return the tests included in this test suite.
73       */
74      public static Test suite() {
75          return (new TestSuite(CatalogBaseTestCase.class));
76      }
77  
78      /**
79       * Tear down instance variables required by this test case.
80       */
81      public void tearDown() {
82          catalog = null;
83      }
84  
85  
86      // ------------------------------------------------ Individual Test Methods
87  
88  
89      // Test adding commands
90      public void testAddCommand() {
91          addCommands();
92          checkCommandCount(8);
93      }
94  
95  
96      // Test getting commands
97      public void testGetCommand() {
98  
99          addCommands();
100         Command command = null;
101 
102         command = catalog.getCommand("AddingCommand");
103         assertNotNull(command);
104         assertTrue(command instanceof AddingCommand);
105 
106         command = catalog.getCommand("DelegatingCommand");
107         assertNotNull(command);
108         assertTrue(command instanceof DelegatingCommand);
109 
110         command = catalog.getCommand("DelegatingFilter");
111         assertNotNull(command);
112         assertTrue(command instanceof DelegatingFilter);
113 
114         command = catalog.getCommand("ExceptionCommand");
115         assertNotNull(command);
116         assertTrue(command instanceof ExceptionCommand);
117 
118         command = catalog.getCommand("ExceptionFilter");
119         assertNotNull(command);
120         assertTrue(command instanceof ExceptionFilter);
121 
122         command = catalog.getCommand("NonDelegatingCommand");
123         assertNotNull(command);
124         assertTrue(command instanceof NonDelegatingCommand);
125 
126         command = catalog.getCommand("NonDelegatingFilter");
127         assertNotNull(command);
128         assertTrue(command instanceof NonDelegatingFilter);
129 
130         command = catalog.getCommand("ChainBase");
131         assertNotNull(command);
132         assertTrue(command instanceof ChainBase);
133 
134     }
135 
136 
137     // The getNames() method is implicitly tested by checkCommandCount()
138 
139 
140     // Test pristine instance
141     public void testPristine() {
142         checkCommandCount(0);
143         assertNull(catalog.getCommand("AddingCommand"));
144         assertNull(catalog.getCommand("DelegatingCommand"));
145         assertNull(catalog.getCommand("DelegatingFilter"));
146         assertNull(catalog.getCommand("ExceptionCommand"));
147         assertNull(catalog.getCommand("ExceptionFilter"));
148         assertNull(catalog.getCommand("NonDelegatingCommand"));
149         assertNull(catalog.getCommand("NonDelegatingFilter"));
150         assertNull(catalog.getCommand("ChainBase"));
151     }
152 
153 
154 
155 
156     // -------------------------------------------------------- Support Methods
157 
158 
159     // Add an interesting set of commands to the catalog
160     protected void addCommands() {
161         catalog.addCommand("AddingCommand", new AddingCommand("", null));
162         catalog.addCommand("DelegatingCommand", new DelegatingCommand(""));
163         catalog.addCommand("DelegatingFilter", new DelegatingFilter("", ""));
164         catalog.addCommand("ExceptionCommand", new ExceptionCommand(""));
165         catalog.addCommand("ExceptionFilter", new ExceptionFilter("", ""));
166         catalog.addCommand("NonDelegatingCommand", new NonDelegatingCommand(""));
167         catalog.addCommand("NonDelegatingFilter", new NonDelegatingFilter("", ""));
168         catalog.addCommand("ChainBase", new ChainBase());
169     }
170 
171 
172     // Verify the number of configured commands
173     protected void checkCommandCount(int expected) {
174         int n = 0;
175         Iterator names = catalog.getNames();
176         while (names.hasNext()) {
177             String name = (String) names.next();
178             n++;
179             assertNotNull(name + " exists", catalog.getCommand(name));
180         }
181         assertEquals("Correct command count", expected, n);
182     }
183 
184 
185 }