KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossnet > jmx > JmxUnitTestCase


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 // $Id: JmxUnitTestCase.java,v 1.1.1.1.6.3 2005/03/02 14:51:03 tdiesler Exp $
9

10 package org.jboss.test.jbossnet.jmx;
11
12 import com.ibm.wsdl.factory.WSDLFactoryImpl;
13 import junit.framework.Test;
14 import org.jboss.axis.client.Service;
15 import org.jboss.net.axis.AxisInvocationHandler;
16 import org.jboss.net.jmx.MBeanInvocationHandler;
17 import org.jboss.net.jmx.adaptor.RemoteAdaptor;
18 import org.jboss.net.jmx.adaptor.RemoteAdaptorInvocationHandler;
19 import org.jboss.test.jbossnet.JBossNetTestBase;
20 import org.xml.sax.InputSource JavaDoc;
21
22 import javax.management.ObjectName JavaDoc;
23 import javax.wsdl.Definition;
24 import javax.wsdl.factory.WSDLFactory;
25 import javax.wsdl.xml.WSDLReader;
26 import java.io.InputStream JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.Map JavaDoc;
29
30 /**
31  * Tests remote accessibility of JMX services
32  * @since 11. Oktober 2001
33  * @author <a HREF="mailto:Christoph.Jung@infor.de">Christoph G. Jung</a>
34  * @author <a HREF="mailto:pbraswell@utopiansoft.com">Peter Braswell</a>
35  * @version $Revision: 1.1.1.1.6.3 $
36  */

37 public class JmxUnitTestCase extends JBossNetTestBase
38 {
39    // static that holds the configured Axis jmx name
40
private static String JavaDoc AXIS_JMX_NAME = "jboss.net:service=Axis";
41
42    /* Member variables */
43    private String JavaDoc JMX_SERVICE_SOAP_ACTION = "JMXTest";
44    private String JavaDoc JMX_ADAPTOR_SOAP_ACTION = "RemoteAdaptor";
45    private String JavaDoc JMX_DYNAMIC_SERVICE_SOAP_ACTION = "JMXDynamicTest";
46
47    /** the map of methods to interface names */
48    private Map JavaDoc interfaceMap = new AxisInvocationHandler.DefaultInterfaceMap();
49
50    /** the map of methods to method names */
51    private Map JavaDoc methodMap = new AxisInvocationHandler.DefaultMethodMap();
52
53    // Constructors --------------------------------------------------
54
public JmxUnitTestCase(String JavaDoc name)
55    {
56       super(name);
57    }
58
59    /** where the config is stored */
60    protected String JavaDoc getAxisConfiguration()
61    {
62       return "jbossnet/jmx/client/" + super.getAxisConfiguration();
63    }
64
65    /** tests a very (untyped) basic call through the normal invocation handler */
66    public void testBasic() throws Exception JavaDoc
67    {
68       log.info("+++ testBasic");
69       AxisInvocationHandler handler =
70               createAxisInvocationHandler(JMX_ADAPTOR_SOAP_ACTION);
71       assertEquals("Testing basic invocation",
72               "jboss",
73               handler.invoke("RemoteAdaptor", "getDefaultDomain", new Object JavaDoc[0]));
74       assertEquals("Testing complex invocation",
75               Boolean.TRUE,
76               handler.invoke("RemoteAdaptor",
77                       "isRegistered",
78                       new Object JavaDoc[]{new ObjectName JavaDoc(AXIS_JMX_NAME)}));
79    }
80
81    /** tests a very (untyped) basic call through the mbean invocation handler */
82    public void testMBeanHandler() throws Exception JavaDoc
83    {
84       log.info("+++ testMBeanHandler");
85       MBeanInvocationHandler handler =
86               createMBeanInvocationHandler(JMX_ADAPTOR_SOAP_ACTION);
87       assertEquals("Testing mbean specific invocation",
88               "jboss",
89               handler.invoke("RemoteAdaptor",
90                       "getDefaultDomain",
91                       new Object JavaDoc[0],
92                       new Class JavaDoc[0]));
93       assertEquals("Testing custom serializer",
94               Boolean.TRUE,
95               handler.invoke("RemoteAdaptor",
96                       "isRegistered",
97                       new Object JavaDoc[]{new ObjectName JavaDoc(AXIS_JMX_NAME)},
98                       new Class JavaDoc[]{ObjectName JavaDoc.class}));
99    }
100
101    /** tests the (typed) adaptor access */
102    public void testAdaptor() throws Exception JavaDoc
103    {
104       log.info("+++ testAdaptor");
105       RemoteAdaptor handler = createRemoteAdaptor(JMX_ADAPTOR_SOAP_ACTION);
106       assertEquals("Testing handler", "jboss", handler.getDefaultDomain());
107       assertTrue("Testing handler with custom serializer",
108               handler.isRegistered(new ObjectName JavaDoc(AXIS_JMX_NAME)));
109    }
110
111    public void testGetter() throws Exception JavaDoc
112    {
113       MBeanInvocationHandler handler =
114               createMBeanInvocationHandler(JMX_SERVICE_SOAP_ACTION);
115       String JavaDoc str = (String JavaDoc)handler.invoke("JMXTest", // serviceName
116
"getTestString", // methodName
117
null, // arguments
118
null); // classes
119
assertEquals(str, "JMX_TEST_STRING");
120    }
121
122    public void testDynamicGetter() throws Exception JavaDoc
123    {
124       MBeanInvocationHandler handler =
125               createMBeanInvocationHandler(JMX_DYNAMIC_SERVICE_SOAP_ACTION);
126       String JavaDoc str = (String JavaDoc)handler.invoke("JMXDynamicTest", // serviceName
127
"getTestString", // methodName
128
null, // arguments
129
null); // classes
130
assertEquals(str, "JMX_TEST_STRING");
131    }
132
133    public void testSetter() throws Exception JavaDoc
134    {
135       MBeanInvocationHandler handler =
136               createMBeanInvocationHandler(JMX_SERVICE_SOAP_ACTION);
137       handler.invoke("JMXTest", // serviceName
138
"setTestString", // methodName
139
new String JavaDoc[]{"foo-dog"}, // arguments
140
new Class JavaDoc[]{String JavaDoc.class}); // classes
141
// invoke the getter and compare the answer with the
142
// set string value
143
String JavaDoc str = (String JavaDoc)handler.invoke("JMXTest", // serviceName
144
"getTestString", // methodName
145
null, // arguments
146
null); // classes
147
assertEquals(str, "foo-dog");
148    }
149
150    public void testSetterDynamic() throws Exception JavaDoc
151    {
152       MBeanInvocationHandler handler =
153               createMBeanInvocationHandler(JMX_DYNAMIC_SERVICE_SOAP_ACTION);
154       handler.invoke("JMXDynamicTest", // serviceName
155
"setTestString", // methodName
156
new String JavaDoc[]{"foo-dog"}, // arguments
157
new Class JavaDoc[]{String JavaDoc.class}); // classes
158
// invoke the getter and compare the answer with the
159
// set string value
160
String JavaDoc str = (String JavaDoc)handler.invoke("JMXDynamicTest", // serviceName
161
"getTestString", // methodName
162
null, // arguments
163
null); // classes
164
assertEquals(str, "foo-dog");
165    }
166
167    public void testMethodInvoke() throws Exception JavaDoc
168    {
169       MBeanInvocationHandler handler =
170               createMBeanInvocationHandler(JMX_SERVICE_SOAP_ACTION);
171       handler.invoke("jboss.net:service=JMXTestMBean", // serviceName
172
"noopOperation", // methodName
173
null, // arguments
174
null); // classes
175
}
176
177    public void testDynamicMethodInvoke() throws Exception JavaDoc
178    {
179       MBeanInvocationHandler handler =
180               createMBeanInvocationHandler(JMX_DYNAMIC_SERVICE_SOAP_ACTION);
181       handler.invoke("jboss.net:service=JMXDynamicTestMBean", // serviceName
182
"noopOperation", // methodName
183
null, // arguments
184
null); // classes
185
}
186
187    /** tests wsdl access */
188    public void checkWsdlOf(String JavaDoc service) throws Exception JavaDoc
189    {
190       // check if the WSDL is available
191
URL JavaDoc urlWSDL = new URL JavaDoc(SERVICES_LOCATION + "/" + service + "?wsdl");
192       InputStream JavaDoc isWSDL = urlWSDL.openStream();
193
194       // check if it is a valid WSDL
195
WSDLFactory wsdlFactory = new WSDLFactoryImpl();
196       WSDLReader wsdlReader = wsdlFactory.newWSDLReader();
197       Definition wsdlDefinition =
198               wsdlReader.readWSDL(null, new InputSource JavaDoc(isWSDL));
199       assertNotNull("cannot obtain the wsdl", wsdlDefinition);
200    }
201
202    /** tests wsdl access */
203    public void testWsdl() throws Exception JavaDoc
204    {
205       checkWsdlOf(JMX_SERVICE_SOAP_ACTION);
206       checkWsdlOf(JMX_DYNAMIC_SERVICE_SOAP_ACTION);
207    }
208
209    /** creates a new service */
210    private Service createService()
211    {
212       Service result = new Service();
213       result.setMaintainSession(true);
214       return result;
215    }
216
217    /** creates a new Axis service using the test engine*/
218    private AxisInvocationHandler createAxisInvocationHandler(String JavaDoc endpoint, String JavaDoc soapAction)
219            throws Exception JavaDoc
220    {
221       return new AxisInvocationHandler(new URL JavaDoc(endpoint),
222               soapAction,
223               createService(),
224               methodMap,
225               interfaceMap);
226    }
227
228    /** creates a new Axis service using the test engine*/
229    private AxisInvocationHandler createAxisInvocationHandler(String JavaDoc soapAction)
230            throws Exception JavaDoc
231    {
232       return createAxisInvocationHandler(SERVICES_LOCATION,
233               soapAction);
234    }
235
236    /** creates a new Axis service using the test engine*/
237    private MBeanInvocationHandler createMBeanInvocationHandler(String JavaDoc soapAction)
238            throws Exception JavaDoc
239    {
240       return new MBeanInvocationHandler(new URL JavaDoc(SERVICES_LOCATION),
241               soapAction,
242               createService(),
243               methodMap,
244               interfaceMap);
245    }
246
247    /** creates a new remote adaptor using the test engine*/
248    private RemoteAdaptor createRemoteAdaptor(String JavaDoc soapAction)
249            throws Exception JavaDoc
250    {
251       return RemoteAdaptorInvocationHandler.createRemoteAdaptor(createMBeanInvocationHandler(soapAction));
252    }
253
254    /**
255     * Method suite, deploys additional jmx bean+web service
256     * @return
257     * @throws Exception
258     */

259
260    public static Test suite() throws Exception JavaDoc
261    {
262       return getDeploySetup(JmxUnitTestCase.class, "jbossnet-jmx.sar");
263    }
264 }
265
Popular Tags