1 16 17 package org.springframework.jmx.support; 18 19 import java.util.List ; 20 21 import javax.management.MBeanServer ; 22 import javax.management.MBeanServerFactory ; 23 24 import junit.framework.TestCase; 25 26 29 public class MBeanServerFactoryBeanTests extends TestCase { 30 31 public void testGetObject() throws Exception { 32 MBeanServerFactoryBean bean = new MBeanServerFactoryBean(); 33 bean.afterPropertiesSet(); 34 try { 35 MBeanServer server = (MBeanServer ) bean.getObject(); 36 assertNotNull("The MBeanServer should not be null", server); 37 } 38 finally { 39 bean.destroy(); 40 } 41 } 42 43 public void testDefaultDomain() throws Exception { 44 MBeanServerFactoryBean bean = new MBeanServerFactoryBean(); 45 bean.setDefaultDomain("foo"); 46 bean.afterPropertiesSet(); 47 try { 48 MBeanServer server = (MBeanServer ) bean.getObject(); 49 assertEquals("The default domain should be foo", "foo", server.getDefaultDomain()); 50 } 51 finally { 52 bean.destroy(); 53 } 54 } 55 56 public void testCreateMBeanServer() throws Exception { 57 testCreation(true, "The server should be available in the list"); 58 } 59 60 public void testNewMBeanServer() throws Exception { 61 testCreation(false, "The server should not be available in the list"); 62 } 63 64 private void testCreation(boolean referenceShouldExist, String failMsg) throws Exception { 65 MBeanServerFactoryBean bean = new MBeanServerFactoryBean(); 66 bean.setRegisterWithFactory(referenceShouldExist); 67 bean.afterPropertiesSet(); 68 69 try { 70 MBeanServer server = (MBeanServer ) bean.getObject(); 71 List servers = MBeanServerFactory.findMBeanServer(null); 72 73 boolean found = false; 74 for (int x = 0; x < servers.size(); x++) { 75 if (servers.get(x) == server) { 76 found = true; 77 break; 78 } 79 } 80 81 if (!(found == referenceShouldExist)) { 82 fail(failMsg); 83 } 84 } 85 finally { 86 bean.destroy(); 87 } 88 } 89 90 } 91 | Popular Tags |