KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > kernel > test > TestComponentSystem


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2003 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64 package com.jcorporate.expresso.kernel.test;
65
66 import com.jcorporate.expresso.kernel.DataContext;
67 import com.jcorporate.expresso.kernel.RootContainerInterface;
68 import com.jcorporate.expresso.kernel.SystemFactory;
69 import com.jcorporate.expresso.kernel.management.ExpressoRuntimeMap;
70 import junit.framework.TestCase;
71
72 /**
73  * Unit tests for the whole component system
74  *
75  * @author Michael Rimov
76  * @version $Revision: 1.2 $ on $Date: 2004/11/17 20:48:23 $
77  */

78
79 public class TestComponentSystem extends TestCase {
80
81     public TestComponentSystem(String JavaDoc _name) {
82         super(_name);
83     }
84
85     /**
86      * setUp method for test case
87      */

88     protected void setUp() {
89     }
90
91     /**
92      * tearDown method for test case
93      */

94     protected void tearDown() {
95     }
96
97     public void testInitRoot() {
98         RootContainerInterface globalContainer = null;
99         try {
100             globalContainer = SystemFactory.buildExpressoComponentSystem(
101                     this.getClass().getResource("Test1ExpressoServices.xml"),
102                     this.getClass().getResource("TestLog4jConfig.xml"),
103                     System.getProperty("junit.argv.logDir", "/d:/expresso/log"));
104
105             assertTrue(globalContainer != null);
106             assertTrue(globalContainer.getExpressoServicesConfig() != null);
107             assertTrue(globalContainer.getContainerImplementation() != null);
108             assertTrue(globalContainer.getSetupValues() != null);
109             assertTrue("abcdefg".equals(globalContainer.getSetupValue("test1")));
110             assertTrue(globalContainer.getLogManager() != null);
111             assertTrue(globalContainer.getParent() == null);
112             assertTrue(globalContainer.getMetaData() != null);
113             assertTrue(globalContainer.getExpressoServicesConfig() != null);
114             globalContainer.destroy();
115         } catch (Exception JavaDoc ex) {
116             ex.printStackTrace();
117             fail("Caught Exception constructing Global Container");
118         }
119
120     }
121
122
123     public void testInit() {
124         RootContainerInterface globalContainer = null;
125         try {
126             globalContainer = SystemFactory.buildExpressoComponentSystem(
127                     this.getClass().getResource("Test1ExpressoServices.xml"),
128                     this.getClass().getResource("TestLog4jConfig.xml"),
129                     "D:/Expresso/log");
130
131
132             DataContext dc = (DataContext) globalContainer.locateComponent("default");
133             assertTrue("DataContext should be non-null", dc != null);
134             assertTrue("DataContext should have setup tables"
135                     , dc.getHasSetupTables().booleanValue() == true);
136
137             assertTrue("Security Context should be the default database",
138                     dc.getSecurityContext().equals("default"));
139
140             assertTrue("Should have correct description as set by services.xml"
141                     , dc.getContextDescription().equals("A Sample Default Database"));
142
143             assertTrue("Should have correct test setup value: got " + dc.getSetupValue("test1") + "instead",
144                     "Test Setup Value".equals(dc.getSetupValue("test1")));
145
146             TestComponent tc = (TestComponent) dc.locateComponent("TestComponent");
147             assertTrue("Test Component must not be null", tc != null);
148
149
150             globalContainer.destroy();
151         } catch (Exception JavaDoc ex) {
152             ex.printStackTrace();
153             fail("Caught Exception constructing Global Container");
154         }
155
156     }
157
158     /**
159      * This, unfortunately, is a visual 'joke' in that you should see a warning
160      * message about having to GC a Rootcontainer interface when it should
161      * have been explicitly destroyed.
162      */

163     public void testGC() {
164         RootContainerInterface globalContainer = null;
165         try {
166             globalContainer = SystemFactory.buildExpressoComponentSystem(
167                     this.getClass().getResource("Test1ExpressoServices.xml"),
168                     this.getClass().getResource("TestLog4jConfig.xml"),
169                     "D:/Expresso/log");
170
171             assertTrue(globalContainer != null);
172             globalContainer = null;
173             ExpressoRuntimeMap.unregisterRuntime(null);
174             System.gc();
175             System.runFinalization();
176
177             assertTrue(ExpressoRuntimeMap.getDefaultRuntime() == null);
178         } catch (Exception JavaDoc ex) {
179             ex.printStackTrace();
180             fail("Caught Exception constructing Global Container");
181         }
182
183     }
184
185     /**
186      * This test case actually tests the management class ExpressoRuntimeMap
187      * by initializing several containers and making sure that the ExpressoRuntime
188      * map seems to be keeping track of them properly.
189      */

190     public void testMultipleInits() {
191         RootContainerInterface globalContainer = null;
192
193         try {
194             globalContainer = SystemFactory.buildExpressoComponentSystem(
195                     this.getClass().getResource("Test1ExpressoServices.xml"),
196                     this.getClass().getResource("TestLog4jConfig.xml"),
197                     "D:/Expresso/log");
198
199             RootContainerInterface testContainer = SystemFactory.buildExpressoComponentSystem(
200                     this.getClass().getResource("Test2ExpressoServices.xml"),
201                     this.getClass().getResource("TestLog4jConfig.xml"),
202                     "D:/Expresso/log");
203
204             RootContainerInterface retrievedContainer = ExpressoRuntimeMap.getDefaultRuntime();
205             assertTrue("Retrieved container should be the global test container",
206                     retrievedContainer == globalContainer);
207
208             retrievedContainer = ExpressoRuntimeMap.getRuntime("sampleRuntime");
209             assertTrue(retrievedContainer != null);
210             assertTrue("Retrieved container should be the second created container",
211                     retrievedContainer == testContainer);
212
213             testContainer.destroy();
214             retrievedContainer = ExpressoRuntimeMap.getRuntime("sampleRuntime");
215             assertTrue("sampleRuntime should no longer exist", retrievedContainer == null);
216             globalContainer.destroy();
217         } catch (Exception JavaDoc ex) {
218             ex.printStackTrace();
219             fail("Caught Exception constructing Containers");
220         }
221     }
222
223
224     /**
225      * Executes the test case
226      */

227     public static void main(String JavaDoc[] argv) {
228         String JavaDoc[] testCaseList = {TestComponentSystem.class.getName()};
229         junit.textui.TestRunner.main(testCaseList);
230     }
231 }
Popular Tags