KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > test > JMXInvokerUnitTestCase


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.test.jmx.test;
23
24 import javax.management.Attribute JavaDoc;
25 import javax.management.ListenerNotFoundException JavaDoc;
26 import javax.management.MBeanInfo JavaDoc;
27 import javax.management.MBeanServerConnection JavaDoc;
28 import javax.management.MalformedObjectNameException JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import javax.management.ReflectionException JavaDoc;
31
32 import junit.framework.Test;
33 import junit.framework.TestSuite;
34
35 import org.jboss.jmx.adaptor.rmi.RMIAdaptor;
36 import org.jboss.test.JBossTestCase;
37 import org.jboss.test.jmx.invoker.CustomClass;
38 import org.jboss.test.jmx.invoker.InvokerTestMBean;
39 import org.jboss.test.jmx.invoker.RMIBadListener;
40 import org.jboss.test.jmx.invoker.RMIListener;
41
42 /**
43  * Tests for the jmx invoker adaptor.
44  *
45  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>
46  * @author Scott.Stark@jboss.org
47  * @author <a HREF="mailto:tom@jboss.com">Tom Elrod</a>
48  * @version $Revision: 56694 $
49  */

50 public class JMXInvokerUnitTestCase extends JBossTestCase
51 {
52    public JMXInvokerUnitTestCase(String JavaDoc name)
53    {
54       super(name);
55    }
56
57    public static Test suite() throws Exception JavaDoc
58    {
59       // JBAS-3605, the execution order of tests in this test case is important
60
// so it must be defined explicitly when running under some JVMs
61
TestSuite suite = new TestSuite();
62       suite.addTest(new JMXInvokerUnitTestCase("testGetSomething"));
63       suite.addTest(new JMXInvokerUnitTestCase("testGetCustom"));
64       suite.addTest(new JMXInvokerUnitTestCase("testGetCustomXMBean"));
65       suite.addTest(new JMXInvokerUnitTestCase("testGetXMBeanInfo"));
66       suite.addTest(new JMXInvokerUnitTestCase("testXMBeanDoSomething"));
67       suite.addTest(new JMXInvokerUnitTestCase("testSetCustom"));
68       suite.addTest(new JMXInvokerUnitTestCase("testClassNotFoundException"));
69       suite.addTest(new JMXInvokerUnitTestCase("testNotification"));
70       suite.addTest(new JMXInvokerUnitTestCase("testNotificationWithBadListener"));
71       
72       return getDeploySetup(suite, "invoker-adaptor-test.ear");
73    }
74
75    /**
76     * The jmx object name name of the mbean under test
77     * @return The name of the mbean under test
78     * @throws MalformedObjectNameException
79     */

80    ObjectName JavaDoc getObjectName() throws MalformedObjectNameException JavaDoc
81    {
82       return InvokerTestMBean.OBJECT_NAME;
83    }
84
85    public void testGetSomething()
86       throws Exception JavaDoc
87    {
88       log.info("+++ testGetSomething");
89       RMIAdaptor server = (RMIAdaptor) getInitialContext().lookup("jmx/invoker/RMIAdaptor");
90       assertEquals("something", server.getAttribute(getObjectName(), "Something"));
91    }
92
93    public void testGetCustom()
94       throws Exception JavaDoc
95    {
96       log.info("+++ testGetCustom");
97       RMIAdaptor server = (RMIAdaptor) getInitialContext().lookup("jmx/invoker/RMIAdaptor");
98       CustomClass custom = (CustomClass) server.getAttribute(getObjectName(), "Custom");
99       assertEquals("InitialValue", custom.getValue());
100    }
101
102    public void testGetCustomXMBean()
103       throws Exception JavaDoc
104    {
105       log.info("+++ testGetCustomXMBean");
106       RMIAdaptor server = (RMIAdaptor) getInitialContext().lookup("jmx/invoker/RMIAdaptor");
107       ObjectName JavaDoc xmbean = new ObjectName JavaDoc("jboss.test:service=InvokerTest,type=XMBean");
108       CustomClass custom = (CustomClass) server.getAttribute(xmbean, "Custom");
109       assertEquals("InitialValue", custom.getValue());
110    }
111    public void testGetXMBeanInfo()
112       throws Exception JavaDoc
113    {
114       log.info("+++ testGetXMBeanInfo");
115       RMIAdaptor server = (RMIAdaptor) getInitialContext().lookup("jmx/invoker/RMIAdaptor");
116       ObjectName JavaDoc xmbean = new ObjectName JavaDoc("jboss.test:service=InvokerTest,type=XMBean");
117       MBeanInfo JavaDoc info = server.getMBeanInfo(xmbean);
118       log.info("MBeanInfo: "+info);
119    }
120    public void testXMBeanDoSomething()
121       throws Exception JavaDoc
122    {
123       log.info("+++ testXMBeanDoSomething");
124       RMIAdaptor server = (RMIAdaptor) getInitialContext().lookup("jmx/invoker/RMIAdaptor");
125       ObjectName JavaDoc xmbean = new ObjectName JavaDoc("jboss.test:service=InvokerTest,type=XMBean");
126       Object JavaDoc[] args = {};
127       String JavaDoc[] sig = {};
128       CustomClass custom = (CustomClass) server.invoke(xmbean, "doSomething", args, sig);
129       log.info("doSomething: "+custom);
130    }
131
132    public void testSetCustom()
133       throws Exception JavaDoc
134    {
135       log.info("+++ testSetCustom");
136       RMIAdaptor server = (RMIAdaptor) getInitialContext().lookup("jmx/invoker/RMIAdaptor");
137       server.setAttribute(getObjectName(), new Attribute JavaDoc("Custom", new CustomClass("changed")));
138       CustomClass custom = (CustomClass) server.getAttribute(getObjectName(), "Custom");
139       assertEquals("changed", custom.getValue());
140    }
141
142    /**
143     * Create an mbean whose class does not exist to test that the exception
144     * seen from the adaptor is a ClassNotFoundException wrapped in a
145     * ReflectionException
146     * @throws Exception
147     */

148    public void testClassNotFoundException() throws Exception JavaDoc
149    {
150       log.info("+++ testClassNotFoundException");
151       MBeanServerConnection JavaDoc server = (MBeanServerConnection JavaDoc) getInitialContext().lookup("jmx/invoker/RMIAdaptor");
152       ObjectName JavaDoc name = new ObjectName JavaDoc("jboss.test:test=testClassNotFoundException");
153       try
154       {
155          server.createMBean("org.jboss.text.jmx.DoesNotExist", name);
156          fail("Was able to create org.jboss.text.jmx.DoesNotExist mbean");
157       }
158       catch (ReflectionException JavaDoc e)
159       {
160          Exception JavaDoc ex = e.getTargetException();
161          assertTrue("ReflectionException.target is ClassNotFoundException",
162             ex instanceof ClassNotFoundException JavaDoc);
163       }
164    }
165
166    /** Test the remoting of JMX Notifications
167     * @throws Exception
168     */

169    public void testNotification() throws Exception JavaDoc
170    {
171       log.info("+++ testNotification");
172       RMIListener listener = new RMIListener(10);
173       listener.export();
174       RMIAdaptor server = (RMIAdaptor) getInitialContext().lookup("jmx/invoker/RMIAdaptor");
175       server.addNotificationListener(getObjectName(), listener, null, "runTimer");
176       synchronized( listener )
177       {
178          listener.wait(15000);
179       }
180       server.removeNotificationListener(getObjectName(), listener);
181       listener.unexport();
182       int count = listener.getCount();
183       assertTrue("Received 10 notifications, count="+count, count == 10);
184    }
185
186    /** Test the remoting of JMX Notifications with a valid listener
187     * and a bad listener that attempts to hang the service by sleeping
188     * in the notification callback.
189     *
190     * @throws Exception
191     */

192    public void testNotificationWithBadListener() throws Exception JavaDoc
193    {
194       log.info("+++ testNotificationWithBadListener");
195       RMIAdaptor server = (RMIAdaptor) getInitialContext().lookup("jmx/invoker/RMIAdaptor");
196       // Add a bad listener
197
RMIBadListener badListener = new RMIBadListener();
198       badListener.export();
199       server.addNotificationListener(getObjectName(), badListener, null, "BadListener");
200       RMIListener listener = new RMIListener(10);
201       listener.export();
202       // Add a good listener
203
server.addNotificationListener(getObjectName(), listener, null, "runTimer");
204       // Wait 25 seconds for the good listener events to complete
205
synchronized( listener )
206       {
207          listener.wait(25000);
208       }
209       server.removeNotificationListener(getObjectName(), listener);
210       listener.unexport();
211       int count = listener.getCount();
212       assertTrue("Received 10 notifications from Listener, count="+count,
213          count == 10);
214       count = badListener.getCount();
215       assertTrue("Received >= 1 notifications from BadListener, count="+count,
216          count >= 1);
217       try
218       {
219          server.removeNotificationListener(getObjectName(), badListener);
220          badListener.unexport();
221       }
222       catch(ListenerNotFoundException JavaDoc e)
223       {
224
225          log.debug("The BadListener was not found", e);
226       }
227    }
228 }
229
Popular Tags