From 88c693c6e15eced6e335aee276408280af709a11 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 10 Jun 2015 13:10:00 -0700 Subject: [PATCH] Provide a way to disable the Restarter Allow the Restarter to be disabled via a System property. Fixes gh-3173 --- .../restart/RestartApplicationListener.java | 16 +++++++++- .../boot/devtools/restart/Restarter.java | 24 ++++++++++++++ .../RestartApplicationListenerTests.java | 31 +++++++++++++------ 3 files changed, 61 insertions(+), 10 deletions(-) diff --git a/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/RestartApplicationListener.java b/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/RestartApplicationListener.java index dc8492b465..e1c5fda293 100644 --- a/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/RestartApplicationListener.java +++ b/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/RestartApplicationListener.java @@ -35,10 +35,12 @@ public class RestartApplicationListener implements ApplicationListener() { @@ -402,6 +418,14 @@ public class Restarter { return this.initialUrls; } + /** + * Initialize and disable restart support. + */ + public static void disable() { + initialize(NO_ARGS, false, RestartInitializer.NONE); + getInstance().setEnabled(false); + } + /** * Initialize restart support. See * {@link #initialize(String[], boolean, RestartInitializer)} for details. diff --git a/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestartApplicationListenerTests.java b/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestartApplicationListenerTests.java index 647d312713..ad18132905 100644 --- a/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestartApplicationListenerTests.java +++ b/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestartApplicationListenerTests.java @@ -23,8 +23,6 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.context.event.ApplicationFailedEvent; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.boot.context.event.ApplicationStartedEvent; -import org.springframework.boot.devtools.restart.RestartApplicationListener; -import org.springframework.boot.devtools.restart.Restarter; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.Ordered; import org.springframework.test.util.ReflectionTestUtils; @@ -42,10 +40,15 @@ import static org.mockito.Mockito.mock; */ public class RestartApplicationListenerTests { + private static final String ENABLED_PROPERTY = "spring.devtools.restart.enabled"; + + private static final String[] ARGS = new String[] { "a", "b", "c" }; + @Before @After public void cleanup() { Restarter.clearInstance(); + System.clearProperty(ENABLED_PROPERTY); } @Test @@ -57,11 +60,25 @@ public class RestartApplicationListenerTests { @Test public void initializeWithReady() throws Exception { testInitialize(false); + assertThat(ReflectionTestUtils.getField(Restarter.getInstance(), "args"), + equalTo((Object) ARGS)); + assertThat(Restarter.getInstance().isFinished(), equalTo(true)); } @Test public void initializeWithFail() throws Exception { testInitialize(true); + assertThat(ReflectionTestUtils.getField(Restarter.getInstance(), "args"), + equalTo((Object) ARGS)); + assertThat(Restarter.getInstance().isFinished(), equalTo(true)); + } + + @Test + public void disableWithSystemProperty() throws Exception { + System.setProperty(ENABLED_PROPERTY, "false"); + testInitialize(false); + assertThat(ReflectionTestUtils.getField(Restarter.getInstance(), "enabled"), + equalTo((Object) false)); } private void testInitialize(boolean failed) { @@ -69,21 +86,17 @@ public class RestartApplicationListenerTests { RestartApplicationListener listener = new RestartApplicationListener(); SpringApplication application = new SpringApplication(); ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class); - String[] args = new String[] { "a", "b", "c" }; - listener.onApplicationEvent(new ApplicationStartedEvent(application, args)); + listener.onApplicationEvent(new ApplicationStartedEvent(application, ARGS)); assertThat(Restarter.getInstance(), not(nullValue())); assertThat(Restarter.getInstance().isFinished(), equalTo(false)); - assertThat(ReflectionTestUtils.getField(Restarter.getInstance(), "args"), - equalTo((Object) args)); if (failed) { - listener.onApplicationEvent(new ApplicationFailedEvent(application, args, + listener.onApplicationEvent(new ApplicationFailedEvent(application, ARGS, context, new RuntimeException())); } else { - listener.onApplicationEvent(new ApplicationReadyEvent(application, args, + listener.onApplicationEvent(new ApplicationReadyEvent(application, ARGS, context)); } - assertThat(Restarter.getInstance().isFinished(), equalTo(true)); } }