OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / sdk / eclipse / plugins / com.android.ide.eclipse.tests / src / com / android / ide / eclipse / tests / EclipseTestCollector.java
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * 
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
7  * 
8  * http://www.eclipse.org/org/documents/epl-v10.php
9  * 
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
14  * the License.
15  */
16 package com.android.ide.eclipse.tests;
17
18 import org.eclipse.core.runtime.Plugin;
19
20 import java.lang.reflect.Modifier;
21 import java.net.URL;
22 import java.util.Enumeration;
23
24 import junit.framework.TestCase;
25 import junit.framework.TestSuite;
26
27 /**
28  * Class for collecting all test cases in an eclipse plugin
29  * 
30  */
31 public class EclipseTestCollector {
32
33     /**
34      * Constructor
35      */
36     public EclipseTestCollector() {
37         
38     }
39
40     /**
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
46      */
47     public void addTestCases(TestSuite suite, Plugin plugin, String expectedPackage) {
48         if (plugin != null) {
49             Enumeration<?> entries = plugin.getBundle().findEntries("/", "*.class", true);
50     
51             while (entries.hasMoreElements()) {
52                 URL entry = (URL)entries.nextElement();
53                 String filePath = entry.getPath().replace(".class", "");
54                 try {
55                   Class<?> testClass = getClass(filePath, expectedPackage);
56                   if (isTestClass(testClass)) {
57                       suite.addTestSuite(testClass);
58                   }
59                 } 
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);
63               }
64             }
65         }
66     }
67     
68     /**
69      * Returns true if given class should be added to suite
70      */
71     protected boolean isTestClass(Class<?> testClass) {
72         return TestCase.class.isAssignableFrom(testClass) &&
73           Modifier.isPublic(testClass.getModifiers()) &&
74           hasPublicConstructor(testClass);
75     }
76     
77     /**
78      * Returns true if given class has a public constructor
79      */
80     protected boolean hasPublicConstructor(Class<?> testClass) {
81         try {
82             TestSuite.getTestConstructor(testClass);
83         } catch(NoSuchMethodException e) {
84             return false;
85         }
86         return true;
87     }
88     
89     /**
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
94      */
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);
99         if (index == -1) {
100             throw new ClassNotFoundException();
101         }
102         String packagePath = dotPath.substring(index);
103         return Class.forName(packagePath);   
104     }
105 }