Disable DevTools' post-processors and auto-config when running tests
Closes gh-5307pull/16593/head
parent
b164b16c21
commit
ac2b0093c7
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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."));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
/*
|
||||
* 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.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link OnEnabledDevtoolsCondition}.
|
||||
*
|
||||
* @author Madhura Bhave
|
||||
*/
|
||||
public class OnEnabledDevtoolsConditionTests {
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(TestConfiguration.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void outcomeWhenDevtoolsShouldBeEnabledIsTrueShouldMatch() throws Exception {
|
||||
Thread thread = new Thread(() -> {
|
||||
OnEnabledDevtoolsConditionTests.this.context.refresh();
|
||||
assertThat(OnEnabledDevtoolsConditionTests.this.context.containsBean("test"))
|
||||
.isTrue();
|
||||
});
|
||||
thread.start();
|
||||
thread.join();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void outcomeWhenDevtoolsShouldBeEnabledIsFalseShouldNotMatch() {
|
||||
OnEnabledDevtoolsConditionTests.this.context.refresh();
|
||||
assertThat(OnEnabledDevtoolsConditionTests.this.context.containsBean("test"))
|
||||
.isFalse();
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class TestConfiguration {
|
||||
|
||||
@Bean
|
||||
@Conditional(OnEnabledDevtoolsCondition.class)
|
||||
public String test() {
|
||||
return "hello";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue