Skip to content

Commit

Permalink
reapply Marccins #822 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry Coles committed Jun 1, 2021
1 parent d51eb95 commit f6da0bc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
10 changes: 6 additions & 4 deletions pitest/src/main/java/org/pitest/classpath/ClassPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import java.util.logging.Logger;
import java.util.zip.ZipException;

import static org.pitest.util.ManifestUtils.CLASSPATH_JAR_FILE_PREFIX;

public class ClassPath {

private static final Logger LOG = Log.getLogger();
Expand Down Expand Up @@ -128,14 +130,14 @@ public static Collection<File> getClassPathElementsAsFiles() {
/**
* Because classpaths can become longer than the OS supports pitest creates temporary jar files and places the classpath
* in the manifest where there is no size limit.
*
*
* We must therefore parse them out again here.
*
*
* @param elements existing elements
*/
private static void addEntriesFromClasspathManifest(final Set<File> elements) {
Optional<File> maybeJar = elements.stream().filter( f -> f.getName().startsWith("classpath") && f.getName().endsWith(".jar"))
.findFirst();
Optional<File> maybeJar = elements.stream().filter(f -> f.getName().startsWith(CLASSPATH_JAR_FILE_PREFIX) && f.getName().endsWith(".jar"))
.findFirst();
maybeJar.ifPresent(file -> elements.addAll(ManifestUtils.readClasspathManifest(file)));
}

Expand Down
32 changes: 17 additions & 15 deletions pitest/src/main/java/org/pitest/util/ManifestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,52 +34,54 @@
*/
public class ManifestUtils {

public static final String CLASSPATH_JAR_FILE_PREFIX = "pitest-classpath-jar-file-";

// Method based on
// https://github.com/JetBrains/intellij-community/blob/master/java/java-runtime/src/com/intellij/rt/execution/testFrameworks/ForkedByModuleSplitter.java
// JetBrains copyright notice and licence retained above.
public static File createClasspathJarFile(String classpath)
throws IOException {
throws IOException {
final Manifest manifest = new Manifest();
final Attributes attributes = manifest.getMainAttributes();
attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");

String classpathForManifest = "";
StringBuilder classpathForManifest = new StringBuilder();
int idx = 0;
int endIdx = 0;
while (endIdx >= 0) {
endIdx = classpath.indexOf(File.pathSeparator, idx);
String path = endIdx < 0 ? classpath.substring(idx)
: classpath.substring(idx, endIdx);
: classpath.substring(idx, endIdx);
if (classpathForManifest.length() > 0) {
classpathForManifest += " ";
classpathForManifest.append(" ");
}

classpathForManifest += new File(path).toURI().toURL().toString();
classpathForManifest.append(new File(path).toURI().toURL());
idx = endIdx + File.pathSeparator.length();
}
attributes.put(Attributes.Name.CLASS_PATH, classpathForManifest);
attributes.put(Attributes.Name.CLASS_PATH, classpathForManifest.toString());

File jarFile = File.createTempFile("classpath", ".jar");
File jarFile = File.createTempFile(CLASSPATH_JAR_FILE_PREFIX, ".jar");
try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(jarFile));
ZipOutputStream jarPlugin = new JarOutputStream(out, manifest)
) {
jarFile.deleteOnExit();
ZipOutputStream jarPlugin = new JarOutputStream(out, manifest)
) {
jarFile.deleteOnExit();
}

return jarFile;
}

public static Collection<File> readClasspathManifest(File file) {
try (FileInputStream fis = new FileInputStream(file);
JarInputStream jarStream = new JarInputStream(fis)) {
JarInputStream jarStream = new JarInputStream(fis)) {
Manifest mf = jarStream.getManifest();
Attributes att = mf.getMainAttributes();
String cp = att.getValue(Attributes.Name.CLASS_PATH);
String[] parts = cp.split("file:");
return Arrays.stream(parts)
.filter(part -> !part.isEmpty())
.map(part -> new File(part.trim()))
.collect(Collectors.toList());
.filter(part -> !part.isEmpty())
.map(part -> new File(part.trim()))
.collect(Collectors.toList());
} catch (IOException ex) {
throw new RuntimeException("Could not read classpath jar manifest", ex);
}
Expand Down

0 comments on commit f6da0bc

Please sign in to comment.