Skip to content

Commit 006ed5e

Browse files
committed
Better e2e tests coverage
- Add integration tests for e2e samples. - Add some missing commands for new annotation system. - Fixes #654
1 parent a95e60d commit 006ed5e

18 files changed

+680
-13
lines changed

spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/ArityCommands.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.springframework.context.annotation.Bean;
2121
import org.springframework.shell.command.CommandRegistration;
2222
import org.springframework.shell.command.CommandRegistration.OptionArity;
23+
import org.springframework.shell.command.annotation.Command;
24+
import org.springframework.shell.command.annotation.Option;
2325
import org.springframework.shell.standard.ShellComponent;
2426
import org.springframework.shell.standard.ShellMethod;
2527
import org.springframework.shell.standard.ShellOption;
@@ -58,6 +60,34 @@ public String testArityFloatArrayLegacyAnnotation(
5860

5961
}
6062

63+
@Command(command = BaseE2ECommands.ANNO, group = BaseE2ECommands.GROUP)
64+
public static class Annotation extends BaseE2ECommands {
65+
66+
@Command(command = "arity-boolean-default-true")
67+
public String testArityBooleanDefaultTrueAnnotation(
68+
@Option(longNames = "overwrite", defaultValue = "true", arity = OptionArity.ZERO_OR_ONE)
69+
Boolean overwrite
70+
) {
71+
return "Hello " + overwrite;
72+
}
73+
74+
@Command(command = "arity-string-array")
75+
public String testArityStringArrayAnnotation(
76+
@Option(longNames = "arg1", defaultValue = "true", arity = OptionArity.ZERO_OR_MORE)
77+
String[] arg1
78+
) {
79+
return "Hello " + Arrays.asList(arg1);
80+
}
81+
82+
@Command(command = "arity-float-array")
83+
public String testArityFloatArrayAnnotation(
84+
@Option(longNames = "arg1", defaultValue = "true", arity = OptionArity.ZERO_OR_MORE)
85+
float[] arg1
86+
) {
87+
return "Hello " + stringOfFloats(arg1);
88+
}
89+
}
90+
6191
@Component
6292
public static class Registration extends BaseE2ECommands {
6393

spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/DefaultValueCommands.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import org.springframework.context.annotation.Bean;
1919
import org.springframework.shell.command.CommandRegistration;
20+
import org.springframework.shell.command.annotation.Command;
21+
import org.springframework.shell.command.annotation.Option;
2022
import org.springframework.shell.standard.ShellComponent;
2123
import org.springframework.shell.standard.ShellMethod;
2224
import org.springframework.shell.standard.ShellOption;
@@ -62,6 +64,42 @@ public String testDefaultValueBoolean3(
6264
}
6365
}
6466

67+
@Command(command = BaseE2ECommands.ANNO, group = BaseE2ECommands.GROUP)
68+
public static class Annotation extends BaseE2ECommands {
69+
70+
@Command(command = "default-value")
71+
public String testDefaultValueAnnotation(
72+
@Option(longNames = "arg1", defaultValue = "hi")
73+
String arg1
74+
) {
75+
return "Hello " + arg1;
76+
}
77+
78+
@Command(command = "default-value-boolean1")
79+
public String testDefaultValueBoolean1Annotation(
80+
@Option(longNames = "arg1", defaultValue = "false")
81+
boolean arg1
82+
) {
83+
return "Hello " + arg1;
84+
}
85+
86+
@Command(command = "default-value-boolean2")
87+
public String testDefaultValueBoolean2Annotation(
88+
@Option(longNames = "arg1", defaultValue = "true")
89+
boolean arg1
90+
) {
91+
return "Hello " + arg1;
92+
}
93+
94+
@Command(command = "default-value-boolean3")
95+
public String testDefaultValueBoolean3Annotation(
96+
@Option(longNames = "arg1")
97+
boolean arg1
98+
) {
99+
return "Hello " + arg1;
100+
}
101+
}
102+
65103
@Component
66104
public static class Registration extends BaseE2ECommands {
67105

spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/ErrorHandlingCommands.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ public CommandHandlingResult resolve(Exception e) {
167167
return CommandHandlingResult.of("Hi, handled custom exception 3\n", 3);
168168
}
169169
if (e instanceof CustomException4) {
170-
return CommandHandlingResult.of("Hi, handled custom exception\n", 42);
170+
String msg = String.format("Hi, handled custom exception %s\n", e);
171+
return CommandHandlingResult.of(msg, 42);
171172
}
172173
if (e instanceof IllegalArgumentException) {
173174
return CommandHandlingResult.of("Hi, handled illegal exception\n", 42);

spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/HiddenCommands.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,21 @@
1717

1818
import org.springframework.context.annotation.Bean;
1919
import org.springframework.shell.command.CommandRegistration;
20+
import org.springframework.shell.command.annotation.Command;
2021
import org.springframework.stereotype.Component;
2122

2223
public class HiddenCommands {
2324

25+
@Command(command = BaseE2ECommands.ANNO, group = BaseE2ECommands.GROUP)
26+
public static class Annotation extends BaseE2ECommands {
27+
28+
@Command(command = "hidden-1", hidden = true)
29+
public String testHidden1Annotation(
30+
) {
31+
return "Hello from hidden command";
32+
}
33+
}
34+
2435
@Component
2536
public static class Registration extends BaseE2ECommands {
2637

spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/OptionalValueCommands.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import org.springframework.context.annotation.Bean;
1919
import org.springframework.shell.command.CommandRegistration;
20+
import org.springframework.shell.command.annotation.Command;
21+
import org.springframework.shell.command.annotation.Option;
2022
import org.springframework.shell.standard.ShellComponent;
2123
import org.springframework.shell.standard.ShellMethod;
2224
import org.springframework.shell.standard.ShellOption;
@@ -40,6 +42,18 @@ public String testOptionalValue(
4042
}
4143
}
4244

45+
@Command(command = BaseE2ECommands.ANNO, group = BaseE2ECommands.GROUP)
46+
public static class Annotation extends BaseE2ECommands {
47+
48+
@Command(command = "optional-value")
49+
public String testOptionalValueAnnotation(
50+
@Option(longNames = "arg1")
51+
String arg1
52+
) {
53+
return "Hello " + arg1;
54+
}
55+
}
56+
4357
@Component
4458
public static class Registration extends BaseE2ECommands {
4559

spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/RequiredValueCommands.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public String testRequiredValueAnnotation(
4141
return "Hello " + arg1;
4242
}
4343
}
44+
4445
@Command(command = BaseE2ECommands.ANNO, group = BaseE2ECommands.GROUP)
4546
public static class Annotation extends BaseE2ECommands {
4647

spring-shell-samples/src/test/java/org/springframework/shell/samples/AbstractSampleTests.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,8 +15,11 @@
1515
*/
1616
package org.springframework.shell.samples;
1717

18+
import java.util.List;
1819
import java.util.concurrent.TimeUnit;
1920

21+
import org.assertj.core.api.Condition;
22+
2023
import org.springframework.beans.factory.annotation.Autowired;
2124
import org.springframework.context.annotation.Import;
2225
import org.springframework.shell.samples.standard.ResolvedCommands;
@@ -29,9 +32,10 @@
2932
import org.springframework.test.annotation.DirtiesContext;
3033
import org.springframework.test.annotation.DirtiesContext.ClassMode;
3134

35+
import static org.assertj.core.api.Assertions.assertThat;
3236
import static org.awaitility.Awaitility.await;
3337

34-
@ShellTest
38+
@ShellTest(terminalWidth = 120)
3539
@Import(ResolvedCommands.ResolvedCommandsConfiguration.class)
3640
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
3741
public class AbstractSampleTests {
@@ -45,6 +49,17 @@ protected void assertScreenContainsText(BaseShellSession<?> session, String text
4549
});
4650
}
4751

52+
protected void assertScreenNotContainsText(BaseShellSession<?> session, String textFound, String textNotFound) {
53+
Condition<String> notCondition = new Condition<>(line -> line.contains(textNotFound),
54+
String.format("Text '%s' not found", textNotFound));
55+
56+
await().atMost(2, TimeUnit.SECONDS).untilAsserted(() -> {
57+
ShellAssertions.assertThat(session.screen()).containsText(textFound);
58+
List<String> lines = session.screen().lines();
59+
assertThat(lines).areNot(notCondition);
60+
});
61+
}
62+
4863
protected BaseShellSession<?> createSession(String command, boolean interactive) {
4964
if (interactive) {
5065
InteractiveShellSession session = client.interactive().run();
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.samples.e2e;
17+
18+
import org.junit.jupiter.params.ParameterizedTest;
19+
20+
import org.springframework.shell.command.annotation.EnableCommand;
21+
import org.springframework.shell.samples.AbstractSampleTests;
22+
import org.springframework.shell.samples.e2e.AliasCommands.AliasCommandsAnnotation;
23+
import org.springframework.shell.samples.e2e.AliasCommands.AliasCommandsRegistration;
24+
import org.springframework.shell.test.ShellTestClient.BaseShellSession;
25+
import org.springframework.test.context.ContextConfiguration;
26+
27+
@ContextConfiguration(classes = { AliasCommandsRegistration.class })
28+
@EnableCommand(AliasCommandsAnnotation.class)
29+
public class AliasCommandsTests extends AbstractSampleTests {
30+
31+
@ParameterizedTest
32+
@E2ESource(command = "alias-1", anno = false)
33+
void mainCommandWorks(String command, boolean interactive) {
34+
BaseShellSession<?> session = createSession(command, interactive);
35+
assertScreenContainsText(session, "Hello from alias command");
36+
}
37+
38+
@ParameterizedTest
39+
@E2ESource(command = "aliasfor-1", anno = false)
40+
void aliasCommandWorks(String command, boolean interactive) {
41+
BaseShellSession<?> session = createSession(command, interactive);
42+
assertScreenContainsText(session, "Hello from alias command");
43+
}
44+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.samples.e2e;
17+
18+
import org.junit.jupiter.params.ParameterizedTest;
19+
20+
import org.springframework.shell.command.annotation.EnableCommand;
21+
import org.springframework.shell.samples.AbstractSampleTests;
22+
import org.springframework.shell.samples.e2e.ArityCommands.Annotation;
23+
import org.springframework.shell.samples.e2e.ArityCommands.LegacyAnnotation;
24+
import org.springframework.shell.samples.e2e.ArityCommands.Registration;
25+
import org.springframework.shell.test.ShellTestClient.BaseShellSession;
26+
import org.springframework.test.context.ContextConfiguration;
27+
28+
@ContextConfiguration(classes = { LegacyAnnotation.class, Registration.class })
29+
@EnableCommand(Annotation.class)
30+
class ArityCommandsTests extends AbstractSampleTests {
31+
32+
@ParameterizedTest
33+
@E2ESource(command = "arity-boolean-default-true")
34+
void defaultBooleanValue(String command, boolean interactive) {
35+
BaseShellSession<?> session = createSession(command, interactive);
36+
assertScreenContainsText(session, "Hello true");
37+
}
38+
39+
@ParameterizedTest
40+
@E2ESource(command = "arity-boolean-default-true --overwrite false")
41+
void defaultBooleanValueOverwrite(String command, boolean interactive) {
42+
BaseShellSession<?> session = createSession(command, interactive);
43+
assertScreenContainsText(session, "Hello false");
44+
}
45+
46+
@ParameterizedTest
47+
@E2ESource(command = "arity-string-array --arg1 foo bar")
48+
void arityStringArray(String command, boolean interactive) {
49+
BaseShellSession<?> session = createSession(command, interactive);
50+
assertScreenContainsText(session, "Hello [foo, bar]");
51+
}
52+
53+
@ParameterizedTest
54+
@E2ESource(command = "arity-float-array --arg1 1 2")
55+
void arityFloatArray(String command, boolean interactive) {
56+
BaseShellSession<?> session = createSession(command, interactive);
57+
assertScreenContainsText(session, "Hello [1.0,2.0]");
58+
}
59+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.samples.e2e;
17+
18+
import org.junit.jupiter.params.ParameterizedTest;
19+
20+
import org.springframework.shell.command.annotation.EnableCommand;
21+
import org.springframework.shell.samples.AbstractSampleTests;
22+
import org.springframework.shell.samples.e2e.DefaultValueCommands.Annotation;
23+
import org.springframework.shell.samples.e2e.DefaultValueCommands.LegacyAnnotation;
24+
import org.springframework.shell.samples.e2e.DefaultValueCommands.Registration;
25+
import org.springframework.shell.test.ShellTestClient.BaseShellSession;
26+
import org.springframework.test.context.ContextConfiguration;
27+
28+
@ContextConfiguration(classes = { LegacyAnnotation.class, Registration.class })
29+
@EnableCommand(Annotation.class)
30+
class DefaultValueCommandsTests extends AbstractSampleTests {
31+
32+
@ParameterizedTest
33+
@E2ESource(command = "default-value")
34+
void defaultValue(String command, boolean interactive) {
35+
BaseShellSession<?> session = createSession(command, interactive);
36+
assertScreenContainsText(session, "Hello hi");
37+
}
38+
39+
@ParameterizedTest
40+
@E2ESource(command = "default-value-boolean1")
41+
void defaultValueBoolean1(String command, boolean interactive) {
42+
BaseShellSession<?> session = createSession(command, interactive);
43+
assertScreenContainsText(session, "Hello false");
44+
}
45+
46+
@ParameterizedTest
47+
@E2ESource(command = "default-value-boolean2")
48+
void defaultValueBoolean2(String command, boolean interactive) {
49+
BaseShellSession<?> session = createSession(command, interactive);
50+
assertScreenContainsText(session, "Hello true");
51+
}
52+
53+
@ParameterizedTest
54+
@E2ESource(command = "default-value-boolean3")
55+
void defaultValueBoolean3(String command, boolean interactive) {
56+
BaseShellSession<?> session = createSession(command, interactive);
57+
assertScreenContainsText(session, "Hello false");
58+
}
59+
}

0 commit comments

Comments
 (0)