@ -1,5 +1,5 @@
/ *
/ *
* Copyright 2012 - 201 3 the original author or authors .
* Copyright 2012 - 201 6 the original author or authors .
*
*
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* you may not use this file except in compliance with the License .
@ -16,6 +16,9 @@
package org.springframework.boot.actuate.endpoint ;
package org.springframework.boot.actuate.endpoint ;
import java.net.URL ;
import java.net.URLClassLoader ;
import java.util.Map ;
import java.util.concurrent.CountDownLatch ;
import java.util.concurrent.CountDownLatch ;
import java.util.concurrent.TimeUnit ;
import java.util.concurrent.TimeUnit ;
@ -28,6 +31,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextClosedEvent ;
import org.springframework.context.event.ContextClosedEvent ;
import static org.hamcrest.Matchers.equalTo ;
import static org.hamcrest.Matchers.equalTo ;
import static org.hamcrest.Matchers.is ;
import static org.hamcrest.Matchers.startsWith ;
import static org.hamcrest.Matchers.startsWith ;
import static org.junit.Assert.assertThat ;
import static org.junit.Assert.assertThat ;
import static org.junit.Assert.assertTrue ;
import static org.junit.Assert.assertTrue ;
@ -37,6 +41,7 @@ import static org.junit.Assert.assertTrue;
*
*
* @author Phillip Webb
* @author Phillip Webb
* @author Dave Syer
* @author Dave Syer
* @author Andy Wilkinson
* /
* /
public class ShutdownEndpointTests extends AbstractEndpointTests < ShutdownEndpoint > {
public class ShutdownEndpointTests extends AbstractEndpointTests < ShutdownEndpoint > {
@ -53,18 +58,30 @@ public class ShutdownEndpointTests extends AbstractEndpointTests<ShutdownEndpoin
@Test
@Test
public void invoke ( ) throws Exception {
public void invoke ( ) throws Exception {
CountDownLatch latch = this . context . getBean ( Config . class ) . latch ;
Config config = this . context . getBean ( Config . class ) ;
assertThat ( ( String ) getEndpointBean ( ) . invoke ( ) . get ( "message" ) ,
ClassLoader previousTccl = Thread . currentThread ( ) . getContextClassLoader ( ) ;
startsWith ( "Shutting down" ) ) ;
Map < String , Object > result ;
Thread . currentThread ( ) . setContextClassLoader (
new URLClassLoader ( new URL [ 0 ] , getClass ( ) . getClassLoader ( ) ) ) ;
try {
result = getEndpointBean ( ) . invoke ( ) ;
}
finally {
Thread . currentThread ( ) . setContextClassLoader ( previousTccl ) ;
}
assertThat ( ( String ) result . get ( "message" ) , startsWith ( "Shutting down" ) ) ;
assertTrue ( this . context . isActive ( ) ) ;
assertTrue ( this . context . isActive ( ) ) ;
assertTrue ( latch . await ( 10 , TimeUnit . SECONDS ) ) ;
assertTrue ( config . latch . await ( 10 , TimeUnit . SECONDS ) ) ;
assertThat ( config . threadContextClassLoader , is ( getClass ( ) . getClassLoader ( ) ) ) ;
}
}
@Configuration
@Configuration
@EnableConfigurationProperties
@EnableConfigurationProperties
public static class Config {
public static class Config {
private CountDownLatch latch = new CountDownLatch ( 1 ) ;
private final CountDownLatch latch = new CountDownLatch ( 1 ) ;
private volatile ClassLoader threadContextClassLoader ;
@Bean
@Bean
public ShutdownEndpoint endpoint ( ) {
public ShutdownEndpoint endpoint ( ) {
@ -77,6 +94,8 @@ public class ShutdownEndpointTests extends AbstractEndpointTests<ShutdownEndpoin
return new ApplicationListener < ContextClosedEvent > ( ) {
return new ApplicationListener < ContextClosedEvent > ( ) {
@Override
@Override
public void onApplicationEvent ( ContextClosedEvent event ) {
public void onApplicationEvent ( ContextClosedEvent event ) {
Config . this . threadContextClassLoader = Thread . currentThread ( )
. getContextClassLoader ( ) ;
Config . this . latch . countDown ( ) ;
Config . this . latch . countDown ( ) ;
}
}
} ;
} ;