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 fc9f8433..b35edbe5 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 @@ -20,6 +20,8 @@ import java.util.Arrays; import org.springframework.context.annotation.Bean; import org.springframework.shell.command.CommandRegistration; import org.springframework.shell.command.CommandRegistration.OptionArity; +import org.springframework.shell.command.annotation.Command; +import org.springframework.shell.command.annotation.Option; import org.springframework.shell.standard.ShellComponent; import org.springframework.shell.standard.ShellMethod; import org.springframework.shell.standard.ShellOption; @@ -58,6 +60,34 @@ public class ArityCommands { } + @Command(command = BaseE2ECommands.ANNO, group = BaseE2ECommands.GROUP) + public static class Annotation extends BaseE2ECommands { + + @Command(command = "arity-boolean-default-true") + public String testArityBooleanDefaultTrueAnnotation( + @Option(longNames = "overwrite", defaultValue = "true", arity = OptionArity.ZERO_OR_ONE) + Boolean overwrite + ) { + return "Hello " + overwrite; + } + + @Command(command = "arity-string-array") + public String testArityStringArrayAnnotation( + @Option(longNames = "arg1", defaultValue = "true", arity = OptionArity.ZERO_OR_MORE) + String[] arg1 + ) { + return "Hello " + Arrays.asList(arg1); + } + + @Command(command = "arity-float-array") + public String testArityFloatArrayAnnotation( + @Option(longNames = "arg1", defaultValue = "true", arity = OptionArity.ZERO_OR_MORE) + float[] arg1 + ) { + return "Hello " + stringOfFloats(arg1); + } + } + @Component public static class Registration extends BaseE2ECommands { 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 000901ad..a3b4bc30 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 @@ -17,6 +17,8 @@ package org.springframework.shell.samples.e2e; import org.springframework.context.annotation.Bean; import org.springframework.shell.command.CommandRegistration; +import org.springframework.shell.command.annotation.Command; +import org.springframework.shell.command.annotation.Option; import org.springframework.shell.standard.ShellComponent; import org.springframework.shell.standard.ShellMethod; import org.springframework.shell.standard.ShellOption; @@ -62,6 +64,42 @@ public class DefaultValueCommands { } } + @Command(command = BaseE2ECommands.ANNO, group = BaseE2ECommands.GROUP) + public static class Annotation extends BaseE2ECommands { + + @Command(command = "default-value") + public String testDefaultValueAnnotation( + @Option(longNames = "arg1", defaultValue = "hi") + String arg1 + ) { + return "Hello " + arg1; + } + + @Command(command = "default-value-boolean1") + public String testDefaultValueBoolean1Annotation( + @Option(longNames = "arg1", defaultValue = "false") + boolean arg1 + ) { + return "Hello " + arg1; + } + + @Command(command = "default-value-boolean2") + public String testDefaultValueBoolean2Annotation( + @Option(longNames = "arg1", defaultValue = "true") + boolean arg1 + ) { + return "Hello " + arg1; + } + + @Command(command = "default-value-boolean3") + public String testDefaultValueBoolean3Annotation( + @Option(longNames = "arg1") + boolean arg1 + ) { + return "Hello " + arg1; + } + } + @Component public static class Registration extends BaseE2ECommands { 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 8831187e..c3af77ea 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 @@ -167,7 +167,8 @@ public class ErrorHandlingCommands { return CommandHandlingResult.of("Hi, handled custom exception 3\n", 3); } if (e instanceof CustomException4) { - return CommandHandlingResult.of("Hi, handled custom exception\n", 42); + String msg = String.format("Hi, handled custom exception %s\n", e); + return CommandHandlingResult.of(msg, 42); } if (e instanceof IllegalArgumentException) { return CommandHandlingResult.of("Hi, handled illegal exception\n", 42); 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 d7a9ddaf..2a48b203 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,10 +17,21 @@ package org.springframework.shell.samples.e2e; import org.springframework.context.annotation.Bean; import org.springframework.shell.command.CommandRegistration; +import org.springframework.shell.command.annotation.Command; import org.springframework.stereotype.Component; public class HiddenCommands { + @Command(command = BaseE2ECommands.ANNO, group = BaseE2ECommands.GROUP) + public static class Annotation extends BaseE2ECommands { + + @Command(command = "hidden-1", hidden = true) + public String testHidden1Annotation( + ) { + return "Hello from hidden command"; + } + } + @Component public static class Registration extends BaseE2ECommands { 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 7c1d4cea..ff81f011 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 @@ -17,6 +17,8 @@ package org.springframework.shell.samples.e2e; import org.springframework.context.annotation.Bean; import org.springframework.shell.command.CommandRegistration; +import org.springframework.shell.command.annotation.Command; +import org.springframework.shell.command.annotation.Option; import org.springframework.shell.standard.ShellComponent; import org.springframework.shell.standard.ShellMethod; import org.springframework.shell.standard.ShellOption; @@ -40,6 +42,18 @@ public class OptionalValueCommands { } } + @Command(command = BaseE2ECommands.ANNO, group = BaseE2ECommands.GROUP) + public static class Annotation extends BaseE2ECommands { + + @Command(command = "optional-value") + public String testOptionalValueAnnotation( + @Option(longNames = "arg1") + String arg1 + ) { + return "Hello " + arg1; + } + } + @Component public static class Registration extends BaseE2ECommands { 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 d6509db2..53f79474 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 @@ -41,6 +41,7 @@ public class RequiredValueCommands { return "Hello " + arg1; } } + @Command(command = BaseE2ECommands.ANNO, group = BaseE2ECommands.GROUP) public static class Annotation extends BaseE2ECommands { diff --git a/spring-shell-samples/src/test/java/org/springframework/shell/samples/AbstractSampleTests.java b/spring-shell-samples/src/test/java/org/springframework/shell/samples/AbstractSampleTests.java index 2db9f4fe..3c7484ac 100644 --- a/spring-shell-samples/src/test/java/org/springframework/shell/samples/AbstractSampleTests.java +++ b/spring-shell-samples/src/test/java/org/springframework/shell/samples/AbstractSampleTests.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. @@ -15,8 +15,11 @@ */ package org.springframework.shell.samples; +import java.util.List; import java.util.concurrent.TimeUnit; +import org.assertj.core.api.Condition; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Import; import org.springframework.shell.samples.standard.ResolvedCommands; @@ -29,9 +32,10 @@ import org.springframework.shell.test.autoconfigure.ShellTest; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext.ClassMode; +import static org.assertj.core.api.Assertions.assertThat; import static org.awaitility.Awaitility.await; -@ShellTest +@ShellTest(terminalWidth = 120) @Import(ResolvedCommands.ResolvedCommandsConfiguration.class) @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) public class AbstractSampleTests { @@ -45,6 +49,17 @@ public class AbstractSampleTests { }); } + protected void assertScreenNotContainsText(BaseShellSession session, String textFound, String textNotFound) { + Condition notCondition = new Condition<>(line -> line.contains(textNotFound), + String.format("Text '%s' not found", textNotFound)); + + await().atMost(2, TimeUnit.SECONDS).untilAsserted(() -> { + ShellAssertions.assertThat(session.screen()).containsText(textFound); + List lines = session.screen().lines(); + assertThat(lines).areNot(notCondition); + }); + } + protected BaseShellSession createSession(String command, boolean interactive) { if (interactive) { InteractiveShellSession session = client.interactive().run(); diff --git a/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/AliasCommandsTests.java b/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/AliasCommandsTests.java new file mode 100644 index 00000000..4698b278 --- /dev/null +++ b/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/AliasCommandsTests.java @@ -0,0 +1,44 @@ +/* + * Copyright 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. + * 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.junit.jupiter.params.ParameterizedTest; + +import org.springframework.shell.command.annotation.EnableCommand; +import org.springframework.shell.samples.AbstractSampleTests; +import org.springframework.shell.samples.e2e.AliasCommands.AliasCommandsAnnotation; +import org.springframework.shell.samples.e2e.AliasCommands.AliasCommandsRegistration; +import org.springframework.shell.test.ShellTestClient.BaseShellSession; +import org.springframework.test.context.ContextConfiguration; + +@ContextConfiguration(classes = { AliasCommandsRegistration.class }) +@EnableCommand(AliasCommandsAnnotation.class) +public class AliasCommandsTests extends AbstractSampleTests { + + @ParameterizedTest + @E2ESource(command = "alias-1", anno = false) + void mainCommandWorks(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "Hello from alias command"); + } + + @ParameterizedTest + @E2ESource(command = "aliasfor-1", anno = false) + void aliasCommandWorks(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "Hello from alias command"); + } +} diff --git a/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/ArityCommandsTests.java b/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/ArityCommandsTests.java new file mode 100644 index 00000000..ee207a39 --- /dev/null +++ b/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/ArityCommandsTests.java @@ -0,0 +1,59 @@ +/* + * Copyright 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. + * 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.junit.jupiter.params.ParameterizedTest; + +import org.springframework.shell.command.annotation.EnableCommand; +import org.springframework.shell.samples.AbstractSampleTests; +import org.springframework.shell.samples.e2e.ArityCommands.Annotation; +import org.springframework.shell.samples.e2e.ArityCommands.LegacyAnnotation; +import org.springframework.shell.samples.e2e.ArityCommands.Registration; +import org.springframework.shell.test.ShellTestClient.BaseShellSession; +import org.springframework.test.context.ContextConfiguration; + +@ContextConfiguration(classes = { LegacyAnnotation.class, Registration.class }) +@EnableCommand(Annotation.class) +class ArityCommandsTests extends AbstractSampleTests { + + @ParameterizedTest + @E2ESource(command = "arity-boolean-default-true") + void defaultBooleanValue(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "Hello true"); + } + + @ParameterizedTest + @E2ESource(command = "arity-boolean-default-true --overwrite false") + void defaultBooleanValueOverwrite(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "Hello false"); + } + + @ParameterizedTest + @E2ESource(command = "arity-string-array --arg1 foo bar") + void arityStringArray(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "Hello [foo, bar]"); + } + + @ParameterizedTest + @E2ESource(command = "arity-float-array --arg1 1 2") + void arityFloatArray(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "Hello [1.0,2.0]"); + } +} diff --git a/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/DefaultValueCommandsTests.java b/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/DefaultValueCommandsTests.java new file mode 100644 index 00000000..87f49382 --- /dev/null +++ b/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/DefaultValueCommandsTests.java @@ -0,0 +1,59 @@ +/* + * Copyright 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. + * 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.junit.jupiter.params.ParameterizedTest; + +import org.springframework.shell.command.annotation.EnableCommand; +import org.springframework.shell.samples.AbstractSampleTests; +import org.springframework.shell.samples.e2e.DefaultValueCommands.Annotation; +import org.springframework.shell.samples.e2e.DefaultValueCommands.LegacyAnnotation; +import org.springframework.shell.samples.e2e.DefaultValueCommands.Registration; +import org.springframework.shell.test.ShellTestClient.BaseShellSession; +import org.springframework.test.context.ContextConfiguration; + +@ContextConfiguration(classes = { LegacyAnnotation.class, Registration.class }) +@EnableCommand(Annotation.class) +class DefaultValueCommandsTests extends AbstractSampleTests { + + @ParameterizedTest + @E2ESource(command = "default-value") + void defaultValue(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "Hello hi"); + } + + @ParameterizedTest + @E2ESource(command = "default-value-boolean1") + void defaultValueBoolean1(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "Hello false"); + } + + @ParameterizedTest + @E2ESource(command = "default-value-boolean2") + void defaultValueBoolean2(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "Hello true"); + } + + @ParameterizedTest + @E2ESource(command = "default-value-boolean3") + void defaultValueBoolean3(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "Hello false"); + } +} diff --git a/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/E2EArgumentsProvider.java b/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/E2EArgumentsProvider.java new file mode 100644 index 00000000..7bc1453b --- /dev/null +++ b/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/E2EArgumentsProvider.java @@ -0,0 +1,57 @@ +/* + * Copyright 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. + * 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 java.util.ArrayList; +import java.util.List; +import java.util.stream.Stream; + +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.support.AnnotationConsumer; + +class E2EArgumentsProvider implements ArgumentsProvider, AnnotationConsumer { + + private E2ESource annotation; + + @Override + public void accept(E2ESource annotation) { + this.annotation = annotation; + } + + @Override + public Stream provideArguments(ExtensionContext context) throws Exception { + String command = this.annotation.command(); + List arguments = new ArrayList<>(); + boolean anno = this.annotation.anno(); + boolean annox = this.annotation.annox(); + boolean reg = this.annotation.reg(); + if (anno) { + arguments.add(Arguments.of(String.format("e2e %s %s", "anno", command), false)); + arguments.add(Arguments.of(String.format("e2e %s %s", "anno", command), true)); + } + if (annox) { + arguments.add(Arguments.of(String.format("e2e %s %s", "annox", command), false)); + arguments.add(Arguments.of(String.format("e2e %s %s", "annox", command), true)); + } + if (reg) { + arguments.add(Arguments.of(String.format("e2e %s %s", "reg", command), false)); + arguments.add(Arguments.of(String.format("e2e %s %s", "reg", command), true)); + } + return arguments.stream(); + } +} diff --git a/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/E2ESource.java b/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/E2ESource.java new file mode 100644 index 00000000..5c43ca5b --- /dev/null +++ b/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/E2ESource.java @@ -0,0 +1,44 @@ +/* + * Copyright 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. + * 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 java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.junit.jupiter.params.provider.ArgumentsSource; + +/** + * JUnit source creating a matrix for command combinations. + * + * @author Janne Valkealahti + */ +@Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD }) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@ArgumentsSource(E2EArgumentsProvider.class) +@interface E2ESource { + + String command(); + + boolean anno() default true; + + boolean annox() default true; + + boolean reg() default true; +} diff --git a/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/ErrorHandlingCommandsTests.java b/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/ErrorHandlingCommandsTests.java new file mode 100644 index 00000000..a9900c35 --- /dev/null +++ b/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/ErrorHandlingCommandsTests.java @@ -0,0 +1,73 @@ +/* + * Copyright 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. + * 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.junit.jupiter.api.Disabled; +import org.junit.jupiter.params.ParameterizedTest; + +import org.springframework.shell.samples.AbstractSampleTests; +import org.springframework.shell.samples.e2e.ErrorHandlingCommands.LegacyAnnotation; +import org.springframework.shell.samples.e2e.ErrorHandlingCommands.Registration; +import org.springframework.shell.test.ShellTestClient.BaseShellSession; +import org.springframework.test.context.ContextConfiguration; + +@ContextConfiguration(classes = { LegacyAnnotation.class, Registration.class }) +class ErrorHandlingCommandsTests extends AbstractSampleTests { + + @ParameterizedTest + @E2ESource(command = "error-handling --arg1 throw1", annox = false) + void testErrorHandling1(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "Hi, handled custom exception"); + } + + @Disabled("trouble with spring-shell-test") + @ParameterizedTest + @E2ESource(command = "error-handling --arg1 throw2", annox = false) + void testErrorHandling2(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "org.springframework.shell.samples.e2e.ErrorHandlingCommands$CustomException2"); + } + + @Disabled("trouble with spring-shell-test") + @ParameterizedTest + @E2ESource(command = "error-handling --arg1 throw3", annox = false) + void testErrorHandling3(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "java.lang.RuntimeException"); + } + + @ParameterizedTest + @E2ESource(command = "error-handling --arg1 throw4", annox = false) + void testErrorHandling4(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "Hi, handled illegal exception"); + } + + @ParameterizedTest + @E2ESource(command = "error-handling --arg1 throw5", annox = false) + void testErrorHandling5(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "Hi, handled custom exception 3"); + } + + @ParameterizedTest + @E2ESource(command = "error-handling --arg1 throw6", annox = false) + void testErrorHandling6(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "Hi, handled custom exception org.springframework.shell.samples.e2e.ErrorHandlingCommands$CustomException4"); + } +} diff --git a/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/HelpOptionCommandsTests.java b/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/HelpOptionCommandsTests.java new file mode 100644 index 00000000..8542cc42 --- /dev/null +++ b/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/HelpOptionCommandsTests.java @@ -0,0 +1,49 @@ +/* + * Copyright 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. + * 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.junit.jupiter.params.ParameterizedTest; + +import org.springframework.shell.samples.AbstractSampleTests; +import org.springframework.shell.samples.e2e.HelpOptionCommands.LegacyAnnotation; +import org.springframework.shell.samples.e2e.HelpOptionCommands.Registration; +import org.springframework.shell.test.ShellTestClient.BaseShellSession; +import org.springframework.test.context.ContextConfiguration; + +@ContextConfiguration(classes = { LegacyAnnotation.class, Registration.class }) +class HelpOptionCommandsTests extends AbstractSampleTests { + + @ParameterizedTest + @E2ESource(command = "help-option-default -h", annox = false) + void testHelpOptionDefault(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "NAME"); + } + + @ParameterizedTest + @E2ESource(command = "help-option-exists --help hi", annox = false, anno = false) + void testHelpOptionExists1(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "Hello hi"); + } + + @ParameterizedTest + @E2ESource(command = "help-option-exists --myhelp", annox = false, anno = false) + void testHelpOptionExists2(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "NAME"); + } +} diff --git a/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/HiddenCommandsTests.java b/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/HiddenCommandsTests.java new file mode 100644 index 00000000..ed6e3050 --- /dev/null +++ b/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/HiddenCommandsTests.java @@ -0,0 +1,44 @@ +/* + * Copyright 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. + * 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.junit.jupiter.params.ParameterizedTest; + +import org.springframework.shell.command.annotation.EnableCommand; +import org.springframework.shell.samples.AbstractSampleTests; +import org.springframework.shell.samples.e2e.HiddenCommands.Annotation; +import org.springframework.shell.samples.e2e.HiddenCommands.Registration; +import org.springframework.shell.test.ShellTestClient.BaseShellSession; +import org.springframework.test.context.ContextConfiguration; + +@ContextConfiguration(classes = { Registration.class }) +@EnableCommand(Annotation.class) +class HiddenCommandsTests extends AbstractSampleTests { + + @ParameterizedTest + @E2ESource(command = "hidden-1", anno = false) + void hiddenCommandExecutes(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "Hello from hidden command"); + } + + @ParameterizedTest + @E2ESource(command = "help", anno = false) + void hiddenNotVisibleInHelp(String command, boolean interactive) { + BaseShellSession session = createSession("help", interactive); + assertScreenNotContainsText(session, "help", "hidden-1"); + } +} diff --git a/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/OptionTypeCommandsTests.java b/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/OptionTypeCommandsTests.java new file mode 100644 index 00000000..a1edf79c --- /dev/null +++ b/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/OptionTypeCommandsTests.java @@ -0,0 +1,98 @@ +/* + * Copyright 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. + * 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.junit.jupiter.params.ParameterizedTest; + +import org.springframework.shell.samples.AbstractSampleTests; +import org.springframework.shell.samples.e2e.OptionTypeCommands.LegacyAnnotation; +import org.springframework.shell.samples.e2e.OptionTypeCommands.Registration; +import org.springframework.shell.test.ShellTestClient.BaseShellSession; +import org.springframework.test.context.ContextConfiguration; + +@ContextConfiguration(classes = { LegacyAnnotation.class, Registration.class }) +class OptionTypeCommandsTests extends AbstractSampleTests { + + @ParameterizedTest + @E2ESource(command = "option-type-string --arg1 hi", annox = false) + void optionTypeString(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "Hello hi"); + } + + @ParameterizedTest + @E2ESource(command = "option-type-boolean", annox = false, reg = false) + void optionTypeBooleanWithAnno(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "Hello arg1=false arg2=true arg3=false arg4=false arg5=true arg6=false"); + } + + @ParameterizedTest + @E2ESource(command = "option-type-boolean", annox = false, anno = false) + void optionTypeBooleanWithReg(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "Hello arg1=false arg2=true arg3=false arg4=null arg5=true arg6=false"); + } + + @ParameterizedTest + @E2ESource(command = "option-type-integer --arg1 1 --arg2 2", annox = false) + void optionTypeInteger(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "Hello '1' '2'"); + } + + @ParameterizedTest + @E2ESource(command = "option-type-enum --arg1 ONE", annox = false) + void optionTypeEnum(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "Hello ONE"); + } + + @ParameterizedTest + @E2ESource(command = "option-type-string-array --arg1 one two", annox = false) + void optionTypeStringArray(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "Hello [one,two]"); + } + + @ParameterizedTest + @E2ESource(command = "option-type-int-array --arg1 1 2", annox = false) + void optionTypeIntArray(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "Hello [1,2]"); + } + + @ParameterizedTest + @E2ESource(command = "option-type-string-list --arg1 one two", annox = false) + void optionTypeStringList(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "Hello [one, two]"); + } + + @ParameterizedTest + @E2ESource(command = "option-type-string-set --arg1 one two", annox = false) + void optionTypeStringSet(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "Hello [one, two]"); + } + + @ParameterizedTest + @E2ESource(command = "option-type-string-collection --arg1 one two", annox = false) + void optionTypeStringCollection(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "Hello [one, two]"); + } +} diff --git a/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/OptionalValueCommandsTests.java b/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/OptionalValueCommandsTests.java new file mode 100644 index 00000000..0d2f695d --- /dev/null +++ b/spring-shell-samples/src/test/java/org/springframework/shell/samples/e2e/OptionalValueCommandsTests.java @@ -0,0 +1,38 @@ +/* + * Copyright 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. + * 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.junit.jupiter.params.ParameterizedTest; + +import org.springframework.shell.command.annotation.EnableCommand; +import org.springframework.shell.samples.AbstractSampleTests; +import org.springframework.shell.samples.e2e.OptionalValueCommands.Annotation; +import org.springframework.shell.samples.e2e.OptionalValueCommands.LegacyAnnotation; +import org.springframework.shell.samples.e2e.OptionalValueCommands.Registration; +import org.springframework.shell.test.ShellTestClient.BaseShellSession; +import org.springframework.test.context.ContextConfiguration; + +@ContextConfiguration(classes = { LegacyAnnotation.class, Registration.class }) +@EnableCommand(Annotation.class) +class OptionalValueCommandsTests extends AbstractSampleTests { + + @ParameterizedTest + @E2ESource(command = "optional-value") + void optionalValueResolvesToNull(String command, boolean interactive) { + BaseShellSession session = createSession(command, interactive); + assertScreenContainsText(session, "Hello null"); + } +} 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 f0ac0c65..dd2ad721 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 @@ -16,7 +16,6 @@ package org.springframework.shell.samples.e2e; import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvSource; import org.springframework.shell.command.annotation.EnableCommand; import org.springframework.shell.samples.AbstractSampleTests; @@ -26,19 +25,12 @@ 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}) +@ContextConfiguration(classes = { LegacyAnnotation.class, Registration.class }) @EnableCommand(Annotation.class) class RequiredValueCommandsTests extends AbstractSampleTests { @ParameterizedTest - @CsvSource({ - "e2e anno required-value,false", - "e2e annox required-value,false", - "e2e reg required-value,false", - "e2e anno required-value,true", - "e2e annox required-value,true", - "e2e reg required-value,true" - }) + @E2ESource(command = "required-value") void shouldRequireOption(String command, boolean interactive) { BaseShellSession session = createSession(command, interactive); assertScreenContainsText(session, "Missing mandatory option");