Split e2e sample commands

This commit is contained in:
Janne Valkealahti
2022-06-02 07:46:43 +01:00
parent c2774b9d8f
commit 0be37d164f
7 changed files with 321 additions and 146 deletions

View File

@@ -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();
}
}

View File

@@ -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 ";
}

View File

@@ -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();
}
}

View File

@@ -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);
}
}
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}

View File

@@ -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);
}
}
}