1 22 package org.jboss.test.jmx.compliance.server; 23 24 import javax.management.MBeanServer ; 25 import javax.management.MBeanServerBuilder ; 26 import javax.management.MBeanServerDelegate ; 27 import javax.management.MBeanServerFactory ; 28 import javax.management.ObjectName ; 29 30 import junit.framework.TestCase; 31 32 import org.jboss.test.jmx.compliance.server.support.BuilderTest; 33 import org.jboss.test.jmx.compliance.server.support.DelegateListener; 34 import org.jboss.test.jmx.compliance.server.support.MBeanServerReplaced; 35 import org.jboss.test.jmx.compliance.server.support.MBeanServerWrapper; 36 import org.jboss.test.jmx.compliance.server.support.TestMBeanServerBuilder; 37 import org.jboss.test.jmx.compliance.server.support.TestMBeanServerDelegate; 38 39 40 45 public class MBeanServerBuilderTestCase 46 extends TestCase 47 { 48 50 private ObjectName name; 51 private ObjectName delegateName; 52 private String defaultBuilder; 53 54 private static final String DEFAULT_DOMAIN = "default"; 55 private static final String BUILDER_PROPERTY = "javax.management.builder.initial"; 56 57 59 62 public MBeanServerBuilderTestCase(String s) 63 { 64 super(s); 65 try 66 { 67 name = new ObjectName ("server:test=MBeanServerBuilder"); 68 delegateName = new ObjectName ("JMImplementation:type=MBeanServerDelegate"); 69 } 70 catch (Exception e) 71 { 72 e.printStackTrace(); 73 throw new RuntimeException (e.toString()); 74 } 75 76 defaultBuilder = System.getProperty(BUILDER_PROPERTY, "javax.management.MBeanServerBuilder"); 77 } 78 79 81 public void testNewMBeanServerDelegate() 82 throws Exception 83 { 84 MBeanServerBuilder builder = new MBeanServerBuilder (); 85 MBeanServerDelegate delegate = builder.newMBeanServerDelegate(); 86 assertNotNull(delegate); 87 } 88 89 public void testNewMBeanServer() 90 throws Exception 91 { 92 MBeanServerBuilder builder = new MBeanServerBuilder (); 93 MBeanServerDelegate delegate = builder.newMBeanServerDelegate(); 94 MBeanServer server = builder.newMBeanServer(DEFAULT_DOMAIN, null, delegate); 95 assertTrue(server.getDefaultDomain().equals(DEFAULT_DOMAIN)); 96 } 97 98 public void testNewMBeanServerViaMBeanServerFactory() 99 throws Exception 100 { 101 MBeanServer server = MBeanServerFactory.newMBeanServer(DEFAULT_DOMAIN); 102 assertTrue(server.getDefaultDomain().equals(DEFAULT_DOMAIN)); 103 } 104 105 public void testPreRegisterGetsTheOriginalMBeanServer() 106 throws Exception 107 { 108 MBeanServer server = MBeanServerFactory.newMBeanServer(DEFAULT_DOMAIN); 109 110 BuilderTest test = new BuilderTest(); 111 server.registerMBean(test, name); 112 113 assertTrue("Registered", server.isRegistered(name)); 114 assertTrue("Got the correct MBeanServer", test.server.equals(server)); 115 } 116 117 public void testPreRegisterGetsTheOuterMBeanServer() 118 throws Exception 119 { 120 MBeanServerBuilder builder = new MBeanServerBuilder (); 121 MBeanServerDelegate delegate = builder.newMBeanServerDelegate(); 122 MBeanServer wrapper = MBeanServerWrapper.getWrapper(); 123 MBeanServer server = builder.newMBeanServer(DEFAULT_DOMAIN, wrapper, delegate); 124 125 MBeanServerWrapper.getHandler(wrapper).server = server; 126 127 BuilderTest test = new BuilderTest(); 128 wrapper.registerMBean(test, name); 129 130 assertTrue("Registered", server.isRegistered(name)); 131 assertTrue("Got the correct MBeanServer", test.server.equals(wrapper)); 132 assertTrue("Wrapper invoked", MBeanServerWrapper.getHandler(wrapper).invoked); 133 } 134 135 public void testMBeanServerDelegateReplaced() 136 throws Exception 137 { 138 MBeanServerBuilder builder = new MBeanServerBuilder (); 139 TestMBeanServerDelegate delegate = new TestMBeanServerDelegate(); 140 MBeanServer server = builder.newMBeanServer(DEFAULT_DOMAIN, null, delegate); 141 142 server.getAttribute(delegateName, "MBeanServerId"); 143 144 assertTrue("Delegate replaced", delegate.invoked); 145 } 146 147 public void testReplaceMBeanServerBuilderMBeanServerDelegate() 148 throws Exception 149 { 150 System.setProperty(BUILDER_PROPERTY, TestMBeanServerBuilder.class.getName()); 151 try 152 { 153 MBeanServer server = MBeanServerFactory.newMBeanServer(); 154 155 DelegateListener listener = new DelegateListener(); 156 server.addNotificationListener(delegateName, listener, null, null); 157 158 BuilderTest test = new BuilderTest(); 159 server.registerMBean(test, name); 160 161 assertTrue("Delegate replaced", listener.userData.equals("replaced")); 162 } 163 finally 164 { 165 System.setProperty(BUILDER_PROPERTY, defaultBuilder); 166 } 167 } 168 169 public void testReplaceMBeanServerBuilderMBeanServer() 170 throws Exception 171 { 172 System.setProperty(BUILDER_PROPERTY, TestMBeanServerBuilder.class.getName()); 173 try 174 { 175 MBeanServer server = MBeanServerFactory.newMBeanServer(); 176 177 boolean caught = false; 178 try 179 { 180 server.queryMBeans(null, null); 181 } 182 catch (MBeanServerReplaced e) 183 { 184 caught = true; 185 } 186 187 assertTrue("MBeanServer replaced", caught); 188 } 189 finally 190 { 191 System.setProperty(BUILDER_PROPERTY, defaultBuilder); 192 } 193 } 194 } 195 | Popular Tags |