KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > management > TestMBeanRegistry


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.management;
16
17 import java.util.List JavaDoc;
18
19 import javax.management.MBeanRegistrationException JavaDoc;
20 import javax.management.MBeanServer JavaDoc;
21 import javax.management.NotCompliantMBeanException JavaDoc;
22 import javax.management.ObjectInstance JavaDoc;
23 import javax.management.ObjectName JavaDoc;
24 import javax.management.StandardMBean JavaDoc;
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 /**
43  * Test of {@link org.apache.hivemind.management.impl.MBeanRegistryImpl}.
44  *
45  * @author Achim Huegen
46  * @since 1.1
47  */

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 JavaDoc 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 JavaDoc.class);
65         server = (MBeanServer JavaDoc) serverControl.getMock();
66         objectNameBuilder = new ObjectNameBuilderImpl();
67     }
68
69     /**
70      * Tests the registration of MBeans via contribution
71      */

72     public void testContribution() throws Exception JavaDoc
73     {
74         Registry registry = buildFrameworkRegistry("testMBeanRegistry.xml");
75         List JavaDoc mBeanList = (List JavaDoc) registry.getConfiguration("hivemind.management.MBeans");
76
77         // Training
78
ServicePoint sp1 = ((MBeanRegistrationContribution) mBeanList.get(0)).getServicePoint();
79         Object JavaDoc mBean1 = registry.getService("test.management.MBean1", Runnable JavaDoc.class);
80         ObjectName JavaDoc on1 = objectNameBuilder.createServiceObjectName(sp1);
81         server.registerMBean(mBean1, on1);
82         ObjectInstance JavaDoc oin1 = new ObjectInstance JavaDoc(on1, mBean1.getClass().getName());
83         serverControl.setReturnValue(oin1);
84
85         Object JavaDoc mBean2 = registry.getService("test.management.MBean2", Runnable JavaDoc.class);
86         ObjectName JavaDoc on2 = new ObjectName JavaDoc("hivemind:name=bean2");
87         server.registerMBean(mBean2, on2);
88         serverControl.setReturnValue(new ObjectInstance JavaDoc(on2, mBean2.getClass().getName()));
89
90         // This is a special case. A class without interface
91
Object JavaDoc mBean3 = registry.getService("test.management.MBean3", MBeanNonInterfaceTestService.class);
92         ObjectName JavaDoc on3 = new ObjectName JavaDoc("hivemind:name=bean3");
93         server.registerMBean(mBean3, on3);
94         serverControl.setReturnValue(new ObjectInstance JavaDoc(on3, mBean3.getClass().getName()));
95         
96         // Call from unregisterBean
97
server.getObjectInstance(on1);
98         serverControl.setReturnValue(oin1);
99
100         server.unregisterMBean(on1);
101         // The automatically unregistered beans get unregistered in reverse order
102
server.unregisterMBean(on3);
103         server.unregisterMBean(on2);
104
105         replayControls();
106
107         MBeanRegistry mbeanRegistry = new MBeanRegistryImpl(errorHandler, log, server,
108                 objectNameBuilder, mBeanList);
109
110         // Unregister one bean manually the other one during registry shutdown
111
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     /**
122      * Tests the handling of a not compliant mbean
123      */

124     public void testNotCompliantHandling() throws Exception JavaDoc
125     {
126         Calculator calculatorMBean = new CalculatorImpl();
127         ObjectName JavaDoc objectName = new ObjectName JavaDoc("hivemind:module=test");
128
129         // Training
130
server.registerMBean(calculatorMBean, objectName);
131         serverControl.setThrowable(new NotCompliantMBeanException JavaDoc("Not compliant"));
132         replayControls();
133
134         // Registration must fail since the bean is not mbean compliant and a management
135
// interface is not provided
136
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 JavaDoc expected)
144         {
145         }
146
147         verifyControls();
148     }
149
150     /**
151      * Tests the handling of registrations errors during processing of the contributed mbeans
152      */

153     public void testRegistrationException() throws Exception JavaDoc
154     {
155         Registry registry = buildFrameworkRegistry("testMBeanRegistry.xml");
156         List JavaDoc mBeanList = (List JavaDoc) registry.getConfiguration("hivemind.management.MBeans");
157
158         ServicePoint sp1 = ((MBeanRegistrationContribution) mBeanList.get(0)).getServicePoint();
159         ObjectName JavaDoc on1 = objectNameBuilder.createServiceObjectName(sp1);
160
161         // Training
162
server.registerMBean(null, null);
163         serverControl.setThrowable(new MBeanRegistrationException JavaDoc(new Exception JavaDoc(
164                 "Registration failed")));
165         serverControl.setDefaultMatcher(new AlwaysMatcher());
166         server.registerMBean(null, null);
167         serverControl.setThrowable(new MBeanRegistrationException JavaDoc(new Exception JavaDoc(
168                 "Registration failed")));
169         server.registerMBean(null, null);
170         serverControl.setThrowable(new MBeanRegistrationException JavaDoc(new Exception JavaDoc(
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     /**
183      * Ensures that a bean that doesn't implement one of the standard JMX
184      * interfaces (like DynamicMBean) is registered as StandardMBean.
185      */

186     public void testStandardMBean() throws Exception JavaDoc
187     {
188         Calculator calculatorMBean = new CalculatorImpl();
189         ObjectName JavaDoc objectName = new ObjectName JavaDoc("hivemind:module=test");
190
191         // Training
192
server.registerMBean(calculatorMBean, objectName);
193         serverControl.setThrowable(new NotCompliantMBeanException JavaDoc("Not compliant"));
194
195         server.registerMBean(null, objectName);
196         // Matcher must match both method calls, it's not possible to
197
// define multiple matchers
198
serverControl.setMatcher(new ArgumentsMatcher()
199         {
200             boolean firstCall = true;
201
202             public boolean matches(Object JavaDoc[] arg0, Object JavaDoc[] 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 JavaDoc.class);
212             }
213
214             public String JavaDoc toString(Object JavaDoc[] arg0)
215             {
216                 return "";
217             }
218         });
219         serverControl.setReturnValue(new ObjectInstance JavaDoc(objectName, StandardMBean JavaDoc.class.getName()));
220
221         replayControls();
222
223         MBeanRegistry mbeanRegistry = new MBeanRegistryImpl(errorHandler, log, server,
224                 objectNameBuilder, null);
225
226         // Management interface is specified
227
mbeanRegistry.registerMBean(calculatorMBean, Calculator.class, objectName);
228
229         verifyControls();
230     }
231
232 }
Popular Tags