KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > discovery > test > TestAll


1 /*
2  * $Header$
3  * $Revision: 1.2 $
4  * $Date: 2002/01/17 22:55:43 $
5  *
6  * ====================================================================
7  *
8  * The Apache Software License, Version 1.1
9  *
10  * Copyright (c) 1999-2001 The Apache Software Foundation. All rights
11  * reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer.
19  *
20  * 2. Redistributions in binary form must reproduce the above copyright
21  * notice, this list of conditions and the following disclaimer in
22  * the documentation and/or other materials provided with the
23  * distribution.
24  *
25  * 3. The end-user documentation included with the redistribution, if
26  * any, must include the following acknowlegement:
27  * "This product includes software developed by the
28  * Apache Software Foundation (http://www.apache.org/)."
29  * Alternately, this acknowlegement may appear in the software itself,
30  * if and wherever such third-party acknowlegements normally appear.
31  *
32  * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33  * Foundation" must not be used to endorse or promote products derived
34  * from this software without prior written permission. For written
35  * permission, please contact apache@apache.org.
36  *
37  * 5. Products derived from this software may not be called "Apache"
38  * nor may "Apache" appear in their names without prior written
39  * permission of the Apache Group.
40  *
41  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  * ====================================================================
54  *
55  * This software consists of voluntary contributions made by many
56  * individuals on behalf of the Apache Software Foundation. For more
57  * information on the Apache Software Foundation, please see
58  * <http://www.apache.org/>.
59  *
60  */

61  
62  
63 package org.apache.commons.discovery.test;
64
65
66 import java.util.Properties JavaDoc;
67
68 import junit.framework.Test;
69 import junit.framework.TestCase;
70 import junit.framework.TestSuite;
71 import org.apache.commons.discovery.tools.DefaultClassHolder;
72 import org.apache.commons.discovery.tools.DiscoverSingleton;
73 import org.apache.commons.discovery.tools.ManagedProperties;
74 import org.apache.commons.discovery.tools.PropertiesHolder;
75 import org.apache.commons.discovery.tools.SPInterface;
76
77
78 /**
79   * @author Richard A. Sitze
80   * @version $Revision: 1.2 $
81  */

82 public class TestAll extends TestCase {
83     private static final int logLevel =
84         org.apache.commons.discovery.log.SimpleLog.LOG_LEVEL_INFO;
85
86     
87     public TestAll(String JavaDoc testName) {
88         super(testName);
89     }
90     
91     public static Test suite() {
92         return new TestSuite(TestAll.class);
93     }
94
95     public void testFindDefaultImpl_1() {
96         org.apache.commons.discovery.log.SimpleLog.setLevel(logLevel);
97
98         TestInterface1 ti = null;
99         
100         try {
101             ti = (TestInterface1)DiscoverSingleton.find(TestInterface1.class,
102                                                         TestImpl1_1.class.getName());
103
104             assertTrue(ti.getClass().getName() + "!=" + TestImpl1_1.class.getName(),
105                        ti.getClass().getName().equals(TestImpl1_1.class.getName()));
106         } finally {
107             DiscoverSingleton.release();
108         }
109     }
110     
111     public void testFindDefaultImpl_2() {
112         org.apache.commons.discovery.log.SimpleLog.setLevel(logLevel);
113
114         TestInterface1 ti = null;
115
116         try {
117             ti = (TestInterface1)DiscoverSingleton.find(TestInterface1.class,
118                                                         TestImpl1_2.class.getName());
119
120             assertTrue(ti.getClass().getName() + "!=" + TestImpl1_2.class.getName(),
121                        ti.getClass().getName().equals(TestImpl1_2.class.getName()));
122         } finally {
123             DiscoverSingleton.release();
124         }
125     }
126     
127     public void testCache() {
128         org.apache.commons.discovery.log.SimpleLog.setLevel(logLevel);
129
130         TestInterface1 ti = null;
131         
132         try {
133             ti = (TestInterface1)DiscoverSingleton.find(TestInterface1.class,
134                                                         TestImpl1_1.class.getName());
135
136             assertTrue("1. " + ti.getClass().getName() + "!=" + TestImpl1_1.class.getName(),
137                        ti.getClass().getName().equals(TestImpl1_1.class.getName()));
138             
139             // no release, should get cached value..
140

141             ti = (TestInterface1)DiscoverSingleton.find(TestInterface1.class,
142                                                         TestImpl1_2.class.getName());
143
144             // factory should be cached
145
assertTrue("2. " + ti.getClass().getName() + "!=" + TestImpl1_1.class.getName(),
146                        ti.getClass().getName().equals(TestImpl1_1.class.getName()));
147         } finally {
148             DiscoverSingleton.release();
149         }
150     }
151     
152     public void testRelease() {
153         org.apache.commons.discovery.log.SimpleLog.setLevel(logLevel);
154
155         TestInterface1 ti = null;
156         
157         try {
158             ti = (TestInterface1)DiscoverSingleton.find(TestInterface1.class,
159                                                         TestImpl1_1.class.getName());
160
161             assertTrue("1. " + ti.getClass().getName() + "!=" + TestImpl1_1.class.getName(),
162                        ti.getClass().getName().equals(TestImpl1_1.class.getName()));
163             
164             DiscoverSingleton.release();
165             
166             ti = (TestInterface1)DiscoverSingleton.find(TestInterface1.class,
167                                                         TestImpl1_2.class.getName());
168
169             // factory should be cached
170
assertTrue("2. " + ti.getClass().getName() + "!=" + TestImpl1_2.class.getName(),
171                        ti.getClass().getName().equals(TestImpl1_2.class.getName()));
172         } finally {
173             DiscoverSingleton.release();
174         }
175     }
176     
177     public void testFindPropertyImpl_1() {
178         org.apache.commons.discovery.log.SimpleLog.setLevel(logLevel);
179
180         TestInterface1 ti = null;
181
182         try {
183             Properties JavaDoc props = new Properties JavaDoc();
184             
185             props.setProperty(TestInterface1.class.getName(),
186                               TestImpl1_2.class.getName());
187             
188             ti = (TestInterface1)DiscoverSingleton.find(TestInterface1.class, props);
189
190             assertTrue(ti.getClass().getName() + "!=" + TestImpl1_2.class.getName(),
191                        ti.getClass().getName().equals(TestImpl1_2.class.getName()));
192         } finally {
193             DiscoverSingleton.release();
194         }
195     }
196     
197     public void testMyFactoryManagedProperty() {
198         org.apache.commons.discovery.log.SimpleLog.setLevel(logLevel);
199
200         TestInterface1 ti = null;
201
202         try {
203             ManagedProperties.setProperty(TestInterface1.class.getName(),
204                                           TestImpl1_2.class.getName());
205                               
206             ti = (TestInterface1)DiscoverSingleton.find(TestInterface1.class);
207
208             assertTrue(ti.getClass().getName() + "!=" + TestImpl1_2.class.getName(),
209                        ti.getClass().getName().equals(TestImpl1_2.class.getName()));
210         } finally {
211             DiscoverSingleton.release();
212             
213             /**
214              * Cleanup, don't want to affect next test..
215              */

216             ManagedProperties.setProperty(TestInterface1.class.getName(), null);
217         }
218     }
219     
220
221     public void testFindPropFileDefault() {
222         org.apache.commons.discovery.log.SimpleLog.setLevel(logLevel);
223
224         TestInterface1 ti = null;
225         
226         try {
227             ti = (TestInterface1)DiscoverSingleton.find(null,
228                                    new SPInterface(TestInterface1.class),
229                                    new PropertiesHolder("TestInterface.properties"),
230                                    new DefaultClassHolder(TestImpl1_2.class.getName()));
231
232             assertTrue(ti.getClass().getName() + "!=" + TestImpl1_1.class.getName(),
233                        ti.getClass().getName().equals(TestImpl1_1.class.getName()));
234         } finally {
235             DiscoverSingleton.release();
236         }
237     }
238
239     public void testFindServiceFileDefault() {
240 // org.apache.commons.discovery.log.SimpleLog.setLevel(org.apache.commons.discovery.log.SimpleLog.LOG_LEVEL_DEBUG);
241
org.apache.commons.discovery.log.SimpleLog.setLevel(logLevel);
242
243         TestInterface2 ti = null;
244         
245         try {
246             ti = (TestInterface2)DiscoverSingleton.find(null,
247                                    new SPInterface(TestInterface2.class),
248                                    null,
249                                    new DefaultClassHolder(TestImpl2_2.class.getName()));
250
251             assertTrue(ti.getClass().getName() + "!=" + TestImpl2_1.class.getName(),
252                        ti.getClass().getName().equals(TestImpl2_1.class.getName()));
253         } finally {
254             DiscoverSingleton.release();
255         }
256     }
257
258     /**
259      * This allows the tests to run as a standalone application.
260      */

261     public static void main(String JavaDoc args[]) {
262         String JavaDoc[] testCaseName = { TestAll.class.getName() };
263         junit.textui.TestRunner.main(testCaseName);
264     }
265 }
266
Popular Tags