diff --git a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/ArityCommands.java b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/ArityCommands.java index 62388b42..fc9f8433 100644 --- a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/ArityCommands.java +++ b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/ArityCommands.java @@ -16,8 +16,6 @@ package org.springframework.shell.samples.e2e; import java.util.Arrays; -import java.util.stream.Collectors; -import java.util.stream.IntStream; import org.springframework.context.annotation.Bean; import org.springframework.shell.command.CommandRegistration; @@ -25,121 +23,122 @@ import org.springframework.shell.command.CommandRegistration.OptionArity; import org.springframework.shell.standard.ShellComponent; import org.springframework.shell.standard.ShellMethod; import org.springframework.shell.standard.ShellOption; +import org.springframework.stereotype.Component; /** * Commands used for e2e test. * * @author Janne Valkealahti */ -@ShellComponent -public class ArityCommands extends BaseE2ECommands { +public class ArityCommands { + + @ShellComponent + public static class LegacyAnnotation extends BaseE2ECommands { + + @ShellMethod(key = LEGACY_ANNO + "arity-boolean-default-true", group = GROUP) + public String testArityBooleanDefaultTrueLegacyAnnotation( + @ShellOption(value = "--overwrite", arity = 1, defaultValue = "true") Boolean overwrite + ) { + return "Hello " + overwrite; + } + + @ShellMethod(key = LEGACY_ANNO + "arity-string-array", group = GROUP) + public String testArityStringArrayLegacyAnnotation( + @ShellOption(value = "--arg1", arity = 3) String[] arg1 + ) { + return "Hello " + Arrays.asList(arg1); + } + + @ShellMethod(key = LEGACY_ANNO + "arity-float-array", group = GROUP) + public String testArityFloatArrayLegacyAnnotation( + @ShellOption(value = "--arg1", arity = 3) float[] arg1 + ) { + return "Hello " + stringOfFloats(arg1); + } - @ShellMethod(key = LEGACY_ANNO + "arity-boolean-default-true", group = GROUP) - public String testArityBooleanDefaultTrueLegacyAnnotation( - @ShellOption(value = "--overwrite", arity = 1, defaultValue = "true") Boolean overwrite - ) { - return "Hello " + overwrite; } - @Bean - public CommandRegistration testArityBooleanDefaultTrueRegistration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "arity-boolean-default-true") - .group(GROUP) - .withOption() - .longNames("overwrite") - .type(Boolean.class) - .defaultValue("true") - .arity(OptionArity.ZERO_OR_ONE) - .and() - .withTarget() - .function(ctx -> { - Boolean overwrite = ctx.getOptionValue("overwrite"); - return "Hello " + overwrite; - }) - .and() - .build(); - } + @Component + public static class Registration extends BaseE2ECommands { - @ShellMethod(key = LEGACY_ANNO + "arity-string-array", group = GROUP) - public String testArityStringArrayLegacyAnnotation( - @ShellOption(value = "--arg1", arity = 3) String[] arg1 - ) { - return "Hello " + Arrays.asList(arg1); - } + @Bean + public CommandRegistration testArityBooleanDefaultTrueRegistration() { + return getBuilder() + .command(REG, "arity-boolean-default-true") + .group(GROUP) + .withOption() + .longNames("overwrite") + .type(Boolean.class) + .defaultValue("true") + .arity(OptionArity.ZERO_OR_ONE) + .and() + .withTarget() + .function(ctx -> { + Boolean overwrite = ctx.getOptionValue("overwrite"); + return "Hello " + overwrite; + }) + .and() + .build(); + } - @Bean - public CommandRegistration testArityStringArrayRegistration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "arity-string-array") - .group(GROUP) - .withOption() - .longNames("arg1") - .required() - .type(String[].class) - .arity(0, 3) - .position(0) - .and() - .withTarget() - .function(ctx -> { - String[] arg1 = ctx.getOptionValue("arg1"); - return "Hello " + Arrays.asList(arg1); - }) - .and() - .build(); - } + @Bean + public CommandRegistration testArityStringArrayRegistration() { + return getBuilder() + .command(REG, "arity-string-array") + .group(GROUP) + .withOption() + .longNames("arg1") + .required() + .type(String[].class) + .arity(0, 3) + .position(0) + .and() + .withTarget() + .function(ctx -> { + String[] arg1 = ctx.getOptionValue("arg1"); + return "Hello " + Arrays.asList(arg1); + }) + .and() + .build(); + } - @ShellMethod(key = LEGACY_ANNO + "arity-float-array", group = GROUP) - public String testArityFloatArrayLegacyAnnotation( - @ShellOption(value = "--arg1", arity = 3) float[] arg1 - ) { - return "Hello " + floatsToString(arg1); - } + @Bean + public CommandRegistration testArityFloatArrayRegistration() { + return getBuilder() + .command(REG, "arity-float-array") + .group(GROUP) + .withOption() + .longNames("arg1") + .type(float[].class) + .arity(0, 3) + .and() + .withTarget() + .function(ctx -> { + float[] arg1 = ctx.getOptionValue("arg1"); + return "Hello " + stringOfFloats(arg1); + }) + .and() + .build(); + } - @Bean - public CommandRegistration testArityFloatArrayRegistration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "arity-float-array") - .group(GROUP) - .withOption() - .longNames("arg1") - .type(float[].class) - .arity(0, 3) - .and() - .withTarget() - .function(ctx -> { - float[] arg1 = ctx.getOptionValue("arg1"); - return "Hello " + floatsToString(arg1); - }) - .and() - .build(); - } - - @Bean - public CommandRegistration testArityErrorsRegistration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "arity-errors") - .group(GROUP) - .withOption() - .longNames("arg1") - .type(String[].class) - .required() - .arity(1, 2) - .and() - .withTarget() - .function(ctx -> { - String[] arg1 = ctx.getOptionValue("arg1"); - return "Hello " + Arrays.asList(arg1); - }) - .and() - .build(); - } - - private static String floatsToString(float[] arg1) { - return IntStream.range(0, arg1.length) - .mapToDouble(i -> arg1[i]) - .boxed() - .map(d -> d.toString()) - .collect(Collectors.joining(",")); + @Bean + public CommandRegistration testArityErrorsRegistration() { + return getBuilder() + .command(REG, "arity-errors") + .group(GROUP) + .withOption() + .longNames("arg1") + .type(String[].class) + .required() + .arity(1, 2) + .and() + .withTarget() + .function(ctx -> { + String[] arg1 = ctx.getOptionValue("arg1"); + return "Hello " + Arrays.asList(arg1); + }) + .and() + .build(); + } } } diff --git a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/BaseE2ECommands.java b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/BaseE2ECommands.java index 0d4d5e30..108143f4 100644 --- a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/BaseE2ECommands.java +++ b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/BaseE2ECommands.java @@ -18,6 +18,8 @@ package org.springframework.shell.samples.e2e; import java.util.stream.Collectors; import java.util.stream.IntStream; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.shell.command.CommandRegistration; import org.springframework.util.StringUtils; /** @@ -31,6 +33,13 @@ abstract class BaseE2ECommands { static final String REG = "e2e reg"; static final String LEGACY_ANNO = "e2e anno "; + @Autowired + private CommandRegistration.BuilderSupplier builder; + + CommandRegistration.Builder getBuilder() { + return builder.get(); + } + static String stringOfStrings(String[] values) { return String.format("[%s]", StringUtils.arrayToCommaDelimitedString(values)); } diff --git a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/DefaultValueCommands.java b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/DefaultValueCommands.java index cb6c6910..000901ad 100644 --- a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/DefaultValueCommands.java +++ b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/DefaultValueCommands.java @@ -20,6 +20,7 @@ import org.springframework.shell.command.CommandRegistration; import org.springframework.shell.standard.ShellComponent; import org.springframework.shell.standard.ShellMethod; import org.springframework.shell.standard.ShellOption; +import org.springframework.stereotype.Component; /** * Commands used for e2e test. @@ -27,109 +28,117 @@ import org.springframework.shell.standard.ShellOption; * @author Janne Valkealahti */ @ShellComponent -public class DefaultValueCommands extends BaseE2ECommands { +public class DefaultValueCommands { - @ShellMethod(key = LEGACY_ANNO + "default-value", group = GROUP) - public String testDefaultValue( - @ShellOption(defaultValue = "hi") String arg1 - ) { - return "Hello " + arg1; + @ShellComponent + public static class LegacyAnnotation extends BaseE2ECommands { + + @ShellMethod(key = LEGACY_ANNO + "default-value", group = GROUP) + public String testDefaultValue( + @ShellOption(defaultValue = "hi") String arg1 + ) { + return "Hello " + arg1; + } + + @ShellMethod(key = LEGACY_ANNO + "default-value-boolean1", group = GROUP) + public String testDefaultValueBoolean1( + @ShellOption(defaultValue = "false") boolean arg1 + ) { + return "Hello " + arg1; + } + + @ShellMethod(key = LEGACY_ANNO + "default-value-boolean2", group = GROUP) + public String testDefaultValueBoolean2( + @ShellOption(defaultValue = "true") boolean arg1 + ) { + return "Hello " + arg1; + } + + @ShellMethod(key = LEGACY_ANNO + "default-value-boolean3", group = GROUP) + public String testDefaultValueBoolean3( + @ShellOption boolean arg1 + ) { + return "Hello " + arg1; + } } - @Bean - public CommandRegistration testDefaultValueRegistration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "default-value") - .group(GROUP) - .withOption() - .longNames("arg1") - .defaultValue("hi") - .and() - .withTarget() - .function(ctx -> { - String arg1 = ctx.getOptionValue("arg1"); - return "Hello " + arg1; - }) - .and() - .build(); - } + @Component + public static class Registration extends BaseE2ECommands { - @ShellMethod(key = LEGACY_ANNO + "default-value-boolean1", group = GROUP) - public String testDefaultValueBoolean1( - @ShellOption(defaultValue = "false") boolean arg1 - ) { - return "Hello " + arg1; - } + @Bean + public CommandRegistration testDefaultValueRegistration() { + return getBuilder() + .command(REG, "default-value") + .group(GROUP) + .withOption() + .longNames("arg1") + .defaultValue("hi") + .and() + .withTarget() + .function(ctx -> { + String arg1 = ctx.getOptionValue("arg1"); + return "Hello " + arg1; + }) + .and() + .build(); + } - @Bean - public CommandRegistration testDefaultValueBoolean1Registration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "default-value-boolean1") - .group(GROUP) - .withOption() - .longNames("arg1") - .defaultValue("false") - .type(boolean.class) - .and() - .withTarget() - .function(ctx -> { - boolean arg1 = ctx.getOptionValue("arg1"); - return "Hello " + arg1; - }) - .and() - .build(); - } + @Bean + public CommandRegistration testDefaultValueBoolean1Registration() { + return getBuilder() + .command(REG, "default-value-boolean1") + .group(GROUP) + .withOption() + .longNames("arg1") + .defaultValue("false") + .type(boolean.class) + .and() + .withTarget() + .function(ctx -> { + boolean arg1 = ctx.getOptionValue("arg1"); + return "Hello " + arg1; + }) + .and() + .build(); + } - @ShellMethod(key = LEGACY_ANNO + "default-value-boolean2", group = GROUP) - public String testDefaultValueBoolean2( - @ShellOption(defaultValue = "true") boolean arg1 - ) { - return "Hello " + arg1; - } + @Bean + public CommandRegistration testDefaultValueBoolean2Registration() { + return getBuilder() + .command(REG, "default-value-boolean2") + .group(GROUP) + .withOption() + .longNames("arg1") + .defaultValue("true") + .type(boolean.class) + .and() + .withTarget() + .function(ctx -> { + boolean arg1 = ctx.getOptionValue("arg1"); + return "Hello " + arg1; + }) + .and() + .build(); + } - @Bean - public CommandRegistration testDefaultValueBoolean2Registration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "default-value-boolean2") - .group(GROUP) - .withOption() - .longNames("arg1") - .defaultValue("true") - .type(boolean.class) - .and() - .withTarget() - .function(ctx -> { - boolean arg1 = ctx.getOptionValue("arg1"); - return "Hello " + arg1; - }) - .and() - .build(); - } - - @ShellMethod(key = LEGACY_ANNO + "default-value-boolean3", group = GROUP) - public String testDefaultValueBoolean3( - @ShellOption boolean arg1 - ) { - return "Hello " + arg1; - } - - @Bean - public CommandRegistration testDefaultValueBoolean3Registration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "default-value-boolean3") - .group(GROUP) - .withOption() - .longNames("arg1") - .required(false) - .type(boolean.class) - .defaultValue("false") - .and() - .withTarget() - .function(ctx -> { - boolean arg1 = ctx.getOptionValue("arg1"); - return "Hello " + arg1; - }) - .and() - .build(); + @Bean + public CommandRegistration testDefaultValueBoolean3Registration() { + return getBuilder() + .command(REG, "default-value-boolean3") + .group(GROUP) + .withOption() + .longNames("arg1") + .required(false) + .type(boolean.class) + .defaultValue("false") + .and() + .withTarget() + .function(ctx -> { + boolean arg1 = ctx.getOptionValue("arg1"); + return "Hello " + arg1; + }) + .and() + .build(); + } } } diff --git a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/ErrorHandlingCommands.java b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/ErrorHandlingCommands.java index cc050c83..8831187e 100644 --- a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/ErrorHandlingCommands.java +++ b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/ErrorHandlingCommands.java @@ -29,100 +29,108 @@ import org.springframework.shell.command.annotation.ExceptionResolver; import org.springframework.shell.command.annotation.ExitCode; import org.springframework.shell.standard.ShellComponent; import org.springframework.shell.standard.ShellMethod; +import org.springframework.stereotype.Component; /** * Commands used for e2e test. * * @author Janne Valkealahti */ -@ShellComponent -public class ErrorHandlingCommands extends BaseE2ECommands { +public class ErrorHandlingCommands { - @ShellMethod(key = LEGACY_ANNO + "error-handling", group = GROUP) - String testErrorHandling(String arg1) throws IOException { - if ("throw1".equals(arg1)) { - throw new CustomException1(); + @ShellComponent + public static class LegacyAnnotation extends BaseE2ECommands { + + @ShellMethod(key = LEGACY_ANNO + "error-handling", group = GROUP) + String testErrorHandling(String arg1) throws IOException { + if ("throw1".equals(arg1)) { + throw new CustomException1(); + } + if ("throw2".equals(arg1)) { + throw new CustomException2(11); + } + if ("throw3".equals(arg1)) { + throw new RuntimeException(); + } + if ("throw4".equals(arg1)) { + throw new IllegalArgumentException(); + } + if ("throw5".equals(arg1)) { + throw new CustomException3(); + } + if ("throw6".equals(arg1)) { + throw new CustomException4(); + } + return "Hello " + arg1; } - if ("throw2".equals(arg1)) { - throw new CustomException2(11); + + @ExceptionResolver({ CustomException1.class }) + CommandHandlingResult errorHandler1(CustomException1 e) { + return CommandHandlingResult.of("Hi, handled custom exception\n", 42); } - if ("throw3".equals(arg1)) { - throw new RuntimeException(); + + @ExceptionResolver + CommandHandlingResult errorHandler2(IllegalArgumentException e) { + return CommandHandlingResult.of("Hi, handled illegal exception\n", 42); } - if ("throw4".equals(arg1)) { - throw new IllegalArgumentException(); + + @ExceptionResolver({ CustomException3.class }) + @ExitCode(3) + String errorHandler3(CustomException3 e) { + return "Hi, handled custom exception 3\n"; } - if ("throw5".equals(arg1)) { - throw new CustomException3(); + + @ExceptionResolver({ CustomException4.class }) + @ExitCode(code = 4) + void errorHandler3(CustomException4 e, Terminal terminal) { + PrintWriter writer = terminal.writer(); + writer.println(String.format("Hi, handled custom exception %s", e)); + writer.flush(); } - if ("throw6".equals(arg1)) { - throw new CustomException4(); - } - return "Hello " + arg1; } - @ExceptionResolver({ CustomException1.class }) - CommandHandlingResult errorHandler1(CustomException1 e) { - return CommandHandlingResult.of("Hi, handled custom exception\n", 42); - } + @Component + public static class Registration extends BaseE2ECommands { - @ExceptionResolver - CommandHandlingResult errorHandler2(IllegalArgumentException e) { - return CommandHandlingResult.of("Hi, handled illegal exception\n", 42); - } + @Bean + CommandRegistration testErrorHandlingRegistration() { + return getBuilder() + .command(REG, "error-handling") + .group(GROUP) + .withOption() + .longNames("arg1") + .required() + .and() + .withErrorHandling() + .resolver(new CustomExceptionResolver()) + .and() + .withTarget() + .function(ctx -> { + String arg1 = ctx.getOptionValue("arg1"); + if ("throw1".equals(arg1)) { + throw new CustomException1(); + } + if ("throw2".equals(arg1)) { + throw new CustomException2(11); + } + if ("throw3".equals(arg1)) { + throw new RuntimeException(); + } + if ("throw4".equals(arg1)) { + throw new IllegalArgumentException(); + } + if ("throw5".equals(arg1)) { + throw new CustomException3(); + } + if ("throw6".equals(arg1)) { + throw new CustomException4(); + } - @ExceptionResolver({ CustomException3.class }) - @ExitCode(3) - String errorHandler3(CustomException3 e) { - return "Hi, handled custom exception 3\n"; - } - - @ExceptionResolver({ CustomException4.class }) - @ExitCode(code = 4) - void errorHandler3(CustomException4 e, Terminal terminal) { - PrintWriter writer = terminal.writer(); - writer.println(String.format("Hi, handled custom exception %s", e)); - writer.flush(); - } - - @Bean - CommandRegistration testErrorHandlingRegistration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "error-handling") - .group(GROUP) - .withOption() - .longNames("arg1") - .required() - .and() - .withErrorHandling() - .resolver(new CustomExceptionResolver()) - .and() - .withTarget() - .function(ctx -> { - String arg1 = ctx.getOptionValue("arg1"); - if ("throw1".equals(arg1)) { - throw new CustomException1(); - } - if ("throw2".equals(arg1)) { - throw new CustomException2(11); - } - if ("throw3".equals(arg1)) { - throw new RuntimeException(); - } - if ("throw4".equals(arg1)) { - throw new IllegalArgumentException(); - } - if ("throw5".equals(arg1)) { - throw new CustomException3(); - } - if ("throw6".equals(arg1)) { - throw new CustomException4(); - } - - return "Hello " + arg1; - }) - .and() - .build(); + return "Hello " + arg1; + }) + .and() + .build(); + } } private static class CustomException1 extends RuntimeException { diff --git a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/ExitCodeCommands.java b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/ExitCodeCommands.java index 9afb3794..241a23b5 100644 --- a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/ExitCodeCommands.java +++ b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/ExitCodeCommands.java @@ -17,45 +17,47 @@ package org.springframework.shell.samples.e2e; import org.springframework.context.annotation.Bean; import org.springframework.shell.command.CommandRegistration; -import org.springframework.shell.standard.ShellComponent; +import org.springframework.stereotype.Component; /** * Commands used for e2e test. * * @author Janne Valkealahti */ -@ShellComponent -public class ExitCodeCommands extends BaseE2ECommands { +public class ExitCodeCommands { - @Bean - public CommandRegistration testExitCodeRegistration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "exit-code") - .group(GROUP) - .withOption() - .longNames("arg1") - .required() - .and() - .withTarget() - .consumer(ctx -> { - String arg1 = ctx.getOptionValue("arg1"); - throw new MyException(arg1); - }) - .and() - .withExitCode() - .map(MyException.class, 3) - .map(t -> { - String msg = t.getMessage(); - if (msg != null && msg.contains("ok")) { + @Component + public static class Registration extends BaseE2ECommands { + @Bean + public CommandRegistration testExitCodeRegistration() { + return getBuilder() + .command(REG, "exit-code") + .group(GROUP) + .withOption() + .longNames("arg1") + .required() + .and() + .withTarget() + .consumer(ctx -> { + String arg1 = ctx.getOptionValue("arg1"); + throw new MyException(arg1); + }) + .and() + .withExitCode() + .map(MyException.class, 3) + .map(t -> { + String msg = t.getMessage(); + if (msg != null && msg.contains("ok")) { + return 0; + } + else if (msg != null && msg.contains("fun")) { + return 4; + } return 0; - } - else if (msg != null && msg.contains("fun")) { - return 4; - } - return 0; - }) - .and() - .build(); + }) + .and() + .build(); + } } static class MyException extends RuntimeException { diff --git a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/HelpOptionCommands.java b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/HelpOptionCommands.java index 98da846f..e392d173 100644 --- a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/HelpOptionCommands.java +++ b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/HelpOptionCommands.java @@ -15,69 +15,67 @@ */ package org.springframework.shell.samples.e2e; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.shell.command.CommandRegistration; import org.springframework.shell.standard.ShellComponent; import org.springframework.shell.standard.ShellMethod; import org.springframework.shell.standard.ShellOption; +import org.springframework.stereotype.Component; @ShellComponent public class HelpOptionCommands extends BaseE2ECommands { - @Autowired - CommandRegistration.BuilderSupplier builder; + @ShellComponent + public static class LegacyAnnotation extends BaseE2ECommands { - @ShellMethod(key = LEGACY_ANNO + "help-option-default", group = GROUP) - public String testHelpOptionDefault( - @ShellOption(defaultValue = "hi") String arg1 - ) { - return "Hello " + arg1; + @ShellMethod(key = LEGACY_ANNO + "help-option-default", group = GROUP) + public String testHelpOptionDefault( + @ShellOption(defaultValue = "hi") String arg1 + ) { + return "Hello " + arg1; + } } - @Bean - public CommandRegistration testHelpOptionDefaultRegistration() { - return builder.get() - .command(REG, "help-option-default") - .group(GROUP) - .withOption() - .longNames("arg1") - .defaultValue("hi") - .and() - .withTarget() - .function(ctx -> { - String arg1 = ctx.getOptionValue("arg1"); - return "Hello " + arg1; - }) - .and() - .build(); - } + @Component + public static class Registration extends BaseE2ECommands { - // @ShellMethod(key = LEGACY_ANNO + "help-option-exists", group = GROUP) - // public String testHelpOptionExists( - // @ShellOption(defaultValue = "hi") String help - // ) { - // return "Hello " + help; - // } + @Bean + public CommandRegistration testHelpOptionDefaultRegistration() { + return getBuilder() + .command(REG, "help-option-default") + .group(GROUP) + .withOption() + .longNames("arg1") + .defaultValue("hi") + .and() + .withTarget() + .function(ctx -> { + String arg1 = ctx.getOptionValue("arg1"); + return "Hello " + arg1; + }) + .and() + .build(); + } - @Bean - public CommandRegistration testHelpOptionExistsRegistration() { - return builder.get() - .command(REG, "help-option-exists") - .group(GROUP) - .withOption() - .longNames("help") - .defaultValue("hi") - .and() - .withHelpOptions() - .longNames("myhelp") - .and() - .withTarget() - .function(ctx -> { - String arg1 = ctx.getOptionValue("help"); - return "Hello " + arg1; - }) - .and() - .build(); + @Bean + public CommandRegistration testHelpOptionExistsRegistration() { + return getBuilder() + .command(REG, "help-option-exists") + .group(GROUP) + .withOption() + .longNames("help") + .defaultValue("hi") + .and() + .withHelpOptions() + .longNames("myhelp") + .and() + .withTarget() + .function(ctx -> { + String arg1 = ctx.getOptionValue("help"); + return "Hello " + arg1; + }) + .and() + .build(); + } } } diff --git a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/HiddenCommands.java b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/HiddenCommands.java index d7bf164d..d7a9ddaf 100644 --- a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/HiddenCommands.java +++ b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/HiddenCommands.java @@ -17,22 +17,25 @@ package org.springframework.shell.samples.e2e; import org.springframework.context.annotation.Bean; import org.springframework.shell.command.CommandRegistration; -import org.springframework.shell.standard.ShellComponent; +import org.springframework.stereotype.Component; -@ShellComponent -public class HiddenCommands extends BaseE2ECommands { +public class HiddenCommands { - @Bean - public CommandRegistration testHidden1Registration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "hidden-1") - .group(GROUP) - .hidden() - .withTarget() - .function(ctx -> { - return "Hello from hidden command"; - }) - .and() - .build(); + @Component + public static class Registration extends BaseE2ECommands { + + @Bean + public CommandRegistration testHidden1Registration() { + return getBuilder() + .command(REG, "hidden-1") + .group(GROUP) + .hidden() + .withTarget() + .function(ctx -> { + return "Hello from hidden command"; + }) + .and() + .build(); + } } } diff --git a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/InteractiveCompletionCommands.java b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/InteractiveCompletionCommands.java index 3139fb06..fffe9f2d 100644 --- a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/InteractiveCompletionCommands.java +++ b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/InteractiveCompletionCommands.java @@ -27,50 +27,59 @@ import org.springframework.shell.standard.ShellComponent; import org.springframework.shell.standard.ShellMethod; import org.springframework.shell.standard.ShellOption; import org.springframework.shell.standard.ValueProvider; +import org.springframework.stereotype.Component; -@ShellComponent -public class InteractiveCompletionCommands extends BaseE2ECommands { +public class InteractiveCompletionCommands { - @ShellMethod(key = LEGACY_ANNO + "interactive-completion-1", group = GROUP) - public String testInteractiveCompletion1( - @ShellOption(valueProvider = Test1ValuesProvider.class) String arg1, - @ShellOption(valueProvider = Test2ValuesProvider.class) String arg2 - ) { - return "Hello " + arg1; + @ShellComponent + public static class LegacyAnnotation extends BaseE2ECommands { + + @ShellMethod(key = LEGACY_ANNO + "interactive-completion-1", group = GROUP) + public String testInteractiveCompletion1( + @ShellOption(valueProvider = Test1ValuesProvider.class) String arg1, + @ShellOption(valueProvider = Test2ValuesProvider.class) String arg2 + ) { + return "Hello " + arg1; + } + + + @Bean + Test1ValuesProvider test1ValuesProvider() { + return new Test1ValuesProvider(); + } + + @Bean + Test2ValuesProvider test2ValuesProvider() { + return new Test2ValuesProvider(); + } } - @Bean - CommandRegistration testInteractiveCompletion1Registration(CommandRegistration.BuilderSupplier builder) { - Test1ValuesProvider test1ValuesProvider = new Test1ValuesProvider(); - Test2ValuesProvider test2ValuesProvider = new Test2ValuesProvider(); - return builder.get() - .command(REG, "interactive-completion-1") - .group(GROUP) - .withOption() - .longNames("arg1") - .completion(ctx -> test1ValuesProvider.complete(ctx)) - .and() - .withOption() - .longNames("arg2") - .completion(ctx -> test2ValuesProvider.complete(ctx)) - .and() - .withTarget() - .function(ctx -> { - String arg1 = ctx.getOptionValue("arg1"); - return "Hello " + arg1; - }) - .and() - .build(); - } + @Component + public static class Registration extends BaseE2ECommands { - @Bean - Test1ValuesProvider test1ValuesProvider() { - return new Test1ValuesProvider(); - } - - @Bean - Test2ValuesProvider test2ValuesProvider() { - return new Test2ValuesProvider(); + @Bean + CommandRegistration testInteractiveCompletion1Registration() { + Test1ValuesProvider test1ValuesProvider = new Test1ValuesProvider(); + Test2ValuesProvider test2ValuesProvider = new Test2ValuesProvider(); + return getBuilder() + .command(REG, "interactive-completion-1") + .group(GROUP) + .withOption() + .longNames("arg1") + .completion(ctx -> test1ValuesProvider.complete(ctx)) + .and() + .withOption() + .longNames("arg2") + .completion(ctx -> test2ValuesProvider.complete(ctx)) + .and() + .withTarget() + .function(ctx -> { + String arg1 = ctx.getOptionValue("arg1"); + return "Hello " + arg1; + }) + .and() + .build(); + } } static class Test1ValuesProvider implements ValueProvider { diff --git a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/OptionNamingCommands.java b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/OptionNamingCommands.java index 3e2909e7..7faa35a7 100644 --- a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/OptionNamingCommands.java +++ b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/OptionNamingCommands.java @@ -25,7 +25,7 @@ import org.springframework.stereotype.Component; public class OptionNamingCommands { @ShellComponent - public static class OptionNamingCommandsLegacyAnnotation extends BaseE2ECommands { + public static class LegacyAnnotation extends BaseE2ECommands { @ShellMethod(key = LEGACY_ANNO + "option-naming-1", group = GROUP) public void testOptionNaming1Annotation( @@ -39,11 +39,11 @@ public class OptionNamingCommands { } @Component - public static class OptionNamingCommandsRegistration extends BaseE2ECommands { + public static class Registration extends BaseE2ECommands { @Bean - public CommandRegistration testOptionNaming1Registration(CommandRegistration.BuilderSupplier builder) { - return builder.get() + public CommandRegistration testOptionNaming1Registration() { + return getBuilder() .command(REG, "option-naming-1") .group(GROUP) .withOption() diff --git a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/OptionTypeCommands.java b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/OptionTypeCommands.java index f3fe697c..cef52abb 100644 --- a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/OptionTypeCommands.java +++ b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/OptionTypeCommands.java @@ -15,7 +15,6 @@ */ package org.springframework.shell.samples.e2e; -import java.io.PrintWriter; import java.util.Collection; import java.util.List; import java.util.Set; @@ -25,382 +24,317 @@ import org.springframework.shell.command.CommandRegistration; import org.springframework.shell.standard.ShellComponent; import org.springframework.shell.standard.ShellMethod; import org.springframework.shell.standard.ShellOption; +import org.springframework.stereotype.Component; /** * Commands used for e2e test. * * @author Janne Valkealahti */ -@ShellComponent -public class OptionTypeCommands extends BaseE2ECommands { +public class OptionTypeCommands { - @Bean - public CommandRegistration testOptionTypeRegistration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "option-type") - .group(GROUP) - .withOption() - .longNames("arg1") - .and() - .withOption() - .longNames("arg2") - .type(String.class) - .and() - .withOption() - .longNames("arg3") - .type(int.class) - .and() - .withOption() - .longNames("arg4") - .label("MYLABEL") - .and() - .withTarget() - .consumer(ctx -> { - PrintWriter writer = ctx.getTerminal().writer(); - if (ctx.hasMappedOption("arg3")) { - int v = ctx.getOptionValue("arg3"); - writer.append("arg3=" + Integer.toString(v) + "\n"); - } - writer.flush(); - }) - .and() - .build(); + @ShellComponent + public static class LegacyAnnotation extends BaseE2ECommands { + + @ShellMethod(key = LEGACY_ANNO + "option-type-string", group = GROUP) + public String optionTypeStringAnnotation( + @ShellOption(help = "Desc arg1") String arg1 + ) { + return "Hello " + arg1; + } + + @ShellMethod(key = LEGACY_ANNO + "option-type-boolean", group = GROUP) + public String optionTypeBooleanAnnotation( + @ShellOption() boolean arg1, + @ShellOption(defaultValue = "true") boolean arg2, + @ShellOption(defaultValue = "false") boolean arg3, + @ShellOption() Boolean arg4, + @ShellOption(defaultValue = "true") Boolean arg5, + @ShellOption(defaultValue = "false") Boolean arg6 + ) { + return String.format("Hello arg1=%s arg2=%s arg3=%s arg4=%s arg5=%s arg6=%s", arg1, arg2, arg3, arg4, arg5, + arg6); + } + + @ShellMethod(key = LEGACY_ANNO + "option-type-integer", group = GROUP) + public String optionTypeIntegerAnnotation( + @ShellOption int arg1, + @ShellOption Integer arg2 + ) { + return String.format("Hello '%s' '%s'", arg1, arg2); + } + + @ShellMethod(key = LEGACY_ANNO + "option-type-enum", group = GROUP) + public String optionTypeEnumAnnotation( + @ShellOption(help = "Desc arg1") OptionTypeEnum arg1 + ) { + return "Hello " + arg1; + } + + @ShellMethod(key = LEGACY_ANNO + "option-type-string-array", group = GROUP) + public String optionTypeStringArrayAnnotation( + @ShellOption(help = "Desc arg1") String[] arg1 + ) { + return "Hello " + stringOfStrings(arg1); + } + + @ShellMethod(key = LEGACY_ANNO + "option-type-int-array", group = GROUP) + public String optionTypeIntArrayAnnotation( + @ShellOption(help = "Desc arg1") int[] arg1 + ) { + return "Hello " + stringOfInts(arg1); + } + + @ShellMethod(key = LEGACY_ANNO + "option-type-string-list", group = GROUP) + public String optionTypeStringListAnnotation( + @ShellOption(help = "Desc arg1") List arg1 + ) { + return "Hello " + arg1; + } + + @ShellMethod(key = LEGACY_ANNO + "option-type-string-set", group = GROUP) + public String optionTypeStringSetAnnotation( + @ShellOption(help = "Desc arg1") Set arg1 + ) { + return "Hello " + arg1; + } + + @ShellMethod(key = LEGACY_ANNO + "option-type-string-collection", group = GROUP) + public String optionTypeStringCollectionAnnotation( + @ShellOption(help = "Desc arg1") Collection arg1 + ) { + return "Hello " + arg1; + } } - // - // String - // + @Component + public static class Registration extends BaseE2ECommands { - @ShellMethod(key = LEGACY_ANNO + "option-type-string", group = GROUP) - public String optionTypeStringAnnotation( - @ShellOption(help = "Desc arg1") String arg1 - ) { - return "Hello " + arg1; + @Bean + public CommandRegistration optionTypeStringRegistration() { + return getBuilder() + .command(REG, "option-type-string") + .group(GROUP) + .withOption() + .longNames("arg1") + .type(String.class) + .required() + .and() + .withTarget() + .function(ctx -> { + String arg1 = ctx.getOptionValue("arg1"); + return "Hello " + arg1; + }) + .and() + .build(); + } + + @Bean + public CommandRegistration optionTypeBooleanRegistration() { + return getBuilder() + .command(REG, "option-type-boolean") + .group(GROUP) + .withOption() + .longNames("arg1") + .type(boolean.class) + .and() + .withOption() + .longNames("arg2") + .type(boolean.class) + .defaultValue("true") + .and() + .withOption() + .longNames("arg3") + .type(boolean.class) + .defaultValue("false") + .and() + .withOption() + .longNames("arg4") + .type(Boolean.class) + .and() + .withOption() + .longNames("arg5") + .type(Boolean.class) + .defaultValue("true") + .and() + .withOption() + .longNames("arg6") + .type(Boolean.class) + .defaultValue("false") + .and() + .withTarget() + .function(ctx -> { + boolean arg1 = ctx.hasMappedOption("arg1") ? ctx.getOptionValue("arg1") : false; + boolean arg2 = ctx.getOptionValue("arg2"); + boolean arg3 = ctx.getOptionValue("arg3"); + Boolean arg4 = ctx.getOptionValue("arg4"); + Boolean arg5 = ctx.getOptionValue("arg5"); + Boolean arg6 = ctx.getOptionValue("arg6"); + return String.format("Hello arg1=%s arg2=%s arg3=%s arg4=%s arg5=%s arg6=%s", arg1, arg2, arg3, + arg4, arg5, arg6); + }) + .and() + .build(); + } + + @Bean + public CommandRegistration optionTypeIntegerRegistration() { + return getBuilder() + .command(REG, "option-type-integer") + .group(GROUP) + .withOption() + .longNames("arg1") + .type(int.class) + .required() + .and() + .withOption() + .longNames("arg2") + .type(Integer.class) + .required() + .and() + .withTarget() + .function(ctx -> { + int arg1 = ctx.getOptionValue("arg1"); + Integer arg2 = ctx.getOptionValue("arg2"); + return String.format("Hello '%s' '%s'", arg1, arg2); + }) + .and() + .build(); + } + + @Bean + public CommandRegistration optionTypeEnumRegistration() { + return getBuilder() + .command(REG, "option-type-enum") + .group(GROUP) + .withOption() + .longNames("arg1") + .type(OptionTypeEnum.class) + .required() + .and() + .withTarget() + .function(ctx -> { + OptionTypeEnum arg1 = ctx.getOptionValue("arg1"); + return "Hello " + arg1; + }) + .and() + .build(); + } + + @Bean + public CommandRegistration optionTypeStringArrayRegistration() { + return getBuilder() + .command(REG, "option-type-string-array") + .group(GROUP) + .withOption() + .longNames("arg1") + .type(String[].class) + .required() + .and() + .withTarget() + .function(ctx -> { + String[] arg1 = ctx.getOptionValue("arg1"); + return "Hello " + stringOfStrings(arg1); + }) + .and() + .build(); + } + + @Bean + public CommandRegistration optionTypeIntArrayRegistration() { + return getBuilder() + .command(REG, "option-type-int-array") + .group(GROUP) + .withOption() + .longNames("arg1") + .type(int[].class) + .required() + .and() + .withTarget() + .function(ctx -> { + int[] arg1 = ctx.getOptionValue("arg1"); + return "Hello " + stringOfInts(arg1); + }) + .and() + .build(); + } + + @Bean + public CommandRegistration optionTypeStringListRegistration() { + return getBuilder() + .command(REG, "option-type-string-list") + .group(GROUP) + .withOption() + .longNames("arg1") + .type(List.class) + .required() + .and() + .withTarget() + .function(ctx -> { + List arg1 = ctx.getOptionValue("arg1"); + return "Hello " + arg1; + }) + .and() + .build(); + } + + @Bean + public CommandRegistration optionTypeStringSetRegistration() { + return getBuilder() + .command(REG, "option-type-string-set") + .group(GROUP) + .withOption() + .longNames("arg1") + .type(Set.class) + .required() + .and() + .withTarget() + .function(ctx -> { + Set arg1 = ctx.getOptionValue("arg1"); + return "Hello " + arg1; + }) + .and() + .build(); + } + + @Bean + public CommandRegistration optionTypeStringCollectionRegistration() { + return getBuilder() + .command(REG, "option-type-string-collection") + .group(GROUP) + .withOption() + .longNames("arg1") + .type(Collection.class) + .required() + .and() + .withTarget() + .function(ctx -> { + Collection arg1 = ctx.getOptionValue("arg1"); + return "Hello " + arg1; + }) + .and() + .build(); + } + + @Bean + public CommandRegistration optionTypeVoidRegistration() { + return getBuilder() + .command(REG, "option-type-void") + .group(GROUP) + .withOption() + .longNames("arg1") + .type(void.class) + .and() + .withTarget() + .function(ctx -> { + return "Hello "; + }) + .and() + .build(); + } } - @Bean - public CommandRegistration optionTypeStringRegistration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "option-type-string") - .group(GROUP) - .withOption() - .longNames("arg1") - .type(String.class) - .required() - .and() - .withTarget() - .function(ctx -> { - String arg1 = ctx.getOptionValue("arg1"); - return "Hello " + arg1; - }) - .and() - .build(); - } - - // - // Boolean - // - - @ShellMethod(key = LEGACY_ANNO + "option-type-boolean", group = GROUP) - public String optionTypeBooleanAnnotation( - @ShellOption() boolean arg1, - @ShellOption(defaultValue = "true") boolean arg2, - @ShellOption(defaultValue = "false") boolean arg3, - @ShellOption() Boolean arg4, - @ShellOption(defaultValue = "true") Boolean arg5, - @ShellOption(defaultValue = "false") Boolean arg6 - ) { - return String.format("Hello arg1=%s arg2=%s arg3=%s arg4=%s arg5=%s arg6=%s", arg1, arg2, arg3, arg4, arg5, - arg6); - } - - @Bean - public CommandRegistration optionTypeBooleanRegistration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "option-type-boolean") - .group(GROUP) - .withOption() - .longNames("arg1") - .type(boolean.class) - .and() - .withOption() - .longNames("arg2") - .type(boolean.class) - .defaultValue("true") - .and() - .withOption() - .longNames("arg3") - .type(boolean.class) - .defaultValue("false") - .and() - .withOption() - .longNames("arg4") - .type(Boolean.class) - .and() - .withOption() - .longNames("arg5") - .type(Boolean.class) - .defaultValue("true") - .and() - .withOption() - .longNames("arg6") - .type(Boolean.class) - .defaultValue("false") - .and() - .withTarget() - .function(ctx -> { - boolean arg1 = ctx.hasMappedOption("arg1") ? ctx.getOptionValue("arg1") : false; - boolean arg2 = ctx.getOptionValue("arg2"); - boolean arg3 = ctx.getOptionValue("arg3"); - Boolean arg4 = ctx.getOptionValue("arg4"); - Boolean arg5 = ctx.getOptionValue("arg5"); - Boolean arg6 = ctx.getOptionValue("arg6"); - return String.format("Hello arg1=%s arg2=%s arg3=%s arg4=%s arg5=%s arg6=%s", arg1, arg2, arg3, - arg4, arg5, arg6); - }) - .and() - .build(); - } - - // - // Integer - // - - @ShellMethod(key = LEGACY_ANNO + "option-type-integer", group = GROUP) - public String optionTypeIntegerAnnotation( - @ShellOption int arg1, - @ShellOption Integer arg2 - ) { - return String.format("Hello '%s' '%s'", arg1, arg2); - } - - @Bean - public CommandRegistration optionTypeIntegerRegistration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "option-type-integer") - .group(GROUP) - .withOption() - .longNames("arg1") - .type(int.class) - .required() - .and() - .withOption() - .longNames("arg2") - .type(Integer.class) - .required() - .and() - .withTarget() - .function(ctx -> { - int arg1 = ctx.getOptionValue("arg1"); - Integer arg2 = ctx.getOptionValue("arg2"); - return String.format("Hello '%s' '%s'", arg1, arg2); - }) - .and() - .build(); - } - - // - // Enum - // - public static enum OptionTypeEnum { ONE,TWO,THREE } - - @ShellMethod(key = LEGACY_ANNO + "option-type-enum", group = GROUP) - public String optionTypeEnumAnnotation( - @ShellOption(help = "Desc arg1") OptionTypeEnum arg1 - ) { - return "Hello " + arg1; - } - - @Bean - public CommandRegistration optionTypeEnumRegistration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "option-type-enum") - .group(GROUP) - .withOption() - .longNames("arg1") - .type(OptionTypeEnum.class) - .required() - .and() - .withTarget() - .function(ctx -> { - OptionTypeEnum arg1 = ctx.getOptionValue("arg1"); - return "Hello " + arg1; - }) - .and() - .build(); - } - - // - // String[] - // - - @ShellMethod(key = LEGACY_ANNO + "option-type-string-array", group = GROUP) - public String optionTypeStringArrayAnnotation( - @ShellOption(help = "Desc arg1") String[] arg1 - ) { - return "Hello " + stringOfStrings(arg1); - } - - @Bean - public CommandRegistration optionTypeStringArrayRegistration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "option-type-string-array") - .group(GROUP) - .withOption() - .longNames("arg1") - .type(String[].class) - .required() - .and() - .withTarget() - .function(ctx -> { - String[] arg1 = ctx.getOptionValue("arg1"); - return "Hello " + stringOfStrings(arg1); - }) - .and() - .build(); - } - // - // int[] - // - - @ShellMethod(key = LEGACY_ANNO + "option-type-int-array", group = GROUP) - public String optionTypeIntArrayAnnotation( - @ShellOption(help = "Desc arg1") int[] arg1 - ) { - return "Hello " + stringOfInts(arg1); - } - - @Bean - public CommandRegistration optionTypeIntArrayRegistration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "option-type-int-array") - .group(GROUP) - .withOption() - .longNames("arg1") - .type(int[].class) - .required() - .and() - .withTarget() - .function(ctx -> { - int[] arg1 = ctx.getOptionValue("arg1"); - return "Hello " + stringOfInts(arg1); - }) - .and() - .build(); - } - - // - // List - // - - @ShellMethod(key = LEGACY_ANNO + "option-type-string-list", group = GROUP) - public String optionTypeStringListAnnotation( - @ShellOption(help = "Desc arg1") List arg1 - ) { - return "Hello " + arg1; - } - - @Bean - public CommandRegistration optionTypeStringListRegistration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "option-type-string-list") - .group(GROUP) - .withOption() - .longNames("arg1") - .type(List.class) - .required() - .and() - .withTarget() - .function(ctx -> { - List arg1 = ctx.getOptionValue("arg1"); - return "Hello " + arg1; - }) - .and() - .build(); - } - - // - // Set - // - - @ShellMethod(key = LEGACY_ANNO + "option-type-string-set", group = GROUP) - public String optionTypeStringSetAnnotation( - @ShellOption(help = "Desc arg1") Set arg1 - ) { - return "Hello " + arg1; - } - - @Bean - public CommandRegistration optionTypeStringSetRegistration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "option-type-string-set") - .group(GROUP) - .withOption() - .longNames("arg1") - .type(Set.class) - .required() - .and() - .withTarget() - .function(ctx -> { - Set arg1 = ctx.getOptionValue("arg1"); - return "Hello " + arg1; - }) - .and() - .build(); - } - - // - // Collection - // - - @ShellMethod(key = LEGACY_ANNO + "option-type-string-collection", group = GROUP) - public String optionTypeStringCollectionAnnotation( - @ShellOption(help = "Desc arg1") Collection arg1 - ) { - return "Hello " + arg1; - } - - @Bean - public CommandRegistration optionTypeStringCollectionRegistration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "option-type-string-collection") - .group(GROUP) - .withOption() - .longNames("arg1") - .type(Collection.class) - .required() - .and() - .withTarget() - .function(ctx -> { - Collection arg1 = ctx.getOptionValue("arg1"); - return "Hello " + arg1; - }) - .and() - .build(); - } - - // - // Void - // - - @Bean - public CommandRegistration optionTypeVoidRegistration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "option-type-void") - .group(GROUP) - .withOption() - .longNames("arg1") - .type(void.class) - .and() - .withTarget() - .function(ctx -> { - return "Hello "; - }) - .and() - .build(); - } - } diff --git a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/OptionalValueCommands.java b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/OptionalValueCommands.java index 1f48e4cd..7c1d4cea 100644 --- a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/OptionalValueCommands.java +++ b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/OptionalValueCommands.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 the original author or authors. + * Copyright 2022-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,36 +20,44 @@ import org.springframework.shell.command.CommandRegistration; import org.springframework.shell.standard.ShellComponent; import org.springframework.shell.standard.ShellMethod; import org.springframework.shell.standard.ShellOption; +import org.springframework.stereotype.Component; /** * Commands used for e2e test. * * @author Janne Valkealahti */ -@ShellComponent -public class OptionalValueCommands extends BaseE2ECommands { +public class OptionalValueCommands { - @ShellMethod(key = LEGACY_ANNO + "optional-value", group = GROUP) - public String testOptionalValue( - @ShellOption(defaultValue = ShellOption.NULL) String arg1 - ) { - return "Hello " + arg1; + @ShellComponent + public static class LegacyAnnotation extends BaseE2ECommands { + + @ShellMethod(key = LEGACY_ANNO + "optional-value", group = GROUP) + public String testOptionalValue( + @ShellOption(defaultValue = ShellOption.NULL) String arg1 + ) { + return "Hello " + arg1; + } } - @Bean - public CommandRegistration testOptionalValueRegistration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "optional-value") - .group(GROUP) - .withOption() - .longNames("arg1") - .and() - .withTarget() - .function(ctx -> { - String arg1 = ctx.getOptionValue("arg1"); - return "Hello " + arg1; - }) - .and() - .build(); + @Component + public static class Registration extends BaseE2ECommands { + + @Bean + public CommandRegistration testOptionalValueRegistration() { + return getBuilder() + .command(REG, "optional-value") + .group(GROUP) + .withOption() + .longNames("arg1") + .and() + .withTarget() + .function(ctx -> { + String arg1 = ctx.getOptionValue("arg1"); + return "Hello " + arg1; + }) + .and() + .build(); + } } } diff --git a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/RequiredValueCommands.java b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/RequiredValueCommands.java index cf0b3c80..b1dfc8ec 100644 --- a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/RequiredValueCommands.java +++ b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/RequiredValueCommands.java @@ -20,38 +20,46 @@ import org.springframework.shell.command.CommandRegistration; import org.springframework.shell.standard.ShellComponent; import org.springframework.shell.standard.ShellMethod; import org.springframework.shell.standard.ShellOption; +import org.springframework.stereotype.Component; /** * Commands used for e2e test. * * @author Janne Valkealahti */ -@ShellComponent -public class RequiredValueCommands extends BaseE2ECommands { +public class RequiredValueCommands { - @ShellMethod(key = LEGACY_ANNO + "required-value", group = GROUP) - public String testRequiredValueAnnotation( - @ShellOption(help = "Desc arg1") String arg1 - ) { - return "Hello " + arg1; + @ShellComponent + public static class LegacyAnnotation extends BaseE2ECommands { + + @ShellMethod(key = LEGACY_ANNO + "required-value", group = GROUP) + public String testRequiredValueAnnotation( + @ShellOption(help = "Desc arg1") String arg1 + ) { + return "Hello " + arg1; + } } - @Bean - public CommandRegistration testRequiredValueRegistration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "required-value") - .group(GROUP) - .withOption() - .longNames("arg1") - .description("Desc arg1") - .required() - .and() - .withTarget() - .function(ctx -> { - String arg1 = ctx.getOptionValue("arg1"); - return "Hello " + arg1; - }) - .and() - .build(); + @Component + public static class Registration extends BaseE2ECommands { + + @Bean + public CommandRegistration testRequiredValueRegistration() { + return getBuilder() + .command(REG, "required-value") + .group(GROUP) + .withOption() + .longNames("arg1") + .description("Desc arg1") + .required() + .and() + .withTarget() + .function(ctx -> { + String arg1 = ctx.getOptionValue("arg1"); + return "Hello " + arg1; + }) + .and() + .build(); + } } } diff --git a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/UnrecognisedOptionCommands.java b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/UnrecognisedOptionCommands.java index a7ab0b8f..b9915571 100644 --- a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/UnrecognisedOptionCommands.java +++ b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/UnrecognisedOptionCommands.java @@ -20,52 +20,60 @@ import org.springframework.shell.command.CommandRegistration; import org.springframework.shell.standard.ShellComponent; import org.springframework.shell.standard.ShellMethod; import org.springframework.shell.standard.ShellOption; +import org.springframework.stereotype.Component; -@ShellComponent -public class UnrecognisedOptionCommands extends BaseE2ECommands { +public class UnrecognisedOptionCommands { - @ShellMethod(key = LEGACY_ANNO + "unrecognised-option-noother", group = GROUP) - public String testUnrecognisedOptionNoOtherAnnotation( - ) { - return "Hi"; + @ShellComponent + public static class LegacyAnnotation extends BaseE2ECommands { + + @ShellMethod(key = LEGACY_ANNO + "unrecognised-option-noother", group = GROUP) + public String testUnrecognisedOptionNoOtherAnnotation( + ) { + return "Hi"; + } + + @ShellMethod(key = LEGACY_ANNO + "unrecognised-option-withrequired", group = GROUP) + public String testUnrecognisedOptionWithRequiredAnnotation( + @ShellOption(help = "Desc arg1") String arg1 + ) { + return "Hello " + arg1; + } } - @Bean - public CommandRegistration testUnrecognisedOptionNoOtherRegistration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "unrecognised-option-noother") - .group(GROUP) - .withTarget() - .function(ctx -> { - return "Hi"; - }) - .and() - .build(); - } + @Component + public static class Registration extends BaseE2ECommands { - @ShellMethod(key = LEGACY_ANNO + "unrecognised-option-withrequired", group = GROUP) - public String testUnrecognisedOptionWithRequiredAnnotation( - @ShellOption(help = "Desc arg1") String arg1 - ) { - return "Hello " + arg1; - } + @Bean + public CommandRegistration testUnrecognisedOptionNoOtherRegistration() { + return getBuilder() + .command(REG, "unrecognised-option-noother") + .group(GROUP) + .withTarget() + .function(ctx -> { + return "Hi"; + }) + .and() + .build(); + } - @Bean - public CommandRegistration testUnrecognisedOptionWithRequiredRegistration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "unrecognised-option-withrequired") - .group(GROUP) - .withOption() - .longNames("arg1") - .description("Desc arg1") - .required() - .and() - .withTarget() - .function(ctx -> { - String arg1 = ctx.getOptionValue("arg1"); - return "Hello " + arg1; - }) - .and() - .build(); + @Bean + public CommandRegistration testUnrecognisedOptionWithRequiredRegistration() { + return getBuilder() + .command(REG, "unrecognised-option-withrequired") + .group(GROUP) + .withOption() + .longNames("arg1") + .description("Desc arg1") + .required() + .and() + .withTarget() + .function(ctx -> { + String arg1 = ctx.getOptionValue("arg1"); + return "Hello " + arg1; + }) + .and() + .build(); + } } } diff --git a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/ValidatedValueCommands.java b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/ValidatedValueCommands.java index cb0ad878..81118b03 100644 --- a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/ValidatedValueCommands.java +++ b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/ValidatedValueCommands.java @@ -22,45 +22,52 @@ import org.springframework.shell.command.CommandRegistration; import org.springframework.shell.standard.ShellComponent; import org.springframework.shell.standard.ShellMethod; import org.springframework.shell.standard.ShellOption; +import org.springframework.stereotype.Component; /** * Commands used for e2e test. * * @author Janne Valkealahti */ -@ShellComponent -public class ValidatedValueCommands extends BaseE2ECommands { +public class ValidatedValueCommands { - @ShellMethod(key = LEGACY_ANNO + "validated-value", group = GROUP) - public String testValidatedValueAnnotation( - @ShellOption @Min(value = 1) Integer arg1, - @ShellOption @Min(value = 1) Integer arg2 - ) { - return "Hello " + arg1; + @ShellComponent + public static class LegacyAnnotation extends BaseE2ECommands { + + @ShellMethod(key = LEGACY_ANNO + "validated-value", group = GROUP) + public String testValidatedValueAnnotation( + @ShellOption @Min(value = 1) Integer arg1, + @ShellOption @Min(value = 1) Integer arg2 + ) { + return "Hello " + arg1; + } } - @Bean - public CommandRegistration testValidatedValueRegistration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "validated-value") - .group(GROUP) - .withOption() - .longNames("arg1") - .type(Integer.class) - .required() - .and() - .withOption() - .longNames("arg2") - .type(Integer.class) - .required() - .and() - .withTarget() - .function(ctx -> { - Integer arg1 = ctx.getOptionValue("arg1"); - return "Hello " + arg1; - }) - .and() - .build(); - } + @Component + public static class Registration extends BaseE2ECommands { + @Bean + public CommandRegistration testValidatedValueRegistration() { + return getBuilder() + .command(REG, "validated-value") + .group(GROUP) + .withOption() + .longNames("arg1") + .type(Integer.class) + .required() + .and() + .withOption() + .longNames("arg2") + .type(Integer.class) + .required() + .and() + .withTarget() + .function(ctx -> { + Integer arg1 = ctx.getOptionValue("arg1"); + return "Hello " + arg1; + }) + .and() + .build(); + } + } } diff --git a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/WriteCommands.java b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/WriteCommands.java index 2bbd1965..a3901d43 100644 --- a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/WriteCommands.java +++ b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/WriteCommands.java @@ -22,48 +22,56 @@ import org.springframework.context.annotation.Bean; import org.springframework.shell.command.CommandRegistration; import org.springframework.shell.standard.ShellComponent; import org.springframework.shell.standard.ShellMethod; +import org.springframework.stereotype.Component; -@ShellComponent -public class WriteCommands extends BaseE2ECommands { +public class WriteCommands { - @Autowired - Terminal terminal; + @ShellComponent + public static class LegacyAnnotation extends BaseE2ECommands { - @Bean - public CommandRegistration writeTerminalWriterRegistration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "write-terminalwriter") - .group(GROUP) - .withTarget() - .consumer(ctx -> { - ctx.getTerminal().writer().println("hi"); - ctx.getTerminal().writer().flush(); - }) - .and() - .build(); + @Autowired + Terminal terminal; + + @ShellMethod(key = LEGACY_ANNO + "write-terminalwriter", group = GROUP) + public void writeTerminalWriterAnnotation() { + terminal.writer().println("hi"); + terminal.writer().flush(); + } + + @ShellMethod(key = LEGACY_ANNO + "write-systemout", group = GROUP) + public void writeSystemOutAnnotation() { + System.out.println("hi"); + } } - @ShellMethod(key = LEGACY_ANNO + "write-terminalwriter", group = GROUP) - public void writeTerminalWriterAnnotation() { - terminal.writer().println("hi"); - terminal.writer().flush(); - } + @Component + public static class Registration extends BaseE2ECommands { - @Bean - public CommandRegistration writeSystemOutRegistration(CommandRegistration.BuilderSupplier builder) { - return builder.get() - .command(REG, "write-terminalwriter") - .group(GROUP) - .withTarget() - .consumer(ctx -> { - System.out.println("hi"); - }) - .and() - .build(); - } + @Bean + public CommandRegistration writeTerminalWriterRegistration() { + return getBuilder() + .command(REG, "write-terminalwriter") + .group(GROUP) + .withTarget() + .consumer(ctx -> { + ctx.getTerminal().writer().println("hi"); + ctx.getTerminal().writer().flush(); + }) + .and() + .build(); + } - @ShellMethod(key = LEGACY_ANNO + "write-systemout", group = GROUP) - public void writeSystemOutAnnotation() { - System.out.println("hi"); + @Bean + public CommandRegistration writeSystemOutRegistration() { + return getBuilder() + .command(REG, "write-terminalwriter") + .group(GROUP) + .withTarget() + .consumer(ctx -> { + System.out.println("hi"); + }) + .and() + .build(); + } } } diff --git a/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/RequiredValueCommandsTests.java b/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/RequiredValueCommandsTests.java index b02b531b..112c9ee9 100644 --- a/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/RequiredValueCommandsTests.java +++ b/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/RequiredValueCommandsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 the original author or authors. + * Copyright 2022-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,8 +19,12 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import org.springframework.shell.samples.AbstractSampleTests; +import org.springframework.shell.samples.e2e.RequiredValueCommands.LegacyAnnotation; +import org.springframework.shell.samples.e2e.RequiredValueCommands.Registration; import org.springframework.shell.test.ShellTestClient.BaseShellSession; +import org.springframework.test.context.ContextConfiguration; +@ContextConfiguration(classes = {LegacyAnnotation.class, Registration.class}) class RequiredValueCommandsTests extends AbstractSampleTests { @ParameterizedTest