KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > junit > runtime > RemotePluginTestRunner


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.junit.runtime;
12
13 import java.io.IOException JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.Enumeration JavaDoc;
16 import java.util.Locale JavaDoc;
17
18 import org.eclipse.core.runtime.Platform;
19 import org.eclipse.jdt.internal.junit.runner.RemoteTestRunner;
20 import org.osgi.framework.Bundle;
21
22 /**
23  * Runs JUnit tests contained inside a plugin.
24  */

25 public class RemotePluginTestRunner extends RemoteTestRunner {
26
27     private String JavaDoc fTestPluginName;
28     private ClassLoader JavaDoc fLoaderClassLoader;
29     
30     class BundleClassLoader extends ClassLoader JavaDoc {
31           private Bundle bundle;
32           public BundleClassLoader(Bundle target) {
33             this.bundle = target;
34           }
35           protected Class JavaDoc findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
36             return bundle.loadClass(name);
37           }
38           protected URL JavaDoc findResource(String JavaDoc name) {
39             return bundle.getResource(name);
40           }
41           protected Enumeration JavaDoc findResources(String JavaDoc name) throws IOException JavaDoc {
42                return bundle.getResources(name);
43           }
44     }
45     
46     /**
47      * The main entry point. Supported arguments in addition
48      * to the ones supported by RemoteTestRunner:
49      * <pre>
50      * -testpluginname: the name of the plugin containing the tests.
51       * </pre>
52      * @see RemoteTestRunner
53      */

54
55     public static void main(String JavaDoc[] args) {
56         RemotePluginTestRunner testRunner= new RemotePluginTestRunner();
57         testRunner.init(args);
58         testRunner.run();
59     }
60     
61     /**
62      * Returns the Plugin class loader of the plugin containing the test.
63      * @see RemoteTestRunner#getTestClassLoader()
64      */

65     protected ClassLoader JavaDoc getTestClassLoader() {
66         final String JavaDoc pluginId = fTestPluginName;
67         return getClassLoader(pluginId);
68     }
69
70     public ClassLoader JavaDoc getClassLoader(final String JavaDoc pluginId) {
71         Bundle bundle = Platform.getBundle(pluginId);
72         if (bundle == null)
73             throw new IllegalArgumentException JavaDoc("No Classloader found for plug-in " + pluginId); //$NON-NLS-1$
74
return new BundleClassLoader(bundle);
75     }
76
77     public void init(String JavaDoc[] args) {
78         readPluginArgs(args);
79         defaultInit(args);
80     }
81
82     public void readPluginArgs(String JavaDoc[] args) {
83         for (int i = 0; i < args.length; i++) {
84             if (isFlag(args, i, "-testpluginname")) //$NON-NLS-1$
85
fTestPluginName = args[i + 1];
86             
87             if (isFlag(args, i, "-loaderpluginname")) //$NON-NLS-1$
88
fLoaderClassLoader = getClassLoader(args[i + 1]);
89         }
90
91         if (fTestPluginName == null)
92             throw new IllegalArgumentException JavaDoc("Parameter -testpluginnname not specified."); //$NON-NLS-1$
93

94         if (fLoaderClassLoader == null)
95             fLoaderClassLoader = getClass().getClassLoader();
96     }
97
98     protected Class JavaDoc loadTestLoaderClass(String JavaDoc className) throws ClassNotFoundException JavaDoc {
99         return fLoaderClassLoader.loadClass(className);
100     }
101     
102     private boolean isFlag(String JavaDoc[] args, int i, final String JavaDoc wantedFlag) {
103         String JavaDoc lowerCase = args[i].toLowerCase(Locale.ENGLISH);
104         return lowerCase.equals(wantedFlag) && i < args.length - 1;
105     }
106 }
107
Popular Tags