KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jmx > export > MBeanExporterTests


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

16
17 package org.springframework.jmx.export;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.management.JMException JavaDoc;
25 import javax.management.MBeanServer JavaDoc;
26 import javax.management.MBeanServerFactory JavaDoc;
27 import javax.management.MalformedObjectNameException JavaDoc;
28 import javax.management.ObjectInstance JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import javax.management.modelmbean.ModelMBeanInfo JavaDoc;
31
32 import junit.framework.TestCase;
33
34 import org.springframework.aop.framework.ProxyFactory;
35 import org.springframework.aop.interceptor.NopInterceptor;
36 import org.springframework.beans.factory.xml.XmlBeanFactory;
37 import org.springframework.core.io.ClassPathResource;
38 import org.springframework.jmx.IJmxTestBean;
39 import org.springframework.jmx.JmxTestBean;
40 import org.springframework.jmx.export.assembler.MBeanInfoAssembler;
41 import org.springframework.jmx.export.naming.SelfNaming;
42 import org.springframework.jmx.support.ObjectNameManager;
43
44 /**
45  * @author Rob Harrop
46  */

47 public class MBeanExporterTests extends TestCase {
48
49     private static final String JavaDoc OBJECT_NAME = "spring:test=jmxMBeanAdaptor";
50
51     public void testWithSuppliedMBeanServer() throws Exception JavaDoc {
52         MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
53         try {
54             MBeanExporter adaptor = new MBeanExporter();
55             adaptor.setBeans(getBeanMap());
56             adaptor.setServer(server);
57             adaptor.afterPropertiesSet();
58             assertTrue("The bean was not registered with the MBeanServer",
59                     beanExists(server, ObjectNameManager.getInstance(OBJECT_NAME)));
60         }
61         finally {
62             server.unregisterMBean(new ObjectName JavaDoc(OBJECT_NAME));
63         }
64     }
65
66     public void testWithLocatedMBeanServer() throws Exception JavaDoc {
67         MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
68         try {
69             MBeanExporter adaptor = new MBeanExporter();
70             adaptor.setBeans(getBeanMap());
71             adaptor.afterPropertiesSet();
72             assertTrue("The bean was not registered with the MBeanServer",
73                     beanExists(server, ObjectNameManager.getInstance(OBJECT_NAME)));
74             server.unregisterMBean(new ObjectName JavaDoc(OBJECT_NAME));
75         }
76         finally {
77             MBeanServerFactory.releaseMBeanServer(server);
78         }
79     }
80
81     public void testUserCreatedMBeanRegWithDynamicMBean() throws Exception JavaDoc {
82         Map JavaDoc map = new HashMap JavaDoc();
83         map.put("spring:name=dynBean", new TestDynamicMBean());
84
85         MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
86         try {
87             InvokeDetectAssembler asm = new InvokeDetectAssembler();
88
89             MBeanExporter adaptor = new MBeanExporter();
90             adaptor.setServer(server);
91             adaptor.setBeans(map);
92             adaptor.setAssembler(asm);
93             adaptor.afterPropertiesSet();
94
95             Object JavaDoc name = server.getAttribute(ObjectNameManager.getInstance("spring:name=dynBean"), "name");
96             assertEquals("The name attribute is incorrect", "Rob Harrop", name);
97             assertFalse("Assembler should not have been invoked", asm.invoked);
98         }
99         finally {
100             MBeanServerFactory.releaseMBeanServer(server);
101         }
102     }
103
104     public void testAutodetectMBeans() throws Exception JavaDoc {
105         XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("autodetectMBeans.xml", getClass()));
106         try {
107             bf.getBean("exporter");
108             MBeanServer JavaDoc server = (MBeanServer JavaDoc) bf.getBean("server");
109             ObjectInstance JavaDoc instance = server.getObjectInstance(ObjectNameManager.getInstance("spring:mbean=true"));
110             assertNotNull(instance);
111         }
112         finally {
113             bf.destroySingletons();
114         }
115     }
116
117     public void testAutodetectLazyMBeans() throws Exception JavaDoc {
118         XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("autodetectLazyMBeans.xml", getClass()));
119         try {
120             bf.getBean("exporter");
121             MBeanServer JavaDoc server = (MBeanServer JavaDoc) bf.getBean("server");
122             ObjectInstance JavaDoc instance = server.getObjectInstance(ObjectNameManager.getInstance("spring:mbean=true"));
123             assertNotNull(instance);
124         }
125         finally {
126             bf.destroySingletons();
127         }
128     }
129
130     public void testWithMBeanExporterListeners() throws Exception JavaDoc {
131         MockMBeanExporterListener listener1 = new MockMBeanExporterListener();
132         MockMBeanExporterListener listener2 = new MockMBeanExporterListener();
133
134         MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
135         MBeanExporter adaptor = null;
136         try {
137             adaptor = new MBeanExporter();
138             adaptor.setBeans(getBeanMap());
139             adaptor.setServer(server);
140             adaptor.setListeners(new MBeanExporterListener[] {listener1, listener2});
141             adaptor.afterPropertiesSet();
142         }
143         finally {
144             if (adaptor != null) {
145                 adaptor.destroy();
146             }
147         }
148
149         assertListener(listener1);
150         assertListener(listener2);
151     }
152
153     private void assertListener(MockMBeanExporterListener listener) throws MalformedObjectNameException JavaDoc {
154         ObjectName JavaDoc desired = ObjectNameManager.getInstance(OBJECT_NAME);
155         assertEquals("Incorrect number of registrations", 1, listener.getRegistered().size());
156         assertEquals("Incorrect number of unregistrations", 1, listener.getUnregistered().size());
157         assertEquals("Incorrect ObjectName in register", desired, listener.getRegistered().get(0));
158         assertEquals("Incorrect ObjectName in unregister", desired, listener.getUnregistered().get(0));
159     }
160
161     public void testExportJdkProxy() throws Exception JavaDoc {
162         JmxTestBean bean = new JmxTestBean();
163         bean.setName("Rob Harrop");
164
165         ProxyFactory factory = new ProxyFactory();
166         factory.setTarget(bean);
167         factory.addAdvice(new NopInterceptor());
168         factory.setInterfaces(new Class JavaDoc[]{IJmxTestBean.class});
169
170         IJmxTestBean proxy = (IJmxTestBean) factory.getProxy();
171         String JavaDoc name = "bean:proxy=true";
172
173         Map JavaDoc beans = new HashMap JavaDoc();
174         beans.put(name, proxy);
175         MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
176
177         MBeanExporter exporter = new MBeanExporter();
178         exporter.setServer(server);
179         exporter.setBeans(beans);
180         exporter.registerBeans();
181
182         ObjectName JavaDoc oname = ObjectName.getInstance(name);
183         Object JavaDoc nameValue = server.getAttribute(oname, "Name");
184         assertEquals("Rob Harrop", nameValue);
185     }
186
187     public void testSelfNaming() throws Exception JavaDoc {
188         ObjectName JavaDoc objectName = ObjectNameManager.getInstance(OBJECT_NAME);
189         SelfNamingTestBean testBean = new SelfNamingTestBean();
190         testBean.setObjectName(objectName);
191
192         Map JavaDoc beans = new HashMap JavaDoc();
193         beans.put("foo", testBean);
194
195
196         MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
197         try {
198
199             MBeanExporter adaptor = new MBeanExporter();
200             adaptor.setServer(server);
201             adaptor.setBeans(beans);
202
203             adaptor.afterPropertiesSet();
204
205             ObjectInstance JavaDoc instance = server.getObjectInstance(objectName);
206             assertNotNull(instance);
207         }
208         finally {
209             MBeanServerFactory.releaseMBeanServer(server);
210         }
211     }
212
213     private Map JavaDoc getBeanMap() {
214         Map JavaDoc map = new HashMap JavaDoc();
215         map.put(OBJECT_NAME, new JmxTestBean());
216         return map;
217     }
218
219     private boolean beanExists(MBeanServer JavaDoc server, ObjectName JavaDoc objectName) throws Exception JavaDoc {
220         ObjectInstance JavaDoc inst = server.getObjectInstance(objectName);
221         return (inst != null);
222     }
223
224
225     private static class InvokeDetectAssembler implements MBeanInfoAssembler {
226
227         private boolean invoked = false;
228
229         public ModelMBeanInfo JavaDoc getMBeanInfo(Object JavaDoc managedResource, String JavaDoc beanKey) throws JMException JavaDoc {
230             invoked = true;
231             return null;
232         }
233     }
234
235
236     private static class MockMBeanExporterListener implements MBeanExporterListener {
237
238         private List JavaDoc registered = new ArrayList JavaDoc();
239
240         private List JavaDoc unregistered = new ArrayList JavaDoc();
241
242         public void mbeanRegistered(ObjectName JavaDoc objectName) {
243             registered.add(objectName);
244         }
245
246         public void mbeanUnregistered(ObjectName JavaDoc objectName) {
247             unregistered.add(objectName);
248         }
249
250         public List JavaDoc getRegistered() {
251             return registered;
252         }
253
254         public List JavaDoc getUnregistered() {
255             return unregistered;
256         }
257     }
258
259     private static class SelfNamingTestBean implements SelfNaming {
260
261         private ObjectName JavaDoc objectName;
262
263         public void setObjectName(ObjectName JavaDoc objectName) {
264             this.objectName = objectName;
265         }
266
267         public ObjectName JavaDoc getObjectName() throws MalformedObjectNameException JavaDoc {
268             return this.objectName;
269         }
270     }
271
272 }
273
Popular Tags