Drop spring-shell-test-samples

- Fixes #394
This commit is contained in:
Janne Valkealahti
2022-04-29 15:17:34 +01:00
parent 942108e612
commit d16cda75f3
13 changed files with 0 additions and 617 deletions

View File

@@ -243,7 +243,6 @@
</activation>
<modules>
<module>spring-shell-samples</module>
<module>spring-shell-test-samples</module>
</modules>
</profile>
</profiles>

View File

@@ -1,140 +0,0 @@
# Created by .ignore support plugin (hsz.mobi)
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
*.iml
## Directory-based project format:
.idea
.idea/*.xml
# if you remove the above rule, at least ignore the following:
# User-specific stuff:
.idea/workspace.xml
.idea/tasks.xml
.idea/dictionaries
.idea/vcs.xml
.idea/jsLibraryMappings.xml
# Sensitive or high-churn files:
.idea/dataSources.ids
.idea/dataSources.xml
.idea/dataSources.local.xml
.idea/sqlDataSources.xml
.idea/dynamic.xml
.idea/uiDesigner.xml
# Gradle:
.idea/gradle.xml
.idea/libraries
# Mongo Explorer plugin:
.idea/mongoSettings.xml
## File-based project format:
*.iws
## Plugin-specific files:
# IntelliJ
/out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
### OSX template
*.DS_Store
.Appleint
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### EiffelStudio template
# The compilation directory
EIFGENs
### Xcode template
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
## Build generated
build/
DerivedData/
## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata/
## Other
*.moved-aside
*.xccheckout
*.xcscmblueprint
### Java template
*.class
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.war
*.ear
# virtual machine crash logs, see https://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
## Eclipse
.project
.classpath
*.launch
## Directory-based project format:
.settings/
# if you remove the above rule, at least ignore the following:
### Maven template
target
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
spring-shell.log

View File

@@ -1,10 +0,0 @@
# Spring Shell 2 Tests
Examples of [Spring Shell 2](https://docs.spring.io/spring-shell/docs/current/reference/htmlsingle/) unit, functional and integration tests.
## Run
Execute the following command from the parent directory to run the tests:
```
mvn clean test
```

View File

@@ -1,42 +0,0 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-shell-test-samples</artifactId>
<name>Spring Shell Test Samples</name>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.shell</groupId>
<artifactId>spring-shell-parent</artifactId>
<version>2.1.0-SNAPSHOT</version>
</parent>
<description>Examples of unit, functional and integration tests using Spring Shell 2</description>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<!-- The following starter brings everything you need, including adapters that trigger only if needed-->
<dependency>
<groupId>org.springframework.shell</groupId>
<artifactId>spring-shell-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -1,19 +0,0 @@
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
*
* Main entry point for the illustrative Spring Shell Calculator application.
*
* @author Sualeh Fatehi
*/
@SpringBootApplication
public class CalculatorApplication {
public static void main(final String[] args) {
SpringApplication.run(CalculatorApplication.class, args);
}
}

View File

@@ -1,33 +0,0 @@
package com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.shell.standard.ShellCommandGroup;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
/**
*
* Basic commands for the illustrative Spring Shell Calculator application.
*
* @author Sualeh Fatehi
*/
@ShellComponent
@ShellCommandGroup("Calculator Commands")
public class CalculatorCommands {
@Autowired
private CalculatorState state;
@ShellMethod(value = "Add two integers")
public int add(final int a, final int b) {
return a + b;
}
@ShellMethod(value = "Add an integer to the value in memory")
public int addToMemory(final int b) {
final int sum = state.getMemory() + b;
state.setMemory(sum);
return sum;
}
}

View File

@@ -1,28 +0,0 @@
package com.example;
import org.springframework.stereotype.Component;
/**
*
* Program state for the illustrative Spring Shell Calculator application.
*
* @author Sualeh Fatehi
*/
@Component
public class CalculatorState {
private int memory;
public void clear() {
memory = 0;
}
public int getMemory() {
return memory;
}
public void setMemory(final int memory) {
this.memory = memory;
}
}

View File

@@ -1,26 +0,0 @@
package com.example.test;
import static org.springframework.util.ReflectionUtils.invokeMethod;
import javax.validation.constraints.NotNull;
import org.springframework.shell.CommandRegistry;
import org.springframework.shell.MethodTarget;
/**
*
* Utility methods for tests.
*
* @author Sualeh Fatehi
*/
public class BaseCalculatorTest {
@SuppressWarnings("unchecked")
protected <T> T invoke(final MethodTarget methodTarget, final Object... args) {
return (T) invokeMethod(methodTarget.getMethod(), methodTarget.getBean(), args);
}
protected MethodTarget lookupCommand(@NotNull final CommandRegistry registry,
@NotNull final String command) {
return registry.listCommands().get(command);
}
}

View File

@@ -1,21 +0,0 @@
package com.example.test;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import com.example.CalculatorState;
/**
*
* Spring configuration with beans for setting up tests.
*
* @author Sualeh Fatehi
*/
@TestConfiguration
public class TestCalculatorStateConfig {
@Bean("state")
public CalculatorState calculatorState() {
return new CalculatorState();
}
}

View File

@@ -1,88 +0,0 @@
package com.example.test.functional;
import com.example.CalculatorCommands;
import com.example.CalculatorState;
import com.example.test.BaseCalculatorTest;
import com.example.test.TestCalculatorStateConfig;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.shell.ConfigurableCommandRegistry;
import org.springframework.shell.MethodTarget;
import org.springframework.shell.context.DefaultShellContext;
import org.springframework.shell.standard.StandardMethodTargetRegistrar;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.util.ReflectionUtils.findMethod;
/**
* Illustrative functional tests for the Spring Shell Calculator application. These
* functional tests use Spring Shell commands auto-wired by the Spring Test Runner outside
* of the shell, to test functionality of the commands.
*
* @author Sualeh Fatehi
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { TestCalculatorStateConfig.class, CalculatorCommands.class })
public class CalculatorCommandsTest extends BaseCalculatorTest {
private static final Class<CalculatorCommands> COMMAND_CLASS_UNDER_TEST = CalculatorCommands.class;
private final ConfigurableCommandRegistry registry = new ConfigurableCommandRegistry(new DefaultShellContext());
@Autowired
private CalculatorState state;
@Autowired
private ApplicationContext context;
@BeforeEach
public void setup() {
final StandardMethodTargetRegistrar registrar = new StandardMethodTargetRegistrar();
registrar.setApplicationContext(context);
registrar.register(registry);
state.clear();
}
@Test
public void testAdd() {
final String command = "add";
final String commandMethod = "add";
final MethodTarget commandTarget = lookupCommand(registry, command);
assertThat(commandTarget).isNotNull();
assertThat(commandTarget.getGroup()).isEqualTo("Calculator Commands");
assertThat(commandTarget.getHelp()).isEqualTo("Add two integers");
assertThat(commandTarget.getMethod())
.isEqualTo(findMethod(COMMAND_CLASS_UNDER_TEST, commandMethod, int.class, int.class));
assertThat(commandTarget.getAvailability().isAvailable()).isTrue();
Object invoke = invoke(commandTarget, 1, 2);
assertThat(invoke).isEqualTo(3);
assertThat(state.getMemory()).isEqualTo(0);
}
@Test
public void testaddToMemory() {
final String command = "add-to-memory";
final String commandMethod = "addToMemory";
final MethodTarget commandTarget = lookupCommand(registry, command);
assertThat(commandTarget).isNotNull();
assertThat(commandTarget.getGroup()).isEqualTo("Calculator Commands");
assertThat(commandTarget.getHelp()).isEqualTo("Add an integer to the value in memory");
assertThat(commandTarget.getMethod()).isEqualTo(findMethod(COMMAND_CLASS_UNDER_TEST, commandMethod, int.class));
assertThat(commandTarget.getAvailability().isAvailable()).isTrue();
state.setMemory(1);
Object invoke = invoke(commandTarget, 2);
assertThat(invoke).isEqualTo(3);
assertThat(state.getMemory()).isEqualTo(3);
}
}

View File

@@ -1,85 +0,0 @@
package com.example.test.integration;
import com.example.CalculatorCommands;
import com.example.CalculatorState;
import com.example.test.BaseCalculatorTest;
import com.example.test.TestCalculatorStateConfig;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.shell.CommandRegistry;
import org.springframework.shell.MethodTarget;
import org.springframework.shell.Shell;
import org.springframework.test.context.ContextConfiguration;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.util.ReflectionUtils.findMethod;
/**
*
* Illustrative integration tests for the Spring Shell Calculator application. These
* integration tests use Spring Shell auto-wired by the Spring Test Runner.
*
* @author Sualeh Fatehi
*/
@SpringBootTest(properties = { "spring.shell.interactive.enabled=false" })
@ContextConfiguration(classes = TestCalculatorStateConfig.class)
public class CalculatorCommandsIntegrationTest extends BaseCalculatorTest {
private static final Class<CalculatorCommands> COMMAND_CLASS_UNDER_TEST = CalculatorCommands.class;
@Autowired
private Shell shell;
@Autowired
private CommandRegistry commandRegistry;
@Autowired
private CalculatorState state;
/**
* Test "happy path" or basic addition with positive numbers and zeroes. Use Spring Shell
* auto-wired by the Spring Test Runner.
*/
@Test
public void testAdd() {
final String command = "add";
final String commandMethod = "add";
final MethodTarget commandTarget = lookupCommand(commandRegistry, command);
assertThat(commandTarget).isNotNull();
assertThat(commandTarget.getGroup()).isEqualTo("Calculator Commands");
assertThat(commandTarget.getHelp()).isEqualTo("Add two integers");
assertThat(commandTarget.getMethod())
.isEqualTo(findMethod(COMMAND_CLASS_UNDER_TEST, commandMethod, int.class, int.class));
assertThat(commandTarget.getAvailability().isAvailable()).isTrue();
Object evaluate = shell.evaluate(() -> command + " 1 2");
assertThat(evaluate).isEqualTo(3);
assertThat(state.getMemory()).isEqualTo(0);
}
/**
* Test "happy path" or basic addition with positive numbers and zeroes, using calculator
* memory (program state). Use Spring Shell and calculator memory auto-wired by the Spring
* Test Runner.
*/
@Test
public void testAddToMemory() {
final String command = "add-to-memory";
final String commandMethod = "addToMemory";
final MethodTarget commandTarget = lookupCommand(commandRegistry, command);
assertThat(commandTarget).isNotNull();
assertThat(commandTarget.getGroup()).isEqualTo("Calculator Commands");
assertThat(commandTarget.getHelp()).isEqualTo("Add an integer to the value in memory");
assertThat(commandTarget.getMethod()).isEqualTo(findMethod(COMMAND_CLASS_UNDER_TEST, commandMethod, int.class));
assertThat(commandTarget.getAvailability().isAvailable()).isTrue();
state.setMemory(1);
Object evaluate = shell.evaluate(() -> command + " 2");
assertThat(evaluate).isEqualTo(3);
assertThat(state.getMemory()).isEqualTo(3);
}
}

View File

@@ -1,50 +0,0 @@
package com.example.test.unit;
import com.example.CalculatorCommands;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
*
* Illustrative unit tests for the Spring Shell Calculator application. The unit tests
* test calculator commands as "plain-old Java objects" or POJOs, without relying on the
* Spring Framework. Boundary conditions of individual methods are tested.
*
* @author Sualeh Fatehi
*/
public class AddTest {
private CalculatorCommands commands;
/**
* Setup test calculator commands as "plain-old Java objects" or POJOs, initialized for
* each test.
*/
@BeforeEach
public void setup() {
commands = new CalculatorCommands();
}
/**
* Test "happy path" or basic addition with positive numbers and zeroes.
*/
@Test
public void addHappyPath() {
assertThat(commands.add(0, 1)).isEqualTo(1);
assertThat(commands.add(1, 2)).isEqualTo(3);
assertThat(commands.add(1, 0)).isEqualTo(1);
}
/**
* Test addition with negative numbers and zeroes.
*/
@Test
public void addNegatives() {
assertThat(commands.add(0, -1)).isEqualTo(-1);
assertThat(commands.add(1, -2)).isEqualTo(-1);
assertThat(commands.add(-1, 0)).isEqualTo(-1);
}
}

View File

@@ -1,74 +0,0 @@
package com.example.test.unit;
import java.lang.reflect.Field;
import com.example.CalculatorCommands;
import com.example.CalculatorState;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.util.ReflectionUtils.findField;
import static org.springframework.util.ReflectionUtils.makeAccessible;
import static org.springframework.util.ReflectionUtils.setField;
/**
*
* Illustrative unit tests for the Spring Shell Calculator application. The unit tests
* test calculator commands as "plain-old Java objects" or POJOs, without relying on the
* Spring Framework. Boundary conditions of individual methods are tested.
*
* @author Sualeh Fatehi
*/
public class AddToMemoryTest {
private CalculatorCommands commands;
private CalculatorState state;
/**
* Setup test calculator commands as "plain-old Java objects" or POJOs, initialized for
* each test.
*/
@BeforeEach
public void setup() {
state = new CalculatorState();
commands = new CalculatorCommands();
final Field stateField = findField(CalculatorCommands.class, "state");
makeAccessible(stateField);
setField(stateField, commands, state);
}
/**
* Test "happy path" or basic addition with positive numbers and zeroes, using calculator
* memory (program state).
*/
@Test
public void addToMemoryHappyPath() {
assertThat(commands.addToMemory(1)).isEqualTo(1);
assertThat(state.getMemory()).isEqualTo(1);
assertThat(commands.addToMemory(2)).isEqualTo(3);
assertThat(state.getMemory()).isEqualTo(3);
assertThat(commands.addToMemory(0)).isEqualTo(3);
assertThat(state.getMemory()).isEqualTo(3);
}
/**
* Test addition with negative numbers and zeroes, using calculator memory (program
* state).
*/
@Test
public void addToMemoryNegatives() {
assertThat(commands.addToMemory(-1)).isEqualTo(-1);
assertThat(state.getMemory()).isEqualTo(-1);
assertThat(commands.addToMemory(-2)).isEqualTo(-3);
assertThat(state.getMemory()).isEqualTo(-3);
assertThat(commands.addToMemory(0)).isEqualTo(-3);
assertThat(state.getMemory()).isEqualTo(-3);
}
}