Brett Chabot | 018c2af | 2011-02-24 14:45:34 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.tradefed.util; |
| 17 | |
| 18 | import com.android.ddmlib.Log; |
Brett Chabot | d5c41d4 | 2011-04-18 14:27:31 -0700 | [diff] [blame] | 19 | import com.android.tradefed.util.ClassPathScanner.ExternalClassNameFilter; |
Brett Chabot | 018c2af | 2011-02-24 14:45:34 -0800 | [diff] [blame] | 20 | |
Brett Chabot | 0eabebe | 2011-09-12 16:15:42 -0700 | [diff] [blame] | 21 | import junit.framework.Test; |
| 22 | import junit.framework.TestCase; |
| 23 | import junit.framework.TestSuite; |
| 24 | |
Brett Chabot | 018c2af | 2011-02-24 14:45:34 -0800 | [diff] [blame] | 25 | import java.io.File; |
| 26 | import java.io.IOException; |
Brett Chabot | 27d9d98 | 2011-03-22 20:53:28 -0700 | [diff] [blame] | 27 | import java.net.MalformedURLException; |
Brett Chabot | 018c2af | 2011-02-24 14:45:34 -0800 | [diff] [blame] | 28 | import java.net.URL; |
| 29 | import java.net.URLClassLoader; |
Brett Chabot | 27d9d98 | 2011-03-22 20:53:28 -0700 | [diff] [blame] | 30 | import java.util.Collection; |
| 31 | import java.util.Iterator; |
Brett Chabot | 018c2af | 2011-02-24 14:45:34 -0800 | [diff] [blame] | 32 | import java.util.Set; |
| 33 | |
Brett Chabot | 018c2af | 2011-02-24 14:45:34 -0800 | [diff] [blame] | 34 | /** |
| 35 | * A class for loading all JUnit3 tests in a jar file |
| 36 | */ |
| 37 | public 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 Chabot | 27d9d98 | 2011-03-22 20:53:28 -0700 | [diff] [blame] | 43 | * |
| 44 | * @param testJarFile the jar file to load tests from |
Omari Stephens | c9e5268 | 2013-03-07 19:22:20 -0800 | [diff] [blame] | 45 | * @param dependentJars the additional jar files which classes in testJarFile depend on |
Brett Chabot | 018c2af | 2011-02-24 14:45:34 -0800 | [diff] [blame] | 46 | * @return the {@link Test} containing all tests |
| 47 | */ |
Brett Chabot | 27d9d98 | 2011-03-22 20:53:28 -0700 | [diff] [blame] | 48 | public Test loadTests(File testJarFile, Collection<File> dependentJars) { |
Brett Chabot | 018c2af | 2011-02-24 14:45:34 -0800 | [diff] [blame] | 49 | ClassPathScanner scanner = new ClassPathScanner(); |
| 50 | try { |
Brett Chabot | d5c41d4 | 2011-04-18 14:27:31 -0700 | [diff] [blame] | 51 | Set<String> classNames = scanner.getEntriesFromJar(testJarFile, |
| 52 | new ExternalClassNameFilter()); |
Brett Chabot | 27d9d98 | 2011-03-22 20:53:28 -0700 | [diff] [blame] | 53 | |
| 54 | ClassLoader jarClassLoader = buildJarClassLoader(testJarFile, dependentJars); |
Brett Chabot | 018c2af | 2011-02-24 14:45:34 -0800 | [diff] [blame] | 55 | return loadTests(classNames, jarClassLoader); |
| 56 | } catch (IOException e) { |
| 57 | Log.e(LOG_TAG, String.format("IOException when loading test classes from jar %s", |
Brett Chabot | 27d9d98 | 2011-03-22 20:53:28 -0700 | [diff] [blame] | 58 | testJarFile.getAbsolutePath())); |
Brett Chabot | 018c2af | 2011-02-24 14:45:34 -0800 | [diff] [blame] | 59 | Log.e(LOG_TAG, e); |
| 60 | } |
| 61 | return null; |
| 62 | } |
| 63 | |
Brett Chabot | 27d9d98 | 2011-03-22 20:53:28 -0700 | [diff] [blame] | 64 | 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 Chabot | 7262ffb | 2011-06-17 10:08:37 -0700 | [diff] [blame] | 69 | for (int i=1; i <= dependentJars.size(); i++) { |
Brett Chabot | 27d9d98 | 2011-03-22 20:53:28 -0700 | [diff] [blame] | 70 | urls[i] = jarIter.next().toURI().toURL(); |
| 71 | } |
| 72 | return new URLClassLoader(urls); |
| 73 | } |
| 74 | |
Brett Chabot | ad5a7c4 | 2012-02-21 18:18:33 -0800 | [diff] [blame] | 75 | @SuppressWarnings("unchecked") |
Brett Chabot | 018c2af | 2011-02-24 14:45:34 -0800 | [diff] [blame] | 76 | private Test loadTests(Set<String> classNames, ClassLoader classLoader) { |
| 77 | TestSuite testSuite = new TestSuite(); |
| 78 | for (String className : classNames) { |
| 79 | try { |
Brett Chabot | d5c41d4 | 2011-04-18 14:27:31 -0700 | [diff] [blame] | 80 | Class<?> testClass = Class.forName(className, true, classLoader); |
| 81 | if (TestCase.class.isAssignableFrom(testClass)) { |
Brett Chabot | ad5a7c4 | 2012-02-21 18:18:33 -0800 | [diff] [blame] | 82 | testSuite.addTestSuite((Class<? extends TestCase>)testClass); |
Brett Chabot | 018c2af | 2011-02-24 14:45:34 -0800 | [diff] [blame] | 83 | } |
Brett Chabot | b6efe05 | 2011-03-25 12:33:38 -0700 | [diff] [blame] | 84 | } catch (ClassNotFoundException e) { |
Brett Chabot | 018c2af | 2011-02-24 14:45:34 -0800 | [diff] [blame] | 85 | // ignore for now |
Brett Chabot | b6efe05 | 2011-03-25 12:33:38 -0700 | [diff] [blame] | 86 | } catch (RuntimeException e) { |
| 87 | // catch this to prevent one bad test from stopping run |
| 88 | Log.e(LOG_TAG, e); |
Brett Chabot | 018c2af | 2011-02-24 14:45:34 -0800 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | return testSuite; |
| 92 | } |
| 93 | } |