Search in sources :

Example 1 with Argument

use of dmg.util.command.Argument in project dcache by dCache.

the class AnnotatedCommandScannerTest method shouldUseNegativeArgumentArgumentIndexToCountFromTheEnd.

@Test
public void shouldUseNegativeArgumentArgumentIndexToCountFromTheEnd() throws Exception {
    class SUT {

        @Command(name = "test")
        class TestCommand implements Callable<String> {

            @Argument(index = -2)
            int argument;

            @Override
            public String call() {
                assertThat(argument, is(1));
                return null;
            }
        }
    }
    Map<List<String>, ? extends CommandExecutor> commands = _scanner.scan(new SUT());
    commands.get(asList("test")).execute(new Args("1 2"));
}
Also used : Args(org.dcache.util.Args) Argument(dmg.util.command.Argument) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Test(org.junit.Test)

Example 2 with Argument

use of dmg.util.command.Argument in project dcache by dCache.

the class AnnotatedCommandScannerTest method shouldAcceptCommonTypes.

@Test
public void shouldAcceptCommonTypes() throws Exception {
    class SUT {

        @Command(name = "test")
        class TestCommand implements Callable<String> {

            @Argument(index = 0)
            Integer arg0;

            @Argument(index = 1)
            int arg1;

            @Argument(index = 2)
            Short arg2;

            @Argument(index = 3)
            short arg3;

            @Argument(index = 4)
            Long arg4;

            @Argument(index = 5)
            long arg5;

            @Argument(index = 6)
            Byte arg6;

            @Argument(index = 7)
            byte arg7;

            @Argument(index = 8)
            Float arg8;

            @Argument(index = 9)
            float arg9;

            @Argument(index = 10)
            Double arg10;

            @Argument(index = 11)
            double arg11;

            @Argument(index = 12)
            String arg12;

            @Argument(index = 13)
            Character arg13;

            @Argument(index = 14)
            char arg14;

            @Argument(index = 15)
            AnEnum arg15;

            @Argument(index = 16)
            File arg16;

            @Argument(index = 17)
            Time arg17;

            @Argument(index = 18)
            long[] arg18;

            @Override
            public String call() {
                assertThat(arg0, is(0));
                assertThat(arg1, is(1));
                assertThat(arg2, is((short) 2));
                assertThat(arg3, is((short) 3));
                assertThat(arg4, is(4L));
                assertThat(arg5, is(5L));
                assertThat(arg6, is((byte) 6));
                assertThat(arg7, is((byte) 7));
                assertThat(arg8, is((float) 8.0));
                assertThat(arg9, is((float) 9.0));
                assertThat(arg10, is(10.0));
                assertThat(arg11, is(11.0));
                assertThat(arg12, is("12"));
                assertThat(arg13, is('a'));
                assertThat(arg14, is('b'));
                assertThat(arg15, is(AnEnum.BAR));
                assertThat(arg16, is(new File("/my/file")));
                assertThat(arg17, is(Time.valueOf("12:34:56")));
                assertArrayEquals(arg18, new long[] { 100, 101 });
                return null;
            }
        }
    }
    Map<List<String>, ? extends CommandExecutor> commands = _scanner.scan(new SUT());
    commands.get(asList("test")).execute(new Args("0 1 2 3 4 5 6 7 8.0 9 10 11.00 12 a b BAR /my/file 12:34:56 100 101"));
}
Also used : Argument(dmg.util.command.Argument) Time(java.sql.Time) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Args(org.dcache.util.Args) File(java.io.File) Test(org.junit.Test)

Example 3 with Argument

use of dmg.util.command.Argument in project dcache by dCache.

the class AnnotatedCommandScannerTest method shouldNotRequireOptionalArguments.

@Test
public void shouldNotRequireOptionalArguments() throws Exception {
    class SUT {

        @Command(name = "test")
        class TestCommand implements Callable<String> {

            @Argument(index = -2, required = false)
            Integer optional;

            @Argument(index = -1)
            Integer required;

            @Override
            public String call() {
                assertThat(optional, is((Integer) null));
                assertThat(required, is(2));
                return null;
            }
        }
    }
    Map<List<String>, ? extends CommandExecutor> commands = _scanner.scan(new SUT());
    commands.get(asList("test")).execute(new Args("2"));
}
Also used : Args(org.dcache.util.Args) Argument(dmg.util.command.Argument) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Test(org.junit.Test)

Example 4 with Argument

use of dmg.util.command.Argument in project dcache by dCache.

the class AnnotatedCommandScannerTest method shouldRejectTooManyArguments.

@Test(expected = CommandSyntaxException.class)
public void shouldRejectTooManyArguments() throws Exception {
    class SUT {

        @Command(name = "test")
        class TestCommand implements Callable<String> {

            @Argument
            int required;

            @Override
            public String call() {
                return null;
            }
        }
    }
    Map<List<String>, ? extends CommandExecutor> commands = _scanner.scan(new SUT());
    commands.get(asList("test")).execute(new Args("1 2"));
}
Also used : Args(org.dcache.util.Args) Argument(dmg.util.command.Argument) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Test(org.junit.Test)

Example 5 with Argument

use of dmg.util.command.Argument in project dcache by dCache.

the class AnnotatedCommandScannerTest method shouldAcceptOptionalArguments.

@Test
public void shouldAcceptOptionalArguments() throws Exception {
    class SUT {

        @Command(name = "test")
        class TestCommand implements Callable<String> {

            @Argument(index = -2, required = false)
            Integer optional;

            @Argument(index = -1)
            Integer required;

            @Override
            public String call() {
                assertThat(optional, is(1));
                assertThat(required, is(2));
                return null;
            }
        }
    }
    Map<List<String>, ? extends CommandExecutor> commands = _scanner.scan(new SUT());
    commands.get(asList("test")).execute(new Args("1 2"));
}
Also used : Args(org.dcache.util.Args) Argument(dmg.util.command.Argument) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Test(org.junit.Test)

Aggregations

Argument (dmg.util.command.Argument)9 Arrays.asList (java.util.Arrays.asList)8 List (java.util.List)8 Args (org.dcache.util.Args)8 Test (org.junit.Test)8 CommandLine (dmg.util.command.CommandLine)1 Option (dmg.util.command.Option)1 File (java.io.File)1 Field (java.lang.reflect.Field)1 Time (java.sql.Time)1 HashSet (java.util.HashSet)1