Add non-interactive shell runner customizer
- Add non-interactive shell runner customizer - Update javadoc for DefaultApplicationRunner - Rename DefaultApplicationRunner - Formatting
This commit is contained in:
@@ -21,7 +21,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.shell.DefaultApplicationRunner;
|
||||
import org.springframework.shell.DefaultShellApplicationRunner;
|
||||
import org.springframework.shell.ShellApplicationRunner;
|
||||
import org.springframework.shell.ShellRunner;
|
||||
|
||||
@@ -31,7 +31,7 @@ public class ApplicationRunnerAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(ShellApplicationRunner.class)
|
||||
public DefaultApplicationRunner defaultApplicationRunner(List<ShellRunner> shellRunners) {
|
||||
return new DefaultApplicationRunner(shellRunners);
|
||||
public DefaultShellApplicationRunner defaultShellApplicationRunner(List<ShellRunner> shellRunners) {
|
||||
return new DefaultShellApplicationRunner(shellRunners);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.springframework.shell.boot;
|
||||
|
||||
import org.springframework.shell.jline.NonInteractiveShellRunner;
|
||||
|
||||
/**
|
||||
* Callback interface that can be implemented by beans wishing to customize the
|
||||
* auto-configured {@link NonInteractiveShellRunner}.
|
||||
*
|
||||
* @author Chris Bono
|
||||
* @since 2.1.0
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface NonInteractiveShellRunnerCustomizer {
|
||||
/**
|
||||
* Customize the {@link NonInteractiveShellRunner}.
|
||||
* @param shellRunner the non-interactive shell runner to customize
|
||||
*/
|
||||
void customize(NonInteractiveShellRunner shellRunner);
|
||||
|
||||
}
|
||||
@@ -18,6 +18,7 @@ package org.springframework.shell.boot;
|
||||
import org.jline.reader.LineReader;
|
||||
import org.jline.reader.Parser;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -54,8 +55,10 @@ public class ShellRunnerAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(prefix = "spring.shell.noninteractive", value = "enabled", havingValue = "true", matchIfMissing = true)
|
||||
public NonInteractiveShellRunner nonInteractiveApplicationRunner() {
|
||||
return new NonInteractiveShellRunner(shell, shellContext);
|
||||
public NonInteractiveShellRunner nonInteractiveApplicationRunner(ObjectProvider<NonInteractiveShellRunnerCustomizer> customizer) {
|
||||
NonInteractiveShellRunner shellRunner = new NonInteractiveShellRunner(shell, shellContext);
|
||||
customizer.orderedStream().forEach((c) -> c.customize(shellRunner));
|
||||
return shellRunner;
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* 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.boot;
|
||||
|
||||
import org.jline.reader.LineReader;
|
||||
import org.jline.reader.Parser;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.shell.ParameterResolver;
|
||||
import org.springframework.shell.Shell;
|
||||
import org.springframework.shell.context.ShellContext;
|
||||
import org.springframework.shell.jline.InteractiveShellRunner;
|
||||
import org.springframework.shell.jline.NonInteractiveShellRunner;
|
||||
import org.springframework.shell.jline.PromptProvider;
|
||||
import org.springframework.shell.jline.ScriptShellRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for {@link ShellRunnerAutoConfiguration}.
|
||||
*/
|
||||
class ShellRunnerAutoConfigurationTests {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(ShellRunnerAutoConfiguration.class))
|
||||
.withBean(Shell.class, () -> mock(Shell.class))
|
||||
.withBean(PromptProvider.class, () -> mock(PromptProvider.class))
|
||||
.withBean(LineReader.class, () -> mock(LineReader.class))
|
||||
.withBean(Parser.class, () -> mock(Parser.class))
|
||||
.withBean(ShellContext.class, () -> mock(ShellContext.class))
|
||||
.withBean(ParameterResolver.class, () -> mock(ParameterResolver.class));
|
||||
|
||||
@Nested
|
||||
class Interactive {
|
||||
@Test
|
||||
void enabledByDefault() {
|
||||
contextRunner.run(context -> assertThat(context).hasSingleBean(InteractiveShellRunner.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void disabledWhenPropertySet() {
|
||||
contextRunner.withPropertyValues("spring.shell.interactive.enabled:false")
|
||||
.run(context -> assertThat(context).doesNotHaveBean(InteractiveShellRunner.class));
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class NonInteractive {
|
||||
@Test
|
||||
void enabledByDefault() {
|
||||
contextRunner.run(context -> assertThat(context).hasSingleBean(NonInteractiveShellRunner.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void disabledWhenPropertySet() {
|
||||
contextRunner.withPropertyValues("spring.shell.noninteractive.enabled:false")
|
||||
.run(context -> assertThat(context).doesNotHaveBean(NonInteractiveShellRunner.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void canBeCustomized() {
|
||||
NonInteractiveShellRunnerCustomizer customizer = mock(NonInteractiveShellRunnerCustomizer.class);
|
||||
contextRunner.withBean(NonInteractiveShellRunnerCustomizer.class, () -> customizer)
|
||||
.run(context -> {
|
||||
NonInteractiveShellRunner runner = context.getBean(NonInteractiveShellRunner.class);
|
||||
verify(customizer).customize(runner);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class Script {
|
||||
@Test
|
||||
void enabledByDefault() {
|
||||
contextRunner.run(context -> assertThat(context).hasSingleBean(ScriptShellRunner.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void disabledWhenPropertySet() {
|
||||
contextRunner.withPropertyValues("spring.shell.script.enabled:false")
|
||||
.run(context -> assertThat(context).doesNotHaveBean(ScriptShellRunner.class));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user