Skip to content

Commit

Permalink
Merge pull request #1382 from hcoles/feature/remove_backports
Browse files Browse the repository at this point in the history
Feature/remove backports
  • Loading branch information
hcoles authored Feb 5, 2025
2 parents b50fc19 + 0f9d910 commit 6355a82
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.pitest.classinfo.ClassName;
import org.pitest.classinfo.Repository;
import org.pitest.classinfo.TestToClassMapper;
import org.pitest.functional.Streams;

import java.util.Collection;
import java.util.HashSet;
Expand Down Expand Up @@ -63,7 +62,7 @@ public Optional<ClassName> findTestee(final String className) {

public Collection<ClassHash> fetchClassHashes(final Collection<ClassName> classes) {
return classes.stream()
.flatMap(c -> Streams.fromOptional(classRepository.fetchClassHash(c)))
.flatMap(c -> classRepository.fetchClassHash(c).stream())
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.pitest.classpath.ClassPathByteArraySource;
import java.util.Optional;
import org.pitest.process.JavaAgent;
import org.pitest.util.FileUtil;
import org.pitest.util.PitError;
import org.pitest.util.Unchecked;

Expand Down Expand Up @@ -68,10 +67,14 @@ public Optional<String> getJarLocation() {
private Optional<String> createJar() {
try {

final File randomName = File.createTempFile(FileUtil.randomFilename(),
final File randomName = File.createTempFile("pitest-agent",
".jar");

final FileOutputStream fos = new FileOutputStream(randomName);
createJarFromClassPathResources(fos, randomName.getAbsolutePath());

randomName.deleteOnExit();

return Optional.ofNullable(randomName.getAbsolutePath());

} catch (final IOException ex) {
Expand Down
78 changes: 36 additions & 42 deletions pitest-entry/src/main/java/org/pitest/process/Java9Process.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.pitest.process;

import static java.util.Arrays.asList;
import static org.pitest.functional.prelude.Prelude.or;
import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
Expand All @@ -12,37 +10,35 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;

import org.pitest.functional.FCollection;
import java.util.stream.Collectors;

/**
* Process for java 9+, using file to pass all parameters
*/
public class Java9Process implements WrappingProcess {

private static final Map<Optional<String>, Path> CACHE = new ConcurrentHashMap<>();

private final int port;
private final ProcessArgs processArgs;
private final Class<?> minionClass;
private JavaProcess process;

private final long pid = ProcessHandle.current().pid();

private static int counter = 0;

public Java9Process(int port, ProcessArgs args, Class<?> minionClass) {
this.port = port;
this.processArgs = args;
this.minionClass = minionClass;
}

public void start() throws IOException {
String[] args = { "" + this.port };
String args = "" + this.port;

ProcessBuilder processBuilder = createProcessBuilder(
this.processArgs.getJavaExecutable(),
this.processArgs.getJvmArgs(),
this.minionClass, asList(args),
this.minionClass, args,
this.processArgs.getJavaAgentFinder(),
this.processArgs.getLaunchClassPath());

Expand Down Expand Up @@ -76,21 +72,23 @@ public JavaProcess getProcess() {
}

private ProcessBuilder createProcessBuilder(String javaProc,
List<String> args, Class<?> mainClass, List<String> programArgs,
List<String> args, Class<?> mainClass, String programArgs,
JavaAgent javaAgent, String classPath) {
List<String> cmd = createLaunchArgs(javaAgent, args, mainClass,
programArgs, classPath);
List<String> fileArgs = createLaunchArgs(javaAgent, args, classPath);

removeJacocoAgent(cmd);
removeJacocoAgent(fileArgs);

try {
// all arguments are passed via a temporary file, thereby avoiding command line length limits
Path argsFile = createArgsFile(cmd);
return new ProcessBuilder(asList(javaProc, "@" + argsFile.toFile().getAbsolutePath()));
} catch (IOException e) {
throw new RuntimeException(e);
}

// All arguments are passed via a temporary file, thereby avoiding command line length limits
Path argsFile = CACHE.computeIfAbsent(javaAgent.getJarLocation(), j -> createArgsFile(fileArgs));

List<String> cmd = new ArrayList<>();
cmd.add(javaProc);
cmd.add("@" + argsFile.toFile().getAbsolutePath());
cmd.add(mainClass.getName());
cmd.add(programArgs);

return new ProcessBuilder(cmd);
}
private void removeJacocoAgent(List<String> cmd) {
removeFromClassPath(cmd, line -> line.startsWith("-javaagent") && line.contains("jacoco"));
Expand All @@ -104,8 +102,7 @@ private static void removeFromClassPath(List<String> cmd, Predicate<String> matc
}
}

private List<String> createLaunchArgs(JavaAgent agentJarLocator, List<String> args, Class<?> mainClass,
List<String> programArgs, String classPath) {
private List<String> createLaunchArgs(JavaAgent agentJarLocator, List<String> args, String classPath) {

List<String> cmd = new ArrayList<>();

Expand All @@ -118,22 +115,22 @@ private List<String> createLaunchArgs(JavaAgent agentJarLocator, List<String> ar

addLaunchJavaAgents(cmd);

cmd.add(mainClass.getName());
cmd.addAll(programArgs);
return cmd;
}

private Path createArgsFile(List<String> cmd) throws IOException {
// To avoid conflicts with running analysis, we use the PID as part of the file name
// To prevent conflicts between multiple threads counter is used
// All files should be deleted on process exit, although some garbage may be left
// if the process is killed. Files are however created in the system temp directory
// so should be cleaned up on reboot
String name = "pitest-args-" + pid + "-" + nextCounter();
Path args = Files.createTempFile(name, ".args");
args.toFile().deleteOnExit();
Files.write(args, cmd);
return args;
private Path createArgsFile(List<String> cmd) {
try {
// All files should be deleted on process exit, although some garbage may be left
// if the process is killed. Files are however created in the system temp directory
// so should be cleaned up on reboot
String name = "pitest-args-";
Path args = Files.createTempFile(name, ".args");
args.toFile().deleteOnExit();
Files.write(args, cmd);
return args;
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static void addPITJavaAgent(JavaAgent agentJarLocator,
Expand All @@ -144,8 +141,9 @@ private static void addPITJavaAgent(JavaAgent agentJarLocator,

private static void addLaunchJavaAgents(List<String> cmd) {
RuntimeMXBean rt = ManagementFactory.getRuntimeMXBean();
List<String> agents = FCollection.filter(rt.getInputArguments(),
or(isJavaAgentParam(), isEnvironmentSetting()));
List<String> agents = rt.getInputArguments().stream()
.filter(isJavaAgentParam().or(isEnvironmentSetting()))
.collect(Collectors.toList());
cmd.addAll(agents);
}

Expand All @@ -157,8 +155,4 @@ private static Predicate<String> isJavaAgentParam() {
return a -> a.toLowerCase().startsWith("-javaagent");
}

private static synchronized int nextCounter() {
return counter++;
}

}
31 changes: 0 additions & 31 deletions pitest-entry/src/main/java/org/pitest/util/FileUtil.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import org.pitest.classpath.ClassPath;
import org.pitest.help.PitHelpError;
import org.pitest.mutationtest.engine.gregor.Generated;
import org.pitest.util.FileUtil;
import org.pitest.util.IsolationUtils;

import com.example.BeforeAfterClassTest;
Expand Down Expand Up @@ -233,7 +232,7 @@ public void shouldExcludeFilteredClasses() {
public void shouldMutateClassesSuppliedToAlternateClassPath()
throws IOException {
// yes, this is horrid
final String location = FileUtil.randomFilename() + ".jar";
final String location = ("" + Math.random()).replaceAll("\\.", "") + ".jar";
try {
try (FileOutputStream fos = new FileOutputStream(location)) {
final InputStream stream = IsolationUtils.getContextClassLoader()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;

import org.hamcrest.Matcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.pitest.util.FileUtil;
import org.pitest.util.PitError;

public class FileWriterFactoryTest {
Expand All @@ -33,7 +33,7 @@ public void writeToFile() throws IOException {
writer.write("test");
writerFactory.close();

final String content = FileUtil.readToString(new FileInputStream(file));
final String content = Files.readString(file.toPath());
assertThat(content, equalTo("test"));
}

Expand All @@ -57,7 +57,7 @@ public void writeToFileWithinFolder() throws IOException {
writer.write("test");
writerFactory.close();

final String content = FileUtil.readToString(new FileInputStream(file));
final String content = Files.readString(file.toPath());
assertThat(content, equalTo("test"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public HierarchicalClassId getHierarchicalId() {
@Override
public Collection<ClassHash> fetchClassHashes(Collection<ClassName> classes) {
return classes.stream()
.flatMap(c -> Streams.fromOptional(fetchClassHash(c)))
.flatMap(c -> fetchClassHash(c).stream())
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@
import org.pitest.mutationtest.MutationResultListener;
import org.pitest.mutationtest.SourceLocator;
import org.pitest.mutationtest.verify.BuildMessage;
import org.pitest.util.FileUtil;
import org.pitest.util.IsolationUtils;
import org.pitest.util.Log;
import org.pitest.util.ResultOutputStrategy;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -83,9 +84,9 @@ public MutationHtmlReportListener(Charset outputCharset,
}

private String loadCss() {
try {
return FileUtil.readToString(IsolationUtils.getContextClassLoader()
.getResourceAsStream("templates/mutation/style.css"));
try (InputStream is = IsolationUtils.getContextClassLoader()
.getResourceAsStream("templates/mutation/style.css")) {
return new String(is.readAllBytes(), StandardCharsets.UTF_8);
} catch (final IOException e) {
Log.getLogger().log(Level.SEVERE, "Error while loading css", e);
}
Expand Down
3 changes: 0 additions & 3 deletions pitest/src/main/java/org/pitest/functional/Streams.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import java.util.stream.Stream;

public class Streams {
public static <T> Stream<T> fromOptional(java.util.Optional<T> opt) {
return opt.map(Stream::of).orElseGet(Stream::empty);
}

/**
* Convert to stream with a null check. Added for easy migration
Expand Down

0 comments on commit 6355a82

Please sign in to comment.