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  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.CatalogFactory;
24  import org.apache.commons.chain.Command;
25  import org.apache.commons.chain.impl.CatalogBase;
26  import java.util.Iterator;
27  
28  /**
29   * <p>Test case for the <code>CatalogFactoryBase</code> class.</p>
30   *
31   * @author Craig R. McClanahan
32   * @version $Revision: 532948 $ $Date: 2007-04-27 04:53:59 +0100 (Fri, 27 Apr 2007) $
33   */
34  
35  public class CatalogFactoryBaseTestCase extends TestCase {
36  
37  
38      // ---------------------------------------------------- Instance Variables
39  
40  
41      /**
42       * <p>The {@link CatalogFactory} instance under test.</p>
43       */
44      protected CatalogFactory factory = null;
45  
46  
47      // ---------------------------------------------------------- Constructors
48  
49  
50      /**
51       * Construct a new instance of this test case.
52       *
53       * @param name Name of the test case
54       */
55      public CatalogFactoryBaseTestCase(String name) {
56          super(name);
57      }
58  
59  
60      // -------------------------------------------------- Overall Test Methods
61  
62  
63      /**
64       * <p>Set up instance variables required by this test case.</p>
65       */
66      public void setUp() {
67          CatalogFactory.clear();
68          factory = CatalogFactory.getInstance();
69      }
70  
71  
72      /**
73       * <p>Return the tests included in this test suite.</p>
74       */
75      public static Test suite() {
76          return (new TestSuite(CatalogFactoryBaseTestCase.class));
77      }
78  
79      /**
80       * <p>Tear down instance variables required by this test case.</p>
81       */
82      public void tearDown() {
83          factory = null;
84      }
85  
86  
87      // ------------------------------------------------ Individual Test Methods
88  
89  
90      /**
91       * <p>Test a pristine instance of {@link CatalogFactory}.</p>
92       */
93      public void testPristine() {
94  
95          assertNotNull(factory);
96          assertNull(factory.getCatalog());
97          assertNull(factory.getCatalog("foo"));
98          assertEquals(0, getCatalogCount());
99  
100     }
101 
102 
103     /**
104      * <p>Test the default {@link Catalog} instance.</p>
105      */
106     public void testDefaultCatalog() {
107 
108         Catalog catalog = new CatalogBase();
109         factory.setCatalog(catalog);
110         assertTrue(catalog == factory.getCatalog());
111         assertEquals(0, getCatalogCount());
112 
113     }
114 
115 
116     /**
117      * <p>Test adding a specifically named {@link Catalog} instance.</p>
118      */
119     public void testSpecificCatalog() {
120 
121         Catalog catalog = new CatalogBase();
122         factory.setCatalog(catalog);
123         catalog = new CatalogBase();
124         factory.addCatalog("foo", catalog);
125         assertTrue(catalog == factory.getCatalog("foo"));
126         assertEquals(1, getCatalogCount());
127         factory.addCatalog("foo", new CatalogBase());
128         assertEquals(1, getCatalogCount());
129         assertTrue(!(catalog == factory.getCatalog("foo")));
130         CatalogFactory.clear();
131         factory = CatalogFactory.getInstance();
132         assertEquals(0, getCatalogCount());
133 
134     }
135 
136 
137     /**
138      * <p>Test <code>getCatalog()</code> method.</p>
139      */
140     public void testCatalogIdentifier() {
141 
142         Catalog defaultCatalog = new CatalogBase();
143         Command defaultFoo = new NonDelegatingCommand();
144         defaultCatalog.addCommand("foo", defaultFoo);
145         Command fallback = new NonDelegatingCommand();
146         defaultCatalog.addCommand("noSuchCatalog:fallback", fallback);
147 
148         factory.setCatalog(defaultCatalog);
149 
150         Catalog specificCatalog = new CatalogBase();
151         Command specificFoo = new NonDelegatingCommand();
152         specificCatalog.addCommand("foo", specificFoo);
153         factory.addCatalog("specific", specificCatalog);
154 
155         Command command = factory.getCommand("foo");
156         assertSame(defaultFoo, command);
157 
158         command = factory.getCommand("specific:foo");
159         assertSame(specificFoo, command);
160 
161         command = factory.getCommand("void");
162         assertNull(command);
163 
164         command = factory.getCommand("foo:void");
165         assertNull(command);
166 
167         command = factory.getCommand("specific:void");
168         assertNull(command);
169 
170         command = factory.getCommand("noSuchCatalog:fallback");
171         assertNull(command);
172 
173         try {
174             command = factory.getCommand("multiple:delimiters:reserved");
175             fail("A command ID with more than one delimiter should throw an IllegalArgumentException");
176         }
177         catch (IllegalArgumentException ex) {
178             // expected behavior
179         }
180 
181     }
182 
183 
184     // ------------------------------------------------------- Support Methods
185 
186 
187     /**
188      * <p>Return the number of {@link Catalog}s defined in our
189      * {@link CatalogFactory}.</p>
190      */
191     private int getCatalogCount() {
192 
193         Iterator names = factory.getNames();
194         assertNotNull(names);
195         int n = 0;
196         while (names.hasNext()) {
197             names.next();
198             n++;
199         }
200         return n;
201 
202     }
203 
204 
205 }