16
16
17
17
package org .springframework .boot .devtools .restart ;
18
18
19
- import java .net .URL ;
20
-
21
19
import org .junit .Test ;
22
20
23
21
import static org .assertj .core .api .Assertions .assertThat ;
24
- import static org .hamcrest .Matchers .nullValue ;
22
+ import static org .mockito .BDDMockito .given ;
23
+ import static org .mockito .Mockito .mock ;
25
24
26
25
/**
27
26
* Tests for {@link DefaultRestartInitializer}.
28
27
*
29
28
* @author Phillip Webb
30
29
* @author Andy Wilkinson
30
+ * @author Madhura Bhave
31
31
*/
32
32
public class DefaultRestartInitializerTests {
33
33
34
34
@ Test
35
- public void nullForTests () {
36
- MockRestartInitializer initializer = new MockRestartInitializer (true );
37
- assertThat (initializer .getInitialUrls (Thread .currentThread ())).isNull ();
35
+ public void jUnitStackShouldReturnNull () {
36
+ testSkippedStacks ("org.junit.runners.Something" );
37
+ }
38
+
39
+ @ Test
40
+ public void springTestStackShouldReturnNull () {
41
+ testSkippedStacks ("org.springframework.boot.test.Something" );
42
+ }
43
+
44
+ @ Test
45
+ public void cucumberStackShouldReturnNull () {
46
+ testSkippedStacks ("cucumber.runtime.Runtime.run" );
38
47
}
39
48
40
49
@ Test
41
- public void validMainThread () {
42
- MockRestartInitializer initializer = new MockRestartInitializer ( false );
50
+ public void validMainThreadShouldReturnUrls () {
51
+ DefaultRestartInitializer initializer = new DefaultRestartInitializer ( );
43
52
ClassLoader classLoader = new MockAppClassLoader (getClass ().getClassLoader ());
44
53
Thread thread = new Thread ();
45
54
thread .setName ("main" );
46
55
thread .setContextClassLoader (classLoader );
47
- assertThat (initializer .isMain (thread )).isTrue ();
48
- assertThat (initializer .getInitialUrls (thread )).isNotEqualTo (nullValue ());
56
+ assertThat (initializer .getInitialUrls (thread )).isNotNull ();
49
57
}
50
58
51
59
@ Test
52
- public void threadNotNamedMain () {
53
- MockRestartInitializer initializer = new MockRestartInitializer ( false );
60
+ public void threadNotNamedMainShouldReturnNull () {
61
+ DefaultRestartInitializer initializer = new DefaultRestartInitializer ( );
54
62
ClassLoader classLoader = new MockAppClassLoader (getClass ().getClassLoader ());
55
63
Thread thread = new Thread ();
56
64
thread .setName ("buscuit" );
57
65
thread .setContextClassLoader (classLoader );
58
- assertThat (initializer .isMain (thread )).isFalse ();
59
66
assertThat (initializer .getInitialUrls (thread )).isNull ();
60
67
}
61
68
62
69
@ Test
63
70
public void threadNotUsingAppClassLoader () {
64
- MockRestartInitializer initializer = new MockRestartInitializer ( false );
71
+ DefaultRestartInitializer initializer = new DefaultRestartInitializer ( );
65
72
ClassLoader classLoader = new MockLauncherClassLoader (
66
73
getClass ().getClassLoader ());
67
74
Thread thread = new Thread ();
68
75
thread .setName ("main" );
69
76
thread .setContextClassLoader (classLoader );
70
- assertThat (initializer .isMain (thread )).isFalse ();
71
77
assertThat (initializer .getInitialUrls (thread )).isNull ();
72
78
}
73
79
74
- @ Test
75
- public void skipsDueToJUnitStacks () {
76
- testSkipStack ("org.junit.runners.Something" , true );
77
- }
78
-
79
- @ Test
80
- public void skipsDueToSpringTest () {
81
- testSkipStack ("org.springframework.boot.test.Something" , true );
82
- }
83
-
84
- @ Test
85
- public void skipsDueToCucumber () {
86
- testSkipStack ("cucumber.runtime.Runtime.run" , true );
87
- }
88
-
89
80
@ Test
90
81
public void urlsCanBeRetrieved () {
91
82
assertThat (new DefaultRestartInitializer ().getUrls (Thread .currentThread ()))
92
83
.isNotEmpty ();
93
84
}
94
85
95
- private void testSkipStack (String className , boolean expected ) {
96
- MockRestartInitializer initializer = new MockRestartInitializer (true );
97
- StackTraceElement element = new StackTraceElement (className , "someMethod" ,
98
- "someFile" , 123 );
99
- assertThat (initializer .isSkippedStackElement (element )).isEqualTo (expected );
86
+ protected void testSkippedStacks (String s ) {
87
+ DefaultRestartInitializer initializer = new DefaultRestartInitializer ();
88
+ ClassLoader classLoader = new MockAppClassLoader (getClass ().getClassLoader ());
89
+ Thread thread = mock (Thread .class );
90
+ thread .setName ("main" );
91
+ StackTraceElement element = new StackTraceElement (s , "someMethod" , "someFile" ,
92
+ 123 );
93
+ given (thread .getStackTrace ()).willReturn (new StackTraceElement [] { element });
94
+ given (thread .getContextClassLoader ()).willReturn (classLoader );
95
+ assertThat (initializer .getInitialUrls (thread )).isEqualTo (null );
100
96
}
101
97
102
98
private static class MockAppClassLoader extends ClassLoader {
@@ -115,27 +111,4 @@ private static class MockLauncherClassLoader extends ClassLoader {
115
111
116
112
}
117
113
118
- private static class MockRestartInitializer extends DefaultRestartInitializer {
119
-
120
- private final boolean considerStackElements ;
121
-
122
- MockRestartInitializer (boolean considerStackElements ) {
123
- this .considerStackElements = considerStackElements ;
124
- }
125
-
126
- @ Override
127
- protected boolean isSkippedStackElement (StackTraceElement element ) {
128
- if (!this .considerStackElements ) {
129
- return false ;
130
- }
131
- return true ;
132
- }
133
-
134
- @ Override
135
- protected URL [] getUrls (Thread thread ) {
136
- return new URL [0 ];
137
- }
138
-
139
- }
140
-
141
114
}
0 commit comments