Skip to content

Commit

Permalink
Merge branch '3.0.x' into 3.1.x
Browse files Browse the repository at this point in the history
Closes gh-37920
  • Loading branch information
wilkinsona committed Oct 18, 2023
2 parents 94500f4 + 85c186a commit 60a0b7e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-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.
Expand All @@ -23,6 +23,7 @@
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.boot.devtools.system.DevToolsEnablementDeducer;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
Expand Down Expand Up @@ -66,7 +67,14 @@ private void onApplicationStartingEvent(ApplicationStartingEvent event) {
String enabled = System.getProperty(ENABLED_PROPERTY);
RestartInitializer restartInitializer = null;
if (enabled == null) {
restartInitializer = new DefaultRestartInitializer();
if (implicitlyEnableRestart()) {
restartInitializer = new DefaultRestartInitializer();
}
else {
logger.info("Restart disabled due to context in which it is running");
Restarter.disable();
return;
}
}
else if (Boolean.parseBoolean(enabled)) {
restartInitializer = new DefaultRestartInitializer() {
Expand Down Expand Up @@ -96,6 +104,10 @@ protected boolean isDevelopmentClassLoader(ClassLoader classLoader) {
}
}

boolean implicitlyEnableRestart() {
return DevToolsEnablementDeducer.shouldEnable(Thread.currentThread());
}

private void onApplicationPreparedEvent(ApplicationPreparedEvent event) {
Restarter.getInstance().prepare(event.getApplicationContext());
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-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.
Expand Down Expand Up @@ -408,7 +408,7 @@ boolean isFinished() {
}

void prepare(ConfigurableApplicationContext applicationContext) {
if (applicationContext != null && applicationContext.getParent() != null) {
if (!this.enabled || (applicationContext != null && applicationContext.getParent() != null)) {
return;
}
if (applicationContext instanceof GenericApplicationContext genericContext) {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-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.
Expand Down Expand Up @@ -66,15 +66,15 @@ void isHighestPriority() {

@Test
void initializeWithReady() {
testInitialize(false);
testInitialize(false, new ImplicitlyEnabledRestartApplicationListener());
assertThat(Restarter.getInstance()).hasFieldOrPropertyWithValue("args", ARGS);
assertThat(Restarter.getInstance().isFinished()).isTrue();
assertThat((List<?>) ReflectionTestUtils.getField(Restarter.getInstance(), "rootContexts")).isNotEmpty();
}

@Test
void initializeWithFail() {
testInitialize(true);
testInitialize(true, new ImplicitlyEnabledRestartApplicationListener());
assertThat(Restarter.getInstance()).hasFieldOrPropertyWithValue("args", ARGS);
assertThat(Restarter.getInstance().isFinished()).isTrue();
assertThat((List<?>) ReflectionTestUtils.getField(Restarter.getInstance(), "rootContexts")).isEmpty();
Expand All @@ -83,22 +83,36 @@ void initializeWithFail() {
@Test
void disableWithSystemProperty(CapturedOutput output) {
System.setProperty(ENABLED_PROPERTY, "false");
testInitialize(false);
testInitialize(false, new ImplicitlyEnabledRestartApplicationListener());
assertThat(Restarter.getInstance()).hasFieldOrPropertyWithValue("enabled", false);
assertThat(output).contains("Restart disabled due to System property");
}

@Test
void enableWithSystemProperty(CapturedOutput output) {
System.setProperty(ENABLED_PROPERTY, "true");
testInitialize(false);
testInitialize(false, new ImplicitlyEnabledRestartApplicationListener());
assertThat(Restarter.getInstance()).hasFieldOrPropertyWithValue("enabled", true);
assertThat(output).contains("Restart enabled irrespective of application packaging due to System property");
}

private void testInitialize(boolean failed) {
@Test
void enableWithSystemPropertyWhenImplicitlyDisabled(CapturedOutput output) {
System.setProperty(ENABLED_PROPERTY, "true");
testInitialize(false, new RestartApplicationListener());
assertThat(Restarter.getInstance()).hasFieldOrPropertyWithValue("enabled", true);
assertThat(output).contains("Restart enabled irrespective of application packaging due to System property");
}

@Test
void implicitlyDisabledInTests(CapturedOutput output) {
testInitialize(false, new RestartApplicationListener());
assertThat(Restarter.getInstance()).hasFieldOrPropertyWithValue("enabled", false);
assertThat(output).contains("Restart disabled due to context in which it is running");
}

private void testInitialize(boolean failed, RestartApplicationListener listener) {
Restarter.clearInstance();
RestartApplicationListener listener = new RestartApplicationListener();
DefaultBootstrapContext bootstrapContext = new DefaultBootstrapContext();
SpringApplication application = new SpringApplication();
ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class);
Expand All @@ -114,4 +128,13 @@ private void testInitialize(boolean failed) {
}
}

private static class ImplicitlyEnabledRestartApplicationListener extends RestartApplicationListener {

@Override
boolean implicitlyEnableRestart() {
return true;
}

}

}
Expand Up @@ -24,6 +24,7 @@
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicBoolean;

import org.assertj.core.api.InstanceOfAssertFactories;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -37,13 +38,15 @@
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import static org.assertj.core.api.Assertions.as;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
Expand Down Expand Up @@ -90,6 +93,14 @@ void testRestart(CapturedOutput output) {
});
}

@Test
void testDisabled() {
Restarter.disable();
ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class);
Restarter.getInstance().prepare(context);
assertThat(Restarter.getInstance()).extracting("rootContexts", as(InstanceOfAssertFactories.LIST)).isEmpty();
}

@Test
@SuppressWarnings("rawtypes")
void getOrAddAttributeWithNewAttribute() {
Expand Down

0 comments on commit 60a0b7e

Please sign in to comment.