aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Florian Weikert <fwe@google.com>2017-02-21 12:39:21 +0000
committerGravatar Irina Iancu <elenairina@google.com>2017-02-21 13:50:49 +0000
commit9739b479c5e3acd959cd586a2c0615bfa5011637 (patch)
treec7489da7471368b11e756e6cf80c2f37ef809209
parent57eec8c33d35336049f11db251d2b87c70661805 (diff)
Move Classpath.java from src/test/... to src/main/... since it will be used by the documentation generation in the future.
-- PiperOrigin-RevId: 148081562 MOS_MIGRATED_REVID=148081562
-rw-r--r--src/main/java/com/google/devtools/build/lib/util/Classpath.java (renamed from src/test/java/com/google/devtools/build/lib/testutil/Classpath.java)41
-rw-r--r--src/test/java/com/google/devtools/build/lib/testutil/TestSuiteBuilder.java19
2 files changed, 34 insertions, 26 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/testutil/Classpath.java b/src/main/java/com/google/devtools/build/lib/util/Classpath.java
index 059ef6f1c3..5c26e6505e 100644
--- a/src/test/java/com/google/devtools/build/lib/testutil/Classpath.java
+++ b/src/main/java/com/google/devtools/build/lib/util/Classpath.java
@@ -11,9 +11,8 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
-package com.google.devtools.build.lib.testutil;
+package com.google.devtools.build.lib.util;
-import com.google.devtools.build.lib.util.Preconditions;
import java.io.File;
import java.io.IOException;
import java.net.URI;
@@ -35,13 +34,20 @@ import java.util.zip.ZipFile;
* A helper class to find all classes on the current classpath. This is used to automatically create
* JUnit 3 and 4 test suites.
*/
-final class Classpath {
+public final class Classpath {
private static final String CLASS_EXTENSION = ".class";
/**
- * Finds all classes that live in or below the given package.
+ * Base exception for any classpath related errors.
*/
- static Set<Class<?>> findClasses(String packageName) {
+ public static final class ClassPathException extends Exception {
+ public ClassPathException(String format, Object... args) {
+ super(String.format(format, args));
+ }
+ }
+
+ /** Finds all classes that live in or below the given package. */
+ public static Set<Class<?>> findClasses(String packageName) throws ClassPathException {
Set<Class<?>> result = new LinkedHashSet<>();
String pathPrefix = (packageName + '.').replace('.', '/');
for (String entryName : getClassPath()) {
@@ -59,11 +65,12 @@ final class Classpath {
result.add(clazz);
}
} catch (IOException e) {
- throw new AssertionError("Can't read classpath entry "
- + entryName + ": " + e.getMessage());
+ throw new ClassPathException(
+ "Can't read classpath entry %s: %s", entryName, e.getMessage());
} catch (ClassNotFoundException e) {
- throw new AssertionError("Class not found even though it is on the classpath "
- + entryName + ": " + e.getMessage());
+ throw new ClassPathException(
+ "Class not found even though it is on the classpath %s: %s",
+ entryName, e.getMessage());
}
}
}
@@ -127,23 +134,21 @@ final class Classpath {
}
private static void getClassPathsFromClasspathJar(File classpathJar, Set<String> classPaths)
- throws IOException {
+ throws IOException, ClassPathException {
Manifest manifest = new JarFile(classpathJar).getManifest();
Attributes attributes = manifest.getMainAttributes();
for (String classPath : attributes.getValue("Class-Path").split(" ")) {
try {
classPaths.add(Paths.get(new URI(classPath)).toAbsolutePath().toString());
} catch (URISyntaxException e) {
- throw new AssertionError(
- "Error parsing classpath uri " + classPath + ": " + e.getMessage());
+ throw new ClassPathException(
+ "Error parsing classpath uri %s: %s", classPath, e.getMessage());
}
}
}
- /**
- * Gets the classpath from current classloader.
- */
- private static Set<String> getClassPath() {
+ /** Gets the classpath from current classloader. */
+ private static Set<String> getClassPath() throws ClassPathException {
ClassLoader classloader = Classpath.class.getClassLoader();
if (!(classloader instanceof URLClassLoader)) {
throw new IllegalStateException("Unable to find classes to test, since Test Suite class is "
@@ -163,8 +168,8 @@ final class Classpath {
try {
getClassPathsFromClasspathJar(classPathEntry, completeClassPaths);
} catch (IOException e) {
- throw new AssertionError(
- "Can't read classpath entry " + entryName + ": " + e.getMessage());
+ throw new ClassPathException(
+ "Can't read classpath entry %s: %s", entryName, e.getMessage());
}
}
}
diff --git a/src/test/java/com/google/devtools/build/lib/testutil/TestSuiteBuilder.java b/src/test/java/com/google/devtools/build/lib/testutil/TestSuiteBuilder.java
index 59a5d8583c..81a20dd616 100644
--- a/src/test/java/com/google/devtools/build/lib/testutil/TestSuiteBuilder.java
+++ b/src/test/java/com/google/devtools/build/lib/testutil/TestSuiteBuilder.java
@@ -17,15 +17,14 @@ import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
-
-import junit.framework.TestCase;
-
-import org.junit.runner.RunWith;
-
+import com.google.devtools.build.lib.util.Classpath;
+import com.google.devtools.build.lib.util.Classpath.ClassPathException;
import java.lang.reflect.Modifier;
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.Set;
+import junit.framework.TestCase;
+import org.junit.runner.RunWith;
/**
* A collector for test classes, for both JUnit 3 and 4. To be used in combination with {@link
@@ -73,10 +72,14 @@ public final class TestSuiteBuilder {
private Set<Class<?>> getClassesRecursive(String pkgName) {
Set<Class<?>> result = new LinkedHashSet<>();
- for (Class<?> clazz : Classpath.findClasses(pkgName)) {
- if (isTestClass(clazz)) {
- result.add(clazz);
+ try {
+ for (Class<?> clazz : Classpath.findClasses(pkgName)) {
+ if (isTestClass(clazz)) {
+ result.add(clazz);
+ }
}
+ } catch (ClassPathException e) {
+ throw new AssertionError("Cannot retrive classes: " + e.getMessage());
}
return result;
}