Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature to read the classpath from JAR Manifests #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Documentation:
* [Release Notes](#changes)
* [Open Issues](#issues)
* [Report a bug or ask for a feature](mailto:cpsuite@johanneslink.net)
*

### <a name="getting"></a> Getting it

Expand Down Expand Up @@ -153,6 +152,22 @@ By default, the classpath that can be retrieved via `System.getProperty("java.cl

In case the given property is not set, `java.class.path` is used as fallback value. This allows you to use the same annotated suite in both Ant and non-Ant context.

#### <a name="useClasspathFromJars"></a>Use Classpath From Jars Annotation

Eclipse (and other IDEs) contain an option to work around limitations for long classpaths by creating an empty JAR file
with a Manifest that contains the (otherwise too long) classpath.

If you want to use this feature, you need to add the `UseClasspathFromJars` annotation:

```java
@UseClasspathFromJars(true)
```
By default this feature is set to `false` and no additional scanning of JAR files is performed. If you set this
annotation to `true`, all JAR files on the initial classpath are opened and the classpath from the `Class-Path`
header in the Manifest is added to the classpath as well. (Note that this is not transitive, i.e., if such a
classpath from a JAR Manifest points to yet another JAR, that second JAR will never be checked for additional
classpath entries!)

#### <a name="beforeSuite"></a>BeforeSuite

You can annotate one or more methods with `@BeforeSuite` to indicate that they should be run _once_ before the full suite will be run.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ public boolean searchInJars() {
return true;
}

@Override
public boolean useClasspathFromJars() {
return true;
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/junit/extensions/cpsuite/ClassTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public interface ClassTester {
boolean acceptInnerClass();

boolean searchInJars();

boolean useClasspathFromJars();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

public interface ClassesFinderFactory {
ClassesFinder create(boolean searchInJars, String[] filterPatterns, SuiteType[] suiteTypes, Class<?>[] baseTypes,
Class<?>[] excludedBaseTypes, String classpathProperty);
Class<?>[] excludedBaseTypes, String classpathProperty, boolean useClasspathFromJars);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
package org.junit.extensions.cpsuite;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.jar.*;

/**
* Utility class to find classes within the class path, both inside and outside
Expand Down Expand Up @@ -40,10 +42,57 @@ private String getClasspath() {
return classPath;
}

private List<Class<?>> findClassesInClasspath(String classPath) {
return findClassesInRoots(splitClassPath(classPath));
private List<Class<?>> findClassesInClasspath(final String classPath) {
List<String> classpathRoots = splitClassPath(classPath);
if (this.tester.useClasspathFromJars()) {
classpathRoots = findAndAddClasspathFromJars(classpathRoots);
}
return findClassesInRoots(classpathRoots);
}

private List<String> findAndAddClasspathFromJars(final List<String> classpathRoots)
{
List<String> allRoots = new ArrayList<>(100);
allRoots.addAll(classpathRoots);

for (String root : classpathRoots) {
File classpathFile = new File(root);
if (isJarFile(classpathFile)) {
Manifest manifest = null;
try (URLClassLoader cl = new URLClassLoader(new URL[] { classpathFile.toURI().toURL() })) {
URL url = cl.findResource("META-INF/MANIFEST.MF");
if (url != null) {
try (InputStream inputStream = url.openStream()) {
manifest = new Manifest(inputStream);
}
}
} catch (IOException e) {
// ignore errors and just continue (manifest will be null, so this entry is just ignored)
}
if (manifest != null) {
Attributes attr = manifest.getMainAttributes();
String classPath = attr.getValue("Class-Path");
if (classPath != null) {
Collection<? extends String> segments = splitClassPathFromManifest(classPath);
for (String segment : segments) {
File relativeFile = new File(classpathFile.getParentFile(), segment);
try {
allRoots.add(relativeFile.getCanonicalPath());
} catch (IOException e) {
// just ignore the failure
}
}
}
}
}
}
return allRoots;
}

private Collection<? extends String> splitClassPathFromManifest(final String classPath) {
return Arrays.asList(classPath.split(" "));
}

private List<Class<?>> findClassesInRoots(List<String> roots) {
List<Class<?>> classes = new ArrayList<Class<?>>(100);
for (String root : roots) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
public class ClasspathFinderFactory implements ClassesFinderFactory {

public ClassesFinder create(boolean searchInJars, String[] filterPatterns, SuiteType[] suiteTypes, Class<?>[] baseTypes,
Class<?>[] excludedBaseTypes, String classpathProperty) {
ClassTester tester = new ClasspathSuiteTester(searchInJars, filterPatterns, suiteTypes, baseTypes, excludedBaseTypes);
Class<?>[] excludedBaseTypes, String classpathProperty, boolean useClasspathFromJars) {
ClassTester tester = new ClasspathSuiteTester(searchInJars, filterPatterns, suiteTypes, baseTypes, excludedBaseTypes, useClasspathFromJars);
return new ClasspathClassesFinder(tester, classpathProperty);
}

Expand Down
26 changes: 25 additions & 1 deletion src/main/java/org/junit/extensions/cpsuite/ClasspathSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class ClasspathSuite extends Suite {
private static final Class<?>[] DEFAULT_EXCLUDED_BASES_TYPES = new Class<?>[0];
private static final String[] DEFAULT_CLASSNAME_FILTERS = new String[0];
private static final String DEFAULT_CLASSPATH_PROPERTY = "java.class.path";
private static final boolean DEFAULT_USE_CLASSPATH_FROM_JARS = false;

private final Class<?> suiteClass;

Expand Down Expand Up @@ -94,6 +95,21 @@ public class ClasspathSuite extends Suite {
String value();
}

/**
* The <code>UseClasspathFromJars</code> annotation specifies if the Class-Path headers in the Manifest of Jars on the classpath shall be added to the search
* space or not. If the annotation is missing, the Class-Path header will be ignored.
*
* Some IDEs (e.g., Eclipse) can use a temporary empty Jar with the Class-Path in the Manifest to work around limitations of long classpaths. Set this
* annotation to <code>true</code> to support this workaround for the test suite.
*
* Note: only Jars on the original classpath are considered; i.e., the Jar Manifest analysis is not transitive.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface UseClasspathFromJars {
boolean value();
}

/**
* The <code>BeforeSuite</code> marks a method that will be run before the
* suite is run.
Expand All @@ -120,7 +136,7 @@ public ClasspathSuite(Class<?> suiteClass, RunnerBuilder builder, ClassesFinderF

private static ClassesFinder createFinder(Class<?> suiteClass, ClassesFinderFactory finderFactory) {
return finderFactory.create(getSearchInJars(suiteClass), getClassnameFilters(suiteClass), getSuiteTypes(suiteClass),
getBaseTypes(suiteClass), getExcludedBaseTypes(suiteClass), getClasspathProperty(suiteClass));
getBaseTypes(suiteClass), getExcludedBaseTypes(suiteClass), getClasspathProperty(suiteClass), getUseClasspathFromJars(suiteClass));
}

private static Class<?>[] getSortedTestclasses(ClassesFinder finder) {
Expand Down Expand Up @@ -185,6 +201,14 @@ private static String getClasspathProperty(Class<?> suiteClass) {
return cpPropertyAnnotation.value();
}

private static boolean getUseClasspathFromJars(final Class<?> suiteClass) {
UseClasspathFromJars useClasspathFromJarsAnnotation = suiteClass.getAnnotation(UseClasspathFromJars.class);
if (useClasspathFromJarsAnnotation == null) {
return DEFAULT_USE_CLASSPATH_FROM_JARS;
}
return useClasspathFromJarsAnnotation.value();
}

@Override
public void run(RunNotifier notifier) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class ClasspathSuiteTester implements ClassTester {
private List<JavaStyleClassnameMatcher> negationFilters;
private final Class<?>[] baseTypes;
private final Class<?>[] excludedBaseTypes;
private final boolean useClasspathFromJars;

/**
* @param searchInJars
Expand All @@ -40,13 +41,14 @@ public class ClasspathSuiteTester implements ClassTester {
* @param types
*/
public ClasspathSuiteTester(boolean searchInJars, String[] filterPatterns, SuiteType[] suiteTypes, Class<?>[] baseTypes,
Class<?>[] excludedBaseTypes) {
Class<?>[] excludedBaseTypes, final boolean useClasspathFromJars) {
this.searchInJars = searchInJars;
this.positiveFilters = findPositiveFilters(filterPatterns);
this.negationFilters = findNegationFilters(filterPatterns);
this.suiteTypes = suiteTypes;
this.baseTypes = baseTypes;
this.excludedBaseTypes = excludedBaseTypes;
this.useClasspathFromJars = useClasspathFromJars;
}

public boolean acceptClass(Class<?> clazz) {
Expand Down Expand Up @@ -210,4 +212,9 @@ public Class<?>[] getBaseTypes() {
public Class<?>[] getExcludedBaseTypes() {
return excludedBaseTypes;
}

@Override
public boolean useClasspathFromJars() {
return this.useClasspathFromJars;
}
}