1   /*
2    * Copyright 1999-2004 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.chain.impl;
17  
18  import junit.framework.Test;
19  import junit.framework.TestCase;
20  import junit.framework.TestSuite;
21  import org.apache.commons.chain.Catalog;
22  import org.apache.commons.chain.CatalogFactory;
23  import org.apache.commons.chain.Command;
24  import org.apache.commons.chain.impl.CatalogBase;
25  import java.util.Iterator;
26  
27  /***
28   * <p>Test case for the <code>CatalogFactoryBase</code> class.</p>
29   *
30   * @author Craig R. McClanahan
31   * @version $Revision: 1.3 $ $Date: 2004/12/09 05:23:14 $
32   */
33  
34  public class CatalogFactoryBaseTestCase extends TestCase {
35  
36  
37      // ---------------------------------------------------- Instance Variables
38  
39  
40      /***
41       * <p>The {@link CatalogFactory} instance under test.</p>
42       */
43      protected CatalogFactory factory = null;
44  
45  
46      // ---------------------------------------------------------- Constructors
47  
48  
49      /***
50       * Construct a new instance of this test case.
51       *
52       * @param name Name of the test case
53       */
54      public CatalogFactoryBaseTestCase(String name) {
55          super(name);
56      }
57  
58  
59      // -------------------------------------------------- Overall Test Methods
60  
61  
62      /***
63       * <p>Set up instance variables required by this test case.</p>
64       */
65      public void setUp() {
66          factory = CatalogFactory.getInstance();
67      }
68  
69  
70      /***
71       * <p>Return the tests included in this test suite.</p>
72       */
73      public static Test suite() {
74          return (new TestSuite(CatalogFactoryBaseTestCase.class));
75      }
76  
77      /***
78       * <p>Tear down instance variables required by this test case.</p>
79       */
80      public void tearDown() {
81          factory = null;
82      }
83  
84  
85      // ------------------------------------------------ Individual Test Methods
86  
87  
88      /***
89       * <p>Test a pristine instance of {@link CatalogFactory}.</p>
90       */
91      public void testPristine() {
92  
93          assertNotNull(factory);
94          assertNull(factory.getCatalog());
95          assertNull(factory.getCatalog("foo"));
96          assertEquals(0, getCatalogCount());
97  
98      }
99  
100 
101     /***
102      * <p>Test the default {@link Catalog} instance.</p>
103      */
104     public void testDefaultCatalog() {
105 
106         Catalog catalog = new CatalogBase();
107         factory.setCatalog(catalog);
108         assertTrue(catalog == factory.getCatalog());
109         assertEquals(0, getCatalogCount());
110 
111     }
112 
113 
114     /***
115      * <p>Test adding a specifically named {@link Catalog} instance.</p>
116      */
117     public void testSpecificCatalog() {
118 
119         Catalog catalog = new CatalogBase();
120         factory.setCatalog(catalog);
121         catalog = new CatalogBase();
122         factory.addCatalog("foo", catalog);
123         assertTrue(catalog == factory.getCatalog("foo"));
124         assertEquals(1, getCatalogCount());
125         factory.addCatalog("foo", new CatalogBase());
126         assertEquals(1, getCatalogCount());
127         assertTrue(!(catalog == factory.getCatalog("foo")));
128         CatalogFactory.clear();
129         factory = CatalogFactory.getInstance();
130         assertEquals(0, getCatalogCount());
131 
132     }
133 
134 
135     // ------------------------------------------------------- Support Methods
136 
137 
138     /***
139      * <p>Return the number of {@link Catalog}s defined in our
140      * {@link CatalogFactory}.</p>
141      */
142     private int getCatalogCount() {
143 
144         Iterator names = factory.getNames();
145         assertNotNull(names);
146         int n = 0;
147         while (names.hasNext()) {
148             names.next();
149             n++;
150         }
151         return n;
152 
153     }
154 
155 
156 }