@ -1,5 +1,5 @@
/ *
/ *
* Copyright 2002 - 2013 the original author or authors .
* Copyright 2002 - 2015 the original author or authors .
*
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ; you may not
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ; you may not
* use this file except in compliance with the License . You may obtain a copy of
* use this file except in compliance with the License . You may obtain a copy of
@ -22,6 +22,7 @@ import java.lang.reflect.Method;
import java.net.BindException ;
import java.net.BindException ;
import java.util.HashMap ;
import java.util.HashMap ;
import java.util.Map ;
import java.util.Map ;
import javax.management.Descriptor ;
import javax.management.Descriptor ;
import javax.management.MBeanServerConnection ;
import javax.management.MBeanServerConnection ;
import javax.management.remote.JMXConnectorServer ;
import javax.management.remote.JMXConnectorServer ;
@ -38,10 +39,15 @@ import org.springframework.jmx.export.MBeanExporter;
import org.springframework.jmx.export.assembler.AbstractReflectiveMBeanInfoAssembler ;
import org.springframework.jmx.export.assembler.AbstractReflectiveMBeanInfoAssembler ;
import org.springframework.tests.Assume ;
import org.springframework.tests.Assume ;
import org.springframework.tests.TestGroup ;
import org.springframework.tests.TestGroup ;
import org.springframework.util.SocketUtils ;
import static org.junit.Assert.* ;
import static org.junit.Assert.* ;
import static org.junit.Assume.* ;
/ * *
/ * *
* To run the tests in the class , set the following Java system property :
* { @code - DtestGroups = jmxmp } .
*
* @author Rob Harrop
* @author Rob Harrop
* @author Juergen Hoeller
* @author Juergen Hoeller
* @author Sam Brannen
* @author Sam Brannen
@ -85,16 +91,14 @@ public class MBeanClientInterceptorTests extends AbstractMBeanServerTests {
@Test
@Test
public void testProxyClassIsDifferent ( ) throws Exception {
public void testProxyClassIsDifferent ( ) throws Exception {
if ( ! runTests )
assumeTrue ( runTests ) ;
return ;
IJmxTestBean proxy = getProxy ( ) ;
IJmxTestBean proxy = getProxy ( ) ;
assertTrue ( "The proxy class should be different than the base class" , ( proxy . getClass ( ) ! = IJmxTestBean . class ) ) ;
assertTrue ( "The proxy class should be different than the base class" , ( proxy . getClass ( ) ! = IJmxTestBean . class ) ) ;
}
}
@Test
@Test
public void testDifferentProxiesSameClass ( ) throws Exception {
public void testDifferentProxiesSameClass ( ) throws Exception {
if ( ! runTests )
assumeTrue ( runTests ) ;
return ;
IJmxTestBean proxy1 = getProxy ( ) ;
IJmxTestBean proxy1 = getProxy ( ) ;
IJmxTestBean proxy2 = getProxy ( ) ;
IJmxTestBean proxy2 = getProxy ( ) ;
@ -104,8 +108,7 @@ public class MBeanClientInterceptorTests extends AbstractMBeanServerTests {
@Test
@Test
public void testGetAttributeValue ( ) throws Exception {
public void testGetAttributeValue ( ) throws Exception {
if ( ! runTests )
assumeTrue ( runTests ) ;
return ;
IJmxTestBean proxy1 = getProxy ( ) ;
IJmxTestBean proxy1 = getProxy ( ) ;
int age = proxy1 . getAge ( ) ;
int age = proxy1 . getAge ( ) ;
assertEquals ( "The age should be 100" , 100 , age ) ;
assertEquals ( "The age should be 100" , 100 , age ) ;
@ -113,69 +116,43 @@ public class MBeanClientInterceptorTests extends AbstractMBeanServerTests {
@Test
@Test
public void testSetAttributeValue ( ) throws Exception {
public void testSetAttributeValue ( ) throws Exception {
if ( ! runTests )
assumeTrue ( runTests ) ;
return ;
IJmxTestBean proxy = getProxy ( ) ;
IJmxTestBean proxy = getProxy ( ) ;
proxy . setName ( "Rob Harrop" ) ;
proxy . setName ( "Rob Harrop" ) ;
assertEquals ( "The name of the bean should have been updated" , "Rob Harrop" , target . getName ( ) ) ;
assertEquals ( "The name of the bean should have been updated" , "Rob Harrop" , target . getName ( ) ) ;
}
}
@Test
@Test ( expected = IllegalArgumentException . class )
public void testSetAttributeValueWithRuntimeException ( ) throws Exception {
public void testSetAttributeValueWithRuntimeException ( ) throws Exception {
if ( ! runTests )
assumeTrue ( runTests ) ;
return ;
IJmxTestBean proxy = getProxy ( ) ;
IJmxTestBean proxy = getProxy ( ) ;
try {
proxy . setName ( "Juergen" ) ;
proxy . setName ( "Juergen" ) ;
fail ( "Should have thrown IllegalArgumentException" ) ;
} catch ( IllegalArgumentException ex ) {
// expected
}
}
}
@Test
@Test ( expected = ClassNotFoundException . class )
public void testSetAttributeValueWithCheckedException ( ) throws Exception {
public void testSetAttributeValueWithCheckedException ( ) throws Exception {
if ( ! runTests )
assumeTrue ( runTests ) ;
return ;
IJmxTestBean proxy = getProxy ( ) ;
IJmxTestBean proxy = getProxy ( ) ;
try {
proxy . setName ( "Juergen Class" ) ;
proxy . setName ( "Juergen Class" ) ;
fail ( "Should have thrown ClassNotFoundException" ) ;
} catch ( ClassNotFoundException ex ) {
// expected
}
}
}
@Test
@Test ( expected = IOException . class )
public void testSetAttributeValueWithIOException ( ) throws Exception {
public void testSetAttributeValueWithIOException ( ) throws Exception {
if ( ! runTests )
assumeTrue ( runTests ) ;
return ;
IJmxTestBean proxy = getProxy ( ) ;
IJmxTestBean proxy = getProxy ( ) ;
try {
proxy . setName ( "Juergen IO" ) ;
proxy . setName ( "Juergen IO" ) ;
fail ( "Should have thrown IOException" ) ;
} catch ( IOException ex ) {
// expected
}
}
}
@Test
@Test ( expected = InvalidInvocationException . class )
public void testSetReadOnlyAttribute ( ) throws Exception {
public void testSetReadOnlyAttribute ( ) throws Exception {
if ( ! runTests )
assumeTrue ( runTests ) ;
return ;
IJmxTestBean proxy = getProxy ( ) ;
IJmxTestBean proxy = getProxy ( ) ;
try {
proxy . setAge ( 900 ) ;
proxy . setAge ( 900 ) ;
fail ( "Should not be able to write to a read only attribute" ) ;
} catch ( InvalidInvocationException ex ) {
// success
}
}
}
@Test
@Test
public void testInvokeNoArgs ( ) throws Exception {
public void testInvokeNoArgs ( ) throws Exception {
if ( ! runTests )
assumeTrue ( runTests ) ;
return ;
IJmxTestBean proxy = getProxy ( ) ;
IJmxTestBean proxy = getProxy ( ) ;
long result = proxy . myOperation ( ) ;
long result = proxy . myOperation ( ) ;
assertEquals ( "The operation should return 1" , 1 , result ) ;
assertEquals ( "The operation should return 1" , 1 , result ) ;
@ -183,34 +160,27 @@ public class MBeanClientInterceptorTests extends AbstractMBeanServerTests {
@Test
@Test
public void testInvokeArgs ( ) throws Exception {
public void testInvokeArgs ( ) throws Exception {
if ( ! runTests )
assumeTrue ( runTests ) ;
return ;
IJmxTestBean proxy = getProxy ( ) ;
IJmxTestBean proxy = getProxy ( ) ;
int result = proxy . add ( 1 , 2 ) ;
int result = proxy . add ( 1 , 2 ) ;
assertEquals ( "The operation should return 3" , 3 , result ) ;
assertEquals ( "The operation should return 3" , 3 , result ) ;
}
}
@Test
@Test ( expected = InvalidInvocationException . class )
public void testInvokeUnexposedMethodWithException ( ) throws Exception {
public void testInvokeUnexposedMethodWithException ( ) throws Exception {
if ( ! runTests )
assumeTrue ( runTests ) ;
return ;
IJmxTestBean bean = getProxy ( ) ;
IJmxTestBean bean = getProxy ( ) ;
try {
bean . dontExposeMe ( ) ;
bean . dontExposeMe ( ) ;
fail ( "Method dontExposeMe should throw an exception" ) ;
} catch ( InvalidInvocationException desired ) {
// success
}
}
}
@Test
@Test
public void testTestLazyConnectionToRemote ( ) throws Exception {
public void testTestLazyConnectionToRemote ( ) throws Exception {
if ( ! runTests )
assumeTrue ( runTests ) ;
return ;
Assume . group ( TestGroup . JMXMP ) ;
Assume . group ( TestGroup . JMXMP ) ;
JMXServiceURL url = new JMXServiceURL ( "service:jmx:jmxmp://localhost:9876" ) ;
final int port = SocketUtils . findAvailableTcpPort ( ) ;
JMXServiceURL url = new JMXServiceURL ( "service:jmx:jmxmp://localhost:" + port ) ;
JMXConnectorServer connector = JMXConnectorServerFactory . newJMXConnectorServer ( url , null , getServer ( ) ) ;
JMXConnectorServer connector = JMXConnectorServerFactory . newJMXConnectorServer ( url , null , getServer ( ) ) ;
MBeanProxyFactoryBean factory = new MBeanProxyFactoryBean ( ) ;
MBeanProxyFactoryBean factory = new MBeanProxyFactoryBean ( ) ;
@ -227,9 +197,8 @@ public class MBeanClientInterceptorTests extends AbstractMBeanServerTests {
try {
try {
connector . start ( ) ;
connector . start ( ) ;
} catch ( BindException ex ) {
} catch ( BindException ex ) {
// couldn't bind to local port 9876 - let's skip the remainder of this test
System . out . println ( "Skipping remainder of JMX LazyConnectionToRemote test because binding to local port ["
System . out . println ( "Skipping JMX LazyConnectionToRemote test because binding to local port 9876 failed: "
+ port + "] failed: " + ex . getMessage ( ) ) ;
+ ex . getMessage ( ) ) ;
return ;
return ;
}
}