@ -1,5 +1,5 @@
/ *
* Copyright 2013 the original author or authors .
* Copyright 2013 - 2015 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,8 +16,11 @@
package org.springframework.boot.actuate.endpoint.jmx ;
import java.text.SimpleDateFormat ;
import java.util.Collections ;
import java.util.Date ;
import java.util.HashMap ;
import java.util.LinkedHashMap ;
import java.util.Map ;
import java.util.Properties ;
@ -28,6 +31,7 @@ import javax.management.ObjectName;
import org.junit.After ;
import org.junit.Test ;
import org.springframework.beans.MutablePropertyValues ;
import org.springframework.beans.factory.config.ConstructorArgumentValues ;
import org.springframework.beans.factory.support.RootBeanDefinition ;
import org.springframework.boot.actuate.endpoint.AbstractEndpoint ;
import org.springframework.context.ApplicationContext ;
@ -36,13 +40,19 @@ import org.springframework.jmx.export.MBeanExporter;
import org.springframework.jmx.support.ObjectNameManager ;
import org.springframework.util.ObjectUtils ;
import com.fasterxml.jackson.databind.ObjectMapper ;
import static org.hamcrest.Matchers.instanceOf ;
import static org.hamcrest.Matchers.is ;
import static org.junit.Assert.assertEquals ;
import static org.junit.Assert.assertNotNull ;
import static org.junit.Assert.assertThat ;
/ * *
* Tests for { @link EndpointMBeanExporter }
*
* @author Christian Dupuis
* @author Andy Wilkinson
* /
public class EndpointMBeanExporterTests {
@ -176,6 +186,47 @@ public class EndpointMBeanExporterTests {
parent . close ( ) ;
}
@Test
public void jsonConversionWithDefaultObjectMapper ( ) throws Exception {
this . context = new GenericApplicationContext ( ) ;
this . context . registerBeanDefinition ( "endpointMbeanExporter" ,
new RootBeanDefinition ( EndpointMBeanExporter . class ) ) ;
this . context . registerBeanDefinition ( "endpoint1" , new RootBeanDefinition (
JsonConversionEndpoint . class ) ) ;
this . context . refresh ( ) ;
MBeanExporter mbeanExporter = this . context . getBean ( EndpointMBeanExporter . class ) ;
Object response = mbeanExporter . getServer ( ) . invoke (
getObjectName ( "endpoint1" , this . context ) , "getData" , new Object [ 0 ] ,
new String [ 0 ] ) ;
assertThat ( response , is ( instanceOf ( Map . class ) ) ) ;
assertThat ( ( ( Map < ? , ? > ) response ) . get ( "date" ) , is ( instanceOf ( Long . class ) ) ) ;
}
@Test
public void jsonConversionWithCustomObjectMapper ( ) throws Exception {
this . context = new GenericApplicationContext ( ) ;
ConstructorArgumentValues constructorArgs = new ConstructorArgumentValues ( ) ;
ObjectMapper objectMapper = new ObjectMapper ( ) ;
objectMapper . setDateFormat ( new SimpleDateFormat ( "yyyy-MM-dd" ) ) ;
constructorArgs . addIndexedArgumentValue ( 0 , objectMapper ) ;
this . context
. registerBeanDefinition ( "endpointMbeanExporter" , new RootBeanDefinition (
EndpointMBeanExporter . class , constructorArgs , null ) ) ;
this . context . registerBeanDefinition ( "endpoint1" , new RootBeanDefinition (
JsonConversionEndpoint . class ) ) ;
this . context . refresh ( ) ;
MBeanExporter mbeanExporter = this . context . getBean ( EndpointMBeanExporter . class ) ;
Object response = mbeanExporter . getServer ( ) . invoke (
getObjectName ( "endpoint1" , this . context ) , "getData" , new Object [ 0 ] ,
new String [ 0 ] ) ;
assertThat ( response , is ( instanceOf ( Map . class ) ) ) ;
assertThat ( ( ( Map < ? , ? > ) response ) . get ( "date" ) , is ( instanceOf ( String . class ) ) ) ;
}
private ObjectName getObjectName ( String beanKey , GenericApplicationContext context )
throws MalformedObjectNameException {
return getObjectName ( "org.springframework.boot" , beanKey , false , context ) ;
@ -209,4 +260,20 @@ public class EndpointMBeanExporterTests {
}
}
public static class JsonConversionEndpoint extends
AbstractEndpoint < Map < String , Object > > {
public JsonConversionEndpoint ( ) {
super ( "json-conversion" ) ;
}
@Override
public Map < String , Object > invoke ( ) {
Map < String , Object > result = new LinkedHashMap < String , Object > ( ) ;
result . put ( "date" , new Date ( ) ) ;
return result ;
}
}
}