2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Eclipse Public License, Version 1.0 (the "License"); you
5 * may not use this file except in compliance with the License. You may obtain a
6 * copy of the License at
8 * http://www.eclipse.org/org/documents/epl-v10.php
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
16 package com.android.ide.eclipse.tests;
18 import org.eclipse.core.runtime.Plugin;
20 import java.lang.reflect.Modifier;
22 import java.util.Enumeration;
24 import junit.framework.TestCase;
25 import junit.framework.TestSuite;
28 * Class for collecting all test cases in an eclipse plugin
31 public class EclipseTestCollector {
36 public EclipseTestCollector() {
41 * Searches through given plugin, adding all TestCase classes to given suite
42 * @param suite - TestSuite to add to
43 * @param plugin - Plugin to search for tests
44 * @param expectedPackage - expected package for tests. Only test classes
45 * that start with this package name will be added to suite
47 public void addTestCases(TestSuite suite, Plugin plugin, String expectedPackage) {
49 Enumeration<?> entries = plugin.getBundle().findEntries("/", "*.class", true);
51 while (entries.hasMoreElements()) {
52 URL entry = (URL)entries.nextElement();
53 String filePath = entry.getPath().replace(".class", "");
55 Class<?> testClass = getClass(filePath, expectedPackage);
56 if (isTestClass(testClass)) {
57 suite.addTestSuite(testClass);
60 catch (ClassNotFoundException e) {
61 // ignore, this is not the class we're looking for
62 //sLogger.log(Level.INFO, "Could not load class " + filePath);
69 * Returns true if given class should be added to suite
71 protected boolean isTestClass(Class<?> testClass) {
72 return TestCase.class.isAssignableFrom(testClass) &&
73 Modifier.isPublic(testClass.getModifiers()) &&
74 hasPublicConstructor(testClass);
78 * Returns true if given class has a public constructor
80 protected boolean hasPublicConstructor(Class<?> testClass) {
82 TestSuite.getTestConstructor(testClass);
83 } catch(NoSuchMethodException e) {
90 * Load the class given by the plugin aka bundle file path
91 * @param filePath - path of class in bundle
92 * @param expectedPackage - expected package of class
93 * @throws ClassNotFoundException
95 protected Class<?> getClass(String filePath, String expectedPackage) throws ClassNotFoundException {
96 String dotPath = filePath.replace('/', '.');
97 // remove the output folders, by finding where package name starts
98 int index = dotPath.indexOf(expectedPackage);
100 throw new ClassNotFoundException();
102 String packagePath = dotPath.substring(index);
103 return Class.forName(packagePath);