Cli: Simplify test terminals
This commit simplifies and consolidates the two different implementations of terminals used in tests. There is now a single MockTerminal which captures output, and allows accessing as one large string (with unix style \n as newlines), as well as configuring input.
This commit is contained in:
parent
d079830f10
commit
ed7934ee79
|
@ -26,8 +26,8 @@ import org.elasticsearch.common.Strings;
|
||||||
import org.elasticsearch.common.SuppressForbidden;
|
import org.elasticsearch.common.SuppressForbidden;
|
||||||
import org.elasticsearch.common.cli.CliTool;
|
import org.elasticsearch.common.cli.CliTool;
|
||||||
import org.elasticsearch.common.cli.CliToolConfig;
|
import org.elasticsearch.common.cli.CliToolConfig;
|
||||||
import org.elasticsearch.common.cli.Terminal;
|
|
||||||
import org.elasticsearch.common.cli.UserError;
|
import org.elasticsearch.common.cli.UserError;
|
||||||
|
import org.elasticsearch.common.cli.Terminal;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.env.Environment;
|
import org.elasticsearch.env.Environment;
|
||||||
import org.elasticsearch.monitor.jvm.JvmInfo;
|
import org.elasticsearch.monitor.jvm.JvmInfo;
|
||||||
|
@ -37,7 +37,6 @@ import java.util.Iterator;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import static org.elasticsearch.common.cli.CliToolConfig.Builder.cmd;
|
import static org.elasticsearch.common.cli.CliToolConfig.Builder.cmd;
|
||||||
import static org.elasticsearch.common.cli.CliToolConfig.Builder.optionBuilder;
|
import static org.elasticsearch.common.cli.CliToolConfig.Builder.optionBuilder;
|
||||||
|
|
|
@ -23,6 +23,7 @@ import java.io.BufferedReader;
|
||||||
import java.io.Console;
|
import java.io.Console;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.PrintWriter;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
import org.elasticsearch.common.SuppressForbidden;
|
import org.elasticsearch.common.SuppressForbidden;
|
||||||
|
@ -52,6 +53,13 @@ public abstract class Terminal {
|
||||||
/** The current verbosity for the terminal, defaulting to {@link Verbosity#NORMAL}. */
|
/** The current verbosity for the terminal, defaulting to {@link Verbosity#NORMAL}. */
|
||||||
private Verbosity verbosity = Verbosity.NORMAL;
|
private Verbosity verbosity = Verbosity.NORMAL;
|
||||||
|
|
||||||
|
/** The newline used when calling println. */
|
||||||
|
private final String lineSeparator;
|
||||||
|
|
||||||
|
protected Terminal(String lineSeparator) {
|
||||||
|
this.lineSeparator = lineSeparator;
|
||||||
|
}
|
||||||
|
|
||||||
/** Sets the verbosity of the terminal. */
|
/** Sets the verbosity of the terminal. */
|
||||||
void setVerbosity(Verbosity verbosity) {
|
void setVerbosity(Verbosity verbosity) {
|
||||||
this.verbosity = verbosity;
|
this.verbosity = verbosity;
|
||||||
|
@ -63,8 +71,8 @@ public abstract class Terminal {
|
||||||
/** Reads password text from the terminal input. See {@link Console#readPassword()}}. */
|
/** Reads password text from the terminal input. See {@link Console#readPassword()}}. */
|
||||||
public abstract char[] readSecret(String prompt);
|
public abstract char[] readSecret(String prompt);
|
||||||
|
|
||||||
/** Print a message directly to the terminal. */
|
/** Returns a Writer which can be used to write to the terminal directly. */
|
||||||
protected abstract void doPrint(String msg);
|
public abstract PrintWriter getWriter();
|
||||||
|
|
||||||
/** Prints a line to the terminal at {@link Verbosity#NORMAL} verbosity level. */
|
/** Prints a line to the terminal at {@link Verbosity#NORMAL} verbosity level. */
|
||||||
public final void println(String msg) {
|
public final void println(String msg) {
|
||||||
|
@ -74,7 +82,8 @@ public abstract class Terminal {
|
||||||
/** Prints a line to the terminal at {@code verbosity} level. */
|
/** Prints a line to the terminal at {@code verbosity} level. */
|
||||||
public final void println(Verbosity verbosity, String msg) {
|
public final void println(Verbosity verbosity, String msg) {
|
||||||
if (this.verbosity.ordinal() >= verbosity.ordinal()) {
|
if (this.verbosity.ordinal() >= verbosity.ordinal()) {
|
||||||
doPrint(msg + System.lineSeparator());
|
getWriter().print(msg + lineSeparator);
|
||||||
|
getWriter().flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,14 +91,17 @@ public abstract class Terminal {
|
||||||
|
|
||||||
private static final Console console = System.console();
|
private static final Console console = System.console();
|
||||||
|
|
||||||
|
ConsoleTerminal() {
|
||||||
|
super(System.lineSeparator());
|
||||||
|
}
|
||||||
|
|
||||||
static boolean isSupported() {
|
static boolean isSupported() {
|
||||||
return console != null;
|
return console != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doPrint(String msg) {
|
public PrintWriter getWriter() {
|
||||||
console.printf("%s", msg);
|
return console.writer();
|
||||||
console.flush();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -105,16 +117,25 @@ public abstract class Terminal {
|
||||||
|
|
||||||
private static class SystemTerminal extends Terminal {
|
private static class SystemTerminal extends Terminal {
|
||||||
|
|
||||||
|
private final PrintWriter writer = newWriter();
|
||||||
|
|
||||||
|
SystemTerminal() {
|
||||||
|
super(System.lineSeparator());
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressForbidden(reason = "Writer for System.out")
|
||||||
|
private static PrintWriter newWriter() {
|
||||||
|
return new PrintWriter(System.out);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressForbidden(reason = "System#out")
|
public PrintWriter getWriter() {
|
||||||
public void doPrint(String msg) {
|
return writer;
|
||||||
System.out.print(msg);
|
|
||||||
System.out.flush();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String readText(String text) {
|
public String readText(String text) {
|
||||||
doPrint(text);
|
getWriter().print(text);
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
|
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
|
||||||
try {
|
try {
|
||||||
return reader.readLine();
|
return reader.readLine();
|
||||||
|
|
|
@ -19,41 +19,40 @@
|
||||||
|
|
||||||
package org.elasticsearch.common.cli;
|
package org.elasticsearch.common.cli;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.hasItem;
|
|
||||||
import static org.hamcrest.Matchers.hasSize;
|
|
||||||
|
|
||||||
public class TerminalTests extends CliToolTestCase {
|
public class TerminalTests extends CliToolTestCase {
|
||||||
public void testVerbosity() throws Exception {
|
public void testVerbosity() throws Exception {
|
||||||
CaptureOutputTerminal terminal = new CaptureOutputTerminal(Terminal.Verbosity.SILENT);
|
MockTerminal terminal = new MockTerminal();
|
||||||
|
terminal.setVerbosity(Terminal.Verbosity.SILENT);
|
||||||
assertPrinted(terminal, Terminal.Verbosity.SILENT, "text");
|
assertPrinted(terminal, Terminal.Verbosity.SILENT, "text");
|
||||||
assertNotPrinted(terminal, Terminal.Verbosity.NORMAL, "text");
|
assertNotPrinted(terminal, Terminal.Verbosity.NORMAL, "text");
|
||||||
assertNotPrinted(terminal, Terminal.Verbosity.VERBOSE, "text");
|
assertNotPrinted(terminal, Terminal.Verbosity.VERBOSE, "text");
|
||||||
|
|
||||||
terminal = new CaptureOutputTerminal(Terminal.Verbosity.NORMAL);
|
terminal = new MockTerminal();
|
||||||
assertPrinted(terminal, Terminal.Verbosity.SILENT, "text");
|
assertPrinted(terminal, Terminal.Verbosity.SILENT, "text");
|
||||||
assertPrinted(terminal, Terminal.Verbosity.NORMAL, "text");
|
assertPrinted(terminal, Terminal.Verbosity.NORMAL, "text");
|
||||||
assertNotPrinted(terminal, Terminal.Verbosity.VERBOSE, "text");
|
assertNotPrinted(terminal, Terminal.Verbosity.VERBOSE, "text");
|
||||||
|
|
||||||
terminal = new CaptureOutputTerminal(Terminal.Verbosity.VERBOSE);
|
terminal = new MockTerminal();
|
||||||
|
terminal.setVerbosity(Terminal.Verbosity.VERBOSE);
|
||||||
assertPrinted(terminal, Terminal.Verbosity.SILENT, "text");
|
assertPrinted(terminal, Terminal.Verbosity.SILENT, "text");
|
||||||
assertPrinted(terminal, Terminal.Verbosity.NORMAL, "text");
|
assertPrinted(terminal, Terminal.Verbosity.NORMAL, "text");
|
||||||
assertPrinted(terminal, Terminal.Verbosity.VERBOSE, "text");
|
assertPrinted(terminal, Terminal.Verbosity.VERBOSE, "text");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testEscaping() throws Exception {
|
public void testEscaping() throws Exception {
|
||||||
CaptureOutputTerminal terminal = new CaptureOutputTerminal(Terminal.Verbosity.NORMAL);
|
MockTerminal terminal = new MockTerminal();
|
||||||
assertPrinted(terminal, Terminal.Verbosity.NORMAL, "This message contains percent like %20n");
|
assertPrinted(terminal, Terminal.Verbosity.NORMAL, "This message contains percent like %20n");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertPrinted(CaptureOutputTerminal logTerminal, Terminal.Verbosity verbosity, String text) {
|
private void assertPrinted(MockTerminal logTerminal, Terminal.Verbosity verbosity, String text) throws Exception {
|
||||||
logTerminal.println(verbosity, text);
|
logTerminal.println(verbosity, text);
|
||||||
assertEquals(1, logTerminal.getTerminalOutput().size());
|
assertTrue(logTerminal.getOutput().contains(text));
|
||||||
assertTrue(logTerminal.getTerminalOutput().get(0).contains(text));
|
logTerminal.resetOutput();
|
||||||
logTerminal.terminalOutput.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertNotPrinted(CaptureOutputTerminal logTerminal, Terminal.Verbosity verbosity, String text) {
|
private void assertNotPrinted(MockTerminal logTerminal, Terminal.Verbosity verbosity, String text) throws Exception {
|
||||||
logTerminal.println(verbosity, text);
|
logTerminal.println(verbosity, text);
|
||||||
assertThat(logTerminal.getTerminalOutput(), hasSize(0));
|
String output = logTerminal.getOutput();
|
||||||
|
assertTrue(output, output.isEmpty());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,21 +19,21 @@
|
||||||
|
|
||||||
package org.elasticsearch.common.logging;
|
package org.elasticsearch.common.logging;
|
||||||
|
|
||||||
import org.apache.log4j.Appender;
|
|
||||||
import org.apache.log4j.Logger;
|
|
||||||
import org.elasticsearch.common.cli.CliToolTestCase;
|
|
||||||
import org.elasticsearch.common.settings.Settings;
|
|
||||||
import org.elasticsearch.env.Environment;
|
|
||||||
import org.elasticsearch.node.internal.InternalSettingsPreparer;
|
|
||||||
import org.elasticsearch.test.ESTestCase;
|
|
||||||
import org.junit.Before;
|
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.StandardOpenOption;
|
import java.nio.file.StandardOpenOption;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import org.apache.log4j.Appender;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
import org.elasticsearch.common.cli.MockTerminal;
|
||||||
|
import org.elasticsearch.common.settings.Settings;
|
||||||
|
import org.elasticsearch.env.Environment;
|
||||||
|
import org.elasticsearch.node.internal.InternalSettingsPreparer;
|
||||||
|
import org.elasticsearch.test.ESTestCase;
|
||||||
|
import org.junit.Before;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.is;
|
import static org.hamcrest.Matchers.is;
|
||||||
import static org.hamcrest.Matchers.notNullValue;
|
import static org.hamcrest.Matchers.notNullValue;
|
||||||
import static org.hamcrest.Matchers.nullValue;
|
import static org.hamcrest.Matchers.nullValue;
|
||||||
|
@ -162,7 +162,7 @@ public class LoggingConfigurationTests extends ESTestCase {
|
||||||
.put("appender.console.type", "console")
|
.put("appender.console.type", "console")
|
||||||
.put("appender.console.layout.type", "consolePattern")
|
.put("appender.console.layout.type", "consolePattern")
|
||||||
.put("appender.console.layout.conversionPattern", "[%d{ISO8601}][%-5p][%-25c] %m%n")
|
.put("appender.console.layout.conversionPattern", "[%d{ISO8601}][%-5p][%-25c] %m%n")
|
||||||
.build(), new CliToolTestCase.MockTerminal());
|
.build(), new MockTerminal());
|
||||||
LogConfigurator.configure(environment.settings(), true);
|
LogConfigurator.configure(environment.settings(), true);
|
||||||
// args should overwrite whatever is in the config
|
// args should overwrite whatever is in the config
|
||||||
ESLogger esLogger = ESLoggerFactory.getLogger("test_resolve_order");
|
ESLogger esLogger = ESLoggerFactory.getLogger("test_resolve_order");
|
||||||
|
@ -187,7 +187,7 @@ public class LoggingConfigurationTests extends ESTestCase {
|
||||||
Settings.builder()
|
Settings.builder()
|
||||||
.put(Environment.PATH_CONF_SETTING.getKey(), tmpDir.toAbsolutePath())
|
.put(Environment.PATH_CONF_SETTING.getKey(), tmpDir.toAbsolutePath())
|
||||||
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
|
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
|
||||||
.build(), new CliToolTestCase.MockTerminal());
|
.build(), new MockTerminal());
|
||||||
LogConfigurator.configure(environment.settings(), false);
|
LogConfigurator.configure(environment.settings(), false);
|
||||||
ESLogger esLogger = ESLoggerFactory.getLogger("test_config_not_read");
|
ESLogger esLogger = ESLoggerFactory.getLogger("test_config_not_read");
|
||||||
|
|
||||||
|
|
|
@ -19,9 +19,13 @@
|
||||||
|
|
||||||
package org.elasticsearch.node.internal;
|
package org.elasticsearch.node.internal;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
import org.elasticsearch.common.cli.MockTerminal;
|
||||||
import org.elasticsearch.cluster.ClusterName;
|
import org.elasticsearch.cluster.ClusterName;
|
||||||
import org.elasticsearch.common.cli.CliToolTestCase;
|
|
||||||
import org.elasticsearch.common.cli.Terminal;
|
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.common.settings.SettingsException;
|
import org.elasticsearch.common.settings.SettingsException;
|
||||||
import org.elasticsearch.env.Environment;
|
import org.elasticsearch.env.Environment;
|
||||||
|
@ -29,17 +33,9 @@ import org.elasticsearch.test.ESTestCase;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static org.elasticsearch.common.settings.Settings.settingsBuilder;
|
import static org.elasticsearch.common.settings.Settings.settingsBuilder;
|
||||||
import static org.hamcrest.Matchers.containsString;
|
import static org.hamcrest.Matchers.containsString;
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.is;
|
|
||||||
|
|
||||||
public class InternalSettingsPreparerTests extends ESTestCase {
|
public class InternalSettingsPreparerTests extends ESTestCase {
|
||||||
|
|
||||||
|
@ -81,17 +77,9 @@ public class InternalSettingsPreparerTests extends ESTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testReplacePromptPlaceholders() {
|
public void testReplacePromptPlaceholders() {
|
||||||
final Terminal terminal = new CliToolTestCase.MockTerminal() {
|
MockTerminal terminal = new MockTerminal();
|
||||||
@Override
|
terminal.addTextInput("text");
|
||||||
public char[] readSecret(String message) {
|
terminal.addSecretInput("replaced");
|
||||||
return "replaced".toCharArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String readText(String message) {
|
|
||||||
return "text";
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Settings.Builder builder = settingsBuilder()
|
Settings.Builder builder = settingsBuilder()
|
||||||
.put(baseEnvSettings)
|
.put(baseEnvSettings)
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
package org.elasticsearch.plugins;
|
package org.elasticsearch.plugins;
|
||||||
|
|
||||||
import org.elasticsearch.common.cli.CliToolTestCase;
|
import org.elasticsearch.common.cli.CliToolTestCase;
|
||||||
|
import org.elasticsearch.common.cli.MockTerminal;
|
||||||
|
|
||||||
import static org.elasticsearch.common.cli.CliTool.ExitStatus.OK_AND_EXIT;
|
import static org.elasticsearch.common.cli.CliTool.ExitStatus.OK_AND_EXIT;
|
||||||
import static org.hamcrest.Matchers.containsString;
|
import static org.hamcrest.Matchers.containsString;
|
||||||
|
@ -28,22 +29,22 @@ import static org.hamcrest.Matchers.is;
|
||||||
|
|
||||||
public class PluginCliTests extends CliToolTestCase {
|
public class PluginCliTests extends CliToolTestCase {
|
||||||
public void testHelpWorks() throws Exception {
|
public void testHelpWorks() throws Exception {
|
||||||
CliToolTestCase.CaptureOutputTerminal terminal = new CliToolTestCase.CaptureOutputTerminal();
|
MockTerminal terminal = new MockTerminal();
|
||||||
assertThat(new PluginCli(terminal).execute(args("--help")), is(OK_AND_EXIT));
|
assertThat(new PluginCli(terminal).execute(args("--help")), is(OK_AND_EXIT));
|
||||||
assertTerminalOutputContainsHelpFile(terminal, "/org/elasticsearch/plugins/plugin.help");
|
assertTerminalOutputContainsHelpFile(terminal, "/org/elasticsearch/plugins/plugin.help");
|
||||||
|
|
||||||
terminal.getTerminalOutput().clear();
|
terminal.resetOutput();
|
||||||
assertThat(new PluginCli(terminal).execute(args("install -h")), is(OK_AND_EXIT));
|
assertThat(new PluginCli(terminal).execute(args("install -h")), is(OK_AND_EXIT));
|
||||||
assertTerminalOutputContainsHelpFile(terminal, "/org/elasticsearch/plugins/plugin-install.help");
|
assertTerminalOutputContainsHelpFile(terminal, "/org/elasticsearch/plugins/plugin-install.help");
|
||||||
for (String plugin : InstallPluginCommand.OFFICIAL_PLUGINS) {
|
for (String plugin : InstallPluginCommand.OFFICIAL_PLUGINS) {
|
||||||
assertThat(terminal.getTerminalOutput(), hasItem(containsString(plugin)));
|
assertThat(terminal.getOutput(), containsString(plugin));
|
||||||
}
|
}
|
||||||
|
|
||||||
terminal.getTerminalOutput().clear();
|
terminal.resetOutput();
|
||||||
assertThat(new PluginCli(terminal).execute(args("remove --help")), is(OK_AND_EXIT));
|
assertThat(new PluginCli(terminal).execute(args("remove --help")), is(OK_AND_EXIT));
|
||||||
assertTerminalOutputContainsHelpFile(terminal, "/org/elasticsearch/plugins/plugin-remove.help");
|
assertTerminalOutputContainsHelpFile(terminal, "/org/elasticsearch/plugins/plugin-remove.help");
|
||||||
|
|
||||||
terminal.getTerminalOutput().clear();
|
terminal.resetOutput();
|
||||||
assertThat(new PluginCli(terminal).execute(args("list -h")), is(OK_AND_EXIT));
|
assertThat(new PluginCli(terminal).execute(args("list -h")), is(OK_AND_EXIT));
|
||||||
assertTerminalOutputContainsHelpFile(terminal, "/org/elasticsearch/plugins/plugin-list.help");
|
assertTerminalOutputContainsHelpFile(terminal, "/org/elasticsearch/plugins/plugin-list.help");
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,10 +24,10 @@ import org.elasticsearch.Version;
|
||||||
import org.elasticsearch.common.SuppressForbidden;
|
import org.elasticsearch.common.SuppressForbidden;
|
||||||
import org.elasticsearch.common.cli.CliTool.ExitStatus;
|
import org.elasticsearch.common.cli.CliTool.ExitStatus;
|
||||||
import org.elasticsearch.common.cli.CliToolTestCase;
|
import org.elasticsearch.common.cli.CliToolTestCase;
|
||||||
|
import org.elasticsearch.common.cli.MockTerminal;
|
||||||
import org.elasticsearch.common.cli.UserError;
|
import org.elasticsearch.common.cli.UserError;
|
||||||
import org.elasticsearch.common.collect.Tuple;
|
import org.elasticsearch.common.collect.Tuple;
|
||||||
import org.elasticsearch.monitor.jvm.JvmInfo;
|
import org.elasticsearch.monitor.jvm.JvmInfo;
|
||||||
import org.hamcrest.Matcher;
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
|
||||||
|
@ -37,7 +37,6 @@ import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
import static org.elasticsearch.common.cli.CliTool.ExitStatus.OK;
|
import static org.elasticsearch.common.cli.CliTool.ExitStatus.OK;
|
||||||
import static org.elasticsearch.common.cli.CliTool.ExitStatus.OK_AND_EXIT;
|
import static org.elasticsearch.common.cli.CliTool.ExitStatus.OK_AND_EXIT;
|
||||||
|
@ -50,7 +49,7 @@ import static org.hamcrest.Matchers.nullValue;
|
||||||
@SuppressForbidden(reason = "modifies system properties intentionally")
|
@SuppressForbidden(reason = "modifies system properties intentionally")
|
||||||
public class BootstrapCliParserTests extends CliToolTestCase {
|
public class BootstrapCliParserTests extends CliToolTestCase {
|
||||||
|
|
||||||
private CaptureOutputTerminal terminal = new CaptureOutputTerminal();
|
private MockTerminal terminal = new MockTerminal();
|
||||||
private List<String> propertiesToClear = new ArrayList<>();
|
private List<String> propertiesToClear = new ArrayList<>();
|
||||||
private Map<Object, Object> properties;
|
private Map<Object, Object> properties;
|
||||||
|
|
||||||
|
@ -73,10 +72,11 @@ public class BootstrapCliParserTests extends CliToolTestCase {
|
||||||
ExitStatus status = parser.execute(args("version"));
|
ExitStatus status = parser.execute(args("version"));
|
||||||
assertStatus(status, OK_AND_EXIT);
|
assertStatus(status, OK_AND_EXIT);
|
||||||
|
|
||||||
assertThatTerminalOutput(containsString(Version.CURRENT.toString()));
|
String output = terminal.getOutput();
|
||||||
assertThatTerminalOutput(containsString(Build.CURRENT.shortHash()));
|
assertTrue(output, output.contains(Version.CURRENT.toString()));
|
||||||
assertThatTerminalOutput(containsString(Build.CURRENT.date()));
|
assertTrue(output, output.contains(Build.CURRENT.shortHash()));
|
||||||
assertThatTerminalOutput(containsString(JvmInfo.jvmInfo().version()));
|
assertTrue(output, output.contains(Build.CURRENT.date()));
|
||||||
|
assertTrue(output, output.contains(JvmInfo.jvmInfo().version()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testThatVersionIsReturnedAsStartParameter() throws Exception {
|
public void testThatVersionIsReturnedAsStartParameter() throws Exception {
|
||||||
|
@ -84,20 +84,22 @@ public class BootstrapCliParserTests extends CliToolTestCase {
|
||||||
ExitStatus status = parser.execute(args("start -V"));
|
ExitStatus status = parser.execute(args("start -V"));
|
||||||
assertStatus(status, OK_AND_EXIT);
|
assertStatus(status, OK_AND_EXIT);
|
||||||
|
|
||||||
assertThatTerminalOutput(containsString(Version.CURRENT.toString()));
|
String output = terminal.getOutput();
|
||||||
assertThatTerminalOutput(containsString(Build.CURRENT.shortHash()));
|
assertTrue(output, output.contains(Version.CURRENT.toString()));
|
||||||
assertThatTerminalOutput(containsString(Build.CURRENT.date()));
|
assertTrue(output, output.contains(Build.CURRENT.shortHash()));
|
||||||
assertThatTerminalOutput(containsString(JvmInfo.jvmInfo().version()));
|
assertTrue(output, output.contains(Build.CURRENT.date()));
|
||||||
|
assertTrue(output, output.contains(JvmInfo.jvmInfo().version()));
|
||||||
|
|
||||||
CaptureOutputTerminal terminal = new CaptureOutputTerminal();
|
terminal.resetOutput();
|
||||||
parser = new BootstrapCLIParser(terminal);
|
parser = new BootstrapCLIParser(terminal);
|
||||||
status = parser.execute(args("start --version"));
|
status = parser.execute(args("start --version"));
|
||||||
assertStatus(status, OK_AND_EXIT);
|
assertStatus(status, OK_AND_EXIT);
|
||||||
|
|
||||||
assertThatTerminalOutput(containsString(Version.CURRENT.toString()));
|
output = terminal.getOutput();
|
||||||
assertThatTerminalOutput(containsString(Build.CURRENT.shortHash()));
|
assertTrue(output, output.contains(Version.CURRENT.toString()));
|
||||||
assertThatTerminalOutput(containsString(Build.CURRENT.date()));
|
assertTrue(output, output.contains(Build.CURRENT.shortHash()));
|
||||||
assertThatTerminalOutput(containsString(JvmInfo.jvmInfo().version()));
|
assertTrue(output, output.contains(Build.CURRENT.date()));
|
||||||
|
assertTrue(output, output.contains(JvmInfo.jvmInfo().version()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testThatPidFileCanBeConfigured() throws Exception {
|
public void testThatPidFileCanBeConfigured() throws Exception {
|
||||||
|
@ -173,11 +175,14 @@ public class BootstrapCliParserTests extends CliToolTestCase {
|
||||||
|
|
||||||
ExitStatus status = parser.execute(args("start --network.host"));
|
ExitStatus status = parser.execute(args("start --network.host"));
|
||||||
assertStatus(status, USAGE);
|
assertStatus(status, USAGE);
|
||||||
assertThatTerminalOutput(containsString("Parameter [network.host] needs value"));
|
String output = terminal.getOutput();
|
||||||
|
assertTrue(output, output.contains("Parameter [network.host] needs value"));
|
||||||
|
|
||||||
|
terminal.resetOutput();
|
||||||
status = parser.execute(args("start --network.host --foo"));
|
status = parser.execute(args("start --network.host --foo"));
|
||||||
assertStatus(status, USAGE);
|
assertStatus(status, USAGE);
|
||||||
assertThatTerminalOutput(containsString("Parameter [network.host] needs value"));
|
output = terminal.getOutput();
|
||||||
|
assertTrue(output, output.contains("Parameter [network.host] needs value"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testParsingErrors() throws Exception {
|
public void testParsingErrors() throws Exception {
|
||||||
|
@ -186,28 +191,32 @@ public class BootstrapCliParserTests extends CliToolTestCase {
|
||||||
// unknown params
|
// unknown params
|
||||||
ExitStatus status = parser.execute(args("version --unknown-param /tmp/pid"));
|
ExitStatus status = parser.execute(args("version --unknown-param /tmp/pid"));
|
||||||
assertStatus(status, USAGE);
|
assertStatus(status, USAGE);
|
||||||
assertThatTerminalOutput(containsString("Unrecognized option: --unknown-param"));
|
String output = terminal.getOutput();
|
||||||
|
assertTrue(output, output.contains("Unrecognized option: --unknown-param"));
|
||||||
|
|
||||||
// single dash in extra params
|
// single dash in extra params
|
||||||
terminal = new CaptureOutputTerminal();
|
terminal.resetOutput();
|
||||||
parser = new BootstrapCLIParser(terminal);
|
parser = new BootstrapCLIParser(terminal);
|
||||||
status = parser.execute(args("start -network.host 127.0.0.1"));
|
status = parser.execute(args("start -network.host 127.0.0.1"));
|
||||||
assertStatus(status, USAGE);
|
assertStatus(status, USAGE);
|
||||||
assertThatTerminalOutput(containsString("Parameter [-network.host]does not start with --"));
|
output = terminal.getOutput();
|
||||||
|
assertTrue(output, output.contains("Parameter [-network.host]does not start with --"));
|
||||||
|
|
||||||
// never ended parameter
|
// never ended parameter
|
||||||
terminal = new CaptureOutputTerminal();
|
terminal = new MockTerminal();
|
||||||
parser = new BootstrapCLIParser(terminal);
|
parser = new BootstrapCLIParser(terminal);
|
||||||
status = parser.execute(args("start --network.host"));
|
status = parser.execute(args("start --network.host"));
|
||||||
assertStatus(status, USAGE);
|
assertStatus(status, USAGE);
|
||||||
assertThatTerminalOutput(containsString("Parameter [network.host] needs value"));
|
output = terminal.getOutput();
|
||||||
|
assertTrue(output, output.contains("Parameter [network.host] needs value"));
|
||||||
|
|
||||||
// free floating value
|
// free floating value
|
||||||
terminal = new CaptureOutputTerminal();
|
terminal = new MockTerminal();
|
||||||
parser = new BootstrapCLIParser(terminal);
|
parser = new BootstrapCLIParser(terminal);
|
||||||
status = parser.execute(args("start 127.0.0.1"));
|
status = parser.execute(args("start 127.0.0.1"));
|
||||||
assertStatus(status, USAGE);
|
assertStatus(status, USAGE);
|
||||||
assertThatTerminalOutput(containsString("Parameter [127.0.0.1]does not start with --"));
|
output = terminal.getOutput();
|
||||||
|
assertTrue(output, output.contains("Parameter [127.0.0.1]does not start with --"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testHelpWorks() throws Exception {
|
public void testHelpWorks() throws Exception {
|
||||||
|
@ -220,10 +229,11 @@ public class BootstrapCliParserTests extends CliToolTestCase {
|
||||||
tuples.add(new Tuple<>("-h", "elasticsearch.help"));
|
tuples.add(new Tuple<>("-h", "elasticsearch.help"));
|
||||||
|
|
||||||
for (Tuple<String, String> tuple : tuples) {
|
for (Tuple<String, String> tuple : tuples) {
|
||||||
terminal = new CaptureOutputTerminal();
|
terminal.resetOutput();
|
||||||
BootstrapCLIParser parser = new BootstrapCLIParser(terminal);
|
BootstrapCLIParser parser = new BootstrapCLIParser(terminal);
|
||||||
ExitStatus status = parser.execute(args(tuple.v1()));
|
ExitStatus status = parser.execute(args(tuple.v1()));
|
||||||
assertStatus(status, OK_AND_EXIT);
|
assertStatus(status, OK_AND_EXIT);
|
||||||
|
// nocommit
|
||||||
assertTerminalOutputContainsHelpFile(terminal, "/org/elasticsearch/bootstrap/" + tuple.v2());
|
assertTerminalOutputContainsHelpFile(terminal, "/org/elasticsearch/bootstrap/" + tuple.v2());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -253,16 +263,12 @@ public class BootstrapCliParserTests extends CliToolTestCase {
|
||||||
propertiesToClear.addAll(Arrays.asList(systemProperties));
|
propertiesToClear.addAll(Arrays.asList(systemProperties));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertSystemProperty(String name, String expectedValue) {
|
private void assertSystemProperty(String name, String expectedValue) throws Exception {
|
||||||
String msg = String.format(Locale.ROOT, "Expected property %s to be %s, terminal output was %s", name, expectedValue, terminal.getTerminalOutput());
|
String msg = String.format(Locale.ROOT, "Expected property %s to be %s, terminal output was %s", name, expectedValue, terminal.getOutput());
|
||||||
assertThat(msg, System.getProperty(name), is(expectedValue));
|
assertThat(msg, System.getProperty(name), is(expectedValue));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertStatus(ExitStatus status, ExitStatus expectedStatus) {
|
private void assertStatus(ExitStatus status, ExitStatus expectedStatus) throws Exception {
|
||||||
assertThat(String.format(Locale.ROOT, "Expected status to be [%s], but was [%s], terminal output was %s", expectedStatus, status, terminal.getTerminalOutput()), status, is(expectedStatus));
|
assertThat(String.format(Locale.ROOT, "Expected status to be [%s], but was [%s], terminal output was %s", expectedStatus, status, terminal.getOutput()), status, is(expectedStatus));
|
||||||
}
|
|
||||||
|
|
||||||
private void assertThatTerminalOutput(Matcher<String> matcher) {
|
|
||||||
assertThat(terminal.getTerminalOutput(), hasItem(matcher));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,7 +49,7 @@ import static org.hamcrest.Matchers.is;
|
||||||
*/
|
*/
|
||||||
public class CheckFileCommandTests extends ESTestCase {
|
public class CheckFileCommandTests extends ESTestCase {
|
||||||
|
|
||||||
private CliToolTestCase.CaptureOutputTerminal captureOutputTerminal = new CliToolTestCase.CaptureOutputTerminal();
|
private MockTerminal captureOutputTerminal = new MockTerminal();
|
||||||
|
|
||||||
private Configuration jimFsConfiguration = Configuration.unix().toBuilder().setAttributeViews("basic", "owner", "posix", "unix").build();
|
private Configuration jimFsConfiguration = Configuration.unix().toBuilder().setAttributeViews("basic", "owner", "posix", "unix").build();
|
||||||
private Configuration jimFsConfigurationWithoutPermissions = randomBoolean() ? Configuration.unix().toBuilder().setAttributeViews("basic").build() : Configuration.windows();
|
private Configuration jimFsConfigurationWithoutPermissions = randomBoolean() ? Configuration.unix().toBuilder().setAttributeViews("basic").build() : Configuration.windows();
|
||||||
|
@ -60,62 +60,62 @@ public class CheckFileCommandTests extends ESTestCase {
|
||||||
|
|
||||||
public void testThatCommandLogsErrorMessageOnFail() throws Exception {
|
public void testThatCommandLogsErrorMessageOnFail() throws Exception {
|
||||||
executeCommand(jimFsConfiguration, new PermissionCheckFileCommand(createTempDir(), captureOutputTerminal, Mode.CHANGE));
|
executeCommand(jimFsConfiguration, new PermissionCheckFileCommand(createTempDir(), captureOutputTerminal, Mode.CHANGE));
|
||||||
assertThat(captureOutputTerminal.getTerminalOutput(), hasItem(containsString("Please ensure that the user account running Elasticsearch has read access to this file")));
|
assertThat(captureOutputTerminal.getOutput(), containsString("Please ensure that the user account running Elasticsearch has read access to this file"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testThatCommandLogsNothingWhenPermissionRemains() throws Exception {
|
public void testThatCommandLogsNothingWhenPermissionRemains() throws Exception {
|
||||||
executeCommand(jimFsConfiguration, new PermissionCheckFileCommand(createTempDir(), captureOutputTerminal, Mode.KEEP));
|
executeCommand(jimFsConfiguration, new PermissionCheckFileCommand(createTempDir(), captureOutputTerminal, Mode.KEEP));
|
||||||
assertThat(captureOutputTerminal.getTerminalOutput(), hasSize(0));
|
assertTrue(captureOutputTerminal.getOutput().isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testThatCommandLogsNothingWhenDisabled() throws Exception {
|
public void testThatCommandLogsNothingWhenDisabled() throws Exception {
|
||||||
executeCommand(jimFsConfiguration, new PermissionCheckFileCommand(createTempDir(), captureOutputTerminal, Mode.DISABLED));
|
executeCommand(jimFsConfiguration, new PermissionCheckFileCommand(createTempDir(), captureOutputTerminal, Mode.DISABLED));
|
||||||
assertThat(captureOutputTerminal.getTerminalOutput(), hasSize(0));
|
assertTrue(captureOutputTerminal.getOutput().isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testThatCommandLogsNothingIfFilesystemDoesNotSupportPermissions() throws Exception {
|
public void testThatCommandLogsNothingIfFilesystemDoesNotSupportPermissions() throws Exception {
|
||||||
executeCommand(jimFsConfigurationWithoutPermissions, new PermissionCheckFileCommand(createTempDir(), captureOutputTerminal, Mode.DISABLED));
|
executeCommand(jimFsConfigurationWithoutPermissions, new PermissionCheckFileCommand(createTempDir(), captureOutputTerminal, Mode.DISABLED));
|
||||||
assertThat(captureOutputTerminal.getTerminalOutput(), hasSize(0));
|
assertTrue(captureOutputTerminal.getOutput().isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testThatCommandLogsOwnerChange() throws Exception {
|
public void testThatCommandLogsOwnerChange() throws Exception {
|
||||||
executeCommand(jimFsConfiguration, new OwnerCheckFileCommand(createTempDir(), captureOutputTerminal, Mode.CHANGE));
|
executeCommand(jimFsConfiguration, new OwnerCheckFileCommand(createTempDir(), captureOutputTerminal, Mode.CHANGE));
|
||||||
assertThat(captureOutputTerminal.getTerminalOutput(), hasItem(allOf(containsString("Owner of file ["), containsString("] used to be ["), containsString("], but now is ["))));
|
assertThat(captureOutputTerminal.getOutput(), allOf(containsString("Owner of file ["), containsString("] used to be ["), containsString("], but now is [")));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testThatCommandLogsNothingIfOwnerRemainsSame() throws Exception {
|
public void testThatCommandLogsNothingIfOwnerRemainsSame() throws Exception {
|
||||||
executeCommand(jimFsConfiguration, new OwnerCheckFileCommand(createTempDir(), captureOutputTerminal, Mode.KEEP));
|
executeCommand(jimFsConfiguration, new OwnerCheckFileCommand(createTempDir(), captureOutputTerminal, Mode.KEEP));
|
||||||
assertThat(captureOutputTerminal.getTerminalOutput(), hasSize(0));
|
assertTrue(captureOutputTerminal.getOutput().isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testThatCommandLogsNothingIfOwnerIsDisabled() throws Exception {
|
public void testThatCommandLogsNothingIfOwnerIsDisabled() throws Exception {
|
||||||
executeCommand(jimFsConfiguration, new OwnerCheckFileCommand(createTempDir(), captureOutputTerminal, Mode.DISABLED));
|
executeCommand(jimFsConfiguration, new OwnerCheckFileCommand(createTempDir(), captureOutputTerminal, Mode.DISABLED));
|
||||||
assertThat(captureOutputTerminal.getTerminalOutput(), hasSize(0));
|
assertTrue(captureOutputTerminal.getOutput().isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testThatCommandLogsNothingIfFileSystemDoesNotSupportOwners() throws Exception {
|
public void testThatCommandLogsNothingIfFileSystemDoesNotSupportOwners() throws Exception {
|
||||||
executeCommand(jimFsConfigurationWithoutPermissions, new OwnerCheckFileCommand(createTempDir(), captureOutputTerminal, Mode.DISABLED));
|
executeCommand(jimFsConfigurationWithoutPermissions, new OwnerCheckFileCommand(createTempDir(), captureOutputTerminal, Mode.DISABLED));
|
||||||
assertThat(captureOutputTerminal.getTerminalOutput(), hasSize(0));
|
assertTrue(captureOutputTerminal.getOutput().isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testThatCommandLogsIfGroupChanges() throws Exception {
|
public void testThatCommandLogsIfGroupChanges() throws Exception {
|
||||||
executeCommand(jimFsConfiguration, new GroupCheckFileCommand(createTempDir(), captureOutputTerminal, Mode.CHANGE));
|
executeCommand(jimFsConfiguration, new GroupCheckFileCommand(createTempDir(), captureOutputTerminal, Mode.CHANGE));
|
||||||
assertThat(captureOutputTerminal.getTerminalOutput(), hasItem(allOf(containsString("Group of file ["), containsString("] used to be ["), containsString("], but now is ["))));
|
assertThat(captureOutputTerminal.getOutput(), allOf(containsString("Group of file ["), containsString("] used to be ["), containsString("], but now is [")));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testThatCommandLogsNothingIfGroupRemainsSame() throws Exception {
|
public void testThatCommandLogsNothingIfGroupRemainsSame() throws Exception {
|
||||||
executeCommand(jimFsConfiguration, new GroupCheckFileCommand(createTempDir(), captureOutputTerminal, Mode.KEEP));
|
executeCommand(jimFsConfiguration, new GroupCheckFileCommand(createTempDir(), captureOutputTerminal, Mode.KEEP));
|
||||||
assertThat(captureOutputTerminal.getTerminalOutput(), hasSize(0));
|
assertTrue(captureOutputTerminal.getOutput().isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testThatCommandLogsNothingIfGroupIsDisabled() throws Exception {
|
public void testThatCommandLogsNothingIfGroupIsDisabled() throws Exception {
|
||||||
executeCommand(jimFsConfiguration, new GroupCheckFileCommand(createTempDir(), captureOutputTerminal, Mode.DISABLED));
|
executeCommand(jimFsConfiguration, new GroupCheckFileCommand(createTempDir(), captureOutputTerminal, Mode.DISABLED));
|
||||||
assertThat(captureOutputTerminal.getTerminalOutput(), hasSize(0));
|
assertTrue(captureOutputTerminal.getOutput().isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testThatCommandLogsNothingIfFileSystemDoesNotSupportGroups() throws Exception {
|
public void testThatCommandLogsNothingIfFileSystemDoesNotSupportGroups() throws Exception {
|
||||||
executeCommand(jimFsConfigurationWithoutPermissions, new GroupCheckFileCommand(createTempDir(), captureOutputTerminal, Mode.DISABLED));
|
executeCommand(jimFsConfigurationWithoutPermissions, new GroupCheckFileCommand(createTempDir(), captureOutputTerminal, Mode.DISABLED));
|
||||||
assertThat(captureOutputTerminal.getTerminalOutput(), hasSize(0));
|
assertTrue(captureOutputTerminal.getOutput().isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testThatCommandDoesNotLogAnythingOnFileCreation() throws Exception {
|
public void testThatCommandDoesNotLogAnythingOnFileCreation() throws Exception {
|
||||||
|
@ -130,7 +130,7 @@ public class CheckFileCommandTests extends ESTestCase {
|
||||||
assertThat(Files.exists(path), is(true));
|
assertThat(Files.exists(path), is(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
assertThat(captureOutputTerminal.getTerminalOutput(), hasSize(0));
|
assertTrue(captureOutputTerminal.getOutput().isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testThatCommandWorksIfFileIsDeletedByCommand() throws Exception {
|
public void testThatCommandWorksIfFileIsDeletedByCommand() throws Exception {
|
||||||
|
@ -147,7 +147,7 @@ public class CheckFileCommandTests extends ESTestCase {
|
||||||
assertThat(Files.exists(path), is(false));
|
assertThat(Files.exists(path), is(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
assertThat(captureOutputTerminal.getTerminalOutput(), hasSize(0));
|
assertTrue(captureOutputTerminal.getOutput().isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void executeCommand(Configuration configuration, AbstractTestCheckFileCommand command) throws Exception {
|
private void executeCommand(Configuration configuration, AbstractTestCheckFileCommand command) throws Exception {
|
||||||
|
|
|
@ -20,7 +20,6 @@
|
||||||
package org.elasticsearch.common.cli;
|
package org.elasticsearch.common.cli;
|
||||||
|
|
||||||
import org.apache.commons.cli.CommandLine;
|
import org.apache.commons.cli.CommandLine;
|
||||||
import org.elasticsearch.ElasticsearchException;
|
|
||||||
import org.elasticsearch.common.Strings;
|
import org.elasticsearch.common.Strings;
|
||||||
import org.elasticsearch.common.SuppressForbidden;
|
import org.elasticsearch.common.SuppressForbidden;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
|
@ -133,7 +132,7 @@ public class CliToolTests extends CliToolTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testSingleCommandToolHelp() throws Exception {
|
public void testSingleCommandToolHelp() throws Exception {
|
||||||
CaptureOutputTerminal terminal = new CaptureOutputTerminal();
|
MockTerminal terminal = new MockTerminal();
|
||||||
final AtomicReference<Boolean> executed = new AtomicReference<>(false);
|
final AtomicReference<Boolean> executed = new AtomicReference<>(false);
|
||||||
final NamedCommand cmd = new NamedCommand("cmd1", terminal) {
|
final NamedCommand cmd = new NamedCommand("cmd1", terminal) {
|
||||||
@Override
|
@Override
|
||||||
|
@ -145,12 +144,11 @@ public class CliToolTests extends CliToolTestCase {
|
||||||
SingleCmdTool tool = new SingleCmdTool("tool", terminal, cmd);
|
SingleCmdTool tool = new SingleCmdTool("tool", terminal, cmd);
|
||||||
CliTool.ExitStatus status = tool.execute(args("-h"));
|
CliTool.ExitStatus status = tool.execute(args("-h"));
|
||||||
assertStatus(status, CliTool.ExitStatus.OK_AND_EXIT);
|
assertStatus(status, CliTool.ExitStatus.OK_AND_EXIT);
|
||||||
assertThat(terminal.getTerminalOutput(), hasSize(3));
|
assertThat(terminal.getOutput(), containsString("cmd1 help"));
|
||||||
assertThat(terminal.getTerminalOutput(), hasItem(containsString("cmd1 help")));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testMultiCommandToolHelp() throws Exception {
|
public void testMultiCommandToolHelp() throws Exception {
|
||||||
CaptureOutputTerminal terminal = new CaptureOutputTerminal();
|
MockTerminal terminal = new MockTerminal();
|
||||||
NamedCommand[] cmds = new NamedCommand[2];
|
NamedCommand[] cmds = new NamedCommand[2];
|
||||||
cmds[0] = new NamedCommand("cmd0", terminal) {
|
cmds[0] = new NamedCommand("cmd0", terminal) {
|
||||||
@Override
|
@Override
|
||||||
|
@ -167,12 +165,11 @@ public class CliToolTests extends CliToolTestCase {
|
||||||
MultiCmdTool tool = new MultiCmdTool("tool", terminal, cmds);
|
MultiCmdTool tool = new MultiCmdTool("tool", terminal, cmds);
|
||||||
CliTool.ExitStatus status = tool.execute(args("-h"));
|
CliTool.ExitStatus status = tool.execute(args("-h"));
|
||||||
assertStatus(status, CliTool.ExitStatus.OK_AND_EXIT);
|
assertStatus(status, CliTool.ExitStatus.OK_AND_EXIT);
|
||||||
assertThat(terminal.getTerminalOutput(), hasSize(3));
|
assertThat(terminal.getOutput(), containsString("tool help"));
|
||||||
assertThat(terminal.getTerminalOutput(), hasItem(containsString("tool help")));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testMultiCommandCmdHelp() throws Exception {
|
public void testMultiCommandCmdHelp() throws Exception {
|
||||||
CaptureOutputTerminal terminal = new CaptureOutputTerminal();
|
MockTerminal terminal = new MockTerminal();
|
||||||
NamedCommand[] cmds = new NamedCommand[2];
|
NamedCommand[] cmds = new NamedCommand[2];
|
||||||
cmds[0] = new NamedCommand("cmd0", terminal) {
|
cmds[0] = new NamedCommand("cmd0", terminal) {
|
||||||
@Override
|
@Override
|
||||||
|
@ -189,12 +186,11 @@ public class CliToolTests extends CliToolTestCase {
|
||||||
MultiCmdTool tool = new MultiCmdTool("tool", terminal, cmds);
|
MultiCmdTool tool = new MultiCmdTool("tool", terminal, cmds);
|
||||||
CliTool.ExitStatus status = tool.execute(args("cmd1 -h"));
|
CliTool.ExitStatus status = tool.execute(args("cmd1 -h"));
|
||||||
assertStatus(status, CliTool.ExitStatus.OK_AND_EXIT);
|
assertStatus(status, CliTool.ExitStatus.OK_AND_EXIT);
|
||||||
assertThat(terminal.getTerminalOutput(), hasSize(3));
|
assertThat(terminal.getOutput(), containsString("cmd1 help"));
|
||||||
assertThat(terminal.getTerminalOutput(), hasItem(containsString("cmd1 help")));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNonUserErrorPropagates() throws Exception {
|
public void testNonUserErrorPropagates() throws Exception {
|
||||||
CaptureOutputTerminal terminal = new CaptureOutputTerminal();
|
MockTerminal terminal = new MockTerminal();
|
||||||
NamedCommand cmd = new NamedCommand("cmd", terminal) {
|
NamedCommand cmd = new NamedCommand("cmd", terminal) {
|
||||||
@Override
|
@Override
|
||||||
public CliTool.ExitStatus execute(Settings settings, Environment env) throws Exception {
|
public CliTool.ExitStatus execute(Settings settings, Environment env) throws Exception {
|
||||||
|
@ -225,22 +221,11 @@ public class CliToolTests extends CliToolTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testPromptForSetting() throws Exception {
|
public void testPromptForSetting() throws Exception {
|
||||||
final AtomicInteger counter = new AtomicInteger();
|
|
||||||
final AtomicReference<String> promptedSecretValue = new AtomicReference<>(null);
|
final AtomicReference<String> promptedSecretValue = new AtomicReference<>(null);
|
||||||
final AtomicReference<String> promptedTextValue = new AtomicReference<>(null);
|
final AtomicReference<String> promptedTextValue = new AtomicReference<>(null);
|
||||||
final Terminal terminal = new MockTerminal() {
|
final MockTerminal terminal = new MockTerminal();
|
||||||
@Override
|
terminal.addTextInput("replaced");
|
||||||
public char[] readSecret(String text) {
|
terminal.addSecretInput("changeit");
|
||||||
counter.incrementAndGet();
|
|
||||||
return "changeit".toCharArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String readText(String text) {
|
|
||||||
counter.incrementAndGet();
|
|
||||||
return "replaced";
|
|
||||||
}
|
|
||||||
};
|
|
||||||
final NamedCommand cmd = new NamedCommand("noop", terminal) {
|
final NamedCommand cmd = new NamedCommand("noop", terminal) {
|
||||||
@Override
|
@Override
|
||||||
public CliTool.ExitStatus execute(Settings settings, Environment env) {
|
public CliTool.ExitStatus execute(Settings settings, Environment env) {
|
||||||
|
@ -259,7 +244,6 @@ public class CliToolTests extends CliToolTestCase {
|
||||||
System.clearProperty("es.replace");
|
System.clearProperty("es.replace");
|
||||||
}
|
}
|
||||||
|
|
||||||
assertThat(counter.intValue(), is(2));
|
|
||||||
assertThat(promptedSecretValue.get(), is("changeit"));
|
assertThat(promptedSecretValue.get(), is("changeit"));
|
||||||
assertThat(promptedTextValue.get(), is("replaced"));
|
assertThat(promptedTextValue.get(), is("replaced"));
|
||||||
}
|
}
|
||||||
|
@ -269,7 +253,7 @@ public class CliToolTests extends CliToolTestCase {
|
||||||
final CliToolConfig.Cmd strictCommand = cmd("strict", CliTool.Command.Exit.class).stopAtNonOption(false).build();
|
final CliToolConfig.Cmd strictCommand = cmd("strict", CliTool.Command.Exit.class).stopAtNonOption(false).build();
|
||||||
final CliToolConfig config = CliToolConfig.config("elasticsearch", CliTool.class).cmds(lenientCommand, strictCommand).build();
|
final CliToolConfig config = CliToolConfig.config("elasticsearch", CliTool.class).cmds(lenientCommand, strictCommand).build();
|
||||||
|
|
||||||
final CaptureOutputTerminal terminal = new CaptureOutputTerminal();
|
MockTerminal terminal = new MockTerminal();
|
||||||
final CliTool cliTool = new CliTool(config, terminal) {
|
final CliTool cliTool = new CliTool(config, terminal) {
|
||||||
@Override
|
@Override
|
||||||
protected Command parse(String cmdName, CommandLine cli) throws Exception {
|
protected Command parse(String cmdName, CommandLine cli) throws Exception {
|
||||||
|
@ -292,11 +276,11 @@ public class CliToolTests extends CliToolTestCase {
|
||||||
|
|
||||||
// unknown parameters, error
|
// unknown parameters, error
|
||||||
assertStatus(cliTool.execute(args("strict --unknown")), USAGE);
|
assertStatus(cliTool.execute(args("strict --unknown")), USAGE);
|
||||||
assertThat(terminal.getTerminalOutput(), hasItem(containsString("Unrecognized option: --unknown")));
|
assertThat(terminal.getOutput(), containsString("Unrecognized option: --unknown"));
|
||||||
|
|
||||||
terminal.getTerminalOutput().clear();
|
terminal.resetOutput();
|
||||||
assertStatus(cliTool.execute(args("strict -u")), USAGE);
|
assertStatus(cliTool.execute(args("strict -u")), USAGE);
|
||||||
assertThat(terminal.getTerminalOutput(), hasItem(containsString("Unrecognized option: -u")));
|
assertThat(terminal.getOutput(), containsString("Unrecognized option: -u"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertStatus(CliTool.ExitStatus status, CliTool.ExitStatus expectedStatus) {
|
private void assertStatus(CliTool.ExitStatus status, CliTool.ExitStatus expectedStatus) {
|
||||||
|
|
|
@ -46,6 +46,7 @@ import org.apache.lucene.util.LuceneTestCase;
|
||||||
import org.elasticsearch.Version;
|
import org.elasticsearch.Version;
|
||||||
import org.elasticsearch.common.cli.CliTool;
|
import org.elasticsearch.common.cli.CliTool;
|
||||||
import org.elasticsearch.common.cli.CliToolTestCase;
|
import org.elasticsearch.common.cli.CliToolTestCase;
|
||||||
|
import org.elasticsearch.common.cli.MockTerminal;
|
||||||
import org.elasticsearch.common.cli.Terminal;
|
import org.elasticsearch.common.cli.Terminal;
|
||||||
import org.elasticsearch.common.cli.UserError;
|
import org.elasticsearch.common.cli.UserError;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
|
@ -116,8 +117,8 @@ public class InstallPluginCommandTests extends ESTestCase {
|
||||||
return writeZip(structure, "elasticsearch");
|
return writeZip(structure, "elasticsearch");
|
||||||
}
|
}
|
||||||
|
|
||||||
static CliToolTestCase.CaptureOutputTerminal installPlugin(String pluginUrl, Environment env) throws Exception {
|
static MockTerminal installPlugin(String pluginUrl, Environment env) throws Exception {
|
||||||
CliToolTestCase.CaptureOutputTerminal terminal = new CliToolTestCase.CaptureOutputTerminal(Terminal.Verbosity.NORMAL);
|
MockTerminal terminal = new MockTerminal();
|
||||||
CliTool.ExitStatus status = new InstallPluginCommand(terminal, pluginUrl, true).execute(env.settings(), env);
|
CliTool.ExitStatus status = new InstallPluginCommand(terminal, pluginUrl, true).execute(env.settings(), env);
|
||||||
assertEquals(CliTool.ExitStatus.OK, status);
|
assertEquals(CliTool.ExitStatus.OK, status);
|
||||||
return terminal;
|
return terminal;
|
||||||
|
|
|
@ -28,6 +28,7 @@ import java.util.List;
|
||||||
import org.apache.lucene.util.LuceneTestCase;
|
import org.apache.lucene.util.LuceneTestCase;
|
||||||
import org.elasticsearch.common.cli.CliTool;
|
import org.elasticsearch.common.cli.CliTool;
|
||||||
import org.elasticsearch.common.cli.CliToolTestCase;
|
import org.elasticsearch.common.cli.CliToolTestCase;
|
||||||
|
import org.elasticsearch.common.cli.MockTerminal;
|
||||||
import org.elasticsearch.common.cli.Terminal;
|
import org.elasticsearch.common.cli.Terminal;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.env.Environment;
|
import org.elasticsearch.env.Environment;
|
||||||
|
@ -45,8 +46,8 @@ public class ListPluginsCommandTests extends ESTestCase {
|
||||||
return new Environment(settings);
|
return new Environment(settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
static CliToolTestCase.CaptureOutputTerminal listPlugins(Environment env) throws Exception {
|
static MockTerminal listPlugins(Environment env) throws Exception {
|
||||||
CliToolTestCase.CaptureOutputTerminal terminal = new CliToolTestCase.CaptureOutputTerminal(Terminal.Verbosity.NORMAL);
|
MockTerminal terminal = new MockTerminal();
|
||||||
CliTool.ExitStatus status = new ListPluginsCommand(terminal).execute(env.settings(), env);
|
CliTool.ExitStatus status = new ListPluginsCommand(terminal).execute(env.settings(), env);
|
||||||
assertEquals(CliTool.ExitStatus.OK, status);
|
assertEquals(CliTool.ExitStatus.OK, status);
|
||||||
return terminal;
|
return terminal;
|
||||||
|
@ -62,29 +63,24 @@ public class ListPluginsCommandTests extends ESTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNoPlugins() throws Exception {
|
public void testNoPlugins() throws Exception {
|
||||||
CliToolTestCase.CaptureOutputTerminal terminal = listPlugins(createEnv());
|
MockTerminal terminal = listPlugins(createEnv());
|
||||||
List<String> lines = terminal.getTerminalOutput();
|
assertTrue(terminal.getOutput(), terminal.getOutput().isEmpty());
|
||||||
assertEquals(0, lines.size());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testOnePlugin() throws Exception {
|
public void testOnePlugin() throws Exception {
|
||||||
Environment env = createEnv();
|
Environment env = createEnv();
|
||||||
Files.createDirectory(env.pluginsFile().resolve("fake"));
|
Files.createDirectory(env.pluginsFile().resolve("fake"));
|
||||||
CliToolTestCase.CaptureOutputTerminal terminal = listPlugins(env);
|
MockTerminal terminal = listPlugins(env);
|
||||||
List<String> lines = terminal.getTerminalOutput();
|
assertTrue(terminal.getOutput(), terminal.getOutput().contains("fake"));
|
||||||
assertEquals(1, lines.size());
|
|
||||||
assertTrue(lines.get(0).contains("fake"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testTwoPlugins() throws Exception {
|
public void testTwoPlugins() throws Exception {
|
||||||
Environment env = createEnv();
|
Environment env = createEnv();
|
||||||
Files.createDirectory(env.pluginsFile().resolve("fake1"));
|
Files.createDirectory(env.pluginsFile().resolve("fake1"));
|
||||||
Files.createDirectory(env.pluginsFile().resolve("fake2"));
|
Files.createDirectory(env.pluginsFile().resolve("fake2"));
|
||||||
CliToolTestCase.CaptureOutputTerminal terminal = listPlugins(env);
|
MockTerminal terminal = listPlugins(env);
|
||||||
List<String> lines = terminal.getTerminalOutput();
|
String output = terminal.getOutput();
|
||||||
assertEquals(2, lines.size());
|
assertTrue(output, output.contains("fake1"));
|
||||||
Collections.sort(lines);
|
assertTrue(output, output.contains("fake2"));
|
||||||
assertTrue(lines.get(0).contains("fake1"));
|
|
||||||
assertTrue(lines.get(1).contains("fake2"));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@ import java.nio.file.Path;
|
||||||
import org.apache.lucene.util.LuceneTestCase;
|
import org.apache.lucene.util.LuceneTestCase;
|
||||||
import org.elasticsearch.common.cli.CliTool;
|
import org.elasticsearch.common.cli.CliTool;
|
||||||
import org.elasticsearch.common.cli.CliToolTestCase;
|
import org.elasticsearch.common.cli.CliToolTestCase;
|
||||||
|
import org.elasticsearch.common.cli.MockTerminal;
|
||||||
import org.elasticsearch.common.cli.Terminal;
|
import org.elasticsearch.common.cli.Terminal;
|
||||||
import org.elasticsearch.common.cli.UserError;
|
import org.elasticsearch.common.cli.UserError;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
|
@ -48,8 +49,8 @@ public class RemovePluginCommandTests extends ESTestCase {
|
||||||
return new Environment(settings);
|
return new Environment(settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
static CliToolTestCase.CaptureOutputTerminal removePlugin(String name, Environment env) throws Exception {
|
static MockTerminal removePlugin(String name, Environment env) throws Exception {
|
||||||
CliToolTestCase.CaptureOutputTerminal terminal = new CliToolTestCase.CaptureOutputTerminal(Terminal.Verbosity.VERBOSE);
|
MockTerminal terminal = new MockTerminal();
|
||||||
CliTool.ExitStatus status = new RemovePluginCommand(terminal, name).execute(env.settings(), env);
|
CliTool.ExitStatus status = new RemovePluginCommand(terminal, name).execute(env.settings(), env);
|
||||||
assertEquals(CliTool.ExitStatus.OK, status);
|
assertEquals(CliTool.ExitStatus.OK, status);
|
||||||
return terminal;
|
return terminal;
|
||||||
|
|
|
@ -19,7 +19,8 @@
|
||||||
|
|
||||||
package org.elasticsearch.common.cli;
|
package org.elasticsearch.common.cli;
|
||||||
|
|
||||||
import org.elasticsearch.ExceptionsHelper;
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.elasticsearch.common.Strings;
|
import org.elasticsearch.common.Strings;
|
||||||
import org.elasticsearch.common.SuppressForbidden;
|
import org.elasticsearch.common.SuppressForbidden;
|
||||||
import org.elasticsearch.test.ESTestCase;
|
import org.elasticsearch.test.ESTestCase;
|
||||||
|
@ -27,14 +28,6 @@ import org.elasticsearch.test.StreamsUtils;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.containsString;
|
|
||||||
import static org.hamcrest.Matchers.greaterThan;
|
|
||||||
import static org.hamcrest.Matchers.hasSize;
|
|
||||||
|
|
||||||
public abstract class CliToolTestCase extends ESTestCase {
|
public abstract class CliToolTestCase extends ESTestCase {
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
|
@ -56,63 +49,10 @@ public abstract class CliToolTestCase extends ESTestCase {
|
||||||
return command.split("\\s+");
|
return command.split("\\s+");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public static void assertTerminalOutputContainsHelpFile(MockTerminal terminal, String classPath) throws IOException {
|
||||||
* A terminal implementation that discards everything
|
String output = terminal.getOutput();
|
||||||
*/
|
assertFalse(output, output.isEmpty());
|
||||||
public static class MockTerminal extends Terminal {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void doPrint(String msg) {}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String readText(String prompt) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public char[] readSecret(String prompt) {
|
|
||||||
return new char[0];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A terminal implementation that captures everything written to it
|
|
||||||
*/
|
|
||||||
public static class CaptureOutputTerminal extends MockTerminal {
|
|
||||||
|
|
||||||
List<String> terminalOutput = new ArrayList<>();
|
|
||||||
|
|
||||||
public CaptureOutputTerminal() {
|
|
||||||
this(Verbosity.NORMAL);
|
|
||||||
}
|
|
||||||
|
|
||||||
public CaptureOutputTerminal(Verbosity verbosity) {
|
|
||||||
setVerbosity(verbosity);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void doPrint(String msg) {
|
|
||||||
terminalOutput.add(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<String> getTerminalOutput() {
|
|
||||||
return terminalOutput;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void assertTerminalOutputContainsHelpFile(CliToolTestCase.CaptureOutputTerminal terminal, String classPath) throws IOException {
|
|
||||||
List<String> nonEmptyLines = new ArrayList<>();
|
|
||||||
for (String line : terminal.getTerminalOutput()) {
|
|
||||||
String originalPrintedLine = line.replaceAll(System.lineSeparator(), "");
|
|
||||||
if (Strings.isNullOrEmpty(originalPrintedLine)) {
|
|
||||||
nonEmptyLines.add(originalPrintedLine);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
assertThat(nonEmptyLines, hasSize(greaterThan(0)));
|
|
||||||
|
|
||||||
String expectedDocs = StreamsUtils.copyToStringFromClasspath(classPath);
|
String expectedDocs = StreamsUtils.copyToStringFromClasspath(classPath);
|
||||||
for (String nonEmptyLine : nonEmptyLines) {
|
assertTrue(output, output.contains(expectedDocs));
|
||||||
assertThat(expectedDocs, containsString(nonEmptyLine.replaceAll(System.lineSeparator(), "")));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,85 @@
|
||||||
|
/*
|
||||||
|
* Licensed to Elasticsearch under one or more contributor
|
||||||
|
* license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright
|
||||||
|
* ownership. Elasticsearch licenses this file to you under
|
||||||
|
* the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
* not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.elasticsearch.common.cli;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.Deque;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A terminal for tests which captures all output, and
|
||||||
|
* can be plugged with fake input.
|
||||||
|
*/
|
||||||
|
public class MockTerminal extends Terminal {
|
||||||
|
|
||||||
|
private final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||||
|
private final PrintWriter writer = new PrintWriter(new OutputStreamWriter(buffer, StandardCharsets.UTF_8));
|
||||||
|
private final Deque<String> textInput = new ArrayDeque<>();
|
||||||
|
private final Deque<String> secretInput = new ArrayDeque<>();
|
||||||
|
|
||||||
|
public MockTerminal() {
|
||||||
|
super("\n"); // always *nix newlines for tests
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String readText(String prompt) {
|
||||||
|
if (textInput.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return textInput.removeFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public char[] readSecret(String prompt) {
|
||||||
|
if (secretInput.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return secretInput.removeFirst().toCharArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PrintWriter getWriter() {
|
||||||
|
return writer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Adds an an input that will be return from {@link #readText(String)}. Values are read in FIFO order. */
|
||||||
|
public void addTextInput(String input) {
|
||||||
|
textInput.addLast(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Adds an an input that will be return from {@link #readText(String)}. Values are read in FIFO order. */
|
||||||
|
public void addSecretInput(String input) {
|
||||||
|
secretInput.addLast(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns all output written to this terminal. */
|
||||||
|
public String getOutput() throws UnsupportedEncodingException {
|
||||||
|
return buffer.toString("UTF-8");
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Wipes the output. */
|
||||||
|
public void resetOutput() {
|
||||||
|
buffer.reset();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue