KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > component > factory > test > DefaultComponentFactoryTest


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.core.component.factory.test;
19
20 import org.sape.carbon.core.component.Component;
21 import org.sape.carbon.core.component.factory.ComponentFactory;
22 import org.sape.carbon.core.component.factory.DefaultComponentFactory;
23 import org.sape.carbon.core.component.lifecycle.LifecycleInterceptor;
24 import org.sape.carbon.core.component.lifecycle.LifecycleStateEnum;
25 import org.sape.carbon.core.config.interceptor.ConfigurationInterceptor;
26 import org.sape.carbon.core.exception.InvalidParameterException;
27
28 import junit.framework.Test;
29 import junit.framework.TestCase;
30 import junit.framework.TestSuite;
31
32 /**
33  * This class tests the DefaultComponentFactory. It makes sure that components
34  * are created correctly.
35  *
36  * Copyright 2002 Sapient
37  * @since carbon 1.0
38  * @author Douglas Voet, Febuary 2002
39  * @version $Revision: 1.17 $($Author: ghinkl $ / $Date: 2003/10/16 13:21:15 $)
40  */

41 public class DefaultComponentFactoryTest extends TestCase {
42     private ComponentFactory componentFactory;
43
44     public DefaultComponentFactoryTest(String JavaDoc name) {
45         super(name);
46     }
47
48     /**
49      * Tests to make sure that a component is created successfully and that it
50      * is in the LifecycleStateEnum.STOPPED state and implements all
51      * appropriate interfaces. These interfaces include:
52      * <ul>
53      * <li>the functional interface</li>
54      * <li>the component's configuration interface</li>
55      * <li>lifecycle interfaces</li>
56      * <li>interceptor interfaces (<code>LifecycleInterceptor</code> and
57      * <code>ConfigurationInterceptor</code></li>
58      * </ul>
59      */

60     public void testComponentCreation() {
61         Component testComponent =
62                 this.componentFactory.getInstance(ALL_INTERFACES_COMPONENT);
63
64         // test for null
65
if (testComponent == null) {
66             TestCase.fail("Component was null");
67         }
68
69         // test interfaces
70
// 1. functional interface
71
if (!(testComponent instanceof ComponentFactoryTestComponent)) {
72             TestCase.fail("Component did not implement expected function " +
73                     "interface: " +
74                     ComponentFactoryTestComponent.class.getName());
75         }
76
77         // 2. configuration interface
78
if (!(testComponent instanceof ComponentFactoryTestComponentConfig)) {
79             TestCase.fail("Component did not implement expected function " +
80                     "interface: " +
81                     ComponentFactoryTestComponentConfig.class.getName());
82         }
83
84         // 3. lifecycle interfaces
85
/* if (!(testComponent instanceof Configurable)) {
86             TestCase.fail("Component did not implement expected lifecycle " +
87                     "interface: " + Configurable.class.getName());
88         }
89         if (!(testComponent instanceof Destroyable)) {
90             TestCase.fail("Component did not implement expected lifecycle " +
91                     "interface: " + Destroyable.class.getName());
92         }
93         if (!(testComponent instanceof Initializable)) {
94             TestCase.fail("Component did not implement expected lifecycle " +
95                     "interface: " + Initializable.class.getName());
96         }
97         if (!(testComponent instanceof Startable)) {
98             TestCase.fail("Component did not implement expected lifecycle " +
99                     "interface: " + Startable.class.getName());
100         }
101         if (!(testComponent instanceof Suspendable)) {
102             TestCase.fail("Component did not implement expected lifecycle " +
103                     "interface: " + Suspendable.class.getName());
104         }*/

105
106
107         // 4. interceptor interfaces
108
if (!(testComponent instanceof LifecycleInterceptor)) {
109             TestCase.fail("Component did not implement expected interceptor " +
110                     "interface: " + LifecycleInterceptor.class.getName());
111         }
112         if (!(testComponent instanceof ConfigurationInterceptor)) {
113             TestCase.fail("Component did not implement expected interceptor " +
114                     "interface: " + ConfigurationInterceptor.class.getName());
115         }
116
117         // test state
118
if (((LifecycleInterceptor) testComponent).getLifecycleState() !=
119                 LifecycleStateEnum.STOPPED) {
120             TestCase.fail("Component was not in the " +
121                     "LifecycleStateEnum.STOPPED state");
122         }
123     }
124
125     /**
126      * Tests to make sure that a ComponentNotFoundException is thrown when
127      * when an invalid component name is requested
128      */

129     public void testComponentNotFound() {
130         boolean passedTest = false;
131         try {
132             this.componentFactory.getInstance("");
133         } catch(InvalidParameterException ipe) {
134             passedTest = true;
135         }
136         if (!passedTest) {
137             TestCase.fail("InvalidParameterException was not thrown");
138         }
139     }
140
141     public void testMethodNameCollision() {
142         this.componentFactory.getInstance(NAME_COLLISION_TEST_COMPONENT) ;
143     }
144
145     protected void setUp() {
146         this.componentFactory = new DefaultComponentFactory();
147     }
148
149     // static methods
150
private static final String JavaDoc ALL_INTERFACES_COMPONENT =
151         "/core/test/AllInterfacesComponent";
152     private static final String JavaDoc NAME_COLLISION_TEST_COMPONENT =
153         "/core/test/NameCollisionTestComponent";
154
155
156     /**
157      * Method called by jUnit to get all the tests in this test case.
158      * @return Test the suite of tests in this test case
159      */

160     public static Test suite() {
161         TestSuite masterSuite = new TestSuite("DefaultComponentFactoryTest");
162         // add single threaded tests
163
Test singleThreadedTests = getSingleThreadedTests();
164         if (singleThreadedTests != null) {
165             masterSuite.addTest(singleThreadedTests);
166         }
167         // add multi threaded tests
168
Test multiThreadedTests = getMultiThreadedTests();
169         if (multiThreadedTests != null) {
170             masterSuite.addTest(multiThreadedTests);
171         }
172         return masterSuite;
173     }
174
175     /**
176      * The single threaded tests are:
177      * <ul>
178      * <li>testComponentCreation</li>
179      * <li>testComponentNotFound</li>
180      * <li>testTwoComponentRequests</li>
181      * </ul>
182      *
183      * @return Test the suite of single threaded tests in this test case
184      */

185     private static Test getSingleThreadedTests() {
186         TestSuite suite = new TestSuite();
187
188         suite.addTest(new DefaultComponentFactoryTest("testComponentCreation"));
189         suite.addTest(new DefaultComponentFactoryTest("testComponentNotFound"));
190         suite.addTest(new DefaultComponentFactoryTest("testMethodNameCollision"));
191
192         return suite;
193     }
194
195     /**
196      * There are no multi-threaded tests for DefaultComponentFactory
197      * @return null
198      */

199     private static Test getMultiThreadedTests() {
200         return null;
201     }
202
203     /**
204      * This method will add the give test to the give suite the specified
205      * number of times. This is best used for multi-threaded tests where
206      * suite is an instance of ActiveTestSuite and you want to run the same test in multiple threads.
207      * @param suite the suite to add the test to.
208      * @param testName the name of the test to add.
209      * @param number the number of times to add the test to the suite
210      */

211     private static void addTest(TestSuite suite, String JavaDoc testName, int number) {
212         for (int count = 0; count < number; count++) {
213             suite.addTest(new DefaultComponentFactoryTest(testName));
214         }
215     }
216 }
217
Popular Tags