1
1
/*
2
- * Copyright 2012-2014 the original author or authors.
2
+ * Copyright 2012-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .boot ;
18
18
19
+ import java .util .ArrayList ;
19
20
import java .util .Arrays ;
20
21
import java .util .Collections ;
21
22
import java .util .LinkedHashSet ;
23
+ import java .util .List ;
22
24
import java .util .Set ;
23
25
import java .util .concurrent .atomic .AtomicReference ;
24
26
33
35
import org .springframework .beans .factory .support .DefaultBeanNameGenerator ;
34
36
import org .springframework .boot .context .embedded .AnnotationConfigEmbeddedWebApplicationContext ;
35
37
import org .springframework .boot .context .embedded .jetty .JettyEmbeddedServletContainerFactory ;
38
+ import org .springframework .boot .context .event .ApplicationEnvironmentPreparedEvent ;
36
39
import org .springframework .boot .context .event .ApplicationPreparedEvent ;
40
+ import org .springframework .boot .context .event .ApplicationReadyEvent ;
41
+ import org .springframework .boot .context .event .ApplicationStartedEvent ;
37
42
import org .springframework .context .ApplicationContext ;
38
43
import org .springframework .context .ApplicationContextAware ;
39
44
import org .springframework .context .ApplicationContextInitializer ;
85
90
* @author Dave Syer
86
91
* @author Andy Wilkinson
87
92
* @author Christian Dupuis
93
+ * @author Stephane Nicoll
88
94
*/
89
95
public class SpringApplicationTests {
90
96
@@ -218,6 +224,22 @@ public void initialize(ConfigurableApplicationContext context) {
218
224
assertThat (getEnvironment ().getProperty ("foo" ), equalTo ("bar" ));
219
225
}
220
226
227
+ @ Test
228
+ public void applicationRunningEventListener () {
229
+ SpringApplication application = new SpringApplication (ExampleConfig .class );
230
+ application .setWebEnvironment (false );
231
+ final AtomicReference <ApplicationContext > reference = new AtomicReference <ApplicationContext >();
232
+ class ApplicationReadyEventListener implements ApplicationListener <ApplicationReadyEvent > {
233
+ @ Override
234
+ public void onApplicationEvent (ApplicationReadyEvent event ) {
235
+ reference .set (event .getApplicationContext ());
236
+ }
237
+ }
238
+ application .addListeners (new ApplicationReadyEventListener ());
239
+ this .context = application .run ("--foo=bar" );
240
+ assertThat (this .context , sameInstance (reference .get ()));
241
+ }
242
+
221
243
@ Test
222
244
public void contextRefreshedEventListener () throws Exception {
223
245
SpringApplication application = new SpringApplication (ExampleConfig .class );
@@ -236,6 +258,27 @@ public void onApplicationEvent(ContextRefreshedEvent event) {
236
258
assertThat (getEnvironment ().getProperty ("foo" ), equalTo ("bar" ));
237
259
}
238
260
261
+ @ Test
262
+ public void eventsOrder () {
263
+ SpringApplication application = new SpringApplication (ExampleConfig .class );
264
+ application .setWebEnvironment (false );
265
+ final List <ApplicationEvent > events = new ArrayList <ApplicationEvent >();
266
+ class ApplicationRunningEventListener implements ApplicationListener <ApplicationEvent > {
267
+ @ Override
268
+ public void onApplicationEvent (ApplicationEvent event ) {
269
+ events .add ((event ));
270
+ }
271
+ }
272
+ application .addListeners (new ApplicationRunningEventListener ());
273
+ this .context = application .run ();
274
+ assertThat (5 , is (events .size ()));
275
+ assertThat (events .get (0 ), is (instanceOf (ApplicationStartedEvent .class )));
276
+ assertThat (events .get (1 ), is (instanceOf (ApplicationEnvironmentPreparedEvent .class )));
277
+ assertThat (events .get (2 ), is (instanceOf (ApplicationPreparedEvent .class )));
278
+ assertThat (events .get (3 ), is (instanceOf (ContextRefreshedEvent .class )));
279
+ assertThat (events .get (4 ), is (instanceOf (ApplicationReadyEvent .class )));
280
+ }
281
+
239
282
@ Test
240
283
public void defaultApplicationContext () throws Exception {
241
284
SpringApplication application = new SpringApplication (ExampleConfig .class );
0 commit comments