blob: 36b2af78667e2f1d29058078d02e0caab206785d [file] [log] [blame]
Brett Chabot018c2af2011-02-24 14:45:34 -08001/*
2 * Copyright (C) 2011 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 */
16package com.android.tradefed.util;
17
18import com.android.ddmlib.Log;
Brett Chabotd5c41d42011-04-18 14:27:31 -070019import com.android.tradefed.util.ClassPathScanner.ExternalClassNameFilter;
Brett Chabot018c2af2011-02-24 14:45:34 -080020
Brett Chabot0eabebe2011-09-12 16:15:42 -070021import junit.framework.Test;
22import junit.framework.TestCase;
23import junit.framework.TestSuite;
24
Brett Chabot018c2af2011-02-24 14:45:34 -080025import java.io.File;
26import java.io.IOException;
Brett Chabot27d9d982011-03-22 20:53:28 -070027import java.net.MalformedURLException;
Brett Chabot018c2af2011-02-24 14:45:34 -080028import java.net.URL;
29import java.net.URLClassLoader;
Brett Chabot27d9d982011-03-22 20:53:28 -070030import java.util.Collection;
31import java.util.Iterator;
Brett Chabot018c2af2011-02-24 14:45:34 -080032import java.util.Set;
33
Brett Chabot018c2af2011-02-24 14:45:34 -080034/**
35 * A class for loading all JUnit3 tests in a jar file
36 */
37public class TestLoader {
38
39 private static final String LOG_TAG = "TestLoader";
40
41 /**
42 * Creates a {@link Test} containing all the {@link TestCase} found in given jar
Brett Chabot27d9d982011-03-22 20:53:28 -070043 *
44 * @param testJarFile the jar file to load tests from
Omari Stephensc9e52682013-03-07 19:22:20 -080045 * @param dependentJars the additional jar files which classes in testJarFile depend on
Brett Chabot018c2af2011-02-24 14:45:34 -080046 * @return the {@link Test} containing all tests
47 */
Brett Chabot27d9d982011-03-22 20:53:28 -070048 public Test loadTests(File testJarFile, Collection<File> dependentJars) {
Brett Chabot018c2af2011-02-24 14:45:34 -080049 ClassPathScanner scanner = new ClassPathScanner();
50 try {
Brett Chabotd5c41d42011-04-18 14:27:31 -070051 Set<String> classNames = scanner.getEntriesFromJar(testJarFile,
52 new ExternalClassNameFilter());
Brett Chabot27d9d982011-03-22 20:53:28 -070053
54 ClassLoader jarClassLoader = buildJarClassLoader(testJarFile, dependentJars);
Brett Chabot018c2af2011-02-24 14:45:34 -080055 return loadTests(classNames, jarClassLoader);
56 } catch (IOException e) {
57 Log.e(LOG_TAG, String.format("IOException when loading test classes from jar %s",
Brett Chabot27d9d982011-03-22 20:53:28 -070058 testJarFile.getAbsolutePath()));
Brett Chabot018c2af2011-02-24 14:45:34 -080059 Log.e(LOG_TAG, e);
60 }
61 return null;
62 }
63
Brett Chabot27d9d982011-03-22 20:53:28 -070064 private ClassLoader buildJarClassLoader(File jarFile, Collection<File> dependentJars)
65 throws MalformedURLException {
66 URL[] urls = new URL[dependentJars.size() + 1];
67 urls[0] = jarFile.toURI().toURL();
68 Iterator<File> jarIter = dependentJars.iterator();
Brett Chabot7262ffb2011-06-17 10:08:37 -070069 for (int i=1; i <= dependentJars.size(); i++) {
Brett Chabot27d9d982011-03-22 20:53:28 -070070 urls[i] = jarIter.next().toURI().toURL();
71 }
72 return new URLClassLoader(urls);
73 }
74
Brett Chabotad5a7c42012-02-21 18:18:33 -080075 @SuppressWarnings("unchecked")
Brett Chabot018c2af2011-02-24 14:45:34 -080076 private Test loadTests(Set<String> classNames, ClassLoader classLoader) {
77 TestSuite testSuite = new TestSuite();
78 for (String className : classNames) {
79 try {
Brett Chabotd5c41d42011-04-18 14:27:31 -070080 Class<?> testClass = Class.forName(className, true, classLoader);
81 if (TestCase.class.isAssignableFrom(testClass)) {
Brett Chabotad5a7c42012-02-21 18:18:33 -080082 testSuite.addTestSuite((Class<? extends TestCase>)testClass);
Brett Chabot018c2af2011-02-24 14:45:34 -080083 }
Brett Chabotb6efe052011-03-25 12:33:38 -070084 } catch (ClassNotFoundException e) {
Brett Chabot018c2af2011-02-24 14:45:34 -080085 // ignore for now
Brett Chabotb6efe052011-03-25 12:33:38 -070086 } catch (RuntimeException e) {
87 // catch this to prevent one bad test from stopping run
88 Log.e(LOG_TAG, e);
Brett Chabot018c2af2011-02-24 14:45:34 -080089 }
90 }
91 return testSuite;
92 }
93}