From 1f0e311cb885f52fcc01a1a049f841a241db4a34 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 18 Oct 2023 09:52:47 +0100 Subject: [PATCH] Completely disable restart when running tests Fixes gh-37373 --- .../restart/RestartApplicationListener.java | 16 +++++++- .../boot/devtools/restart/Restarter.java | 4 +- .../RestartApplicationListenerTests.java | 37 +++++++++++++++---- .../boot/devtools/restart/RestarterTests.java | 11 ++++++ 4 files changed, 57 insertions(+), 11 deletions(-) diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/RestartApplicationListener.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/RestartApplicationListener.java index 6969c99e61a..ee293bd7f0d 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/RestartApplicationListener.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/RestartApplicationListener.java @@ -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. @@ -23,6 +23,7 @@ import org.springframework.boot.context.event.ApplicationFailedEvent; 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; @@ -66,7 +67,14 @@ public class RestartApplicationListener implements ApplicationListener) ReflectionTestUtils.getField(Restarter.getInstance(), "rootContexts")).isNotEmpty(); @@ -74,7 +74,7 @@ class RestartApplicationListenerTests { @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(); @@ -83,7 +83,7 @@ class RestartApplicationListenerTests { @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"); } @@ -91,14 +91,28 @@ class RestartApplicationListenerTests { @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); @@ -114,4 +128,13 @@ class RestartApplicationListenerTests { } } + private static class ImplicitlyEnabledRestartApplicationListener extends RestartApplicationListener { + + @Override + boolean implicitlyEnableRestart() { + return true; + } + + } + } diff --git a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestarterTests.java b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestarterTests.java index 7c329dd7869..75689433fff 100644 --- a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestarterTests.java +++ b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestarterTests.java @@ -24,6 +24,7 @@ import java.util.Collections; 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; @@ -37,6 +38,7 @@ import org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles; 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; @@ -44,6 +46,7 @@ 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; @@ -90,6 +93,14 @@ class RestarterTests { }); } + @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() {