Search for Test classes within the classpath used by the current classloader, instead of the system classpath. (2nd Attempt)
Windows tests passed on Jenkins with this patch:
MOS_MIGRATED_REVID=147483103
@@ -18,6 +18,8 @@ import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
+import java.net.URL;
+import java.net.URLClassLoader;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.LinkedHashSet;
@@ -26,7 +28,6 @@ import java.util.TreeSet;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
-import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
@@ -140,20 +141,23 @@ final class Classpath {
}
/**
- * Gets the class path from the System Property "java.class.path" and splits it up into the
- * individual elements.
- *
- * <p>Bazel creates a classpath jar when the class path length exceeds command line length limit,
- * read the class path value from its manifest file if it's a classpath jar.
+ * Gets the classpath from current classloader.
*/
private static Set<String> getClassPath() {
- String classPath = System.getProperty("java.class.path");
- String separator = System.getProperty("path.separator", ":");
- String[] classPaths = classPath.split(Pattern.quote(separator));
+ ClassLoader classloader = Classpath.class.getClassLoader();
+ if (!(classloader instanceof URLClassLoader)) {
+ throw new IllegalStateException("Unable to find classes to test, since Test Suite class is "
+ + "loaded by an unsupported Classloader.");
+ }
+
Set<String> completeClassPaths = new TreeSet<>();
- for (String entryName : classPaths) {
+ URL[] urls = ((URLClassLoader) classloader).getURLs();
+ for (URL url : urls) {
+ String entryName = url.getPath();
completeClassPaths.add(entryName);
if (entryName.endsWith("-classpath.jar")) {
+ // Bazel creates a classpath jar when the class path length exceeds command line length
+ // limit, read the class path value from its manifest file if it's a classpath jar.
File classPathEntry = new File(entryName);
if (classPathEntry.exists() && classPathEntry.isFile()) {
try {
