/** * @return a new instance of the command by invoking the default constructor of the given class. */ @Override public C create(CommandLine cl) { C c = ReflectionUtils.newInstance(clazz); CLIConfigurator.inject(cl, c); return c; }
@Test public void testArgumentInjectionWithConvertedByAndDefaultValue() throws CLIException { CommandForConvertedValueTest command = new CommandForConvertedValueTest(); CLI cli = CLIConfigurator.define(command.getClass()).setName("test"); CommandLine evaluatedCLI = parse(cli, "Bob,Morane"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.reference.get().first).isEqualTo("Bob"); assertThat(command.reference.get().last).isEqualTo("Morane"); evaluatedCLI = parse(cli); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.reference.get().first).isEqualTo("Bill"); assertThat(command.reference.get().last).isEqualTo("Balantine"); }
@Test public void testArgumentWithDefaultValue() throws CLIException { CommandWithDefaultValueOnArgument command = new CommandWithDefaultValueOnArgument(); CLI cli = CLIConfigurator.define(command.getClass()).setName("test"); CommandLine evaluatedCLI = parse(cli, "foo"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.x.get()).isEqualTo("foo"); assertThat(command.y.get()).isEqualTo(25); }
@Test public void testArgumentInjectionWithSeveralArguments() throws CLIException { CommandForMultipleArgumentTest command = new CommandForMultipleArgumentTest(); CLI cli = CLIConfigurator.define(command.getClass()).setName("test"); CommandLine evaluatedCLI = parse(cli, "foo", "1"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.x.get()).isEqualTo("foo"); assertThat(command.y.get()).isEqualTo(1); }
@Test public void testArgumentInjection() throws CLIException { CommandForArgumentInjectionTest command = new CommandForArgumentInjectionTest(); CLI cli = CLIConfigurator.define(command.getClass()).setName("test"); CommandLine evaluatedCLI = parse(cli, "foo"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.reference.get()).isEqualTo("foo"); }
@Test public void testCWD() throws CLIException { CLI cli = CLIConfigurator.define(command.getClass()); CommandLine evaluatedCLI = parse(cli, "--name=vert.x"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.getCwd()).isEqualTo(new File(".")); evaluatedCLI = parse(cli, "--cwd=target", "--name=vert.x"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.getCwd()).isEqualTo(new File("target")); }
CommandLine evaluatedCLI = parse(cli, "--boolean", "--short=1", "--byte=1", "--int=1", "--long=1", "--double=1.1", "--float=1.1", "--char=c", "--string=hello"); CLIConfigurator.inject(evaluatedCLI, command); CLIConfigurator.inject(evaluatedCLI, command); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.aState).isEqualTo(Thread.State.NEW); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.aPerson.name).isEqualTo("vert.x"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.anotherPerson.name).isEqualTo("vert.x"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.aThirdPerson.name).isEqualTo("vert.x"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.aFourthPerson.first).isEqualTo("bob"); assertThat(command.aFourthPerson.last).isEqualTo("morane");
@Test public void testSystemProperties() throws CLIException { CLI cli = CLIConfigurator.define(command.getClass()); VertxCommandLauncher launcher = new VertxCommandLauncher(); CommandLine evaluatedCLI = parse(cli, "--name=vert.x", "-Dfoo=bar", "--systemProp=x=y"); CLIConfigurator.inject(evaluatedCLI, command); command.setUp(new ExecutionContext(command, launcher, evaluatedCLI)); assertThat(System.getProperty("foo")).isEqualToIgnoringCase("bar"); assertThat(System.getProperty("x")).isEqualToIgnoringCase("y"); command.tearDown(); // System properties are not removed by the tearDown. assertThat(System.getProperty("foo")).isEqualToIgnoringCase("bar"); assertThat(System.getProperty("x")).isEqualToIgnoringCase("y"); }
@Test public void testAnnotatedClassWithArgumentReceivingMultipleValues() { CLI cli = CLI.create(CLIUsingMultipleArgument.class); CLIUsingMultipleArgument instance = new CLIUsingMultipleArgument(); CommandLine cl = cli.parse(Arrays.asList("a", "b", "-s=1", "-s=2")); CLIConfigurator.inject(cl, instance); }
@Test public void testInjectionOfString() throws CLIException { HelloClI command = new HelloClI(); CLI cli = CLIConfigurator.define(HelloClI.class); CommandLine evaluatedCLI = cli.parse(Arrays.asList("--name", "vert.x")); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.run()).isEqualToIgnoringCase("Hello vert.x"); assertThat(command.name).isEqualToIgnoringCase("vert.x"); }
CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.persons).hasSize(3); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.persons2).hasSize(3); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.persons3).hasSize(3); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.persons4).hasSize(3); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.states).hasSize(3).containsExactly(Thread.State.NEW, Thread.State.BLOCKED, Thread.State.RUNNABLE); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.ints).hasSize(3).containsExactly(1, 2, 3); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.shorts).hasSize(3).containsExactly((short) 1, (short) 2, (short) 3); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.strings).hasSize(1).containsExactly("a"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.doubles).hasSize(3).containsExactly(1.0, 2.2, 3.3);
CLIConfigurator.inject(evaluated, cmd);
/** * @return a new instance of the command by invoking the default constructor of the given class. */ @Override public C create(CommandLine cl) { C c = ReflectionUtils.newInstance(clazz); CLIConfigurator.inject(cl, c); return c; }
@Test public void testArgumentInjectionWithConvertedByAndDefaultValue() throws CLIException { CommandForConvertedValueTest command = new CommandForConvertedValueTest(); CLI cli = CLIConfigurator.define(command.getClass()).setName("test"); CommandLine evaluatedCLI = parse(cli, "Bob,Morane"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.reference.get().first).isEqualTo("Bob"); assertThat(command.reference.get().last).isEqualTo("Morane"); evaluatedCLI = parse(cli); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.reference.get().first).isEqualTo("Bill"); assertThat(command.reference.get().last).isEqualTo("Balantine"); }
@Test public void testArgumentInjectionWithSeveralArguments() throws CLIException { CommandForMultipleArgumentTest command = new CommandForMultipleArgumentTest(); CLI cli = CLIConfigurator.define(command.getClass()).setName("test"); CommandLine evaluatedCLI = parse(cli, "foo", "1"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.x.get()).isEqualTo("foo"); assertThat(command.y.get()).isEqualTo(1); }
@Test public void testArgumentWithDefaultValue() throws CLIException { CommandWithDefaultValueOnArgument command = new CommandWithDefaultValueOnArgument(); CLI cli = CLIConfigurator.define(command.getClass()).setName("test"); CommandLine evaluatedCLI = parse(cli, "foo"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.x.get()).isEqualTo("foo"); assertThat(command.y.get()).isEqualTo(25); }
@Test public void testCWD() throws CLIException { CLI cli = CLIConfigurator.define(command.getClass()); CommandLine evaluatedCLI = parse(cli, "--name=vert.x"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.getCwd()).isEqualTo(new File(".")); evaluatedCLI = parse(cli, "--cwd=target", "--name=vert.x"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.getCwd()).isEqualTo(new File("target")); }
@Test public void testArgumentInjection() throws CLIException { CommandForArgumentInjectionTest command = new CommandForArgumentInjectionTest(); CLI cli = CLIConfigurator.define(command.getClass()).setName("test"); CommandLine evaluatedCLI = parse(cli, "foo"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.reference.get()).isEqualTo("foo"); }
@Test public void testAnnotatedClassWithArgumentReceivingMultipleValues() { CLI cli = CLI.create(CLIUsingMultipleArgument.class); CLIUsingMultipleArgument instance = new CLIUsingMultipleArgument(); CommandLine cl = cli.parse(Arrays.asList("a", "b", "-s=1", "-s=2")); CLIConfigurator.inject(cl, instance); }
@Test public void testInjectionOfString() throws CLIException { HelloClI command = new HelloClI(); CLI cli = CLIConfigurator.define(HelloClI.class); CommandLine evaluatedCLI = cli.parse(Arrays.asList("--name", "vert.x")); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.run()).isEqualToIgnoringCase("Hello vert.x"); assertThat(command.name).isEqualToIgnoringCase("vert.x"); }