1 15 package org.apache.hivemind.management; 16 17 import java.util.List ; 18 19 import javax.management.MBeanRegistrationException ; 20 import javax.management.MBeanServer ; 21 import javax.management.NotCompliantMBeanException ; 22 import javax.management.ObjectInstance ; 23 import javax.management.ObjectName ; 24 import javax.management.StandardMBean ; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 import org.apache.hivemind.ErrorHandler; 29 import org.apache.hivemind.Registry; 30 import org.apache.hivemind.events.RegistryShutdownListener; 31 import org.apache.hivemind.impl.DefaultErrorHandler; 32 import org.apache.hivemind.internal.ServicePoint; 33 import org.apache.hivemind.management.impl.MBeanRegistrationContribution; 34 import org.apache.hivemind.management.impl.MBeanRegistryImpl; 35 import org.apache.hivemind.management.impl.ObjectNameBuilderImpl; 36 import org.apache.hivemind.xml.XmlTestCase; 37 import org.easymock.ArgumentsMatcher; 38 import org.easymock.MockControl; 39 import org.easymock.internal.AlwaysMatcher; 40 import org.easymock.internal.EqualsMatcher; 41 42 48 public class TestMBeanRegistry extends XmlTestCase 49 { 50 private ErrorHandler errorHandler; 51 52 private Log log; 53 54 private MockControl serverControl; 55 56 private MBeanServer server; 57 58 private ObjectNameBuilder objectNameBuilder; 59 60 public void setUp() 61 { 62 errorHandler = new DefaultErrorHandler(); 63 log = LogFactory.getLog(MBeanRegistry.class); 64 serverControl = newControl(MBeanServer .class); 65 server = (MBeanServer ) serverControl.getMock(); 66 objectNameBuilder = new ObjectNameBuilderImpl(); 67 } 68 69 72 public void testContribution() throws Exception 73 { 74 Registry registry = buildFrameworkRegistry("testMBeanRegistry.xml"); 75 List mBeanList = (List ) registry.getConfiguration("hivemind.management.MBeans"); 76 77 ServicePoint sp1 = ((MBeanRegistrationContribution) mBeanList.get(0)).getServicePoint(); 79 Object mBean1 = registry.getService("test.management.MBean1", Runnable .class); 80 ObjectName on1 = objectNameBuilder.createServiceObjectName(sp1); 81 server.registerMBean(mBean1, on1); 82 ObjectInstance oin1 = new ObjectInstance (on1, mBean1.getClass().getName()); 83 serverControl.setReturnValue(oin1); 84 85 Object mBean2 = registry.getService("test.management.MBean2", Runnable .class); 86 ObjectName on2 = new ObjectName ("hivemind:name=bean2"); 87 server.registerMBean(mBean2, on2); 88 serverControl.setReturnValue(new ObjectInstance (on2, mBean2.getClass().getName())); 89 90 Object mBean3 = registry.getService("test.management.MBean3", MBeanNonInterfaceTestService.class); 92 ObjectName on3 = new ObjectName ("hivemind:name=bean3"); 93 server.registerMBean(mBean3, on3); 94 serverControl.setReturnValue(new ObjectInstance (on3, mBean3.getClass().getName())); 95 96 server.getObjectInstance(on1); 98 serverControl.setReturnValue(oin1); 99 100 server.unregisterMBean(on1); 101 server.unregisterMBean(on3); 103 server.unregisterMBean(on2); 104 105 replayControls(); 106 107 MBeanRegistry mbeanRegistry = new MBeanRegistryImpl(errorHandler, log, server, 108 objectNameBuilder, mBeanList); 109 110 mbeanRegistry.unregisterMBean(on1); 112 ((RegistryShutdownListener) mbeanRegistry).registryDidShutdown(); 113 114 verifyControls(); 115 116 assertTrue("start method has not been called", ((MBeanTestService) mBean1).isStartCalled()); 117 118 registry.shutdown(); 119 } 120 121 124 public void testNotCompliantHandling() throws Exception 125 { 126 Calculator calculatorMBean = new CalculatorImpl(); 127 ObjectName objectName = new ObjectName ("hivemind:module=test"); 128 129 server.registerMBean(calculatorMBean, objectName); 131 serverControl.setThrowable(new NotCompliantMBeanException ("Not compliant")); 132 replayControls(); 133 134 MBeanRegistry mbeanRegistry = new MBeanRegistryImpl(errorHandler, log, server, 137 objectNameBuilder, null); 138 try 139 { 140 mbeanRegistry.registerMBean(calculatorMBean, null, objectName); 141 fail("Not compliant MBean registered"); 142 } 143 catch (NotCompliantMBeanException expected) 144 { 145 } 146 147 verifyControls(); 148 } 149 150 153 public void testRegistrationException() throws Exception 154 { 155 Registry registry = buildFrameworkRegistry("testMBeanRegistry.xml"); 156 List mBeanList = (List ) registry.getConfiguration("hivemind.management.MBeans"); 157 158 ServicePoint sp1 = ((MBeanRegistrationContribution) mBeanList.get(0)).getServicePoint(); 159 ObjectName on1 = objectNameBuilder.createServiceObjectName(sp1); 160 161 server.registerMBean(null, null); 163 serverControl.setThrowable(new MBeanRegistrationException (new Exception ( 164 "Registration failed"))); 165 serverControl.setDefaultMatcher(new AlwaysMatcher()); 166 server.registerMBean(null, null); 167 serverControl.setThrowable(new MBeanRegistrationException (new Exception ( 168 "Registration failed"))); 169 server.registerMBean(null, null); 170 serverControl.setThrowable(new MBeanRegistrationException (new Exception ( 171 "Registration failed"))); 172 173 replayControls(); 174 175 interceptLogging(MBeanRegistry.class.getName()); 176 177 new MBeanRegistryImpl(errorHandler, log, server, objectNameBuilder, mBeanList); 178 179 assertLoggedMessage("Registering MBean " + on1.toString() + " failed"); 180 } 181 182 186 public void testStandardMBean() throws Exception 187 { 188 Calculator calculatorMBean = new CalculatorImpl(); 189 ObjectName objectName = new ObjectName ("hivemind:module=test"); 190 191 server.registerMBean(calculatorMBean, objectName); 193 serverControl.setThrowable(new NotCompliantMBeanException ("Not compliant")); 194 195 server.registerMBean(null, objectName); 196 serverControl.setMatcher(new ArgumentsMatcher() 199 { 200 boolean firstCall = true; 201 202 public boolean matches(Object [] arg0, Object [] arg1) 203 { 204 if (firstCall) 205 { 206 firstCall = false; 207 EqualsMatcher matcher = new EqualsMatcher(); 208 return matcher.matches(arg0, arg1); 209 } 210 211 return arg1[0].getClass().equals(StandardMBean .class); 212 } 213 214 public String toString(Object [] arg0) 215 { 216 return ""; 217 } 218 }); 219 serverControl.setReturnValue(new ObjectInstance (objectName, StandardMBean .class.getName())); 220 221 replayControls(); 222 223 MBeanRegistry mbeanRegistry = new MBeanRegistryImpl(errorHandler, log, server, 224 objectNameBuilder, null); 225 226 mbeanRegistry.registerMBean(calculatorMBean, Calculator.class, objectName); 228 229 verifyControls(); 230 } 231 232 } | Popular Tags |