diff options
author | mikecase <mikecase@chromium.org> | 2015-04-07 12:45:14 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-04-07 19:45:50 +0000 |
commit | 1ad64324d470c43b29854282d5ad5b1ff75dfc6c (patch) | |
tree | c2bc05a56d46d62ebdc2a8b3b55bea6fc73119b6 /testing/android | |
parent | 9a4f22dbb7266733c40a632ece2ddc7eaf56e6ac (diff) | |
download | chromium_src-1ad64324d470c43b29854282d5ad5b1ff75dfc6c.zip chromium_src-1ad64324d470c43b29854282d5ad5b1ff75dfc6c.tar.gz chromium_src-1ad64324d470c43b29854282d5ad5b1ff75dfc6c.tar.bz2 |
Add new GN junit binary template and new GYP java binary template.
Adds a new junit_binary template for gyp which makes it easy to
make new Junit test suites by setting a lot of the options and
necessary dependencies for you.
Also, adds a new java_binary.gypi file which uses the
create_java_binary_script.py similiar to how the java_binary
GN template works when main_class is specified. This lets
GYP and GN work similiar when building a junit binary, so
our test runner can run them the same way.
Finally, because of the slightly different way were now running
the junit binary, the java classpath contains all of the jars
instead of just the test jar (before we were running
java -jar test_jar.jar which set the classpath to just be
test_jar.jar). Therefore, we can simplify things and just look
in the classpath for the paths to the Robolectric dependencies
when running Robolectric junit tests.
BUG=
Review URL: https://codereview.chromium.org/1057783002
Cr-Commit-Position: refs/heads/master@{#324093}
Diffstat (limited to 'testing/android')
5 files changed, 107 insertions, 25 deletions
diff --git a/testing/android/junit/BUILD.gn b/testing/android/junit/BUILD.gn index a338fca..d479629 100644 --- a/testing/android/junit/BUILD.gn +++ b/testing/android/junit/BUILD.gn @@ -14,16 +14,11 @@ java_library("junit_test_support") { "//third_party/junit", "//third_party/mockito:mockito_java", "//third_party/robolectric:robolectric_java", + "//third_party/robolectric:android-all-4.3_r2-robolectric-0", ] } # GYP: //testing/android/junit_test.gyp:junit_unit_tests -java_binary("junit_unittests") { - testonly = true - deps = [ - ":junit_test_support", - "//third_party/junit", - ] - main_class = "org.chromium.testing.local.JunitTestMain" +junit_binary("junit_unittests") { DEPRECATED_java_in_dir = "javatests/src" } diff --git a/testing/android/junit/java/src/org/chromium/testing/local/JunitTestArgParser.java b/testing/android/junit/java/src/org/chromium/testing/local/JunitTestArgParser.java index 2837976..20c5a65 100644 --- a/testing/android/junit/java/src/org/chromium/testing/local/JunitTestArgParser.java +++ b/testing/android/junit/java/src/org/chromium/testing/local/JunitTestArgParser.java @@ -7,16 +7,20 @@ package org.chromium.testing.local; import java.io.File; import java.util.HashSet; import java.util.Set; +import java.util.regex.Pattern; /** * Parses command line arguments for JunitTestMain. */ public class JunitTestArgParser { + private static final Pattern COLON = Pattern.compile(":"); + private final Set<String> mPackageFilters; private final Set<Class<?>> mRunnerFilters; private final Set<String> mGtestFilters; private File mJsonOutput; + private String[] mTestJars; public static JunitTestArgParser parse(String[] args) { @@ -43,6 +47,9 @@ public class JunitTestArgParser { } else if ("json-results-file".equals(argName)) { // Read the command line argument after the flag. parsed.setJsonOutputFile(args[++i]); + } else if ("test-jars".equals(argName)) { + // Read the command line argument after the flag. + parsed.setTestJars(args[++i]); } else { System.out.println("Ignoring flag: \"" + argName + "\""); } @@ -84,6 +91,10 @@ public class JunitTestArgParser { return mJsonOutput; } + public String[] getTestJars() { + return mTestJars; + } + private void addPackageFilter(String packageFilter) { mPackageFilters.add(packageFilter); } @@ -100,5 +111,7 @@ public class JunitTestArgParser { mJsonOutput = new File(path); } -} - + private void setTestJars(String jars) { + mTestJars = COLON.split(jars); + } +}
\ No newline at end of file diff --git a/testing/android/junit/java/src/org/chromium/testing/local/JunitTestMain.java b/testing/android/junit/java/src/org/chromium/testing/local/JunitTestMain.java index 3a305fa..f32579b 100644 --- a/testing/android/junit/java/src/org/chromium/testing/local/JunitTestMain.java +++ b/testing/android/junit/java/src/org/chromium/testing/local/JunitTestMain.java @@ -9,8 +9,9 @@ import org.junit.runner.Request; import org.junit.runner.RunWith; import java.io.IOException; +import java.util.ArrayList; import java.util.Enumeration; -import java.util.LinkedList; +import java.util.List; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.regex.Pattern; @@ -32,10 +33,19 @@ public final class JunitTestMain { /** * Finds all classes on the class path annotated with RunWith. */ - public static Class[] findClassesFromClasspath() { + public static Class[] findClassesFromClasspath(String[] testJars) { String[] jarPaths = COLON.split(System.getProperty("java.class.path")); - LinkedList<Class> classes = new LinkedList<Class>(); - for (String jp : jarPaths) { + List<String> testJarPaths = new ArrayList<String>(testJars.length); + for (String testJar: testJars) { + for (String jarPath: jarPaths) { + if (jarPath.endsWith(testJar)) { + testJarPaths.add(jarPath); + break; + } + } + } + List<Class> classes = new ArrayList<Class>(); + for (String jp : testJarPaths) { try { JarFile jf = new JarFile(jp); for (Enumeration<JarEntry> eje = jf.entries(); eje.hasMoreElements();) { @@ -48,7 +58,7 @@ public final class JunitTestMain { cn = FORWARD_SLASH.matcher(cn).replaceAll("."); Class<?> c = classOrNull(cn); if (c != null && c.isAnnotationPresent(RunWith.class)) { - classes.push(c); + classes.add(c); } } jf.close(); @@ -80,8 +90,9 @@ public final class JunitTestMain { core.addListener(new GtestListener(gtestLogger)); JsonLogger jsonLogger = new JsonLogger(parser.getJsonOutputFile()); core.addListener(new JsonListener(jsonLogger)); - Class[] classes = findClassesFromClasspath(); + Class[] classes = findClassesFromClasspath(parser.getTestJars()); Request testRequest = Request.classes(new GtestComputer(gtestLogger), classes); + for (String packageFilter : parser.getPackageFilters()) { testRequest = testRequest.filterWith(new PackageFilter(packageFilter)); } diff --git a/testing/android/junit/java/src/org/chromium/testing/local/LocalRobolectricTestRunner.java b/testing/android/junit/java/src/org/chromium/testing/local/LocalRobolectricTestRunner.java index 293525e..cf6202b 100644 --- a/testing/android/junit/java/src/org/chromium/testing/local/LocalRobolectricTestRunner.java +++ b/testing/android/junit/java/src/org/chromium/testing/local/LocalRobolectricTestRunner.java @@ -8,13 +8,10 @@ import org.junit.runners.model.InitializationError; import org.robolectric.AndroidManifest; import org.robolectric.DependencyResolver; -import org.robolectric.LocalDependencyResolver; import org.robolectric.RobolectricTestRunner; import org.robolectric.SdkConfig; import org.robolectric.annotation.Config; -import java.io.File; - /** * A custom Robolectric Junit4 Test Runner. This test runner will load the * "real" android jars from a local directory rather than use Maven to fetch @@ -32,13 +29,7 @@ public class LocalRobolectricTestRunner extends RobolectricTestRunner { @Override protected final DependencyResolver getJarResolver() { - String dependencyDir = System.getProperty("robolectric.dependency.dir"); - if (dependencyDir == null) { - throw new IllegalStateException("robolectric.dependency.dir not specified. Make sure" - + " you are setting the robolectric.dependency.dir system property to the" - + " directory containing Robolectric's runtime dependency jars."); - } - return new LocalDependencyResolver(new File(dependencyDir)); + return new RobolectricClasspathDependencyResolver(); } @Override diff --git a/testing/android/junit/java/src/org/chromium/testing/local/RobolectricClasspathDependencyResolver.java b/testing/android/junit/java/src/org/chromium/testing/local/RobolectricClasspathDependencyResolver.java new file mode 100644 index 0000000..47bc1ba --- /dev/null +++ b/testing/android/junit/java/src/org/chromium/testing/local/RobolectricClasspathDependencyResolver.java @@ -0,0 +1,72 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package org.chromium.testing.local; + +import org.robolectric.DependencyJar; +import org.robolectric.DependencyResolver; + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.regex.Pattern; + +/** + * A Robolectric dependency resolver that looks for the Robolectric dependencies + * in the Java classpath. + */ +public class RobolectricClasspathDependencyResolver implements DependencyResolver { + private static final Pattern COLON = Pattern.compile(":"); + private final String[] mClassPathJars; + + /** + * Creates a {@link ClasspathDependencyResolver}. + */ + public RobolectricClasspathDependencyResolver() { + mClassPathJars = COLON.split(System.getProperty("java.class.path")); + } + + /** + * Returns the {@link URL} for a Robolectric dependency. It looks through the jars + * in the classpath to find the dependency's filepath. + */ + @Override + public URL getLocalArtifactUrl(DependencyJar dependency) { + // Jar filenames are constructed identically to how they are built in Robolectric's + // own LocalDependencyResolver. + String dependencyJar = dependency.getArtifactId() + "-" + dependency.getVersion() + "." + + dependency.getType(); + + for (String jarPath : mClassPathJars) { + if (jarPath.endsWith(dependencyJar)) { + return fileToUrl(new File(jarPath)); + } + } + throw new IllegalStateException( + String.format("Robolectric jar %s was not found in classpath.", dependencyJar)); + } + + /** + * Returns the {@link URL} for a list of Robolectric dependencies. + */ + @Override + public URL[] getLocalArtifactUrls(DependencyJar... dependencies) { + URL[] urls = new URL[dependencies.length]; + + for (int i = 0; i < dependencies.length; i++) { + urls[i] = getLocalArtifactUrl(dependencies[i]); + } + + return urls; + } + + private static URL fileToUrl(File file) { + try { + return file.toURI().toURL(); + } catch (MalformedURLException e) { + throw new IllegalArgumentException( + String.format("File \"%s\" cannot be represented as a URL: %s", file, e)); + } + } +}
\ No newline at end of file |