Skip to content

Commit 05ad858

Browse files
committed
Add state to e2e availability tests
1 parent 6ab5f60 commit 05ad858

File tree

1 file changed

+89
-1
lines changed

1 file changed

+89
-1
lines changed

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

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 the original author or authors.
2+
* Copyright 2023-2024 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.
@@ -21,9 +21,11 @@
2121
import org.springframework.shell.command.CommandRegistration;
2222
import org.springframework.shell.command.annotation.Command;
2323
import org.springframework.shell.command.annotation.CommandAvailability;
24+
import org.springframework.shell.command.annotation.Option;
2425
import org.springframework.shell.standard.ShellComponent;
2526
import org.springframework.shell.standard.ShellMethod;
2627
import org.springframework.shell.standard.ShellMethodAvailability;
28+
import org.springframework.shell.standard.ShellOption;
2729
import org.springframework.stereotype.Component;
2830

2931
public class AvailabilityCommands {
@@ -65,6 +67,28 @@ public String testAvailability3LegacyAnnotation(
6567
public Availability testAvailability3LegacyAnnotationAvailability3() {
6668
return Availability.unavailable("not available 3");
6769
}
70+
71+
private boolean connected = false;
72+
73+
@ShellMethod(key = LEGACY_ANNO + "availability-set", group = GROUP)
74+
public void testAvailabilitySetLegacyAnnotation(
75+
@ShellOption(value = "connected") boolean connected
76+
) {
77+
this.connected = connected;
78+
}
79+
80+
@ShellMethod(key = LEGACY_ANNO + "availability-use", group = GROUP)
81+
@ShellMethodAvailability("testAvailabilityLegacyAnnotationConnected")
82+
public void testAvailabilityUseLegacyAnnotation(
83+
) {
84+
}
85+
86+
public Availability testAvailabilityLegacyAnnotationConnected() {
87+
return connected
88+
? Availability.available()
89+
: Availability.unavailable("you are not connected");
90+
}
91+
6892
}
6993

7094
@Command(command = BaseE2ECommands.ANNO, group = BaseE2ECommands.GROUP)
@@ -81,6 +105,29 @@ public String testAvailability1Annotation(
81105
public AvailabilityProvider testAvailability1AnnotationAvailability() {
82106
return () -> Availability.unavailable("not available");
83107
}
108+
109+
private boolean connected = false;
110+
111+
@Command(command = "availability-set")
112+
public void testAvailabilitySetAnnotation(
113+
@Option(longNames = "connected") boolean connected
114+
) {
115+
this.connected = connected;
116+
}
117+
118+
@Command(command = "availability-use")
119+
@CommandAvailability(provider = "testAvailabilityAnnotationConnected")
120+
public void testAvailabilityUseAnnotation(
121+
) {
122+
}
123+
124+
@Bean
125+
public AvailabilityProvider testAvailabilityAnnotationConnected() {
126+
return () -> connected
127+
? Availability.available()
128+
: Availability.unavailable("you are not connected");
129+
}
130+
84131
}
85132

86133
@Component
@@ -119,5 +166,46 @@ public CommandRegistration testAvailability2Registration() {
119166
AvailabilityProvider testAvailability2AnnotationAvailability() {
120167
return () -> Availability.unavailable("not available");
121168
}
169+
170+
private boolean connected = false;
171+
172+
@Bean
173+
public CommandRegistration testAvailabilitySetRegistration() {
174+
return getBuilder()
175+
.command(REG, "availability-set")
176+
.group(GROUP)
177+
.withOption()
178+
.longNames("connected")
179+
.required()
180+
.type(boolean.class)
181+
.and()
182+
.withTarget()
183+
.consumer(ctx -> {
184+
boolean connected = ctx.getOptionValue("connected");
185+
this.connected = connected;
186+
})
187+
.and()
188+
.build();
189+
}
190+
191+
@Bean
192+
public CommandRegistration testAvailabilityUseRegistration() {
193+
return getBuilder()
194+
.command(REG, "availability-use")
195+
.group(GROUP)
196+
.availability(testAvailabilityRegistrationConnected())
197+
.withTarget()
198+
.consumer(ctx -> {
199+
})
200+
.and()
201+
.build();
202+
}
203+
204+
public AvailabilityProvider testAvailabilityRegistrationConnected() {
205+
return () -> connected
206+
? Availability.available()
207+
: Availability.unavailable("you are not connected");
208+
}
209+
122210
}
123211
}

0 commit comments

Comments
 (0)