@ -1,5 +1,5 @@
/ *
* Copyright 2012 - 201 5 the original author or authors .
* Copyright 2012 - 201 6 the original author or authors .
*
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
@ -30,20 +30,26 @@ import org.junit.Test;
import org.junit.rules.ExpectedException ;
import org.springframework.boot.SpringApplication ;
import org.springframework.boot.context.event.ApplicationReadyEvent ;
import org.springframework.context.ApplicationListener ;
import org.springframework.context.ConfigurableApplicationContext ;
import org.springframework.context.annotation.Bean ;
import org.springframework.context.annotation.Configuration ;
import org.springframework.context.event.ContextRefreshedEvent ;
import org.springframework.test.util.ReflectionTestUtils ;
import static org.hamcrest.CoreMatchers.is ;
import static org.hamcrest.CoreMatchers.nullValue ;
import static org.junit.Assert.assertFalse ;
import static org.junit.Assert.assertThat ;
import static org.junit.Assert.assertTrue ;
import static org.mockito.Mockito.mock ;
/ * *
* Tests for { @link SpringApplicationAdminMXBeanRegistrar } .
*
* @author Stephane Nicoll
* @author Andy Wilkinson
* /
public class SpringApplicationAdminMXBeanRegistrarTests {
@ -89,6 +95,25 @@ public class SpringApplicationAdminMXBeanRegistrarTests {
assertThat ( isApplicationReady ( objectName ) , is ( true ) ) ;
}
@Test
public void eventsFromOtherContextsAreIgnored ( ) throws MalformedObjectNameException {
SpringApplicationAdminMXBeanRegistrar registrar = new SpringApplicationAdminMXBeanRegistrar (
OBJECT_NAME ) ;
ConfigurableApplicationContext context = mock (
ConfigurableApplicationContext . class ) ;
registrar . setApplicationContext ( context ) ;
registrar . onApplicationEvent ( new ApplicationReadyEvent ( new SpringApplication ( ) ,
null , mock ( ConfigurableApplicationContext . class ) ) ) ;
assertFalse ( isApplicationReady ( registrar ) ) ;
registrar . onApplicationEvent (
new ApplicationReadyEvent ( new SpringApplication ( ) , null , context ) ) ;
assertTrue ( isApplicationReady ( registrar ) ) ;
}
private boolean isApplicationReady ( SpringApplicationAdminMXBeanRegistrar registrar ) {
return ( Boolean ) ReflectionTestUtils . getField ( registrar , "ready" ) ;
}
@Test
public void environmentIsExposed ( ) {
final ObjectName objectName = createObjectName ( OBJECT_NAME ) ;