diff --git a/java/server/src/org/openqa/selenium/cli/CliCommand.java b/java/server/src/org/openqa/selenium/cli/CliCommand.java index 41a4854848cf1..01e5be60a7b3f 100644 --- a/java/server/src/org/openqa/selenium/cli/CliCommand.java +++ b/java/server/src/org/openqa/selenium/cli/CliCommand.java @@ -17,13 +17,15 @@ package org.openqa.selenium.cli; +import java.io.PrintStream; + public interface CliCommand { String getName(); String getDescription(); - Executable configure(String... args); + Executable configure(PrintStream out, PrintStream err, String... args); default boolean isShown() { return true; diff --git a/java/server/src/org/openqa/selenium/grid/Main.java b/java/server/src/org/openqa/selenium/grid/Main.java index 7b8746278e2d8..2aa0a39cfdead 100644 --- a/java/server/src/org/openqa/selenium/grid/Main.java +++ b/java/server/src/org/openqa/selenium/grid/Main.java @@ -21,6 +21,7 @@ import org.openqa.selenium.cli.WrappedPrintWriter; import java.io.File; +import java.io.PrintStream; import java.io.PrintWriter; import java.io.UncheckedIOException; import java.net.MalformedURLException; @@ -44,9 +45,25 @@ public class Main { private static final Logger LOG = Logger.getLogger(Main.class.getName()); + private final PrintStream out; + private final PrintStream err; + private final String[] args; + public static void main(String[] args) throws Exception { + new Main(System.out, System.err, args).go(); + } + + Main(PrintStream out, PrintStream err, String[] args) { + // It's not private to make it visible for tests + this.out = out; + this.err = err; + this.args = args; + } + + void go() throws Exception { + // It's not private to make it visible for tests if (args.length == 0) { - new Help(loadCommands(Main.class.getClassLoader())).configure().run(); + new Help(loadCommands(Main.class.getClassLoader())).configure(out, err).run(); } else { if ("--ext".equals(args[0])) { if (args.length > 1) { @@ -90,10 +107,10 @@ public static void main(String[] args) throws Exception { System.arraycopy(args, 2, remainingArgs, 0, args.length - 2); launch(remainingArgs, loader); } else { - new Help(loadCommands(loader)).configure().run(); + new Help(loadCommands(loader)).configure(out, err).run(); } } else { - new Help(loadCommands(Main.class.getClassLoader())).configure().run(); + new Help(loadCommands(Main.class.getClassLoader())).configure(out, err).run(); } } else { @@ -108,7 +125,7 @@ private static Set loadCommands(ClassLoader loader) { return commands; } - private static void launch(String[] args, ClassLoader loader) throws Exception { + private void launch(String[] args, ClassLoader loader) throws Exception { String commandName = args[0]; String[] remainingArgs = new String[args.length - 1]; System.arraycopy(args, 1, remainingArgs, 0, args.length - 1); @@ -120,7 +137,7 @@ private static void launch(String[] args, ClassLoader loader) throws Exception { .findFirst() .orElse(new Help(commands)); - command.configure(remainingArgs).run(); + command.configure(out, err, remainingArgs).run(); } private static class Help implements CliCommand { @@ -143,7 +160,7 @@ public String getDescription() { } @Override - public Executable configure(String... args) { + public Executable configure(PrintStream out, PrintStream err, String... args) { return () -> { int longest = commands.stream() .filter(CliCommand::isShown) @@ -152,28 +169,28 @@ public Executable configure(String... args) { .map(String::length) .orElse(0) + 2; // two space padding either side - PrintWriter out = new WrappedPrintWriter(System.out, 72, 0); - out.append(getName()).append("\n\n"); - out.append(getDescription()).append("\n").append("\n"); + PrintWriter outWriter = new WrappedPrintWriter(out, 72, 0); + outWriter.append(getName()).append("\n\n"); + outWriter.append(getDescription()).append("\n").append("\n"); int indent = Math.min(longest + 2, 25); String format = " %-" + longest + "s"; - PrintWriter indented = new WrappedPrintWriter(System.out, 72, indent); + PrintWriter indented = new WrappedPrintWriter(out, 72, indent); commands.stream() .filter(CliCommand::isShown) .forEach(cmd -> indented.format(format, cmd.getName()) .append(cmd.getDescription()) .append("\n")); - out.write("\nFor each command, run with `--help` for command-specific help\n"); - out.write("\nUse the `--ext` flag before the command name to specify an additional " + + outWriter.write("\nFor each command, run with `--help` for command-specific help\n"); + outWriter.write("\nUse the `--ext` flag before the command name to specify an additional " + "classpath to use with the server (for example, to provide additional " + "commands, or to provide additional driver implementations). For example:\n"); - out.write(String.format( + outWriter.write(String.format( "\n java -jar selenium.jar --ext example.jar%sdir standalone --port 1234", File.pathSeparator)); - System.out.println("\n"); + out.println("\n"); }; } } diff --git a/java/server/src/org/openqa/selenium/grid/TemplateGridCommand.java b/java/server/src/org/openqa/selenium/grid/TemplateGridCommand.java index e1eb3e90342c4..d193a093079a3 100644 --- a/java/server/src/org/openqa/selenium/grid/TemplateGridCommand.java +++ b/java/server/src/org/openqa/selenium/grid/TemplateGridCommand.java @@ -29,13 +29,14 @@ import org.openqa.selenium.grid.log.LoggingOptions; import org.openqa.selenium.grid.server.HelpFlags; +import java.io.PrintStream; import java.util.LinkedHashSet; import java.util.Set; public abstract class TemplateGridCommand implements CliCommand { @Override - public final Executable configure(String... args) { + public final Executable configure(PrintStream out, PrintStream err, String... args) { Set allFlags = getFlagObjects(); HelpFlags helpFlags = new HelpFlags(); @@ -52,12 +53,12 @@ public final Executable configure(String... args) { try { commander.parse(args); } catch (ParameterException e) { - System.err.println(e.getMessage()); + err.println(e.getMessage()); commander.usage(); return; } - if (helpFlags.displayHelp(commander, System.out)) { + if (helpFlags.displayHelp(commander, out)) { return; } @@ -70,7 +71,7 @@ public final Executable configure(String... args) { Config config = new CompoundConfig(allConfigs.toArray(new Config[0])); - if (helpFlags.dumpConfig(config, System.out)) { + if (helpFlags.dumpConfig(config, out)) { return; } diff --git a/java/server/src/org/openqa/selenium/grid/commands/InfoCommand.java b/java/server/src/org/openqa/selenium/grid/commands/InfoCommand.java index 86f6c0ced3887..4218e997e51b3 100644 --- a/java/server/src/org/openqa/selenium/grid/commands/InfoCommand.java +++ b/java/server/src/org/openqa/selenium/grid/commands/InfoCommand.java @@ -24,6 +24,7 @@ import java.io.BufferedReader; import java.io.IOException; +import java.io.PrintStream; import java.io.StringReader; import java.nio.charset.StandardCharsets; import org.openqa.selenium.cli.CliCommand; @@ -33,7 +34,6 @@ import java.io.PrintWriter; import java.util.Collections; - @AutoService(CliCommand.class) public class InfoCommand implements CliCommand { @@ -45,7 +45,7 @@ public String getDescription() { return "Prints information for commands and topics."; } - public Executable configure(String... args) { + public Executable configure(PrintStream out, PrintStream err, String... args) { HelpFlags help = new HelpFlags(); InfoFlags topic = new InfoFlags(); @@ -59,12 +59,12 @@ public Executable configure(String... args) { try { commander.parse(args); } catch (ParameterException e) { - System.err.println(e.getMessage()); + err.println(e.getMessage()); commander.usage(); return; } - if (help.displayHelp(commander, System.out)) { + if (help.displayHelp(commander, out)) { return; } @@ -92,11 +92,11 @@ public Executable configure(String... args) { String path = getClass().getPackage().getName().replaceAll("\\.", "/") + "/" + toDisplay; String content = readContent(path); - PrintWriter out = new WrappedPrintWriter(System.out, 72, 0); + PrintWriter outWriter = new WrappedPrintWriter(out, 72, 0); - out.printf("\n%s\n%s\n\n", title, String.join("", Collections.nCopies(title.length(), "="))); - out.print(content); - out.println("\n\n"); + outWriter.printf("\n%s\n%s\n\n", title, String.join("", Collections.nCopies(title.length(), "="))); + outWriter.print(content); + outWriter.println("\n\n"); }; } diff --git a/java/server/src/org/openqa/selenium/grid/distributor/httpd/DistributorServer.java b/java/server/src/org/openqa/selenium/grid/distributor/httpd/DistributorServer.java index 52263bb35ba5f..204416904a51c 100644 --- a/java/server/src/org/openqa/selenium/grid/distributor/httpd/DistributorServer.java +++ b/java/server/src/org/openqa/selenium/grid/distributor/httpd/DistributorServer.java @@ -43,7 +43,6 @@ import java.util.Set; import java.util.logging.Logger; - @AutoService(CliCommand.class) public class DistributorServer extends TemplateGridCommand {