diff --git a/spring-shell-docs/src/main/asciidoc/using-shell-testing-basics.adoc b/spring-shell-docs/src/main/asciidoc/using-shell-testing-basics.adoc index 7fb11d02..d572aee1 100644 --- a/spring-shell-docs/src/main/asciidoc/using-shell-testing-basics.adoc +++ b/spring-shell-docs/src/main/asciidoc/using-shell-testing-basics.adoc @@ -1,5 +1,5 @@ [[using-shell-testing-basics]] -==== Basics +=== Basics ifndef::snippets[:snippets: ../../test/java/org/springframework/shell/docs] Spring Shell provides a number of utilities and annotations to help when testing your application. diff --git a/spring-shell-docs/src/main/asciidoc/using-shell-testing-settings.adoc b/spring-shell-docs/src/main/asciidoc/using-shell-testing-settings.adoc new file mode 100644 index 00000000..851a0c5e --- /dev/null +++ b/spring-shell-docs/src/main/asciidoc/using-shell-testing-settings.adoc @@ -0,0 +1,27 @@ +[[using-shell-testing-settings]] +=== Settings +ifndef::snippets[:snippets: ../../test/java/org/springframework/shell/docs] + +Built in emulation uses terminal width 80 and height 24 on default. +Changing dimensions is useful if output would span into multiple +lines and you don't want to handle those cases in a tests. + +These can be changed using properties `spring.shell.test.terminal-width` +or `spring.shell.test.terminal-height`. + +==== +[source, java, indent=0] +---- +include::{snippets}/TestingSnippets.java[tag=testing-shelltest-dimensions-props] +---- +==== + +`ShellTest` annotation have fields `terminalWidth` and `terminalHeight` +which can also be used to change dimensions. + +==== +[source, java, indent=0] +---- +include::{snippets}/TestingSnippets.java[tag=testing-shelltest-dimensions-field] +---- +==== diff --git a/spring-shell-docs/src/main/asciidoc/using-shell-testing.adoc b/spring-shell-docs/src/main/asciidoc/using-shell-testing.adoc index 2527bd27..b2e5e3fb 100644 --- a/spring-shell-docs/src/main/asciidoc/using-shell-testing.adoc +++ b/spring-shell-docs/src/main/asciidoc/using-shell-testing.adoc @@ -16,3 +16,5 @@ NOTE: Testing support is currently under development and will be unstable for various parts. include::using-shell-testing-basics.adoc[] + +include::using-shell-testing-settings.adoc[] diff --git a/spring-shell-docs/src/test/java/org/springframework/shell/docs/TestingSnippets.java b/spring-shell-docs/src/test/java/org/springframework/shell/docs/TestingSnippets.java index 6cd0542c..a7e00907 100644 --- a/spring-shell-docs/src/test/java/org/springframework/shell/docs/TestingSnippets.java +++ b/spring-shell-docs/src/test/java/org/springframework/shell/docs/TestingSnippets.java @@ -81,4 +81,23 @@ class TestingSnippets { } } // end::testing-shelltest-noninteractive[] + + class Dump1 { + + // tag::testing-shelltest-dimensions-props[] + @ShellTest(properties = { + "spring.shell.test.terminal-width=120", + "spring.shell.test.terminal-height=40" + }) + class ShellSettingsSample {} + // end::testing-shelltest-dimensions-props[] + } + class Dump2 { + + // tag::testing-shelltest-dimensions-field[] + @ShellTest(terminalWidth = 120, terminalHeight = 40) + class ShellSettingsSample {} + // end::testing-shelltest-dimensions-field[] + } + } diff --git a/spring-shell-test-autoconfigure/src/main/java/org/springframework/shell/test/autoconfigure/ShellAutoConfiguration.java b/spring-shell-test-autoconfigure/src/main/java/org/springframework/shell/test/autoconfigure/ShellAutoConfiguration.java index 0f24305e..ddc69497 100644 --- a/spring-shell-test-autoconfigure/src/main/java/org/springframework/shell/test/autoconfigure/ShellAutoConfiguration.java +++ b/spring-shell-test-autoconfigure/src/main/java/org/springframework/shell/test/autoconfigure/ShellAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 the original author or authors. + * Copyright 2022-2023 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. @@ -25,6 +25,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.shell.boot.JLineShellAutoConfiguration; import org.springframework.shell.boot.TerminalCustomizer; @@ -33,6 +34,7 @@ import org.springframework.shell.test.jediterm.terminal.ui.JediTermWidget; import org.springframework.shell.test.jediterm.terminal.ui.TerminalSession; @AutoConfiguration(before = JLineShellAutoConfiguration.class) +@EnableConfigurationProperties(SpringShellTestProperties.class) public class ShellAutoConfiguration { @Bean @@ -55,8 +57,8 @@ public class ShellAutoConfiguration { } @Bean - TerminalSession terminalSession(TtyConnector ttyConnector) { - JediTermWidget widget = new JediTermWidget(80, 24); + TerminalSession terminalSession(TtyConnector ttyConnector, SpringShellTestProperties properties) { + JediTermWidget widget = new JediTermWidget(properties.getTerminalWidth(), properties.getTerminalHeight()); widget.setTtyConnector(ttyConnector); return widget; } diff --git a/spring-shell-test-autoconfigure/src/main/java/org/springframework/shell/test/autoconfigure/ShellTest.java b/spring-shell-test-autoconfigure/src/main/java/org/springframework/shell/test/autoconfigure/ShellTest.java index 15700963..260a1003 100644 --- a/spring-shell-test-autoconfigure/src/main/java/org/springframework/shell/test/autoconfigure/ShellTest.java +++ b/spring-shell-test-autoconfigure/src/main/java/org/springframework/shell/test/autoconfigure/ShellTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 the original author or authors. + * Copyright 2022-2023 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. @@ -28,6 +28,8 @@ import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration; import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters; +import org.springframework.boot.test.autoconfigure.properties.PropertyMapping; +import org.springframework.boot.test.autoconfigure.properties.SkipPropertyMapping; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.core.annotation.AliasFor; import org.springframework.core.env.Environment; @@ -61,6 +63,22 @@ public @interface ShellTest { */ String[] properties() default {}; + /** + * Set emulated terminal width. + * + * @return emulated terminal width + */ + @PropertyMapping(value = "spring.shell.test.terminal-width", skip = SkipPropertyMapping.ON_DEFAULT_VALUE) + int terminalWidth() default 80; + + /** + * Set emulated terminal height. + * + * @return emulated terminal height + */ + @PropertyMapping(value = "spring.shell.test.terminal-height", skip = SkipPropertyMapping.ON_DEFAULT_VALUE) + int terminalHeight() default 24; + /** * Determines if default filtering should be used with * {@link SpringBootApplication @SpringBootApplication}. diff --git a/spring-shell-test-autoconfigure/src/main/java/org/springframework/shell/test/autoconfigure/SpringShellTestProperties.java b/spring-shell-test-autoconfigure/src/main/java/org/springframework/shell/test/autoconfigure/SpringShellTestProperties.java new file mode 100644 index 00000000..97c7bfa9 --- /dev/null +++ b/spring-shell-test-autoconfigure/src/main/java/org/springframework/shell/test/autoconfigure/SpringShellTestProperties.java @@ -0,0 +1,53 @@ +/* + * Copyright 2023 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.test.autoconfigure; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Configuration properties for shell test. + * + * @author Janne Valkealahti + */ +@ConfigurationProperties(prefix = "spring.shell.test") +public class SpringShellTestProperties { + + /** + * Width of an emulated terminal. + */ + private int terminalWidth = 80; + + /** + * Height of an emulated terminal. + */ + private int terminalHeight = 24; + + public int getTerminalWidth() { + return terminalWidth; + } + + public void setTerminalWidth(int terminalWidth) { + this.terminalWidth = terminalWidth; + } + + public int getTerminalHeight() { + return terminalHeight; + } + + public void setTerminalHeight(int terminalHeight) { + this.terminalHeight = terminalHeight; + } +} diff --git a/spring-shell-test-autoconfigure/src/test/java/org/springframework/shell/test/autoconfigure/ShellTestPropertiesIntegrationTests.java b/spring-shell-test-autoconfigure/src/test/java/org/springframework/shell/test/autoconfigure/ShellTestPropertiesIntegrationTests.java index 031b5dd1..daece644 100644 --- a/spring-shell-test-autoconfigure/src/test/java/org/springframework/shell/test/autoconfigure/ShellTestPropertiesIntegrationTests.java +++ b/spring-shell-test-autoconfigure/src/test/java/org/springframework/shell/test/autoconfigure/ShellTestPropertiesIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 the original author or authors. + * Copyright 2022-2023 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. @@ -20,6 +20,7 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.shell.test.autoconfigure.app.ExampleShellApplication; +import org.springframework.shell.test.jediterm.terminal.ui.TerminalSession; import org.springframework.test.context.ContextConfiguration; import static org.assertj.core.api.Assertions.assertThat; @@ -30,15 +31,25 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Janne Valkealahti */ -@ShellTest(properties = "spring.profiles.active=test") +@ShellTest(properties = { "spring.profiles.active=test", "spring.shell.test.terminal-width=81", + "spring.shell.test.terminal-height=25" }) @ContextConfiguration(classes = ExampleShellApplication.class) public class ShellTestPropertiesIntegrationTests { @Autowired private Environment environment; + @Autowired + private TerminalSession session; + @Test void environmentWithNewProfile() { assertThat(this.environment.getActiveProfiles()).containsExactly("test"); } + + @Test + void dimensionsSet() { + assertThat(session.getTerminal().getTerminalWidth()).isEqualTo(81); + assertThat(session.getTerminal().getTerminalHeight()).isEqualTo(25); + } } diff --git a/spring-shell-test-autoconfigure/src/test/java/org/springframework/shell/test/autoconfigure/ShellTestTerminalDimensionsIntegrationTests.java b/spring-shell-test-autoconfigure/src/test/java/org/springframework/shell/test/autoconfigure/ShellTestTerminalDimensionsIntegrationTests.java new file mode 100644 index 00000000..8c19a762 --- /dev/null +++ b/spring-shell-test-autoconfigure/src/test/java/org/springframework/shell/test/autoconfigure/ShellTestTerminalDimensionsIntegrationTests.java @@ -0,0 +1,39 @@ +/* + * Copyright 2023 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.test.autoconfigure; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.shell.test.autoconfigure.app.ExampleShellApplication; +import org.springframework.shell.test.jediterm.terminal.ui.TerminalSession; +import org.springframework.test.context.ContextConfiguration; + +import static org.assertj.core.api.Assertions.assertThat; + +@ShellTest(terminalWidth = 81, terminalHeight = 25) +@ContextConfiguration(classes = ExampleShellApplication.class) +class ShellTestTerminalDimensionsIntegrationTests { + + @Autowired + private TerminalSession session; + + @Test + void dimensionsSet() { + assertThat(session.getTerminal().getTerminalWidth()).isEqualTo(81); + assertThat(session.getTerminal().getTerminalHeight()).isEqualTo(25); + } +} diff --git a/spring-shell-test-autoconfigure/src/test/java/org/springframework/shell/test/autoconfigure/SpringShellTestPropertiesTests.java b/spring-shell-test-autoconfigure/src/test/java/org/springframework/shell/test/autoconfigure/SpringShellTestPropertiesTests.java new file mode 100644 index 00000000..ee113728 --- /dev/null +++ b/spring-shell-test-autoconfigure/src/test/java/org/springframework/shell/test/autoconfigure/SpringShellTestPropertiesTests.java @@ -0,0 +1,56 @@ +/* + * Copyright 2023 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.test.autoconfigure; + +import org.junit.jupiter.api.Test; + +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +class SpringShellTestPropertiesTests { + + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner(); + + @Test + public void defaultNoPropertiesSet() { + this.contextRunner + .withUserConfiguration(Config1.class) + .run((context) -> { + SpringShellTestProperties properties = context.getBean(SpringShellTestProperties.class); + assertThat(properties.getTerminalWidth()).isEqualTo(80); + assertThat(properties.getTerminalHeight()).isEqualTo(24); + }); + } + + @Test + public void setProperties() { + this.contextRunner + .withPropertyValues("spring.shell.test.terminal-width=81") + .withPropertyValues("spring.shell.test.terminal-height=25") + .withUserConfiguration(Config1.class) + .run((context) -> { + SpringShellTestProperties properties = context.getBean(SpringShellTestProperties.class); + assertThat(properties.getTerminalWidth()).isEqualTo(81); + assertThat(properties.getTerminalHeight()).isEqualTo(25); + }); + } + + @EnableConfigurationProperties({ SpringShellTestProperties.class }) + private static class Config1 { + } +}