1/* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.test.runner; 18 19import android.util.Log; 20 21import org.junit.runner.Description; 22import org.junit.runner.notification.Failure; 23 24import java.io.PrintStream; 25import java.lang.reflect.Method; 26import java.util.LinkedList; 27import java.util.List; 28 29/** 30 * A class for loading JUnit3 and JUnit4 test classes given a set of potential class names. 31 */ 32public class TestLoader { 33 34 private static final String LOG_TAG = "TestLoader"; 35 36 private List<Class<?>> mLoadedClasses = new LinkedList<Class<?>>(); 37 private List<Failure> mLoadFailures = new LinkedList<Failure>(); 38 39 private PrintStream mWriter; 40 41 /** 42 * Create a {@link TestLoader}. 43 * 44 * @param writer a {@link PrintStream} used for reporting errors. 45 */ 46 public TestLoader(PrintStream writer) { 47 mWriter = writer; 48 } 49 50 /** 51 * Loads the test class from the given class name. 52 * <p/> 53 * Will store the result internally. Successfully loaded classes can be retrieved via 54 * {@link #getLoadedClasses()}, failures via {@link #getLoadFailures()}. 55 * 56 * @param className the class name to attempt to load 57 * @return the loaded class or null. 58 */ 59 public Class<?> loadClass(String className) { 60 Class<?> loadedClass = doLoadClass(className); 61 if (loadedClass != null) { 62 mLoadedClasses.add(loadedClass); 63 } 64 return loadedClass; 65 } 66 67 private Class<?> doLoadClass(String className) { 68 try { 69 return Class.forName(className); 70 } catch (ClassNotFoundException e) { 71 String errMsg = String.format("Could not find class: %s", className); 72 Log.e(LOG_TAG, errMsg); 73 mWriter.println(errMsg); 74 Description description = Description.createSuiteDescription(className); 75 Failure failure = new Failure(description, e); 76 mLoadFailures.add(failure); 77 } 78 return null; 79 } 80 81 /** 82 * Loads the test class from the given class name. 83 * <p/> 84 * Similar to {@link #loadClass(String, PrintStream))}, but will ignore classes that are 85 * not tests. 86 * 87 * @param className the class name to attempt to load 88 * @return the loaded class or null. 89 */ 90 public Class<?> loadIfTest(String className) { 91 Class<?> loadedClass = doLoadClass(className); 92 if (loadedClass != null && isTestClass(loadedClass)) { 93 mLoadedClasses.add(loadedClass); 94 return loadedClass; 95 } 96 return null; 97 } 98 99 /** 100 * @return whether this {@link TestLoader} contains any loaded classes or load failures. 101 */ 102 public boolean isEmpty() { 103 return mLoadedClasses.isEmpty() && mLoadFailures.isEmpty(); 104 } 105 106 /** 107 * Get the {@link List) of classes successfully loaded via 108 * {@link #loadTest(String, PrintStream)} calls. 109 */ 110 public List<Class<?>> getLoadedClasses() { 111 return mLoadedClasses; 112 } 113 114 /** 115 * Get the {@link List) of {@link Failure} that occurred during 116 * {@link #loadTest(String, PrintStream)} calls. 117 */ 118 public List<Failure> getLoadFailures() { 119 return mLoadFailures; 120 } 121 122 /** 123 * Determines if given class is a valid test class. 124 * 125 * @param loadedClass 126 * @return <code>true</code> if loadedClass is a test 127 */ 128 private boolean isTestClass(Class<?> loadedClass) { 129 // TODO: try to find upstream junit calls to replace these checks 130 if (junit.framework.Test.class.isAssignableFrom(loadedClass)) { 131 return true; 132 } 133 // TODO: look for a 'suite' method? 134 if (loadedClass.isAnnotationPresent(org.junit.runner.RunWith.class)) { 135 return true; 136 } 137 for (Method testMethod : loadedClass.getMethods()) { 138 if (testMethod.isAnnotationPresent(org.junit.Test.class)) { 139 return true; 140 } 141 } 142 Log.v(LOG_TAG, String.format("Skipping class %s: not a test", loadedClass.getName())); 143 return false; 144 } 145} 146