001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.discovery.test;
018:
019: import java.net.URL;
020: import java.util.Properties;
021:
022: import junit.framework.Test;
023: import junit.framework.TestCase;
024: import junit.framework.TestSuite;
025:
026: import org.apache.commons.discovery.Resource;
027: import org.apache.commons.discovery.ResourceClass;
028: import org.apache.commons.discovery.ResourceClassIterator;
029: import org.apache.commons.discovery.ResourceIterator;
030: import org.apache.commons.discovery.jdk.JDKHooks;
031: import org.apache.commons.discovery.resource.ClassLoaders;
032: import org.apache.commons.discovery.resource.DiscoverResources;
033: import org.apache.commons.discovery.resource.classes.DiscoverClasses;
034: import org.apache.commons.discovery.tools.DefaultClassHolder;
035: import org.apache.commons.discovery.tools.DiscoverClass;
036: import org.apache.commons.discovery.tools.DiscoverSingleton;
037: import org.apache.commons.discovery.tools.ManagedProperties;
038: import org.apache.commons.discovery.tools.PropertiesHolder;
039: import org.apache.commons.discovery.tools.SPInterface;
040:
041: /**
042: * @author Richard A. Sitze
043: * @version $Revision: 480374 $
044: */
045: public class TestAll extends TestCase {
046: private static final int logLevel = org.apache.commons.discovery.log.SimpleLog.LOG_LEVEL_INFO;
047:
048: // org.apache.commons.discovery.log.SimpleLog.LOG_LEVEL_DEBUG;
049:
050: public TestAll(String testName) {
051: super (testName);
052: }
053:
054: public static Test suite() {
055: return new TestSuite(TestAll.class);
056: }
057:
058: public void testFindDefaultImpl_1() {
059: org.apache.commons.discovery.log.SimpleLog.setLevel(logLevel);
060:
061: TestInterface1 ti = null;
062:
063: try {
064: ti = (TestInterface1) DiscoverSingleton.find(
065: TestInterface1.class, TestImpl1_1.class.getName());
066:
067: assertTrue(ti.getClass().getName() + "!="
068: + TestImpl1_1.class.getName(), ti.getClass()
069: .getName().equals(TestImpl1_1.class.getName()));
070: } finally {
071: DiscoverSingleton.release();
072: }
073: }
074:
075: public void testFindDefaultImpl_2() {
076: org.apache.commons.discovery.log.SimpleLog.setLevel(logLevel);
077:
078: TestInterface1 ti = null;
079:
080: try {
081: ti = (TestInterface1) DiscoverSingleton.find(
082: TestInterface1.class, TestImpl1_2.class.getName());
083:
084: assertTrue(ti.getClass().getName() + "!="
085: + TestImpl1_2.class.getName(), ti.getClass()
086: .getName().equals(TestImpl1_2.class.getName()));
087: } finally {
088: DiscoverSingleton.release();
089: }
090: }
091:
092: public void testCache() {
093: org.apache.commons.discovery.log.SimpleLog.setLevel(logLevel);
094:
095: TestInterface1 ti = null;
096:
097: try {
098: ti = (TestInterface1) DiscoverSingleton.find(
099: TestInterface1.class, TestImpl1_1.class.getName());
100:
101: assertTrue("1. " + ti.getClass().getName() + "!="
102: + TestImpl1_1.class.getName(), ti.getClass()
103: .getName().equals(TestImpl1_1.class.getName()));
104:
105: // no release, should get cached value..
106:
107: ti = (TestInterface1) DiscoverSingleton.find(
108: TestInterface1.class, TestImpl1_2.class.getName());
109:
110: // factory should be cached
111: assertTrue("2. " + ti.getClass().getName() + "!="
112: + TestImpl1_1.class.getName(), ti.getClass()
113: .getName().equals(TestImpl1_1.class.getName()));
114: } finally {
115: DiscoverSingleton.release();
116: }
117: }
118:
119: public void testRelease() {
120: org.apache.commons.discovery.log.SimpleLog.setLevel(logLevel);
121:
122: TestInterface1 ti = null;
123:
124: try {
125: ti = (TestInterface1) DiscoverSingleton.find(
126: TestInterface1.class, TestImpl1_1.class.getName());
127:
128: assertTrue("1. " + ti.getClass().getName() + "!="
129: + TestImpl1_1.class.getName(), ti.getClass()
130: .getName().equals(TestImpl1_1.class.getName()));
131:
132: DiscoverSingleton.release();
133:
134: ti = (TestInterface1) DiscoverSingleton.find(
135: TestInterface1.class, TestImpl1_2.class.getName());
136:
137: // factory should be cached
138: assertTrue("2. " + ti.getClass().getName() + "!="
139: + TestImpl1_2.class.getName(), ti.getClass()
140: .getName().equals(TestImpl1_2.class.getName()));
141: } finally {
142: DiscoverSingleton.release();
143: }
144: }
145:
146: public void testFindPropertyImpl_1() {
147: org.apache.commons.discovery.log.SimpleLog.setLevel(logLevel);
148:
149: TestInterface1 ti = null;
150:
151: try {
152: Properties props = new Properties();
153:
154: props.setProperty(TestInterface1.class.getName(),
155: TestImpl1_2.class.getName());
156:
157: ti = (TestInterface1) DiscoverSingleton.find(
158: TestInterface1.class, props);
159:
160: assertTrue(ti.getClass().getName() + "!="
161: + TestImpl1_2.class.getName(), ti.getClass()
162: .getName().equals(TestImpl1_2.class.getName()));
163: } finally {
164: DiscoverSingleton.release();
165: }
166: }
167:
168: public void testMyFactoryManagedProperty() {
169: org.apache.commons.discovery.log.SimpleLog.setLevel(logLevel);
170:
171: TestInterface1 ti = null;
172:
173: try {
174: ManagedProperties.setProperty(TestInterface1.class
175: .getName(), TestImpl1_2.class.getName());
176:
177: ti = (TestInterface1) DiscoverSingleton
178: .find(TestInterface1.class);
179:
180: assertTrue(ti.getClass().getName() + "!="
181: + TestImpl1_2.class.getName(), ti.getClass()
182: .getName().equals(TestImpl1_2.class.getName()));
183: } finally {
184: DiscoverSingleton.release();
185:
186: /**
187: * Cleanup, don't want to affect next test..
188: */
189: ManagedProperties.setProperty(TestInterface1.class
190: .getName(), null);
191: }
192: }
193:
194: public void testFindPropFileDefault() {
195: org.apache.commons.discovery.log.SimpleLog.setLevel(logLevel);
196:
197: TestInterface1 ti = null;
198:
199: try {
200: ti = (TestInterface1) DiscoverSingleton
201: .find(null, new SPInterface(TestInterface1.class),
202: new PropertiesHolder(
203: "TestInterface.properties"),
204: new DefaultClassHolder(TestImpl1_2.class
205: .getName()));
206:
207: assertTrue(ti.getClass().getName() + "!="
208: + TestImpl1_1.class.getName(), ti.getClass()
209: .getName().equals(TestImpl1_1.class.getName()));
210: } finally {
211: DiscoverSingleton.release();
212: }
213: }
214:
215: public void testFindServiceFileDefault() {
216: org.apache.commons.discovery.log.SimpleLog.setLevel(logLevel);
217:
218: TestInterface2 ti = null;
219:
220: try {
221: ti = (TestInterface2) DiscoverSingleton
222: .find(null, new SPInterface(TestInterface2.class),
223: null, new DefaultClassHolder(
224: TestImpl2_2.class.getName()));
225:
226: assertTrue(ti.getClass().getName() + "!="
227: + TestImpl2_1.class.getName(), ti.getClass()
228: .getName().equals(TestImpl2_1.class.getName()));
229: } finally {
230: DiscoverSingleton.release();
231: }
232: }
233:
234: public void testLowLevelFind() {
235: org.apache.commons.discovery.log.SimpleLog.setLevel(logLevel);
236:
237: ClassLoaders loaders = ClassLoaders.getAppLoaders(
238: TestInterface2.class, getClass(), false);
239: String name = "org.apache.commons.discovery.test.TestImpl2_1";
240:
241: DiscoverClasses discovery = new DiscoverClasses(loaders);
242: ResourceClassIterator iter = discovery
243: .findResourceClasses(name);
244: while (iter.hasNext()) {
245: ResourceClass resource = iter.nextResourceClass();
246: try {
247: Class implClass = resource.loadClass();
248: if (implClass != null) {
249: assertEquals(
250: "org.apache.commons.discovery.test.TestImpl2_1",
251: implClass.getName());
252: return;
253: }
254: } catch (Exception e) {
255: fail("Could not load service: " + resource);
256: }
257: }
258: fail("failed to load class resource: " + name);
259: }
260:
261: public void testFindResources() {
262: org.apache.commons.discovery.log.SimpleLog.setLevel(logLevel);
263:
264: ClassLoaders loaders = new ClassLoaders();
265:
266: /**
267: * To many class loaders when searching for multiple
268: * resources means that we can find the same (same URL)
269: * resource for each loader...
270: * let's keep this to a minimum.
271: */
272: ClassLoader cl = getClass().getClassLoader();
273: if (cl != null)
274: loaders.put(getClass().getClassLoader(), true);
275: else
276: loaders.put(JDKHooks.getJDKHooks().getSystemClassLoader(),
277: true);
278:
279: String name = "testResource";
280:
281: String partialPaths[] = { "/test/", "/testAlt1/", "/testAlt2/" };
282: int expected = partialPaths.length;
283:
284: DiscoverResources discovery = new DiscoverResources(loaders);
285: ResourceIterator iter = discovery.findResources(name);
286: int count = 0;
287:
288: while (iter.hasNext()) {
289: Resource resource = iter.nextResource();
290: URL url = resource.getResource();
291: if (url != null) {
292: if (url.getFile().indexOf(partialPaths[count]) == -1) {
293: fail(url + " does not contain "
294: + partialPaths[count]);
295: }
296: count++;
297: }
298: }
299:
300: if (count != expected) {
301: fail("located " + count
302: + " resources, failed to locate all " + expected
303: + " resources: " + name);
304: }
305: }
306:
307: public void testViaDiscoverClass() {
308: org.apache.commons.discovery.log.SimpleLog.setLevel(logLevel);
309:
310: ClassLoaders loaders = ClassLoaders.getAppLoaders(
311: TestInterface2.class, getClass(), false);
312:
313: DiscoverClass discover = new DiscoverClass(loaders);
314: Class implClass = discover.find(TestInterface2.class);
315:
316: assertTrue("Failed to find an implementation class",
317: implClass != null);
318: assertEquals("org.apache.commons.discovery.test.TestImpl2_1",
319: implClass.getName());
320:
321: }
322:
323: /**
324: * This allows the tests to run as a standalone application.
325: */
326: public static void main(String args[]) {
327: String[] testCaseName = { TestAll.class.getName() };
328: junit.textui.TestRunner.main(testCaseName);
329: }
330: }
|