diff --git a/plugin/src/test/java/org/elasticsearch/xpack/extensions/ListXPackExtensionCommandTests.java b/plugin/src/test/java/org/elasticsearch/xpack/extensions/ListXPackExtensionCommandTests.java index 07269367a19..94c36605760 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/extensions/ListXPackExtensionCommandTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/extensions/ListXPackExtensionCommandTests.java @@ -9,6 +9,8 @@ import org.apache.lucene.util.LuceneTestCase; import org.elasticsearch.Version; import org.elasticsearch.cli.ExitCodes; import org.elasticsearch.cli.MockTerminal; +import org.elasticsearch.cli.Terminal; +import org.elasticsearch.cli.UserException; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; import org.elasticsearch.test.ESTestCase; @@ -19,6 +21,7 @@ import java.nio.file.Files; import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.util.Arrays; +import java.util.Map; import java.util.stream.Collectors; @LuceneTestCase.SuppressFileSystems("*") @@ -38,6 +41,26 @@ public class ListXPackExtensionCommandTests extends ESTestCase { Files.createDirectories(extensionsFile(env)); } + private static class MockListXPackExtensionCommand extends ListXPackExtensionCommand { + + private final Environment env; + + private MockListXPackExtensionCommand(final Environment env) { + this.env = env; + } + + @Override + protected Environment createEnv(Terminal terminal, Map settings) throws UserException { + return env; + } + + @Override + protected boolean addShutdownHook() { + return false; + } + + } + static String buildMultiline(String... args){ return Arrays.asList(args).stream().collect(Collectors.joining("\n", "", "\n")); } @@ -56,67 +79,57 @@ public class ListXPackExtensionCommandTests extends ESTestCase { return env.pluginsFile().resolve("x-pack").resolve("extensions"); } - static MockTerminal listExtensions(Path home) throws Exception { + static MockTerminal listExtensions(Path home, Environment env) throws Exception { MockTerminal terminal = new MockTerminal(); - int status = new ListXPackExtensionCommand() { - @Override - protected boolean addShutdownHook() { - return false; - } - }.main(new String[] { "-Epath.home=" + home }, terminal); + int status = new MockListXPackExtensionCommand(env).main(new String[] { "-Epath.home=" + home }, terminal); assertEquals(ExitCodes.OK, status); return terminal; } - static MockTerminal listExtensions(Path home, String[] args) throws Exception { + static MockTerminal listExtensions(Path home, Environment env, String[] args) throws Exception { String[] argsAndHome = new String[args.length + 1]; System.arraycopy(args, 0, argsAndHome, 0, args.length); argsAndHome[args.length] = "-Epath.home=" + home; MockTerminal terminal = new MockTerminal(); - int status = new ListXPackExtensionCommand() { - @Override - protected boolean addShutdownHook() { - return false; - } - }.main(argsAndHome, terminal); + int status = new MockListXPackExtensionCommand(env).main(argsAndHome, terminal); assertEquals(ExitCodes.OK, status); return terminal; } public void testExtensionsDirMissing() throws Exception { Files.delete(extensionsFile(env)); - IOException e = expectThrows(IOException.class, () -> listExtensions(home)); + IOException e = expectThrows(IOException.class, () -> listExtensions(home, env)); assertTrue(e.getMessage(), e.getMessage().contains("Extensions directory missing")); } public void testNoExtensions() throws Exception { - MockTerminal terminal = listExtensions(home); + MockTerminal terminal = listExtensions(home, env); assertTrue(terminal.getOutput(), terminal.getOutput().isEmpty()); } public void testNoExtensionsVerbose() throws Exception { String[] params = { "-v" }; - MockTerminal terminal = listExtensions(home, params); + MockTerminal terminal = listExtensions(home, env, params); assertEquals(terminal.getOutput(), buildMultiline("XPack Extensions directory: " + extensionsFile(env))); } public void testOneExtension() throws Exception { buildFakeExtension(env, "", "fake", "org.fake"); - MockTerminal terminal = listExtensions(home); + MockTerminal terminal = listExtensions(home, env); assertEquals(terminal.getOutput(), buildMultiline("fake")); } public void testTwoExtensions() throws Exception { buildFakeExtension(env, "", "fake1", "org.fake1"); buildFakeExtension(env, "", "fake2", "org.fake2"); - MockTerminal terminal = listExtensions(home); + MockTerminal terminal = listExtensions(home, env); assertEquals(terminal.getOutput(), buildMultiline("fake1", "fake2")); } public void testExtensionWithVerbose() throws Exception { buildFakeExtension(env, "fake desc", "fake_extension", "org.fake"); String[] params = { "-v" }; - MockTerminal terminal = listExtensions(home, params); + MockTerminal terminal = listExtensions(home, env, params); assertEquals(terminal.getOutput(), buildMultiline("XPack Extensions directory: " + extensionsFile(env), "fake_extension", "- XPack Extension information:", "Name: fake_extension", "Description: fake desc", "Version: 1.0", " * Classname: org.fake")); @@ -126,7 +139,7 @@ public class ListXPackExtensionCommandTests extends ESTestCase { buildFakeExtension(env, "fake desc 1", "fake_extension1", "org.fake"); buildFakeExtension(env, "fake desc 2", "fake_extension2", "org.fake2"); String[] params = { "-v" }; - MockTerminal terminal = listExtensions(home, params); + MockTerminal terminal = listExtensions(home, env, params); assertEquals(terminal.getOutput(), buildMultiline("XPack Extensions directory: " + extensionsFile(env), "fake_extension1", "- XPack Extension information:", "Name: fake_extension1", "Description: fake desc 1", "Version: 1.0", " * Classname: org.fake", @@ -137,14 +150,14 @@ public class ListXPackExtensionCommandTests extends ESTestCase { public void testExtensionWithoutVerboseMultipleExtensions() throws Exception { buildFakeExtension(env, "fake desc 1", "fake_extension1", "org.fake"); buildFakeExtension(env, "fake desc 2", "fake_extension2", "org.fake2"); - MockTerminal terminal = listExtensions(home, new String[0]); + MockTerminal terminal = listExtensions(home, env, new String[0]); String output = terminal.getOutput(); assertEquals(output, buildMultiline("fake_extension1", "fake_extension2")); } public void testExtensionWithoutDescriptorFile() throws Exception{ Files.createDirectories(extensionsFile(env).resolve("fake1")); - NoSuchFileException e = expectThrows(NoSuchFileException.class, () -> listExtensions(home)); + NoSuchFileException e = expectThrows(NoSuchFileException.class, () -> listExtensions(home, env)); assertEquals(e.getFile(), extensionsFile(env).resolve("fake1").resolve(XPackExtensionInfo.XPACK_EXTENSION_PROPERTIES).toString()); } @@ -152,7 +165,7 @@ public class ListXPackExtensionCommandTests extends ESTestCase { public void testExtensionWithWrongDescriptorFile() throws Exception{ XPackExtensionTestUtil.writeProperties(extensionsFile(env).resolve("fake1"), "description", "fake desc"); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> listExtensions(home)); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> listExtensions(home, env)); assertEquals(e.getMessage(), "Property [name] is missing in [" + extensionsFile(env).resolve("fake1") .resolve(XPackExtensionInfo.XPACK_EXTENSION_PROPERTIES).toString() + "]");