KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > kernel > util > test > TestLocatorUtils


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 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
65 package com.jcorporate.expresso.kernel.util.test;
66
67 import com.jcorporate.expresso.kernel.Containable;
68 import com.jcorporate.expresso.kernel.ExpressoComponent;
69 import com.jcorporate.expresso.kernel.RootContainerInterface;
70 import com.jcorporate.expresso.kernel.SystemFactory;
71 import com.jcorporate.expresso.kernel.digester.ComponentConfig;
72 import com.jcorporate.expresso.kernel.exception.ConfigurationException;
73 import com.jcorporate.expresso.kernel.metadata.ComponentMetadata;
74 import com.jcorporate.expresso.kernel.util.LocatorUtils;
75 import com.jcorporate.expresso.services.test.TestSystemInitializer;
76 import junit.framework.TestCase;
77
78 /**
79  * JUnitTest case for class: com.jcorporate.expresso.kernel.util.LocatorUtils
80  */

81 public class TestLocatorUtils extends TestCase {
82
83     public TestLocatorUtils(String JavaDoc _name) {
84         super(_name);
85     }
86
87     RootContainerInterface globalContainer = null;
88
89     /**
90      * setUp method for test case
91      */

92     protected void setUp() {
93         try {
94             globalContainer = SystemFactory.buildExpressoComponentSystem(
95                     this.getClass().getResource(
96                             "/com/jcorporate/expresso/kernel/management/test/Test1ExpressoServices.xml"),
97                     this.getClass().getResource("/com/jcorporate/expresso/kernel/management/test/TestLog4jConfig.xml"),
98                     TestSystemInitializer.getLogDirectory());
99         } catch (ConfigurationException ex) {
100             ex.printStackTrace();
101             fail("Unable to set up test case: " + ex.getMessage());
102         }
103
104
105     }
106
107     /**
108      * tearDown method for test case
109      */

110     protected void tearDown() {
111         globalContainer.destroy();
112         globalContainer = null;
113         System.gc();
114         System.runFinalization();
115     }
116
117     /**
118      * test for method locateComponent(..)
119      */

120     public void testLocateComponent() {
121         try {
122             //
123
//Basic tests
124
//
125
LocatorUtils lu = new LocatorUtils((ExpressoComponent) globalContainer);
126             ExpressoComponent test = lu.locateComponent("");
127             assertTrue("Root component must not be null", test != null);
128             assertTrue("Test should be the same as the global container", test == globalContainer);
129
130             test = lu.locateComponent("default");
131             assertTrue("Test component must be named default", "default".equals(test.getMetaData().getName()));
132             assertTrue("Test component must be a component container", test instanceof Containable);
133
134             test = lu.locateComponent("default.TestComponent");
135             assertTrue("Test component must be named TestComponent",
136                     "TestComponent".equals(test.getMetaData().getName()));
137
138             //
139
//Now test to make sure that LocatorUtils properly finds the root
140
//given being dumped anywhere on the tree.
141
//
142
lu = new LocatorUtils(test);
143
144             test = lu.locateComponent("");
145             assertTrue("Root component must not be null", test != null);
146             assertTrue("Test should be the same as the global container", test == globalContainer);
147
148             test = lu.locateComponent("default");
149             assertTrue("Test component must be named default", "default".equals(test.getMetaData().getName()));
150             assertTrue("Test component must be a component container", test instanceof Containable);
151
152             test = lu.locateComponent("default.TestComponent");
153             assertTrue("Test component must be named TestComponent",
154                     "TestComponent".equals(test.getMetaData().getName()));
155
156             //
157
//Now test bad input
158
//
159
try {
160                 test = lu.locateComponent("abcdkefjj");
161                 fail("Should have thrown an exception upon bad input");
162             } catch (IllegalArgumentException JavaDoc ex) {
163
164             }
165
166             try {
167                 test = lu.locateComponent("default..TestComponent");
168                 fail("Should have thrown an exception upon bad input");
169             } catch (IllegalArgumentException JavaDoc ex) {
170
171             }
172
173             try {
174                 test = lu.locateComponent(".default.TestComponent");
175                 fail("Should have thrown an exception upon bad input");
176             } catch (IllegalArgumentException JavaDoc ex) {
177
178             }
179
180             try {
181                 test = lu.locateComponent("default.ancdkiekel/");
182                 fail("Should have thrown an exception upon bad input");
183             } catch (IllegalArgumentException JavaDoc ex) {
184
185             }
186         } catch (Exception JavaDoc ex) {
187             ex.printStackTrace();
188             fail("Error locating components " + ex.getMessage());
189         }
190
191     }
192
193     /**
194      * test for method locateConfiguration(..)
195      */

196     public void testLocateConfiguration() {
197         LocatorUtils lu = new LocatorUtils((ExpressoComponent) globalContainer);
198         ComponentConfig test = lu.locateConfiguration("");
199         assertTrue("Root component must not be null", test != null);
200
201         test = lu.locateConfiguration("default");
202         assertTrue("Test component must be named default", "default".equals(test.getName()));
203
204         test = lu.locateConfiguration("default.TestComponent");
205         assertTrue("Test component must be named TestComponent",
206                 "TestComponent".equals(test.getName()));
207
208         //
209
//Now test bad input
210
//
211
try {
212             test = lu.locateConfiguration("abcdkefjj");
213             fail("Should have thrown an exception upon bad input");
214         } catch (IllegalArgumentException JavaDoc ex) {
215
216         }
217
218         try {
219             test = lu.locateConfiguration("default..TestComponent");
220             fail("Should have thrown an exception upon bad input");
221         } catch (IllegalArgumentException JavaDoc ex) {
222
223         }
224
225         try {
226             test = lu.locateConfiguration(".default.TestComponent");
227             fail("Should have thrown an exception upon bad input");
228         } catch (IllegalArgumentException JavaDoc ex) {
229
230         }
231
232         try {
233             test = lu.locateConfiguration("default.ancdkiekel/");
234             fail("Should have thrown an exception upon bad input");
235         } catch (IllegalArgumentException JavaDoc ex) {
236
237         }
238
239     }
240
241     /**
242      * test for method locateMetadata(..)
243      */

244     public void testLocateMetadata() {
245         LocatorUtils lu = new LocatorUtils((ExpressoComponent) globalContainer);
246         ComponentMetadata test = lu.locateMetadata("");
247         assertTrue("Root component must not be null", test != null);
248
249         test = lu.locateMetadata("default");
250         assertTrue("Test component must be named default", "default".equals(test.getName()));
251
252         test = lu.locateMetadata("default.TestComponent");
253         assertTrue("Test component must be named TestComponent",
254                 "TestComponent".equals(test.getName()));
255
256         //
257
//Now test bad input
258
//
259
try {
260             test = lu.locateMetadata("abcdkefjj");
261             fail("Should have thrown an exception upon bad input");
262         } catch (IllegalArgumentException JavaDoc ex) {
263
264         }
265
266         try {
267             test = lu.locateMetadata("default..TestComponent");
268             fail("Should have thrown an exception upon bad input");
269         } catch (IllegalArgumentException JavaDoc ex) {
270
271         }
272
273         try {
274             test = lu.locateMetadata(".default.TestComponent");
275             fail("Should have thrown an exception upon bad input");
276         } catch (IllegalArgumentException JavaDoc ex) {
277
278         }
279
280         try {
281             test = lu.locateMetadata("default.ancdkiekel/");
282             fail("Should have thrown an exception upon bad input");
283         } catch (IllegalArgumentException JavaDoc ex) {
284
285         }
286
287
288     }
289
290     /**
291      * Test to make sure components on the tree are getting correct paths
292      * back.
293      */

294     public void testCreatePath() {
295         LocatorUtils lu = new LocatorUtils((ExpressoComponent) globalContainer);
296         ExpressoComponent test = lu.locateComponent("");
297         assertTrue("Root component must not be null", test != null);
298         assertTrue("".equals(lu.getPath(test)));
299
300         test = lu.locateComponent("default");
301         assertTrue("default component must not be null", test != null);
302         assertTrue("default".equals(lu.getPath(test)));
303
304         test = lu.locateComponent("default.TestComponent");
305         assertTrue("default.TestComponent must not be null", test != null);
306         assertTrue("default.TestComponent".equals(lu.getPath(test)));
307
308     }
309
310     /**
311      * Executes the test case
312      */

313     public static void main(String JavaDoc[] argv) {
314         String JavaDoc[] testCaseList = {TestLocatorUtils.class.getName()};
315         junit.textui.TestRunner.main(testCaseList);
316     }
317 }
318
Popular Tags