blob: 367fb93072c965bbc254a80fb4513ecfddc9c9de [file] [log] [blame]
The Android Open Source Projectf8057102009-03-15 16:47:16 -07001/*
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
Kenny Rootb5b47cb2013-02-27 17:03:16 -080017import org.junit.runner.RunWith;
Brian Muramatsu7f64e852011-02-17 16:52:16 -080018import org.w3c.dom.Document;
19import org.w3c.dom.Element;
20import org.w3c.dom.Node;
21import org.w3c.dom.NodeList;
22
Brian Muramatsu7f64e852011-02-17 16:52:16 -080023import vogar.ExpectationStore;
Brian Muramatsu7f64e852011-02-17 16:52:16 -080024
The Android Open Source Projectf8057102009-03-15 16:47:16 -070025import java.io.BufferedReader;
The Android Open Source Projectf8057102009-03-15 16:47:16 -070026import java.io.File;
27import java.io.FileInputStream;
Urs Grobfc77f6e2009-04-17 02:07:14 -070028import java.io.FileReader;
29import java.io.IOException;
30import java.lang.annotation.Annotation;
The Android Open Source Projectf8057102009-03-15 16:47:16 -070031import java.lang.reflect.Method;
Brian Carlstrom9f2dab82011-04-01 15:47:17 -070032import java.lang.reflect.Modifier;
The Android Open Source Projectf8057102009-03-15 16:47:16 -070033import java.util.ArrayList;
Brian Carlstrom9f2dab82011-04-01 15:47:17 -070034import java.util.Enumeration;
The Android Open Source Projectf8057102009-03-15 16:47:16 -070035import java.util.HashSet;
36import java.util.Iterator;
Urs Grobfc77f6e2009-04-17 02:07:14 -070037import java.util.LinkedHashMap;
The Android Open Source Projectf8057102009-03-15 16:47:16 -070038import java.util.Map;
39import java.util.Set;
Brian Carlstrom9f2dab82011-04-01 15:47:17 -070040import java.util.jar.JarEntry;
41import java.util.jar.JarFile;
The Android Open Source Projectf8057102009-03-15 16:47:16 -070042
43import javax.xml.parsers.DocumentBuilderFactory;
44import javax.xml.parsers.ParserConfigurationException;
45
The Android Open Source Projectf8057102009-03-15 16:47:16 -070046import junit.framework.TestCase;
The Android Open Source Projectf8057102009-03-15 16:47:16 -070047
The Android Open Source Projectf8057102009-03-15 16:47:16 -070048public class CollectAllTests extends DescriptionGenerator {
49
Brian Carlstrom022aff42011-05-17 23:16:51 -070050 private static final String ATTRIBUTE_RUNNER = "runner";
51 private static final String ATTRIBUTE_PACKAGE = "appPackageName";
52 private static final String ATTRIBUTE_NS = "appNameSpace";
53 private static final String ATTRIBUTE_TARGET = "targetNameSpace";
54 private static final String ATTRIBUTE_TARGET_BINARY = "targetBinaryName";
55 private static final String ATTRIBUTE_HOST_SIDE_ONLY = "hostSideOnly";
56 private static final String ATTRIBUTE_VM_HOST_TEST = "vmHostTest";
57 private static final String ATTRIBUTE_JAR_PATH = "jarPath";
58 private static final String ATTRIBUTE_JAVA_PACKAGE_FILTER = "javaPackageFilter";
The Android Open Source Projectf8057102009-03-15 16:47:16 -070059
Brian Carlstrom022aff42011-05-17 23:16:51 -070060 private static final String JAR_PATH = "LOCAL_JAR_PATH :=";
61 private static final String TEST_TYPE = "LOCAL_TEST_TYPE :";
The Android Open Source Projectf8057102009-03-15 16:47:16 -070062
63 public static void main(String[] args) {
Brian Carlstrom022aff42011-05-17 23:16:51 -070064 if (args.length < 4 || args.length > 6) {
65 System.err.println("usage: CollectAllTests <output-file> <manifest-file> <jar-file> "
66 + "<java-package> [expectation-dir [makefile-file]]");
67 if (args.length != 0) {
68 System.err.println("received:");
69 for (String arg : args) {
70 System.err.println(" " + arg);
Brian Carlstrom9f2dab82011-04-01 15:47:17 -070071 }
The Android Open Source Projectf8057102009-03-15 16:47:16 -070072 }
Urs Grobfc77f6e2009-04-17 02:07:14 -070073 System.exit(1);
The Android Open Source Projectf8057102009-03-15 16:47:16 -070074 }
75
Brian Carlstrom022aff42011-05-17 23:16:51 -070076 final String outputPathPrefix = args[0];
77 File manifestFile = new File(args[1]);
78 String jarFileName = args[2];
79 final String javaPackageFilter = args[3];
Tsu Chiang Chuangc87fd6b2011-07-13 17:04:40 -070080 // Validate the javaPackageFilter value if non null.
81 if (javaPackageFilter.length() != 0) {
82 if (!isValidJavaPackage(javaPackageFilter)) {
83 System.err.println("Invalid " + ATTRIBUTE_JAVA_PACKAGE_FILTER + ": " +
84 javaPackageFilter);
85 System.exit(1);
86 return;
87 }
88 }
Brian Carlstrom022aff42011-05-17 23:16:51 -070089 String libcoreExpectationDir = (args.length > 4) ? args[4] : null;
90 String androidMakeFile = (args.length > 5) ? args[5] : null;
The Android Open Source Projectf8057102009-03-15 16:47:16 -070091
Brian Carlstrom022aff42011-05-17 23:16:51 -070092 final TestType testType = TestType.getTestType(androidMakeFile);
93
94 Document manifest;
The Android Open Source Projectf8057102009-03-15 16:47:16 -070095 try {
Brian Carlstrom9f2dab82011-04-01 15:47:17 -070096 manifest = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
Brian Carlstrom022aff42011-05-17 23:16:51 -070097 new FileInputStream(manifestFile));
The Android Open Source Projectf8057102009-03-15 16:47:16 -070098 } catch (Exception e) {
Brian Carlstrom022aff42011-05-17 23:16:51 -070099 System.err.println("cannot open manifest " + manifestFile);
The Android Open Source Projectf8057102009-03-15 16:47:16 -0700100 e.printStackTrace();
Brian Carlstrom9f2dab82011-04-01 15:47:17 -0700101 System.exit(1);
Brian Carlstrom022aff42011-05-17 23:16:51 -0700102 return;
The Android Open Source Projectf8057102009-03-15 16:47:16 -0700103 }
104
105 Element documentElement = manifest.getDocumentElement();
The Android Open Source Projectf8057102009-03-15 16:47:16 -0700106 documentElement.getAttribute("package");
Brian Carlstrom022aff42011-05-17 23:16:51 -0700107 final String runner = getElementAttribute(documentElement,
108 "instrumentation",
109 "android:name");
110 final String packageName = documentElement.getAttribute("package");
111 final String target = getElementAttribute(documentElement,
112 "instrumentation",
113 "android:targetPackage");
The Android Open Source Projectf8057102009-03-15 16:47:16 -0700114
Brian Carlstrom022aff42011-05-17 23:16:51 -0700115 String outputXmlFile = outputPathPrefix + ".xml";
116 final String xmlName = new File(outputPathPrefix).getName();
117 XMLGenerator xmlGenerator;
The Android Open Source Projectf8057102009-03-15 16:47:16 -0700118 try {
Brian Carlstrom022aff42011-05-17 23:16:51 -0700119 xmlGenerator = new XMLGenerator(outputXmlFile) {
120 {
121 Node testPackageElem = mDoc.getDocumentElement();
122
123 setAttribute(testPackageElem, ATTRIBUTE_NAME, xmlName);
124 setAttribute(testPackageElem, ATTRIBUTE_RUNNER, runner);
125 setAttribute(testPackageElem, ATTRIBUTE_PACKAGE, packageName);
126 setAttribute(testPackageElem, ATTRIBUTE_NS, packageName);
127 setAttribute(testPackageElem, ATTRIBUTE_JAVA_PACKAGE_FILTER, javaPackageFilter);
128
129 if (testType.type == TestType.HOST_SIDE_ONLY) {
130 setAttribute(testPackageElem, ATTRIBUTE_HOST_SIDE_ONLY, "true");
131 setAttribute(testPackageElem, ATTRIBUTE_JAR_PATH, testType.jarPath);
132 }
133
134 if (testType.type == TestType.VM_HOST_TEST) {
135 setAttribute(testPackageElem, ATTRIBUTE_VM_HOST_TEST, "true");
136 setAttribute(testPackageElem, ATTRIBUTE_JAR_PATH, testType.jarPath);
137 }
138
139 if (!packageName.equals(target)) {
140 setAttribute(testPackageElem, ATTRIBUTE_TARGET, target);
141 setAttribute(testPackageElem, ATTRIBUTE_TARGET_BINARY, target);
142 }
143 }
144 };
The Android Open Source Projectf8057102009-03-15 16:47:16 -0700145 } catch (ParserConfigurationException e) {
Brian Carlstrom022aff42011-05-17 23:16:51 -0700146 System.err.println("Can't initialize XML Generator " + outputXmlFile);
Urs Grobfc77f6e2009-04-17 02:07:14 -0700147 System.exit(1);
Brian Carlstrom022aff42011-05-17 23:16:51 -0700148 return;
The Android Open Source Projectf8057102009-03-15 16:47:16 -0700149 }
150
Brian Carlstrom022aff42011-05-17 23:16:51 -0700151 ExpectationStore libcoreVogarExpectationStore;
152 ExpectationStore ctsVogarExpectationStore;
153
Brett Chabot5977e942010-12-13 16:42:42 -0800154 try {
Brian Carlstrom9f2dab82011-04-01 15:47:17 -0700155 libcoreVogarExpectationStore
Brian Carlstrom022aff42011-05-17 23:16:51 -0700156 = VogarUtils.provideExpectationStore(libcoreExpectationDir);
Brian Muramatsu7f64e852011-02-17 16:52:16 -0800157 ctsVogarExpectationStore = VogarUtils.provideExpectationStore(CTS_EXPECTATION_DIR);
Brett Chabot5977e942010-12-13 16:42:42 -0800158 } catch (IOException e) {
Brian Carlstrom9f2dab82011-04-01 15:47:17 -0700159 System.err.println("Can't initialize vogar expectation store from "
Brian Carlstrom022aff42011-05-17 23:16:51 -0700160 + libcoreExpectationDir);
Brett Chabot5977e942010-12-13 16:42:42 -0800161 e.printStackTrace(System.err);
162 System.exit(1);
Brian Carlstrom022aff42011-05-17 23:16:51 -0700163 return;
Brett Chabot5977e942010-12-13 16:42:42 -0800164 }
Brian Carlstrom022aff42011-05-17 23:16:51 -0700165 ExpectationStore[] expectations = new ExpectationStore[] {
166 libcoreVogarExpectationStore, ctsVogarExpectationStore
167 };
Brett Chabot5977e942010-12-13 16:42:42 -0800168
Brian Carlstrom9f2dab82011-04-01 15:47:17 -0700169 JarFile jarFile = null;
170 try {
Brian Carlstrom022aff42011-05-17 23:16:51 -0700171 jarFile = new JarFile(jarFileName);
Brian Carlstrom9f2dab82011-04-01 15:47:17 -0700172 } catch (Exception e) {
Brian Carlstrom022aff42011-05-17 23:16:51 -0700173 System.err.println("cannot open jarfile " + jarFileName);
Brian Carlstrom9f2dab82011-04-01 15:47:17 -0700174 e.printStackTrace();
Urs Grobfc77f6e2009-04-17 02:07:14 -0700175 System.exit(1);
The Android Open Source Projectf8057102009-03-15 16:47:16 -0700176 }
177
Brian Carlstrom022aff42011-05-17 23:16:51 -0700178 Map<String,TestClass> testCases = new LinkedHashMap<String, TestClass>();
179
180 String javaPackagePrefix = javaPackageFilter.isEmpty() ? "" : (javaPackageFilter + ".");
Brian Carlstrom9f2dab82011-04-01 15:47:17 -0700181
182 Enumeration<JarEntry> jarEntries = jarFile.entries();
183 while (jarEntries.hasMoreElements()) {
184 JarEntry jarEntry = jarEntries.nextElement();
185 String name = jarEntry.getName();
186 if (!name.endsWith(".class")) {
187 continue;
188 }
189 String className
190 = name.substring(0, name.length() - ".class".length()).replace('/', '.');
Brian Carlstrom022aff42011-05-17 23:16:51 -0700191 if (!className.startsWith(javaPackagePrefix)) {
192 continue;
193 }
Brian Carlstrom9f2dab82011-04-01 15:47:17 -0700194 try {
195 Class<?> klass = Class.forName(className,
196 false,
197 CollectAllTests.class.getClassLoader());
Kenny Rootb5b47cb2013-02-27 17:03:16 -0800198 final int modifiers = klass.getModifiers();
199 if (Modifier.isAbstract(modifiers) || !Modifier.isPublic(modifiers)) {
Brian Carlstrom9f2dab82011-04-01 15:47:17 -0700200 continue;
201 }
Kenny Rootb5b47cb2013-02-27 17:03:16 -0800202
203 final boolean isJunit4Class = isJunit4Class(klass);
204 if (!isJunit4Class && !isJunit3Test(klass)) {
Brian Carlstrom9f2dab82011-04-01 15:47:17 -0700205 continue;
206 }
Kenny Rootb5b47cb2013-02-27 17:03:16 -0800207
Brian Carlstrom9f2dab82011-04-01 15:47:17 -0700208 try {
209 klass.getConstructor(new Class<?>[] { String.class } );
Kenny Rootb5b47cb2013-02-27 17:03:16 -0800210 addToTests(expectations, testCases, klass);
Brian Carlstrom9f2dab82011-04-01 15:47:17 -0700211 continue;
212 } catch (NoSuchMethodException e) {
Brian Carlstromd9f48462011-05-28 18:47:39 -0700213 } catch (SecurityException e) {
Tsu Chiang Chuangc87fd6b2011-07-13 17:04:40 -0700214 System.out.println("Known bug (Working as intended): problem with class "
215 + className);
Brian Carlstromd9f48462011-05-28 18:47:39 -0700216 e.printStackTrace();
Brian Carlstrom9f2dab82011-04-01 15:47:17 -0700217 }
Kenny Rootb5b47cb2013-02-27 17:03:16 -0800218
Brian Carlstrom9f2dab82011-04-01 15:47:17 -0700219 try {
220 klass.getConstructor(new Class<?>[0]);
Kenny Rootb5b47cb2013-02-27 17:03:16 -0800221 addToTests(expectations, testCases, klass);
Brian Carlstrom9f2dab82011-04-01 15:47:17 -0700222 continue;
223 } catch (NoSuchMethodException e) {
Brian Carlstromd9f48462011-05-28 18:47:39 -0700224 } catch (SecurityException e) {
Tsu Chiang Chuangc87fd6b2011-07-13 17:04:40 -0700225 System.out.println("Known bug (Working as intended): problem with class "
226 + className);
Brian Carlstromd9f48462011-05-28 18:47:39 -0700227 e.printStackTrace();
Brian Carlstrom9f2dab82011-04-01 15:47:17 -0700228 }
229 } catch (ClassNotFoundException e) {
230 System.out.println("class not found " + className);
231 e.printStackTrace();
232 System.exit(1);
233 }
234 }
235
The Android Open Source Projectf8057102009-03-15 16:47:16 -0700236 for (Iterator<TestClass> iterator = testCases.values().iterator(); iterator.hasNext();) {
237 TestClass type = iterator.next();
238 xmlGenerator.addTestClass(type);
239 }
240
241 try {
242 xmlGenerator.dump();
243 } catch (Exception e) {
Brian Carlstrom022aff42011-05-17 23:16:51 -0700244 System.err.println("cannot dump xml to " + outputXmlFile);
The Android Open Source Projectf8057102009-03-15 16:47:16 -0700245 e.printStackTrace();
Urs Grobfc77f6e2009-04-17 02:07:14 -0700246 System.exit(1);
The Android Open Source Projectf8057102009-03-15 16:47:16 -0700247 }
248 }
249
Brian Carlstrom022aff42011-05-17 23:16:51 -0700250 private static class TestType {
251 private static final int HOST_SIDE_ONLY = 1;
252 private static final int DEVICE_SIDE_ONLY = 2;
253 private static final int VM_HOST_TEST = 3;
The Android Open Source Projectf8057102009-03-15 16:47:16 -0700254
Brian Carlstrom022aff42011-05-17 23:16:51 -0700255 private final int type;
256 private final String jarPath;
The Android Open Source Projectf8057102009-03-15 16:47:16 -0700257
Brian Carlstrom022aff42011-05-17 23:16:51 -0700258 private TestType (int type, String jarPath) {
259 this.type = type;
260 this.jarPath = jarPath;
The Android Open Source Projectf8057102009-03-15 16:47:16 -0700261 }
262
Brian Carlstrom022aff42011-05-17 23:16:51 -0700263 private static TestType getTestType(String makeFileName) {
264 if (makeFileName == null || makeFileName.isEmpty()) {
265 return new TestType(DEVICE_SIDE_ONLY, null);
266 }
267 int type = TestType.DEVICE_SIDE_ONLY;
268 String jarPath = null;
269 try {
270 BufferedReader reader = new BufferedReader(new FileReader(makeFileName));
271 String line;
272
Kenny Rootb5b47cb2013-02-27 17:03:16 -0800273 while ((line = reader.readLine())!=null) {
Brian Carlstrom022aff42011-05-17 23:16:51 -0700274 if (line.startsWith(TEST_TYPE)) {
275 if (line.indexOf(ATTRIBUTE_VM_HOST_TEST) >= 0) {
276 type = VM_HOST_TEST;
277 } else {
278 type = HOST_SIDE_ONLY;
279 }
280 } else if (line.startsWith(JAR_PATH)) {
281 jarPath = line.substring(JAR_PATH.length(), line.length()).trim();
282 }
283 }
284 reader.close();
285 } catch (IOException e) {
286 }
287 return new TestType(type, jarPath);
288 }
The Android Open Source Projectf8057102009-03-15 16:47:16 -0700289 }
290
291 private static Element getElement(Element element, String tagName) {
292 NodeList elements = element.getElementsByTagName(tagName);
293 if (elements.getLength() > 0) {
294 return (Element) elements.item(0);
295 } else {
296 return null;
297 }
298 }
299
Brian Carlstrom9f2dab82011-04-01 15:47:17 -0700300 private static String getElementAttribute(Element element,
301 String elementName,
302 String attributeName) {
The Android Open Source Projectf8057102009-03-15 16:47:16 -0700303 Element e = getElement(element, elementName);
304 if (e != null) {
305 return e.getAttribute(attributeName);
306 } else {
307 return "";
308 }
309 }
310
Kenny Rootb5b47cb2013-02-27 17:03:16 -0800311 private static String getKnownFailure(final Class<?> testClass,
Urs Grobfc77f6e2009-04-17 02:07:14 -0700312 final String testName) {
313 return getAnnotation(testClass, testName, KNOWN_FAILURE);
314 }
Brian Muramatsub8ed3162010-07-13 12:43:52 -0700315
Kenny Rootb5b47cb2013-02-27 17:03:16 -0800316 private static boolean isKnownFailure(final Class<?> testClass,
Brian Muramatsub8ed3162010-07-13 12:43:52 -0700317 final String testName) {
318 return getAnnotation(testClass, testName, KNOWN_FAILURE) != null;
319 }
320
Kenny Rootb5b47cb2013-02-27 17:03:16 -0800321 private static boolean isSuppressed(final Class<?> testClass,
Brian Muramatsu168beb02010-10-21 12:39:45 -0700322 final String testName) {
323 return getAnnotation(testClass, testName, SUPPRESSED_TEST) != null;
324 }
325
Kenny Rootb5b47cb2013-02-27 17:03:16 -0800326 private static String getAnnotation(final Class<?> testClass,
Urs Grobfc77f6e2009-04-17 02:07:14 -0700327 final String testName, final String annotationName) {
The Android Open Source Project7671bec2009-03-19 23:08:36 -0700328 try {
Urs Grobfc77f6e2009-04-17 02:07:14 -0700329 Method testMethod = testClass.getMethod(testName, (Class[])null);
The Android Open Source Project7671bec2009-03-19 23:08:36 -0700330 Annotation[] annotations = testMethod.getAnnotations();
331 for (Annotation annot : annotations) {
332
Urs Grobfc77f6e2009-04-17 02:07:14 -0700333 if (annot.annotationType().getName().equals(annotationName)) {
The Android Open Source Project7671bec2009-03-19 23:08:36 -0700334 String annotStr = annot.toString();
335 String knownFailure = null;
336 if (annotStr.contains("(value=")) {
337 knownFailure =
Urs Grobfc77f6e2009-04-17 02:07:14 -0700338 annotStr.substring(annotStr.indexOf("=") + 1,
339 annotStr.length() - 1);
The Android Open Source Project7671bec2009-03-19 23:08:36 -0700340
341 }
342
343 if (knownFailure == null) {
344 knownFailure = "true";
345 }
346
347 return knownFailure;
348 }
349
350 }
351
Brian Carlstrom022aff42011-05-17 23:16:51 -0700352 } catch (NoSuchMethodException e) {
The Android Open Source Project7671bec2009-03-19 23:08:36 -0700353 }
354
355 return null;
356 }
357
Brian Carlstrom022aff42011-05-17 23:16:51 -0700358 private static void addToTests(ExpectationStore[] expectations,
359 Map<String,TestClass> testCases,
Kenny Rootb5b47cb2013-02-27 17:03:16 -0800360 Class<?> testClass) {
Brian Carlstrom9f2dab82011-04-01 15:47:17 -0700361 Set<String> testNames = new HashSet<String>();
Kenny Rootb5b47cb2013-02-27 17:03:16 -0800362
363 boolean isJunit3Test = isJunit3Test(testClass);
364
365 Method[] testMethods = testClass.getMethods();
366 for (Method testMethod : testMethods) {
367 String testName = testMethod.getName();
368 if (testNames.contains(testName)) {
369 continue;
The Android Open Source Projectf8057102009-03-15 16:47:16 -0700370 }
Kenny Rootb5b47cb2013-02-27 17:03:16 -0800371
372 /* Make sure the method has the right signature. */
373 if (!Modifier.isPublic(testMethod.getModifiers())) {
374 continue;
375 }
376 if (!testMethod.getReturnType().equals(Void.TYPE)) {
377 continue;
378 }
379 if (testMethod.getParameterTypes().length != 0) {
380 continue;
381 }
382
383 if ((isJunit3Test && !testName.startsWith("test"))
384 || (!isJunit3Test && !isJunit4TestMethod(testMethod))) {
385 continue;
386 }
387
388 testNames.add(testName);
389 addToTests(expectations, testCases, testClass, testName);
The Android Open Source Projectf8057102009-03-15 16:47:16 -0700390 }
Brian Carlstrom9f2dab82011-04-01 15:47:17 -0700391 }
392
Brian Carlstrom022aff42011-05-17 23:16:51 -0700393 private static void addToTests(ExpectationStore[] expectations,
394 Map<String,TestClass> testCases,
Kenny Rootb5b47cb2013-02-27 17:03:16 -0800395 Class<?> test,
Brian Carlstrom022aff42011-05-17 23:16:51 -0700396 String testName) {
Brian Carlstrom9f2dab82011-04-01 15:47:17 -0700397
398 String testClassName = test.getName();
399 String knownFailure = getKnownFailure(test, testName);
400
401 if (isKnownFailure(test, testName)) {
402 System.out.println("ignoring known failure: " + test + "#" + testName);
403 return;
Brian Carlstrom9f2dab82011-04-01 15:47:17 -0700404 } else if (isSuppressed(test, testName)) {
405 System.out.println("ignoring suppressed test: " + test + "#" + testName);
406 return;
Brian Carlstrom022aff42011-05-17 23:16:51 -0700407 } else if (VogarUtils.isVogarKnownFailure(expectations,
Brian Carlstrom9f2dab82011-04-01 15:47:17 -0700408 testClassName,
409 testName)) {
Brian Carlstrom022aff42011-05-17 23:16:51 -0700410 System.out.println("ignoring expectation known failure: " + test
Brian Carlstrom9f2dab82011-04-01 15:47:17 -0700411 + "#" + testName);
412 return;
413 }
414
Kenny Rootb5b47cb2013-02-27 17:03:16 -0800415 TestClass testClass;
The Android Open Source Projectf8057102009-03-15 16:47:16 -0700416 if (testCases.containsKey(testClassName)) {
417 testClass = testCases.get(testClassName);
418 } else {
419 testClass = new TestClass(testClassName, new ArrayList<TestMethod>());
420 testCases.put(testClassName, testClass);
421 }
422
Brian Muramatsu168beb02010-10-21 12:39:45 -0700423 testClass.mCases.add(new TestMethod(testName, "", "", knownFailure, false, false));
The Android Open Source Projectf8057102009-03-15 16:47:16 -0700424 }
Tsu Chiang Chuangc87fd6b2011-07-13 17:04:40 -0700425
Kenny Rootb5b47cb2013-02-27 17:03:16 -0800426 private static boolean isJunit3Test(Class<?> klass) {
427 return TestCase.class.isAssignableFrom(klass);
428 }
429
430 private static boolean isJunit4Class(Class<?> klass) {
431 for (Annotation a : klass.getAnnotations()) {
432 if (RunWith.class.isAssignableFrom(a.annotationType())) {
433 // @RunWith is currently not supported for CTS tests because tradefed cannot handle
434 // a single test spawning other tests with different names.
435 System.out.println("Skipping test class " + klass.getName()
436 + ": JUnit4 @RunWith is not supported");
437 return false;
438 }
439 }
440
441 for (Method m : klass.getMethods()) {
442 if (isJunit4TestMethod(m)) {
443 return true;
444 }
445 }
446
447 return false;
448 }
449
450 private static boolean isJunit4TestMethod(Method method) {
451 for (Annotation a : method.getAnnotations()) {
452 if (org.junit.Test.class.isAssignableFrom(a.annotationType())) {
453 return true;
454 }
455 }
456
457 return false;
458 }
459
Tsu Chiang Chuangc87fd6b2011-07-13 17:04:40 -0700460 /**
461 * Determines if a given string is a valid java package name
462 * @param javaPackageName
463 * @return true if it is valid, false otherwise
464 */
465 private static boolean isValidJavaPackage(String javaPackageName) {
466 String[] strSections = javaPackageName.split(".");
467 for (String strSection : strSections) {
468 if (!isValidJavaIdentifier(strSection)) {
469 return false;
470 }
471 }
472 return true;
473 }
474
475 /**
476 * Determines if a given string is a valid java identifier.
477 * @param javaIdentifier
478 * @return true if it is a valid identifier, false otherwise
479 */
480 private static boolean isValidJavaIdentifier(String javaIdentifier) {
481 if (javaIdentifier.length() == 0 ||
482 !Character.isJavaIdentifierStart(javaIdentifier.charAt(0))) {
483 return false;
484 }
485 for (int i = 1; i < javaIdentifier.length(); i++) {
486 if (!Character.isJavaIdentifierPart(javaIdentifier.charAt(i))) {
487 return false;
488 }
489 }
490 return true;
491 }
The Android Open Source Projectf8057102009-03-15 16:47:16 -0700492}