Disable DevTools' post-processors and auto-config when running tests
Closes gh-5307
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2012-2019 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.boot.devtools;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Utility to deduce if Devtools should be enabled in the current context.
|
||||
*
|
||||
* @author Madhura Bhave
|
||||
*/
|
||||
public final class DevtoolsEnablementDeducer {
|
||||
|
||||
private static final Set<String> SKIPPED_STACK_ELEMENTS;
|
||||
|
||||
static {
|
||||
Set<String> skipped = new LinkedHashSet<>();
|
||||
skipped.add("org.junit.runners.");
|
||||
skipped.add("org.junit.platform.");
|
||||
skipped.add("org.springframework.boot.test.");
|
||||
skipped.add("cucumber.runtime.");
|
||||
SKIPPED_STACK_ELEMENTS = Collections.unmodifiableSet(skipped);
|
||||
}
|
||||
|
||||
private DevtoolsEnablementDeducer() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a specific {@link StackTraceElement} in the current thread's stacktrace
|
||||
* should cause devtools to be disabled.
|
||||
* @param thread the current thread
|
||||
* @return {@code true} if devtools should be enabled skipped
|
||||
*/
|
||||
public static boolean shouldEnable(Thread thread) {
|
||||
for (StackTraceElement element : thread.getStackTrace()) {
|
||||
if (isSkippedStackElement(element)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean isSkippedStackElement(StackTraceElement element) {
|
||||
for (String skipped : SKIPPED_STACK_ELEMENTS) {
|
||||
if (element.getClassName().startsWith(skipped)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -55,7 +55,7 @@ import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
* @since 1.3.3
|
||||
*/
|
||||
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
|
||||
@Conditional(DevToolsDataSourceCondition.class)
|
||||
@Conditional({ OnEnabledDevtoolsCondition.class, DevToolsDataSourceCondition.class })
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
public class DevToolsDataSourceAutoConfiguration {
|
||||
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2012-2019 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.boot.devtools.autoconfigure;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
|
||||
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
|
||||
import org.springframework.boot.devtools.DevtoolsEnablementDeducer;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
|
||||
/**
|
||||
* A condition that checks if DevTools should be enabled.
|
||||
*
|
||||
* @author Madhura Bhave
|
||||
*/
|
||||
public class OnEnabledDevtoolsCondition extends SpringBootCondition {
|
||||
|
||||
@Override
|
||||
public ConditionOutcome getMatchOutcome(ConditionContext context,
|
||||
AnnotatedTypeMetadata metadata) {
|
||||
ConditionMessage.Builder message = ConditionMessage.forCondition("Devtools");
|
||||
boolean shouldEnable = DevtoolsEnablementDeducer
|
||||
.shouldEnable(Thread.currentThread());
|
||||
if (!shouldEnable) {
|
||||
return ConditionOutcome.noMatch(
|
||||
message.because("devtools is disabled for current context."));
|
||||
}
|
||||
return ConditionOutcome.match(message.because("devtools enabled."));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -43,6 +43,7 @@ import org.springframework.boot.devtools.restart.server.HttpRestartServer;
|
||||
import org.springframework.boot.devtools.restart.server.HttpRestartServerHandler;
|
||||
import org.springframework.boot.devtools.restart.server.SourceFolderUrlFilter;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.server.ServerHttpRequest;
|
||||
|
||||
@@ -55,6 +56,7 @@ import org.springframework.http.server.ServerHttpRequest;
|
||||
* @since 1.3.0
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Conditional(OnEnabledDevtoolsCondition.class)
|
||||
@ConditionalOnProperty(prefix = "spring.devtools.remote", name = "secret")
|
||||
@ConditionalOnClass({ Filter.class, ServerHttpRequest.class })
|
||||
@EnableConfigurationProperties({ ServerProperties.class, DevToolsProperties.class })
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.devtools.DevtoolsEnablementDeducer;
|
||||
import org.springframework.boot.env.EnvironmentPostProcessor;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.PropertiesPropertySource;
|
||||
@@ -43,18 +44,20 @@ public class DevToolsHomePropertiesPostProcessor implements EnvironmentPostProce
|
||||
@Override
|
||||
public void postProcessEnvironment(ConfigurableEnvironment environment,
|
||||
SpringApplication application) {
|
||||
File home = getHomeFolder();
|
||||
File propertyFile = (home != null) ? new File(home, FILE_NAME) : null;
|
||||
if (propertyFile != null && propertyFile.exists() && propertyFile.isFile()) {
|
||||
FileSystemResource resource = new FileSystemResource(propertyFile);
|
||||
Properties properties;
|
||||
try {
|
||||
properties = PropertiesLoaderUtils.loadProperties(resource);
|
||||
environment.getPropertySources().addFirst(
|
||||
new PropertiesPropertySource("devtools-local", properties));
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new IllegalStateException("Unable to load " + FILE_NAME, ex);
|
||||
if (DevtoolsEnablementDeducer.shouldEnable(Thread.currentThread())) {
|
||||
File home = getHomeFolder();
|
||||
File propertyFile = (home != null) ? new File(home, FILE_NAME) : null;
|
||||
if (propertyFile != null && propertyFile.exists() && propertyFile.isFile()) {
|
||||
FileSystemResource resource = new FileSystemResource(propertyFile);
|
||||
Properties properties;
|
||||
try {
|
||||
properties = PropertiesLoaderUtils.loadProperties(resource);
|
||||
environment.getPropertySources().addFirst(
|
||||
new PropertiesPropertySource("devtools-local", properties));
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new IllegalStateException("Unable to load " + FILE_NAME, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.util.Map;
|
||||
import org.apache.commons.logging.Log;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.devtools.DevtoolsEnablementDeducer;
|
||||
import org.springframework.boot.devtools.logger.DevToolsLogFactory;
|
||||
import org.springframework.boot.devtools.restart.Restarter;
|
||||
import org.springframework.boot.env.EnvironmentPostProcessor;
|
||||
@@ -79,7 +80,8 @@ public class DevToolsPropertyDefaultsPostProcessor implements EnvironmentPostPro
|
||||
@Override
|
||||
public void postProcessEnvironment(ConfigurableEnvironment environment,
|
||||
SpringApplication application) {
|
||||
if (isLocalApplication(environment)) {
|
||||
if (DevtoolsEnablementDeducer.shouldEnable(Thread.currentThread())
|
||||
&& isLocalApplication(environment)) {
|
||||
if (canAddProperties(environment)) {
|
||||
logger.info("Devtools property defaults active! Set '" + ENABLED
|
||||
+ "' to 'false' to disable");
|
||||
|
||||
@@ -17,9 +17,8 @@
|
||||
package org.springframework.boot.devtools.restart;
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.boot.devtools.DevtoolsEnablementDeducer;
|
||||
|
||||
/**
|
||||
* Default {@link RestartInitializer} that only enable initial restart when running a
|
||||
@@ -32,26 +31,13 @@ import java.util.Set;
|
||||
*/
|
||||
public class DefaultRestartInitializer implements RestartInitializer {
|
||||
|
||||
private static final Set<String> SKIPPED_STACK_ELEMENTS;
|
||||
|
||||
static {
|
||||
Set<String> skipped = new LinkedHashSet<>();
|
||||
skipped.add("org.junit.runners.");
|
||||
skipped.add("org.junit.platform.");
|
||||
skipped.add("org.springframework.boot.test.");
|
||||
skipped.add("cucumber.runtime.");
|
||||
SKIPPED_STACK_ELEMENTS = Collections.unmodifiableSet(skipped);
|
||||
}
|
||||
|
||||
@Override
|
||||
public URL[] getInitialUrls(Thread thread) {
|
||||
if (!isMain(thread)) {
|
||||
return null;
|
||||
}
|
||||
for (StackTraceElement element : thread.getStackTrace()) {
|
||||
if (isSkippedStackElement(element)) {
|
||||
return null;
|
||||
}
|
||||
if (!DevtoolsEnablementDeducer.shouldEnable(thread)) {
|
||||
return null;
|
||||
}
|
||||
return getUrls(thread);
|
||||
}
|
||||
@@ -67,22 +53,6 @@ public class DefaultRestartInitializer implements RestartInitializer {
|
||||
.getClass().getName().contains("AppClassLoader");
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a specific {@link StackTraceElement} should cause the initializer to be
|
||||
* skipped.
|
||||
* @param element the stack element to check
|
||||
* @return {@code true} if the stack element means that the initializer should be
|
||||
* skipped
|
||||
*/
|
||||
private boolean isSkippedStackElement(StackTraceElement element) {
|
||||
for (String skipped : SKIPPED_STACK_ELEMENTS) {
|
||||
if (element.getClassName().startsWith(skipped)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the URLs that should be used with initialization.
|
||||
* @param thread the source thread
|
||||
|
||||
Reference in New Issue
Block a user