KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > clients > mbeans > F_WebServicesMBeans


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id$
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas.jtests.clients.mbeans;
26
27 import java.util.Properties JavaDoc;
28
29 import javax.management.ObjectName JavaDoc;
30 import javax.management.j2ee.Management JavaDoc;
31 import javax.management.j2ee.ManagementHome JavaDoc;
32 import javax.naming.InitialContext JavaDoc;
33 import javax.rmi.PortableRemoteObject JavaDoc;
34
35 import junit.framework.Test;
36 import junit.framework.TestSuite;
37
38 import org.objectweb.jonas.jtests.util.JWebServicesTestCase;
39
40 /**
41  * Test WebServices MBeans (WebServices, Service, PortComponent and Handler).
42  *
43  * @author Guillaume Sauthier
44  */

45 public class F_WebServicesMBeans extends JWebServicesTestCase {
46
47     /**
48      * MEJB instance
49      */

50     private Management JavaDoc mejb = null;
51
52     /**
53      * Service of Time Sample's ObjectName
54      */

55     private static final String JavaDoc SERVICE_MBEAN_ON = "jonas:type=WebService,name=TimeWebService,J2EEServer=jonas,J2EEApplication=time-test,EJBModule=time";
56
57     /**
58      * PortComponent of Time Sample's ObjectName
59      */

60     private static final String JavaDoc PC_MBEAN_ON = "jonas:type=WebServicePortComponent,name=TimePort,WebService=TimeWebService,J2EEServer=jonas,EJBModule=time,J2EEApplication=time-test";
61
62     /**
63      * Handler of Time Sample's ObjectName
64      */

65     private static final String JavaDoc HANDLER_MBEAN_ON = "jonas:type=WebServiceHandler,name=TimeBeanHandler,WebServicePortComponent=TimePort,EJBModule=time,J2EEServer=jonas,J2EEApplication=time-test,WebService=TimeWebService";
66
67     /**
68      * SessionBean realizing the Time Service
69      */

70     private static final String JavaDoc SSB_MBEAN_ON = "jonas:j2eeType=StatelessSessionBean,name=TimeBeanSLR,EJBModule=time,J2EEApplication=time-test,J2EEServer=jonas";
71
72     // =================== Attributes pre-filling ======================================
73

74     /**
75      * Service.portComponents
76      */

77     private static final String JavaDoc[] SERVICE_PCS_ATT = new String JavaDoc[] { PC_MBEAN_ON };
78
79     /**
80      * PortComponent.handlers
81      */

82     private static final String JavaDoc[] PC_HANDLERS_ATT = new String JavaDoc[] { HANDLER_MBEAN_ON };
83
84     /**
85      * Handler.soapHeaders
86      */

87     private static final String JavaDoc[] SOAP_HEADERS = new String JavaDoc[] { };
88
89     /**
90      * Handler.soapRoles
91      */

92     private static final String JavaDoc[] SOAP_ROLES = new String JavaDoc[] { };
93
94     /**
95      * Handler.initParams
96      */

97     private static Properties JavaDoc INIT_PARAMS = null;
98
99     /**
100      * Inner static class used to store tests expected results
101      *
102      * @author Guillaume Sauthier
103      */

104     private static class WsTest {
105         /**
106          * MBean attribute name
107          */

108         public String JavaDoc name;
109
110         /**
111          * MBean attribute type
112          */

113         public Class JavaDoc type;
114
115         /**
116          * Expected result
117          */

118         public Object JavaDoc expected;
119
120         /**
121          * does the String represents an ObjectName ?
122          */

123         public boolean isObjectName;
124
125         /**
126          * WsTest Constructor
127          * @param name att name
128          * @param type att type
129          * @param expected att value
130          */

131         public WsTest(String JavaDoc name, Class JavaDoc type, Object JavaDoc expected) {
132             this(name, type, expected, false);
133         }
134
135         /**
136          * WsTest Constructor
137          * @param name att name
138          * @param type att type
139          * @param expected att value
140          */

141         public WsTest(String JavaDoc name, Class JavaDoc type, Object JavaDoc expected, boolean on) {
142             super();
143             this.name = name;
144             this.type = type;
145             this.expected = expected;
146             this.isObjectName = on;
147         }
148     }
149
150     /**
151      * Tests values for Service MBean
152      */

153     private WsTest[] SERVICE_TEST = null;
154
155     /**
156      * Tests values for PortComponent MBean
157      */

158     private WsTest[] PORT_TEST = null;
159
160     /**
161      * Tests values for Handler MBean
162      */

163     private WsTest[] HANDLER_TEST = null;
164
165     /**
166      * Are the tables init ?
167      */

168     private boolean initialized = false;
169
170     public F_WebServicesMBeans(String JavaDoc s) {
171         super(s);
172     }
173
174     public static Test suite() {
175         return new TestSuite(F_WebServicesMBeans.class);
176     }
177
178     public void setUp() throws Exception JavaDoc {
179         super.setUp();
180         useEar("time-test");
181         mejb = getMEJB();
182         if (!initialized) {
183             initTestsTables();
184             initialized = true;
185         }
186     }
187
188     /**
189      * init tables
190      */

191     private void initTestsTables() {
192
193         INIT_PARAMS = new Properties JavaDoc();
194         INIT_PARAMS.put("jonas.test.server.handler", "JOnAS");
195
196         SERVICE_TEST = new WsTest[] {
197                 new WsTest("name", java.lang.String JavaDoc.class, "TimeWebService"),
198                 new WsTest("portComponents", java.lang.String JavaDoc[].class, SERVICE_PCS_ATT, true),
199                 new WsTest("wsdlURL", java.lang.String JavaDoc.class, getAbsoluteUrl("/time/TimePort/TimePort?JWSDL")),
200                 new WsTest("mappingFilename", java.lang.String JavaDoc.class, "META-INF/mapping.xml"),
201                 new WsTest("wsdlFilename", java.lang.String JavaDoc.class, "META-INF/wsdl/TimePort.wsdl") };
202
203         PORT_TEST = new WsTest[] {
204                 new WsTest("name", java.lang.String JavaDoc.class, "TimePort"),
205                 new WsTest("handlers", java.lang.String JavaDoc[].class, PC_HANDLERS_ATT, true),
206                 new WsTest("wsdlPort", java.lang.String JavaDoc.class, "{jonas:Time}TimePort"),
207                 new WsTest("serviceEndpointInterface", java.lang.String JavaDoc.class, "org.objectweb.jonas.jtests.beans.time.TimeEndpoint"),
208                 new WsTest("endpoint", java.lang.String JavaDoc.class, getAbsoluteUrl("/time/TimePort/TimePort")),
209                 new WsTest("implementationBean", java.lang.String JavaDoc.class, SSB_MBEAN_ON, true) };
210
211         HANDLER_TEST = new WsTest[] {
212                 new WsTest("name", java.lang.String JavaDoc.class, "TimeBeanHandler"),
213                 new WsTest("classname", java.lang.String JavaDoc.class, "org.objectweb.jonas.jtests.beans.time.TimeBeanHandler"),
214                 new WsTest("soapHeaders", java.lang.String JavaDoc[].class, SOAP_HEADERS),
215                 new WsTest("soapRoles", java.lang.String JavaDoc[].class, SOAP_ROLES),
216                 new WsTest("initParams", java.util.Properties JavaDoc.class, INIT_PARAMS) };
217
218     }
219
220
221     private Management JavaDoc getMEJB() throws Exception JavaDoc {
222         InitialContext JavaDoc ic = new InitialContext JavaDoc();
223         Object JavaDoc o = ic.lookup("ejb/mgmt/MEJB");
224         ManagementHome JavaDoc home = (ManagementHome JavaDoc) PortableRemoteObject.narrow(o, ManagementHome JavaDoc.class);
225         return home.create();
226     }
227
228     public void tearDown() throws Exception JavaDoc {
229         super.tearDown();
230         mejb = null;
231     }
232
233     public void testWebServiceMBean() throws Exception JavaDoc {
234         checkMBean(SERVICE_MBEAN_ON, SERVICE_TEST);
235     }
236
237     public void testPortComponentMBean() throws Exception JavaDoc {
238         checkMBean(PC_MBEAN_ON, PORT_TEST);
239         // check that the implementationBean is live
240
String JavaDoc se = (String JavaDoc) mejb.getAttribute(new ObjectName JavaDoc(PC_MBEAN_ON), "implementationBean");
241         assertTrue("implementationBean MBean must be registered", mejb.isRegistered(new ObjectName JavaDoc(se)));
242     }
243
244     public void testHandlerMBean() throws Exception JavaDoc {
245         checkMBean(HANDLER_MBEAN_ON, HANDLER_TEST);
246     }
247
248     private void checkMBean(String JavaDoc onStr, WsTest[] tests) throws Exception JavaDoc {
249         ObjectName JavaDoc on = ObjectName.getInstance(onStr);
250         assertTrue("ObjectName not registered " + on, mejb.isRegistered(on));
251
252         for (int i = 0; i < tests.length; i++) {
253
254             boolean isObjectName = tests[i].isObjectName;
255
256             Object JavaDoc o = mejb.getAttribute(on, tests[i].name);
257             assertNotNull("Cannot retrieve attribute '" + tests[i].name
258                     + "' from " + on, o);
259             assertEquals("Expecting type " + tests[i].type + " for attribute "
260                     + tests[i].name, tests[i].type, o.getClass());
261             if (tests[i].type.isArray()) {
262                 Object JavaDoc[] a1 = (Object JavaDoc[]) o;
263                 Object JavaDoc[] a2 = (Object JavaDoc[]) tests[i].expected;
264                 assertEquals("'" + tests[i].name + "' Array size error", a2.length, a1.length);
265
266                 for (int j = 0; j < a1.length; j++) {
267                     if (isObjectName) {
268                         assertEquals("'" + tests[i].name + "[" + j + "]' mismatch", new ObjectName JavaDoc((String JavaDoc) a2[j]), new ObjectName JavaDoc((String JavaDoc) a1[j]));
269                     } else {
270                         assertEquals("'" + tests[i].name + "[" + j + "]' mismatch", a2[j], a1[j]);
271                     }
272                 }
273             } else {
274                 if (isObjectName) {
275                     assertEquals("'" + tests[i].name + "' mismatch", new ObjectName JavaDoc((String JavaDoc) tests[i].expected), new ObjectName JavaDoc((String JavaDoc) o));
276                 } else {
277                     assertEquals("'" + tests[i].name + "' mismatch", tests[i].expected, o);
278                 }
279             }
280         }
281     }
282
283 }
284
Popular Tags