29
29
import org .springframework .boot .SpringApplication ;
30
30
import org .springframework .boot .context .event .ApplicationEnvironmentPreparedEvent ;
31
31
import org .springframework .boot .context .event .ApplicationPreparedEvent ;
32
+ import org .springframework .boot .context .event .ApplicationReadyEvent ;
32
33
import org .springframework .boot .context .event .ApplicationStartedEvent ;
33
34
import org .springframework .boot .context .event .SpringApplicationEvent ;
34
35
import org .springframework .context .ConfigurableApplicationContext ;
@@ -99,7 +100,7 @@ public void overridePidFileWithSpring() throws Exception {
99
100
}
100
101
101
102
@ Test
102
- public void differentEventTypes () throws Exception {
103
+ public void tryEnvironmentPreparedEvent () throws Exception {
103
104
File file = this .temporaryFolder .newFile ();
104
105
SpringApplicationEvent event = createEnvironmentPreparedEvent ("spring.pid.file" ,
105
106
file .getAbsolutePath ());
@@ -111,6 +112,19 @@ public void differentEventTypes() throws Exception {
111
112
assertThat (FileCopyUtils .copyToString (new FileReader (file ))).isNotEmpty ();
112
113
}
113
114
115
+ @ Test
116
+ public void tryReadyEvent () throws Exception {
117
+ File file = this .temporaryFolder .newFile ();
118
+ SpringApplicationEvent event = createReadyEvent ("spring.pid.file" ,
119
+ file .getAbsolutePath ());
120
+ ApplicationPidFileWriter listener = new ApplicationPidFileWriter ();
121
+ listener .onApplicationEvent (event );
122
+ assertThat (FileCopyUtils .copyToString (new FileReader (file ))).isEmpty ();
123
+ listener .setTriggerEventType (ApplicationReadyEvent .class );
124
+ listener .onApplicationEvent (event );
125
+ assertThat (FileCopyUtils .copyToString (new FileReader (file ))).isNotEmpty ();
126
+ }
127
+
114
128
@ Test
115
129
public void withNoEnvironment () throws Exception {
116
130
File file = this .temporaryFolder .newFile ();
@@ -170,6 +184,15 @@ private SpringApplicationEvent createPreparedEvent(String propName,
170
184
context );
171
185
}
172
186
187
+ private SpringApplicationEvent createReadyEvent (String propName , String propValue ) {
188
+ ConfigurableEnvironment environment = createEnvironment (propName , propValue );
189
+ ConfigurableApplicationContext context = mock (
190
+ ConfigurableApplicationContext .class );
191
+ given (context .getEnvironment ()).willReturn (environment );
192
+ return new ApplicationReadyEvent (new SpringApplication (), new String [] {},
193
+ context );
194
+ }
195
+
173
196
private ConfigurableEnvironment createEnvironment (String propName , String propValue ) {
174
197
MockPropertySource propertySource = mockPropertySource (propName , propValue );
175
198
ConfigurableEnvironment environment = new StandardEnvironment ();
0 commit comments