@ -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" ) ;
* you may not use this file except in compliance with the License .
@ -16,6 +16,9 @@
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.TimeUnit ;
@ -28,6 +31,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextClosedEvent ;
import static org.hamcrest.Matchers.equalTo ;
import static org.hamcrest.Matchers.is ;
import static org.hamcrest.Matchers.startsWith ;
import static org.junit.Assert.assertThat ;
import static org.junit.Assert.assertTrue ;
@ -37,6 +41,7 @@ import static org.junit.Assert.assertTrue;
*
* @author Phillip Webb
* @author Dave Syer
* @author Andy Wilkinson
* /
public class ShutdownEndpointTests extends AbstractEndpointTests < ShutdownEndpoint > {
@ -53,18 +58,30 @@ public class ShutdownEndpointTests extends AbstractEndpointTests<ShutdownEndpoin
@Test
public void invoke ( ) throws Exception {
CountDownLatch latch = this . context . getBean ( Config . class ) . latch ;
assertThat ( ( String ) getEndpointBean ( ) . invoke ( ) . get ( "message" ) ,
startsWith ( "Shutting down" ) ) ;
Config config = this . context . getBean ( Config . class ) ;
ClassLoader previousTccl = Thread . currentThread ( ) . getContextClassLoader ( ) ;
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 ( latch . await ( 10 , TimeUnit . SECONDS ) ) ;
assertTrue ( config . latch . await ( 10 , TimeUnit . SECONDS ) ) ;
assertThat ( config . threadContextClassLoader , is ( getClass ( ) . getClassLoader ( ) ) ) ;
}
@Configuration
@EnableConfigurationProperties
public static class Config {
private CountDownLatch latch = new CountDownLatch ( 1 ) ;
private final CountDownLatch latch = new CountDownLatch ( 1 ) ;
private volatile ClassLoader threadContextClassLoader ;
@Bean
public ShutdownEndpoint endpoint ( ) {
@ -77,6 +94,8 @@ public class ShutdownEndpointTests extends AbstractEndpointTests<ShutdownEndpoin
return new ApplicationListener < ContextClosedEvent > ( ) {
@Override
public void onApplicationEvent ( ContextClosedEvent event ) {
Config . this . threadContextClassLoader = Thread . currentThread ( )
. getContextClassLoader ( ) ;
Config . this . latch . countDown ( ) ;
}
} ;