1/* 2 * Copyright (C) 2008 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 android.test.suitebuilder; 18 19import junit.framework.TestCase; 20 21import java.lang.annotation.Annotation; 22import java.lang.reflect.Constructor; 23import java.lang.reflect.InvocationTargetException; 24import java.lang.reflect.Method; 25 26/** 27 * Represents a test to be run. Can be constructed without instantiating the TestCase or even 28 * loading the class. 29 * 30 * @deprecated New tests should be written using the 31 * <a href="{@docRoot}tools/testing-support-library/index.html">Android Testing Support Library</a>. 32 */ 33@Deprecated 34public class TestMethod { 35 36 private final String enclosingClassname; 37 private final String testMethodName; 38 private final Class<? extends TestCase> enclosingClass; 39 40 public TestMethod(Method method, Class<? extends TestCase> enclosingClass) { 41 this(method.getName(), enclosingClass); 42 } 43 44 public TestMethod(String methodName, Class<? extends TestCase> enclosingClass) { 45 this.enclosingClass = enclosingClass; 46 this.enclosingClassname = enclosingClass.getName(); 47 this.testMethodName = methodName; 48 } 49 50 public TestMethod(TestCase testCase) { 51 this(testCase.getName(), testCase.getClass()); 52 } 53 54 public String getName() { 55 return testMethodName; 56 } 57 58 public String getEnclosingClassname() { 59 return enclosingClassname; 60 } 61 62 public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { 63 try { 64 return getEnclosingClass().getMethod(getName()).getAnnotation(annotationClass); 65 } catch (NoSuchMethodException e) { 66 return null; 67 } 68 } 69 70 @SuppressWarnings("unchecked") 71 public Class<? extends TestCase> getEnclosingClass() { 72 return enclosingClass; 73 } 74 75 public TestCase createTest() 76 throws InvocationTargetException, IllegalAccessException, InstantiationException { 77 return instantiateTest(enclosingClass, testMethodName); 78 } 79 80 @SuppressWarnings("unchecked") 81 private TestCase instantiateTest(Class testCaseClass, String testName) 82 throws InvocationTargetException, IllegalAccessException, InstantiationException { 83 Constructor[] constructors = testCaseClass.getConstructors(); 84 85 if (constructors.length == 0) { 86 return instantiateTest(testCaseClass.getSuperclass(), testName); 87 } else { 88 for (Constructor constructor : constructors) { 89 Class[] params = constructor.getParameterTypes(); 90 if (noargsConstructor(params)) { 91 TestCase test = ((Constructor<? extends TestCase>) constructor).newInstance(); 92 // JUnit will run just the one test if you call 93 // {@link TestCase#setName(String)} 94 test.setName(testName); 95 return test; 96 } else if (singleStringConstructor(params)) { 97 return ((Constructor<? extends TestCase>) constructor) 98 .newInstance(testName); 99 } 100 } 101 } 102 throw new RuntimeException("Unable to locate a constructor for " 103 + testCaseClass.getName()); 104 } 105 106 private boolean singleStringConstructor(Class[] params) { 107 return (params.length == 1) && (params[0].equals(String.class)); 108 } 109 110 private boolean noargsConstructor(Class[] params) { 111 return params.length == 0; 112 } 113 114 @Override 115 public boolean equals(Object o) { 116 if (this == o) { 117 return true; 118 } 119 if (o == null || getClass() != o.getClass()) { 120 return false; 121 } 122 123 TestMethod that = (TestMethod) o; 124 125 if (enclosingClassname != null 126 ? !enclosingClassname.equals(that.enclosingClassname) 127 : that.enclosingClassname != null) { 128 return false; 129 } 130 if (testMethodName != null 131 ? !testMethodName.equals(that.testMethodName) 132 : that.testMethodName != null) { 133 return false; 134 } 135 return true; 136 } 137 138 @Override 139 public int hashCode() { 140 int result; 141 result = (enclosingClassname != null ? enclosingClassname.hashCode() : 0); 142 result = 31 * result + (testMethodName != null ? testMethodName.hashCode() : 0); 143 return result; 144 } 145 146 @Override 147 public String toString() { 148 return enclosingClassname + "." + testMethodName; 149 } 150} 151