|
|
|
@ -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));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|