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 new file mode 100644 index 00000000..4eda48d4 --- /dev/null +++ b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/ArityCommands.java @@ -0,0 +1,59 @@ +/* + * Copyright 2022 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.shell.samples.e2e; + +import org.springframework.context.annotation.Bean; +import org.springframework.shell.command.CommandRegistration; +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; + +/** + * Commands used for e2e test. + * + * @author Janne Valkealahti + */ +@ShellComponent +public class ArityCommands extends BaseE2ECommands { + + @ShellMethod(key = LEGACY_ANNO + "boolean-arity1-default-true", group = GROUP) + public String testBooleanArity1DefaultTrue( + @ShellOption(value = "--overwrite", arity = 1, defaultValue = "true") Boolean overwrite + ) { + return "Hello " + overwrite; + } + + @Bean + public CommandRegistration testBooleanArity1DefaultTrueRegistration() { + return CommandRegistration.builder() + .command(REG, "boolean-arity1-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(); + } +} 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 new file mode 100644 index 00000000..5bfe2102 --- /dev/null +++ b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/BaseE2ECommands.java @@ -0,0 +1,28 @@ +/* + * Copyright 2022 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.shell.samples.e2e; + +/** + * Base class for all e2e commands. + * + * @author Janne Valkealahti + */ +abstract class BaseE2ECommands { + + static final String GROUP = "E2E Commands"; + static final String REG = "e2e reg"; + static final String LEGACY_ANNO = "e2e anno "; +} 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 new file mode 100644 index 00000000..242e692b --- /dev/null +++ b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/DefaultValueCommands.java @@ -0,0 +1,56 @@ +/* + * Copyright 2022 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +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.shell.standard.ShellMethod; +import org.springframework.shell.standard.ShellOption; + +/** + * Commands used for e2e test. + * + * @author Janne Valkealahti + */ +@ShellComponent +public class DefaultValueCommands extends BaseE2ECommands { + + @ShellMethod(key = LEGACY_ANNO + "default-value", group = GROUP) + public String testDefaultValue( + @ShellOption(defaultValue = "hi") String arg1 + ) { + return "Hello " + arg1; + } + + @Bean + public CommandRegistration testDefaultValueRegistration() { + return CommandRegistration.builder() + .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(); + } +} 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 new file mode 100644 index 00000000..67fb28b3 --- /dev/null +++ b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/ExitCodeCommands.java @@ -0,0 +1,67 @@ +/* + * Copyright 2022 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.shell.samples.e2e; + +import org.springframework.context.annotation.Bean; +import org.springframework.shell.command.CommandRegistration; +import org.springframework.shell.standard.ShellComponent; + +/** + * Commands used for e2e test. + * + * @author Janne Valkealahti + */ +@ShellComponent +public class ExitCodeCommands extends BaseE2ECommands { + + @Bean + public CommandRegistration testExitCodeRegistration() { + return CommandRegistration.builder() + .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; + }) + .and() + .build(); + } + + static class MyException extends RuntimeException { + + MyException(String msg) { + super(msg); + } + } +} 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 new file mode 100644 index 00000000..cf9fe952 --- /dev/null +++ b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/OptionalValueCommands.java @@ -0,0 +1,55 @@ +/* + * Copyright 2022 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +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.shell.standard.ShellMethod; +import org.springframework.shell.standard.ShellOption; + +/** + * Commands used for e2e test. + * + * @author Janne Valkealahti + */ +@ShellComponent +public class OptionalValueCommands 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() { + return CommandRegistration.builder() + .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 new file mode 100644 index 00000000..0d7c6095 --- /dev/null +++ b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/RequiredValueCommands.java @@ -0,0 +1,56 @@ +/* + * Copyright 2022 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +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.shell.standard.ShellMethod; +import org.springframework.shell.standard.ShellOption; + +/** + * Commands used for e2e test. + * + * @author Janne Valkealahti + */ +@ShellComponent +public class RequiredValueCommands extends BaseE2ECommands { + + @ShellMethod(key = LEGACY_ANNO + "required-value", group = GROUP) + public String testRequiredValueAnnotation( + @ShellOption String arg1 + ) { + return "Hello " + arg1; + } + + @Bean + public CommandRegistration testRequiredValueRegistration() { + return CommandRegistration.builder() + .command(REG, "required-value") + .group(GROUP) + .withOption() + .longNames("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/standard/E2ECommands.java b/spring-shell-samples/src/main/java/org/springframework/shell/samples/standard/E2ECommands.java deleted file mode 100644 index 42e4b229..00000000 --- a/spring-shell-samples/src/main/java/org/springframework/shell/samples/standard/E2ECommands.java +++ /dev/null @@ -1,146 +0,0 @@ -/* - * Copyright 2022 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. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.shell.samples.standard; - -import org.springframework.context.annotation.Bean; -import org.springframework.shell.command.CommandRegistration; -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; - -/** - * Commands used for e2e test. - * - * @author Janne Valkealahti - */ -@ShellComponent -public class E2ECommands { - - @ShellMethod(key = "e2e anno optional-value") - public String testOptionalValue( - @ShellOption(defaultValue = ShellOption.NULL) String arg1 - ) { - return "Hello " + arg1; - } - - @Bean - public CommandRegistration testOptionalValueRegistration() { - return CommandRegistration.builder() - .command("e2e", "reg", "optional-value") - .group("E2E Commands") - .withOption() - .longNames("arg1") - .and() - .withTarget() - .function(ctx -> { - String arg1 = ctx.getOptionValue("arg1"); - return "Hello " + arg1; - }) - .and() - .build(); - } - - @ShellMethod(key = "e2e anno default-value") - public String testDefaultValue( - @ShellOption(defaultValue = "hi") String arg1 - ) { - return "Hello " + arg1; - } - - @Bean - public CommandRegistration testDefaultValueRegistration() { - return CommandRegistration.builder() - .command("e2e", "reg", "default-value") - .group("E2E Commands") - .withOption() - .longNames("arg1") - .defaultValue("hi") - .and() - .withTarget() - .function(ctx -> { - String arg1 = ctx.getOptionValue("arg1"); - return "Hello " + arg1; - }) - .and() - .build(); - } - - @ShellMethod(key = "e2e anno boolean-arity1-default-true") - public String testBooleanArity1DefaultTrue( - @ShellOption(value = "--overwrite", arity = 1, defaultValue = "true") Boolean overwrite - ) { - return "Hello " + overwrite; - } - - @Bean - public CommandRegistration testBooleanArity1DefaultTrueRegistration() { - return CommandRegistration.builder() - .command("e2e", "reg", "boolean-arity1-default-true") - .group("E2E Commands") - .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 testExitCodeRegistration() { - return CommandRegistration.builder() - .command("e2e", "reg", "exit-code") - .group("E2E Commands") - .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; - }) - .and() - .build(); - } - - static class MyException extends RuntimeException { - - MyException(String msg) { - super(msg); - } - } -}