|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2015 the original author or authors. |
| 2 | + * Copyright 2012-2016 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.
|
|
30 | 30 | import org.junit.rules.ExpectedException;
|
31 | 31 |
|
32 | 32 | import org.springframework.boot.SpringApplication;
|
| 33 | +import org.springframework.boot.context.event.ApplicationReadyEvent; |
33 | 34 | import org.springframework.context.ApplicationListener;
|
34 | 35 | import org.springframework.context.ConfigurableApplicationContext;
|
35 | 36 | import org.springframework.context.annotation.Bean;
|
36 | 37 | import org.springframework.context.annotation.Configuration;
|
37 | 38 | import org.springframework.context.event.ContextRefreshedEvent;
|
| 39 | +import org.springframework.test.util.ReflectionTestUtils; |
38 | 40 |
|
39 | 41 | import static org.hamcrest.CoreMatchers.is;
|
40 | 42 | import static org.hamcrest.CoreMatchers.nullValue;
|
| 43 | +import static org.junit.Assert.assertFalse; |
41 | 44 | import static org.junit.Assert.assertThat;
|
| 45 | +import static org.junit.Assert.assertTrue; |
| 46 | +import static org.mockito.Mockito.mock; |
42 | 47 |
|
43 | 48 | /**
|
44 | 49 | * Tests for {@link SpringApplicationAdminMXBeanRegistrar}.
|
45 | 50 | *
|
46 | 51 | * @author Stephane Nicoll
|
| 52 | + * @author Andy Wilkinson |
47 | 53 | */
|
48 | 54 | public class SpringApplicationAdminMXBeanRegistrarTests {
|
49 | 55 |
|
@@ -89,6 +95,25 @@ public void onApplicationEvent(ContextRefreshedEvent event) {
|
89 | 95 | assertThat(isApplicationReady(objectName), is(true));
|
90 | 96 | }
|
91 | 97 |
|
| 98 | + @Test |
| 99 | + public void eventsFromOtherContextsAreIgnored() throws MalformedObjectNameException { |
| 100 | + SpringApplicationAdminMXBeanRegistrar registrar = new SpringApplicationAdminMXBeanRegistrar( |
| 101 | + OBJECT_NAME); |
| 102 | + ConfigurableApplicationContext context = mock( |
| 103 | + ConfigurableApplicationContext.class); |
| 104 | + registrar.setApplicationContext(context); |
| 105 | + registrar.onApplicationEvent(new ApplicationReadyEvent(new SpringApplication(), |
| 106 | + null, mock(ConfigurableApplicationContext.class))); |
| 107 | + assertFalse(isApplicationReady(registrar)); |
| 108 | + registrar.onApplicationEvent( |
| 109 | + new ApplicationReadyEvent(new SpringApplication(), null, context)); |
| 110 | + assertTrue(isApplicationReady(registrar)); |
| 111 | + } |
| 112 | + |
| 113 | + private boolean isApplicationReady(SpringApplicationAdminMXBeanRegistrar registrar) { |
| 114 | + return (Boolean) ReflectionTestUtils.getField(registrar, "ready"); |
| 115 | + } |
| 116 | + |
92 | 117 | @Test
|
93 | 118 | public void environmentIsExposed() {
|
94 | 119 | final ObjectName objectName = createObjectName(OBJECT_NAME);
|
|
0 commit comments