KickJava   Java API By Example, From Geeks To Geeks.

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


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.MBeanInfo JavaDoc;
26 import javax.management.MBeanServerConnection JavaDoc;
27 import javax.management.MalformedObjectNameException JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import javax.management.ReflectionException JavaDoc;
30 import javax.management.ListenerNotFoundException JavaDoc;
31 import javax.management.Notification JavaDoc;
32 import javax.management.NotificationFilter JavaDoc;
33 import javax.management.remote.JMXServiceURL JavaDoc;
34 import javax.management.remote.JMXConnector JavaDoc;
35 import javax.management.remote.JMXConnectorFactory JavaDoc;
36
37 import junit.framework.Test;
38 import org.jboss.jmx.adaptor.rmi.RMIAdaptor;
39 import org.jboss.test.JBossTestCase;
40 import org.jboss.test.jmx.invoker.CustomClass;
41 import org.jboss.test.jmx.invoker.InvokerTestMBean;
42 import org.jboss.test.jmx.invoker.Listener;
43 import org.jboss.test.jmx.invoker.BadListener;
44 import org.jboss.test.jmx.invoker.RunTimerFilter;
45
46 /**
47  * Tests for the jmx invoker adaptor.
48  *
49  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>
50  * @author Scott.Stark@jboss.org
51  * @version $Revision: 37406 $
52  */

53 public class JMXConnectorUnitTestCase
54    extends JBossTestCase
55 {
56    private JMXConnector JavaDoc connector;
57    private MBeanServerConnection JavaDoc server;
58
59    public JMXConnectorUnitTestCase(String JavaDoc name)
60    {
61       super(name);
62    }
63
64    public static Test suite()
65       throws Exception JavaDoc
66    {
67       return getDeploySetup(JMXConnectorUnitTestCase.class, "invoker-adaptor-test.ear");
68    }
69
70    /**
71     * The jmx object name name of the mbean under test
72     * @return The name of the mbean under test
73     * @throws javax.management.MalformedObjectNameException
74     */

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

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

172    public void testNotification() throws Exception JavaDoc
173    {
174       log.info("+++ testNotification");
175       Listener listener = new Listener(10);
176       server.addNotificationListener(getObjectName(), listener, new RunTimerFilter(), "runTimer");
177       synchronized( listener )
178       {
179          listener.wait(15000);
180       }
181       server.removeNotificationListener(getObjectName(), listener);
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       // Add a bad listener
196
BadListener badListener = new BadListener();
197       server.addNotificationListener(getObjectName(), badListener, null, "BadListener");
198       Listener listener = new Listener(10);
199       // Add a good listener
200
server.addNotificationListener(getObjectName(), listener, new RunTimerFilter(), "runTimer");
201       // Wait 25 seconds for the good listener events to complete
202
synchronized( listener )
203       {
204          listener.wait(25000);
205       }
206       server.removeNotificationListener(getObjectName(), listener);
207       int count = listener.getCount();
208       assertTrue("Received 10 notifications from Listener, count="+count,
209          count == 10);
210       count = badListener.getCount();
211       assertTrue("Received >= 1 notifications from BadListener, count="+count,
212          count >= 1);
213       try
214       {
215          server.removeNotificationListener(getObjectName(), badListener);
216       }
217       catch(ListenerNotFoundException JavaDoc e)
218       {
219          log.debug("The BadListener was not found", e);
220       }
221    }
222
223 }
224
Popular Tags