Skip to content

Commit 1a1ddaa

Browse files
committed
Reorganise e2e samples
- Backport #642 - Fixes #643
1 parent 32fc723 commit 1a1ddaa

16 files changed

+952
-938
lines changed

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

Lines changed: 102 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -16,130 +16,129 @@
1616
package org.springframework.shell.samples.e2e;
1717

1818
import java.util.Arrays;
19-
import java.util.stream.Collectors;
20-
import java.util.stream.IntStream;
2119

2220
import org.springframework.context.annotation.Bean;
2321
import org.springframework.shell.command.CommandRegistration;
2422
import org.springframework.shell.command.CommandRegistration.OptionArity;
2523
import org.springframework.shell.standard.ShellComponent;
2624
import org.springframework.shell.standard.ShellMethod;
2725
import org.springframework.shell.standard.ShellOption;
26+
import org.springframework.stereotype.Component;
2827

2928
/**
3029
* Commands used for e2e test.
3130
*
3231
* @author Janne Valkealahti
3332
*/
34-
@ShellComponent
35-
public class ArityCommands extends BaseE2ECommands {
33+
public class ArityCommands {
3634

37-
@ShellMethod(key = LEGACY_ANNO + "arity-boolean-default-true", group = GROUP)
38-
public String testArityBooleanDefaultTrueLegacyAnnotation(
39-
@ShellOption(value = "--overwrite", arity = 1, defaultValue = "true") Boolean overwrite
40-
) {
41-
return "Hello " + overwrite;
42-
}
35+
@ShellComponent
36+
public static class LegacyAnnotation extends BaseE2ECommands {
4337

44-
@Bean
45-
public CommandRegistration testArityBooleanDefaultTrueRegistration(CommandRegistration.BuilderSupplier builder) {
46-
return builder.get()
47-
.command(REG, "arity-boolean-default-true")
48-
.group(GROUP)
49-
.withOption()
50-
.longNames("overwrite")
51-
.type(Boolean.class)
52-
.defaultValue("true")
53-
.arity(OptionArity.ZERO_OR_ONE)
54-
.and()
55-
.withTarget()
56-
.function(ctx -> {
57-
Boolean overwrite = ctx.getOptionValue("overwrite");
58-
return "Hello " + overwrite;
59-
})
60-
.and()
61-
.build();
62-
}
38+
@ShellMethod(key = LEGACY_ANNO + "arity-boolean-default-true", group = GROUP)
39+
public String testArityBooleanDefaultTrueLegacyAnnotation(
40+
@ShellOption(value = "--overwrite", arity = 1, defaultValue = "true") Boolean overwrite
41+
) {
42+
return "Hello " + overwrite;
43+
}
6344

64-
@ShellMethod(key = LEGACY_ANNO + "arity-string-array", group = GROUP)
65-
public String testArityStringArrayLegacyAnnotation(
66-
@ShellOption(value = "--arg1", arity = 3) String[] arg1
67-
) {
68-
return "Hello " + Arrays.asList(arg1);
69-
}
45+
@ShellMethod(key = LEGACY_ANNO + "arity-string-array", group = GROUP)
46+
public String testArityStringArrayLegacyAnnotation(
47+
@ShellOption(value = "--arg1", arity = 3) String[] arg1
48+
) {
49+
return "Hello " + Arrays.asList(arg1);
50+
}
7051

71-
@Bean
72-
public CommandRegistration testArityStringArrayRegistration(CommandRegistration.BuilderSupplier builder) {
73-
return builder.get()
74-
.command(REG, "arity-string-array")
75-
.group(GROUP)
76-
.withOption()
77-
.longNames("arg1")
78-
.required()
79-
.type(String[].class)
80-
.arity(0, 3)
81-
.position(0)
82-
.and()
83-
.withTarget()
84-
.function(ctx -> {
85-
String[] arg1 = ctx.getOptionValue("arg1");
86-
return "Hello " + Arrays.asList(arg1);
87-
})
88-
.and()
89-
.build();
90-
}
52+
@ShellMethod(key = LEGACY_ANNO + "arity-float-array", group = GROUP)
53+
public String testArityFloatArrayLegacyAnnotation(
54+
@ShellOption(value = "--arg1", arity = 3) float[] arg1
55+
) {
56+
return "Hello " + stringOfFloats(arg1);
57+
}
9158

92-
@ShellMethod(key = LEGACY_ANNO + "arity-float-array", group = GROUP)
93-
public String testArityFloatArrayLegacyAnnotation(
94-
@ShellOption(value = "--arg1", arity = 3) float[] arg1
95-
) {
96-
return "Hello " + floatsToString(arg1);
9759
}
9860

99-
@Bean
100-
public CommandRegistration testArityFloatArrayRegistration(CommandRegistration.BuilderSupplier builder) {
101-
return builder.get()
102-
.command(REG, "arity-float-array")
103-
.group(GROUP)
104-
.withOption()
105-
.longNames("arg1")
106-
.type(float[].class)
107-
.arity(0, 3)
108-
.and()
109-
.withTarget()
110-
.function(ctx -> {
111-
float[] arg1 = ctx.getOptionValue("arg1");
112-
return "Hello " + floatsToString(arg1);
113-
})
114-
.and()
115-
.build();
116-
}
61+
@Component
62+
public static class Registration extends BaseE2ECommands {
11763

118-
@Bean
119-
public CommandRegistration testArityErrorsRegistration(CommandRegistration.BuilderSupplier builder) {
120-
return builder.get()
121-
.command(REG, "arity-errors")
122-
.group(GROUP)
123-
.withOption()
124-
.longNames("arg1")
125-
.type(String[].class)
126-
.required()
127-
.arity(1, 2)
128-
.and()
129-
.withTarget()
130-
.function(ctx -> {
131-
String[] arg1 = ctx.getOptionValue("arg1");
132-
return "Hello " + Arrays.asList(arg1);
133-
})
134-
.and()
135-
.build();
136-
}
64+
@Bean
65+
public CommandRegistration testArityBooleanDefaultTrueRegistration() {
66+
return getBuilder()
67+
.command(REG, "arity-boolean-default-true")
68+
.group(GROUP)
69+
.withOption()
70+
.longNames("overwrite")
71+
.type(Boolean.class)
72+
.defaultValue("true")
73+
.arity(OptionArity.ZERO_OR_ONE)
74+
.and()
75+
.withTarget()
76+
.function(ctx -> {
77+
Boolean overwrite = ctx.getOptionValue("overwrite");
78+
return "Hello " + overwrite;
79+
})
80+
.and()
81+
.build();
82+
}
83+
84+
@Bean
85+
public CommandRegistration testArityStringArrayRegistration() {
86+
return getBuilder()
87+
.command(REG, "arity-string-array")
88+
.group(GROUP)
89+
.withOption()
90+
.longNames("arg1")
91+
.required()
92+
.type(String[].class)
93+
.arity(0, 3)
94+
.position(0)
95+
.and()
96+
.withTarget()
97+
.function(ctx -> {
98+
String[] arg1 = ctx.getOptionValue("arg1");
99+
return "Hello " + Arrays.asList(arg1);
100+
})
101+
.and()
102+
.build();
103+
}
104+
105+
@Bean
106+
public CommandRegistration testArityFloatArrayRegistration() {
107+
return getBuilder()
108+
.command(REG, "arity-float-array")
109+
.group(GROUP)
110+
.withOption()
111+
.longNames("arg1")
112+
.type(float[].class)
113+
.arity(0, 3)
114+
.and()
115+
.withTarget()
116+
.function(ctx -> {
117+
float[] arg1 = ctx.getOptionValue("arg1");
118+
return "Hello " + stringOfFloats(arg1);
119+
})
120+
.and()
121+
.build();
122+
}
137123

138-
private static String floatsToString(float[] arg1) {
139-
return IntStream.range(0, arg1.length)
140-
.mapToDouble(i -> arg1[i])
141-
.boxed()
142-
.map(d -> d.toString())
143-
.collect(Collectors.joining(","));
124+
@Bean
125+
public CommandRegistration testArityErrorsRegistration() {
126+
return getBuilder()
127+
.command(REG, "arity-errors")
128+
.group(GROUP)
129+
.withOption()
130+
.longNames("arg1")
131+
.type(String[].class)
132+
.required()
133+
.arity(1, 2)
134+
.and()
135+
.withTarget()
136+
.function(ctx -> {
137+
String[] arg1 = ctx.getOptionValue("arg1");
138+
return "Hello " + Arrays.asList(arg1);
139+
})
140+
.and()
141+
.build();
142+
}
144143
}
145144
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.util.stream.Collectors;
1919
import java.util.stream.IntStream;
2020

21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.shell.command.CommandRegistration;
2123
import org.springframework.util.StringUtils;
2224

2325
/**
@@ -31,6 +33,13 @@ abstract class BaseE2ECommands {
3133
static final String REG = "e2e reg";
3234
static final String LEGACY_ANNO = "e2e anno ";
3335

36+
@Autowired
37+
private CommandRegistration.BuilderSupplier builder;
38+
39+
CommandRegistration.Builder getBuilder() {
40+
return builder.get();
41+
}
42+
3443
static String stringOfStrings(String[] values) {
3544
return String.format("[%s]", StringUtils.arrayToCommaDelimitedString(values));
3645
}

0 commit comments

Comments
 (0)