/** * Main entry point. * * @param args the user command line arguments. */ public static void main(String[] args) { new Launcher().dispatch(args); }
@Test public void testRunVerticleWithMainVerticleInManifestWithCustomCommand() throws Exception { // Copy the right manifest File manifest = new File("target/test-classes/META-INF/MANIFEST-Launcher-hello.MF"); if (!manifest.isFile()) { throw new IllegalStateException("Cannot find the MANIFEST-Launcher-hello.MF file"); } File target = new File("target/test-classes/META-INF/MANIFEST.MF"); Files.copy(manifest.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING); Launcher launcher = new Launcher(); HelloCommand.called = false; String[] args = {"--name=vert.x"}; launcher.dispatch(args); assertWaitUntil(() -> HelloCommand.called); }
@Test public void testRunVerticleWithoutMainVerticleInManifestButWithCustomCommand() throws Exception { // Copy the right manifest File manifest = new File("target/test-classes/META-INF/MANIFEST-Launcher-Default-Command.MF"); if (!manifest.isFile()) { throw new IllegalStateException("Cannot find the MANIFEST-Default-Command.MF file"); } File target = new File("target/test-classes/META-INF/MANIFEST.MF"); Files.copy(manifest.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING); Launcher launcher = new Launcher(); HelloCommand.called = false; String[] args = {"--name=vert.x"}; launcher.dispatch(args); assertWaitUntil(() -> HelloCommand.called); }
@Test public void testRunWithOverriddenDefaultCommandRequiringArgs() throws Exception { // Copy the right manifest File manifest = new File("target/test-classes/META-INF/MANIFEST-Launcher-run.MF"); if (!manifest.isFile()) { throw new IllegalStateException("Cannot find the MANIFEST-Launcher-run.MF file"); } File target = new File("target/test-classes/META-INF/MANIFEST.MF"); Files.copy(manifest.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING); String[] args = {TestVerticle.class.getName()}; Launcher launcher = new Launcher(); launcher.dispatch(args); assertWaitUntil(() -> TestVerticle.instanceCount.get() == 1); cleanup(launcher); }
@Test public void testRunWithOverriddenDefaultCommand() throws Exception { // Copy the right manifest File manifest = new File("target/test-classes/META-INF/MANIFEST-Launcher-hello.MF"); if (!manifest.isFile()) { throw new IllegalStateException("Cannot find the MANIFEST-Launcher-hello.MF file"); } File target = new File("target/test-classes/META-INF/MANIFEST.MF"); Files.copy(manifest.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING); HelloCommand.called = false; String[] args = {"run", TestVerticle.class.getName(), "--name=vert.x"}; Launcher launcher = new Launcher(); launcher.dispatch(args); assertWaitUntil(() -> TestVerticle.instanceCount.get() == 1); cleanup(launcher); }
@Test public void testFatJarWithMissingCommandHelp() throws Exception { // Copy the right manifest File manifest = new File("target/test-classes/META-INF/MANIFEST-Launcher.MF"); if (!manifest.isFile()) { throw new IllegalStateException("Cannot find the MANIFEST-Launcher.MF file"); } File target = new File("target/test-classes/META-INF/MANIFEST.MF"); Files.copy(manifest.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING); Launcher launcher = new Launcher() { @Override public PrintStream getPrintStream() { return stream; } }; String[] args = {"not-a-command", "--help"}; launcher.dispatch(args); assertTrue(out.toString().contains("The command 'not-a-command' is not a valid command.")); }
@Test public void testFatJarWithCommandHelp() throws Exception { // Copy the right manifest File manifest = new File("target/test-classes/META-INF/MANIFEST-Launcher.MF"); if (!manifest.isFile()) { throw new IllegalStateException("Cannot find the MANIFEST-Launcher.MF file"); } File target = new File("target/test-classes/META-INF/MANIFEST.MF"); Files.copy(manifest.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING); Launcher launcher = new Launcher() { @Override public PrintStream getPrintStream() { return stream; } }; String[] args = {"hello", "--help"}; launcher.dispatch(args); assertTrue(out.toString().contains("Usage")); assertTrue(out.toString().contains("hello")); assertTrue(out.toString().contains("A simple command to wish you a good day.")); // Description text. }
@Test public void testRunVerticleWithMainVerticleInManifestWithArgs() throws Exception { // Copy the right manifest File manifest = new File("target/test-classes/META-INF/MANIFEST-Launcher.MF"); if (!manifest.isFile()) { throw new IllegalStateException("Cannot find the MANIFEST-Launcher.MF file"); } File target = new File("target/test-classes/META-INF/MANIFEST.MF"); Files.copy(manifest.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING); Launcher launcher = new Launcher(); String[] args = {"-cluster", "-worker", "-instances=10"}; launcher.dispatch(args); assertWaitUntil(() -> TestVerticle.instanceCount.get() == 10); assertEquals(Arrays.asList(args), TestVerticle.processArgs); cleanup(launcher); }
@Test public void testFatJarWithHelp() throws Exception { // Copy the right manifest File manifest = new File("target/test-classes/META-INF/MANIFEST-Launcher.MF"); if (!manifest.isFile()) { throw new IllegalStateException("Cannot find the MANIFEST-Launcher.MF file"); } File target = new File("target/test-classes/META-INF/MANIFEST.MF"); Files.copy(manifest.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING); Launcher launcher = new Launcher() { @Override public PrintStream getPrintStream() { return stream; } }; String[] args = {"--help"}; launcher.dispatch(args); assertTrue(out.toString().contains("Usage")); assertTrue(out.toString().contains("run")); assertTrue(out.toString().contains("version")); assertTrue(out.toString().contains("bare")); }
@Test public void testRunVerticleWithMainVerticleInManifestNoArgs() throws Exception { // Copy the right manifest File manifest = new File("target/test-classes/META-INF/MANIFEST-Launcher.MF"); if (!manifest.isFile()) { throw new IllegalStateException("Cannot find the MANIFEST-Launcher.MF file"); } File target = new File("target/test-classes/META-INF/MANIFEST.MF"); Files.copy(manifest.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING); Launcher launcher = new Launcher(); String[] args = new String[0]; launcher.dispatch(args); assertWaitUntil(() -> TestVerticle.instanceCount.get() == 1); assertEquals(Arrays.asList(args), TestVerticle.processArgs); cleanup(launcher); }
@Test public void testThatALauncherCanHideACommand() { Launcher myLauncher = new Launcher() { @Override protected void load() { super.load(); unregister("start"); } }; record(); myLauncher.dispatch(new String[]{"start"}); stop(); assertThat(output.toString()).contains("The command 'start' is not a valid command."); assertThat(myLauncher.getCommandNames()).doesNotContain("start"); }
myLauncher.dispatch(new String[0]); assertWaitUntil(() -> { try {
@Test public void testThatALauncherCanAddACommand() { Launcher myLauncher = new Launcher() { @Override protected void load() { super.load(); register(FooCommand.class); } }; myLauncher.dispatch(new String[]{"foo"}); assertThat(myLauncher.getCommandNames()).contains("foo"); assertWaitUntil(spy::get); }
@Test public void testExtendingMainVerticle() { Launcher myLauncher = new Launcher() { @Override protected String getMainVerticle() { return HttpTestVerticle.class.getName(); } @Override public void afterStartingVertx(Vertx vertx) { LauncherExtensibilityTest.this.vertx = vertx; } }; myLauncher.dispatch(new String[0]); assertWaitUntil(() -> { try { return RunCommandTest.getHttpCode() == 200; } catch (IOException e) { return false; } }); }
@Test public void testThatCustomLauncherCanUpdateConfiguration() throws IOException { long time = System.nanoTime(); Launcher myLauncher = new Launcher() { @Override protected String getMainVerticle() { return HttpTestVerticle.class.getName(); } @Override public void afterStartingVertx(Vertx vertx) { LauncherExtensibilityTest.this.vertx = vertx; } @Override public void afterConfigParsed(JsonObject config) { config.put("time", time); } }; myLauncher.dispatch(new String[] {"-conf=\"{\"time\":345667}"}); assertWaitUntil(() -> { try { return RunCommandTest.getHttpCode() == 200; } catch (IOException e) { return false; } }); assertThat(RunCommandTest.getContent().getJsonObject("conf").getLong("time")).isEqualTo(time); }
@Test public void testThatCustomLauncherCanUpdateConfigurationWhenNoneArePassed() throws IOException { long time = System.nanoTime(); Launcher myLauncher = new Launcher() { @Override protected String getMainVerticle() { return HttpTestVerticle.class.getName(); } @Override public void afterStartingVertx(Vertx vertx) { LauncherExtensibilityTest.this.vertx = vertx; } @Override public void afterConfigParsed(JsonObject config) { config.put("time", time); } }; myLauncher.dispatch(new String[0]); assertWaitUntil(() -> { try { return RunCommandTest.getHttpCode() == 200; } catch (IOException e) { return false; } }); assertThat(RunCommandTest.getContent().getJsonObject("conf").getLong("time")).isEqualTo(time); }
/** * Main entry point. * * @param args the user command line arguments. */ public static void main(String[] args) { new Launcher().dispatch(args); }
@Test public void testRunVerticleWithoutMainVerticleInManifestButWithCustomCommand() throws Exception { // Copy the right manifest File manifest = new File("target/test-classes/META-INF/MANIFEST-Launcher-Default-Command.MF"); if (!manifest.isFile()) { throw new IllegalStateException("Cannot find the MANIFEST-Default-Command.MF file"); } File target = new File("target/test-classes/META-INF/MANIFEST.MF"); Files.copy(manifest.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING); Launcher launcher = new Launcher(); HelloCommand.called = false; String[] args = {"--name=vert.x"}; launcher.dispatch(args); assertWaitUntil(() -> HelloCommand.called); }
@Test public void testThatALauncherCanHideACommand() { Launcher myLauncher = new Launcher() { @Override protected void load() { super.load(); unregister("start"); } }; record(); myLauncher.dispatch(new String[]{"start"}); stop(); assertThat(output.toString()).contains("The command 'start' is not a valid command."); assertThat(myLauncher.getCommandNames()).doesNotContain("start"); }
@Test public void testThatALauncherCanAddACommand() { Launcher myLauncher = new Launcher() { @Override protected void load() { super.load(); register(FooCommand.class); } }; myLauncher.dispatch(new String[]{"foo"}); assertThat(myLauncher.getCommandNames()).contains("foo"); assertWaitUntil(spy::get); }