From bc58d4456f6fd61593ef3e3321f61550c37de68a Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Tue, 23 Oct 2018 13:23:04 -0700 Subject: [PATCH] Rationalize DefaultRestartInitializerTests Closes gh-14927 --- .../restart/DefaultRestartInitializer.java | 2 +- .../DefaultRestartInitializerTests.java | 89 +++++++------------ 2 files changed, 32 insertions(+), 59 deletions(-) diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/DefaultRestartInitializer.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/DefaultRestartInitializer.java index 63b4b3fcd4c..3526a024201 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/DefaultRestartInitializer.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/DefaultRestartInitializer.java @@ -73,7 +73,7 @@ public class DefaultRestartInitializer implements RestartInitializer { * @return {@code true} if the stack element means that the initializer should be * skipped */ - protected boolean isSkippedStackElement(StackTraceElement element) { + private boolean isSkippedStackElement(StackTraceElement element) { for (String skipped : SKIPPED_STACK_ELEMENTS) { if (element.getClassName().startsWith(skipped)) { return true; diff --git a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/DefaultRestartInitializerTests.java b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/DefaultRestartInitializerTests.java index a22f1614250..ba8bb8e3743 100644 --- a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/DefaultRestartInitializerTests.java +++ b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/DefaultRestartInitializerTests.java @@ -16,87 +16,83 @@ package org.springframework.boot.devtools.restart; -import java.net.URL; - import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; -import static org.hamcrest.Matchers.nullValue; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; /** * Tests for {@link DefaultRestartInitializer}. * * @author Phillip Webb * @author Andy Wilkinson + * @author Madhura Bhave */ public class DefaultRestartInitializerTests { @Test - public void nullForTests() { - MockRestartInitializer initializer = new MockRestartInitializer(true); - assertThat(initializer.getInitialUrls(Thread.currentThread())).isNull(); + public void jUnitStackShouldReturnNull() { + testSkippedStacks("org.junit.runners.Something"); + } + + @Test + public void springTestStackShouldReturnNull() { + testSkippedStacks("org.springframework.boot.test.Something"); + } + + @Test + public void cucumberStackShouldReturnNull() { + testSkippedStacks("cucumber.runtime.Runtime.run"); } @Test - public void validMainThread() { - MockRestartInitializer initializer = new MockRestartInitializer(false); + public void validMainThreadShouldReturnUrls() { + DefaultRestartInitializer initializer = new DefaultRestartInitializer(); ClassLoader classLoader = new MockAppClassLoader(getClass().getClassLoader()); Thread thread = new Thread(); thread.setName("main"); thread.setContextClassLoader(classLoader); - assertThat(initializer.isMain(thread)).isTrue(); - assertThat(initializer.getInitialUrls(thread)).isNotEqualTo(nullValue()); + assertThat(initializer.getInitialUrls(thread)).isNotNull(); } @Test - public void threadNotNamedMain() { - MockRestartInitializer initializer = new MockRestartInitializer(false); + public void threadNotNamedMainShouldReturnNull() { + DefaultRestartInitializer initializer = new DefaultRestartInitializer(); ClassLoader classLoader = new MockAppClassLoader(getClass().getClassLoader()); Thread thread = new Thread(); thread.setName("buscuit"); thread.setContextClassLoader(classLoader); - assertThat(initializer.isMain(thread)).isFalse(); assertThat(initializer.getInitialUrls(thread)).isNull(); } @Test public void threadNotUsingAppClassLoader() { - MockRestartInitializer initializer = new MockRestartInitializer(false); + DefaultRestartInitializer initializer = new DefaultRestartInitializer(); ClassLoader classLoader = new MockLauncherClassLoader( getClass().getClassLoader()); Thread thread = new Thread(); thread.setName("main"); thread.setContextClassLoader(classLoader); - assertThat(initializer.isMain(thread)).isFalse(); assertThat(initializer.getInitialUrls(thread)).isNull(); } - @Test - public void skipsDueToJUnitStacks() { - testSkipStack("org.junit.runners.Something", true); - } - - @Test - public void skipsDueToSpringTest() { - testSkipStack("org.springframework.boot.test.Something", true); - } - - @Test - public void skipsDueToCucumber() { - testSkipStack("cucumber.runtime.Runtime.run", true); - } - @Test public void urlsCanBeRetrieved() { assertThat(new DefaultRestartInitializer().getUrls(Thread.currentThread())) .isNotEmpty(); } - private void testSkipStack(String className, boolean expected) { - MockRestartInitializer initializer = new MockRestartInitializer(true); - StackTraceElement element = new StackTraceElement(className, "someMethod", - "someFile", 123); - assertThat(initializer.isSkippedStackElement(element)).isEqualTo(expected); + protected void testSkippedStacks(String s) { + DefaultRestartInitializer initializer = new DefaultRestartInitializer(); + ClassLoader classLoader = new MockAppClassLoader(getClass().getClassLoader()); + Thread thread = mock(Thread.class); + thread.setName("main"); + StackTraceElement element = new StackTraceElement(s, "someMethod", "someFile", + 123); + given(thread.getStackTrace()).willReturn(new StackTraceElement[] { element }); + given(thread.getContextClassLoader()).willReturn(classLoader); + assertThat(initializer.getInitialUrls(thread)).isEqualTo(null); } private static class MockAppClassLoader extends ClassLoader { @@ -115,27 +111,4 @@ public class DefaultRestartInitializerTests { } - private static class MockRestartInitializer extends DefaultRestartInitializer { - - private final boolean considerStackElements; - - MockRestartInitializer(boolean considerStackElements) { - this.considerStackElements = considerStackElements; - } - - @Override - protected boolean isSkippedStackElement(StackTraceElement element) { - if (!this.considerStackElements) { - return false; - } - return true; - } - - @Override - protected URL[] getUrls(Thread thread) { - return new URL[0]; - } - - } - }