1 16 17 package org.springframework.jmx.support; 18 19 import java.io.IOException ; 20 import java.net.MalformedURLException ; 21 22 import javax.management.InstanceNotFoundException ; 23 import javax.management.MBeanServer ; 24 import javax.management.MBeanServerConnection ; 25 import javax.management.ObjectInstance ; 26 import javax.management.ObjectName ; 27 import javax.management.remote.JMXConnector ; 28 import javax.management.remote.JMXConnectorFactory ; 29 import javax.management.remote.JMXServiceURL ; 30 31 import org.springframework.core.JdkVersion; 32 import org.springframework.jmx.AbstractJmxTests; 33 34 37 public class ConnectorServerFactoryBeanTests extends AbstractJmxTests { 38 39 private static final String OBJECT_NAME = "spring:type=connector,name=test"; 40 41 public void testStartupWithLocatedServer() throws Exception { 42 if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) { 43 return; 45 } 46 47 ConnectorServerFactoryBean bean = new ConnectorServerFactoryBean(); 48 bean.afterPropertiesSet(); 49 50 try { 51 checkServerConnection(server); 52 } 53 finally { 54 bean.destroy(); 55 } 56 } 57 58 public void testStartupWithSuppliedServer() throws Exception { 59 if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) { 60 return; 62 } 63 64 ConnectorServerFactoryBean bean = new ConnectorServerFactoryBean(); 65 bean.setServer(server); 66 bean.afterPropertiesSet(); 67 68 try { 69 checkServerConnection(server); 70 } 71 finally { 72 bean.destroy(); 73 } 74 } 75 76 public void testRegisterWithMBeanServer() throws Exception { 77 ConnectorServerFactoryBean bean = new ConnectorServerFactoryBean(); 78 bean.setObjectName(OBJECT_NAME); 79 bean.afterPropertiesSet(); 80 81 try { 82 ObjectInstance instance = server.getObjectInstance(ObjectName.getInstance(OBJECT_NAME)); 84 assertNotNull("ObjectInstance should not be null", instance); 85 } 86 finally { 87 bean.destroy(); 88 } 89 } 90 91 public void testNoRegisterWithMBeanServer() throws Exception { 92 ConnectorServerFactoryBean bean = new ConnectorServerFactoryBean(); 93 bean.afterPropertiesSet(); 94 95 try { 96 ObjectInstance instance = server.getObjectInstance(ObjectName.getInstance(OBJECT_NAME)); 98 fail("Instance should not be found"); 99 } 100 catch (InstanceNotFoundException ex) { 101 } 103 finally { 104 bean.destroy(); 105 } 106 } 107 108 private void checkServerConnection(MBeanServer hostedServer) throws IOException , MalformedURLException { 109 JMXConnector connector = JMXConnectorFactory.connect( 111 new JMXServiceURL (ConnectorServerFactoryBean.DEFAULT_SERVICE_URL)); 112 assertNotNull("Client Connector should not be null", connector); 113 114 MBeanServerConnection connection = connector.getMBeanServerConnection(); 116 assertNotNull("MBeanServerConnection should not be null", connection); 117 118 assertEquals("Registered MBean count should be the same", 120 hostedServer.getMBeanCount(), connection.getMBeanCount()); 121 } 122 123 } 124 | Popular Tags |