diff --git a/tools/jbang/src/main/java/org/citrusframework/jbang/commands/CitrusCommand.java b/tools/jbang/src/main/java/org/citrusframework/jbang/commands/CitrusCommand.java index 28d5683550..e94830e178 100644 --- a/tools/jbang/src/main/java/org/citrusframework/jbang/commands/CitrusCommand.java +++ b/tools/jbang/src/main/java/org/citrusframework/jbang/commands/CitrusCommand.java @@ -33,7 +33,7 @@ public abstract class CitrusCommand implements Callable { CommandSpec spec; private final CitrusJBangMain main; - private File citrusDir; + private File userDir; //CHECKSTYLE:OFF @CommandLine.Option(names = { "-h", "--help" }, usageHelp = true, description = "Display the help and sub-commands") @@ -49,17 +49,19 @@ public CitrusJBangMain getMain() { } public File getStatusFile(String pid) { - if (citrusDir == null) { - citrusDir = new File(System.getProperty("user.home"), ".citrus"); - } - return new File(citrusDir, pid + "-status.json"); + return new File(getUserDir(), pid + "-status.json"); } public File getOutputFile(String pid) { - if (citrusDir == null) { - citrusDir = new File(System.getProperty("user.home"), ".citrus"); + return new File(getUserDir(), pid + "-output.json"); + } + + protected File getUserDir() { + if (userDir == null) { + userDir = new File(System.getProperty("user.home"), ".citrus"); } - return new File(citrusDir, pid + "-output.json"); + + return userDir; } protected abstract static class ParameterConsumer implements IParameterConsumer { diff --git a/tools/jbang/src/main/java/org/citrusframework/jbang/commands/ListTests.java b/tools/jbang/src/main/java/org/citrusframework/jbang/commands/ListTests.java index c27728b4b8..1b13cc2a43 100644 --- a/tools/jbang/src/main/java/org/citrusframework/jbang/commands/ListTests.java +++ b/tools/jbang/src/main/java/org/citrusframework/jbang/commands/ListTests.java @@ -16,22 +16,16 @@ */ package org.citrusframework.jbang.commands; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; import java.time.Duration; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import org.citrusframework.util.FileUtils; -import com.fasterxml.jackson.databind.JsonNode; import com.github.freva.asciitable.AsciiTable; import com.github.freva.asciitable.Column; import com.github.freva.asciitable.HorizontalAlign; import com.github.freva.asciitable.OverflowBehaviour; import main.CitrusJBang; -import org.citrusframework.jbang.JsonSupport; import org.citrusframework.jbang.CitrusJBangMain; import picocli.CommandLine; import picocli.CommandLine.Command; @@ -58,16 +52,15 @@ public Integer call() throws Exception { final long cur = ProcessHandle.current().pid(); ProcessHandle.allProcesses() .filter(ph -> ph.pid() != cur) + .filter(ph -> isTestProcess(ph.info())) .forEach(ph -> { - if (ph.info().commandLine().orElse("").contains(CitrusJBang.class.getSimpleName())) { - Row row = new Row(); - row.pid = "" + ph.pid(); - row.uptime = extractSince(ph); - row.ago = printSince(row.uptime); - row.name = extractName(ph); - row.status = ph.isAlive() ? "Running" : "Finished"; - rows.add(row); - } + Row row = new Row(); + row.pid = "" + ph.pid(); + row.uptime = extractSince(ph); + row.ago = printSince(row.uptime); + row.name = extractName(ph); + row.status = ph.isAlive() ? "Running" : "Finished"; + rows.add(row); }); // sort rows @@ -88,9 +81,13 @@ public Integer call() throws Exception { return 0; } + protected boolean isTestProcess(ProcessHandle.Info info) { + return info.commandLine().orElse("").contains(CitrusJBang.class.getSimpleName()); + } + private String extractName(ProcessHandle ph) { String cl = ph.info().commandLine().orElse(""); - String citrusJBangRun = String.format("main.%s run ", CitrusJBang.class.getSimpleName()); + String citrusJBangRun = getJBangRunCommand(); if (cl.contains(citrusJBangRun)) { return cl.substring(cl.indexOf(citrusJBangRun) + citrusJBangRun.length()); } @@ -98,6 +95,10 @@ private String extractName(ProcessHandle ph) { return ""; } + protected String getJBangRunCommand() { + return String.format("main.%s run ", CitrusJBang.class.getSimpleName()); + } + private String printSince(long timestamp) { long age = System.currentTimeMillis() - timestamp; Duration duration = Duration.ofMillis(age); @@ -138,22 +139,7 @@ protected int sortRow(Row o1, Row o2) { } } - private JsonNode loadStatus(long pid) { - try { - File f = getStatusFile("" + pid); - if (f != null && f.exists()) { - try (FileInputStream fis = new FileInputStream(f)) { - String text = FileUtils.readToString(fis); - return JsonSupport.json().reader().readTree(text); - } - } - } catch (IOException e) { - // ignore - } - return null; - } - - static long extractSince(ProcessHandle ph) { + protected static long extractSince(ProcessHandle ph) { long since = 0; if (ph.info().startInstant().isPresent()) { since = ph.info().startInstant().get().toEpochMilli(); @@ -161,7 +147,7 @@ static long extractSince(ProcessHandle ph) { return since; } - private static class Row { + protected static class Row { String pid; String name; String status; diff --git a/tools/jbang/src/main/java/org/citrusframework/jbang/commands/Run.java b/tools/jbang/src/main/java/org/citrusframework/jbang/commands/Run.java index d8a9941d4d..519cbaf358 100644 --- a/tools/jbang/src/main/java/org/citrusframework/jbang/commands/Run.java +++ b/tools/jbang/src/main/java/org/citrusframework/jbang/commands/Run.java @@ -147,7 +147,7 @@ private int run() throws Exception { return exitStatus.exitStatus(); } - private TestRunConfiguration getRunConfiguration(List files) { + protected TestRunConfiguration getRunConfiguration(List files) { TestRunConfiguration configuration = new TestRunConfiguration(); String ext = FileUtils.getFileExtension(files.get(0));