(0);
- for (CompilerMessage compilerMessage : result.getCompilerMessages()) {
- if (compilerMessage.isError()) {
- errors.add(compilerMessage);
+ /**
+ * Returns this module's test classpath, which is what the verifiers and the generated sources are compiled
+ * against.
+ *
+ * Surefire normally hands the forked JVM a manifest-only "booter" jar rather than a real classpath, so
+ * {@code java.class.path} on its own is a single jar whose {@code Class-Path} manifest entry holds the actual
+ * entries. Expanding that keeps this in step with whatever the POM declares, with no build step to copy jars
+ * around and no second list to maintain here.
+ */
+ private static List resolveTestClasspath() {
+ List classPath = new ArrayList<>();
+ for (String entry : System.getProperty("java.class.path", "").split(File.pathSeparator)) {
+ if (entry.isEmpty()) {
+ continue;
}
+ classPath.add(entry);
+ // a jar may carry its own Class-Path, so keep both it and whatever it points at
+ classPath.addAll(expandManifestClassPath(new File(entry)));
}
+ return classPath;
+ }
- assertEquals(0, errors.size(), "There was compilation errors: " + errors);
+ private static List expandManifestClassPath(File jar) {
+ if (!jar.isFile() || !jar.getName().endsWith(".jar")) {
+ return new ArrayList<>();
+ }
+ try (JarFile jarFile = new JarFile(jar)) {
+ Manifest manifest = jarFile.getManifest();
+ if (manifest == null) {
+ return new ArrayList<>();
+ }
+ String classPath = manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
+ if (classPath == null || classPath.trim().isEmpty()) {
+ return new ArrayList<>();
+ }
+ List entries = new ArrayList<>();
+ for (String entry : classPath.trim().split("\\s+")) {
+ try {
+ entries.add(Paths.get(URI.create(entry)).toString());
+ } catch (IllegalArgumentException | java.nio.file.FileSystemNotFoundException e) {
+ // a relative entry, resolved against the jar itself
+ entries.add(new File(jar.getParentFile(), entry).getAbsolutePath());
+ }
+ }
+ return entries;
+ } catch (IOException e) {
+ throw new UncheckedIOException("Could not read the manifest of " + jar, e);
+ }
}
/**
@@ -213,6 +264,14 @@ protected void verifyCompiledGeneratedSources(String verifierClassName) {
addClassPathFile(getTestFile("target/test-classes"));
+ // the verifier runs in a classloader with no parent, so it needs the test classpath spelled out
+ for (String entry : resolveTestClasspath()) {
+ File file = new File(entry);
+ if (file.exists()) {
+ addClassPathFile(file);
+ }
+ }
+
ClassLoader oldCCL = Thread.currentThread().getContextClassLoader();
URLClassLoader classLoader = URLClassLoader.newInstance(urls.toArray(new URL[urls.size()]), null);