001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id$
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.jtests.clients.mbeans;
025:
026: import java.util.Properties;
027:
028: import javax.management.ObjectName;
029: import javax.management.j2ee.Management;
030: import javax.management.j2ee.ManagementHome;
031: import javax.naming.InitialContext;
032: import javax.rmi.PortableRemoteObject;
033:
034: import junit.framework.Test;
035: import junit.framework.TestSuite;
036:
037: import org.objectweb.jonas.jtests.util.JWebServicesTestCase;
038:
039: /**
040: * Test WebServices MBeans (WebServices, Service, PortComponent and Handler).
041: *
042: * @author Guillaume Sauthier
043: */
044: public class F_WebServicesMBeans extends JWebServicesTestCase {
045:
046: /**
047: * MEJB instance
048: */
049: private Management mejb = null;
050:
051: /**
052: * Service of Time Sample's ObjectName
053: */
054: private static final String SERVICE_MBEAN_ON = "jonas:type=WebService,name=TimeWebService,J2EEServer=jonas,J2EEApplication=time-test,EJBModule=time";
055:
056: /**
057: * PortComponent of Time Sample's ObjectName
058: */
059: private static final String PC_MBEAN_ON = "jonas:type=WebServicePortComponent,name=TimePort,WebService=TimeWebService,J2EEServer=jonas,EJBModule=time,J2EEApplication=time-test";
060:
061: /**
062: * Handler of Time Sample's ObjectName
063: */
064: private static final String HANDLER_MBEAN_ON = "jonas:type=WebServiceHandler,name=TimeBeanHandler,WebServicePortComponent=TimePort,EJBModule=time,J2EEServer=jonas,J2EEApplication=time-test,WebService=TimeWebService";
065:
066: /**
067: * SessionBean realizing the Time Service
068: */
069: private static final String SSB_MBEAN_ON = "jonas:j2eeType=StatelessSessionBean,name=TimeBeanSLR,EJBModule=time,J2EEApplication=time-test,J2EEServer=jonas";
070:
071: // =================== Attributes pre-filling ======================================
072:
073: /**
074: * Service.portComponents
075: */
076: private static final String[] SERVICE_PCS_ATT = new String[] { PC_MBEAN_ON };
077:
078: /**
079: * PortComponent.handlers
080: */
081: private static final String[] PC_HANDLERS_ATT = new String[] { HANDLER_MBEAN_ON };
082:
083: /**
084: * Handler.soapHeaders
085: */
086: private static final String[] SOAP_HEADERS = new String[] {};
087:
088: /**
089: * Handler.soapRoles
090: */
091: private static final String[] SOAP_ROLES = new String[] {};
092:
093: /**
094: * Handler.initParams
095: */
096: private static Properties INIT_PARAMS = null;
097:
098: /**
099: * Inner static class used to store tests expected results
100: *
101: * @author Guillaume Sauthier
102: */
103: private static class WsTest {
104: /**
105: * MBean attribute name
106: */
107: public String name;
108:
109: /**
110: * MBean attribute type
111: */
112: public Class type;
113:
114: /**
115: * Expected result
116: */
117: public Object expected;
118:
119: /**
120: * does the String represents an ObjectName ?
121: */
122: public boolean isObjectName;
123:
124: /**
125: * WsTest Constructor
126: * @param name att name
127: * @param type att type
128: * @param expected att value
129: */
130: public WsTest(String name, Class type, Object expected) {
131: this (name, type, expected, false);
132: }
133:
134: /**
135: * WsTest Constructor
136: * @param name att name
137: * @param type att type
138: * @param expected att value
139: */
140: public WsTest(String name, Class type, Object expected,
141: 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 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 {
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();
194: INIT_PARAMS.put("jonas.test.server.handler", "JOnAS");
195:
196: SERVICE_TEST = new WsTest[] {
197: new WsTest("name", java.lang.String.class,
198: "TimeWebService"),
199: new WsTest("portComponents", java.lang.String[].class,
200: SERVICE_PCS_ATT, true),
201: new WsTest("wsdlURL", java.lang.String.class,
202: getAbsoluteUrl("/time/TimePort/TimePort?JWSDL")),
203: new WsTest("mappingFilename", java.lang.String.class,
204: "META-INF/mapping.xml"),
205: new WsTest("wsdlFilename", java.lang.String.class,
206: "META-INF/wsdl/TimePort.wsdl") };
207:
208: PORT_TEST = new WsTest[] {
209: new WsTest("name", java.lang.String.class, "TimePort"),
210: new WsTest("handlers", java.lang.String[].class,
211: PC_HANDLERS_ATT, true),
212: new WsTest("wsdlPort", java.lang.String.class,
213: "{jonas:Time}TimePort"),
214: new WsTest("serviceEndpointInterface",
215: java.lang.String.class,
216: "org.objectweb.jonas.jtests.beans.time.TimeEndpoint"),
217: new WsTest("endpoint", java.lang.String.class,
218: getAbsoluteUrl("/time/TimePort/TimePort")),
219: new WsTest("implementationBean",
220: java.lang.String.class, SSB_MBEAN_ON, true) };
221:
222: HANDLER_TEST = new WsTest[] {
223: new WsTest("name", java.lang.String.class,
224: "TimeBeanHandler"),
225: new WsTest("classname", java.lang.String.class,
226: "org.objectweb.jonas.jtests.beans.time.TimeBeanHandler"),
227: new WsTest("soapHeaders", java.lang.String[].class,
228: SOAP_HEADERS),
229: new WsTest("soapRoles", java.lang.String[].class,
230: SOAP_ROLES),
231: new WsTest("initParams", java.util.Properties.class,
232: INIT_PARAMS) };
233:
234: }
235:
236: private Management getMEJB() throws Exception {
237: InitialContext ic = new InitialContext();
238: Object o = ic.lookup("ejb/mgmt/MEJB");
239: ManagementHome home = (ManagementHome) PortableRemoteObject
240: .narrow(o, ManagementHome.class);
241: return home.create();
242: }
243:
244: public void tearDown() throws Exception {
245: super .tearDown();
246: mejb = null;
247: }
248:
249: public void testWebServiceMBean() throws Exception {
250: checkMBean(SERVICE_MBEAN_ON, SERVICE_TEST);
251: }
252:
253: public void testPortComponentMBean() throws Exception {
254: checkMBean(PC_MBEAN_ON, PORT_TEST);
255: // check that the implementationBean is live
256: String se = (String) mejb.getAttribute(new ObjectName(
257: PC_MBEAN_ON), "implementationBean");
258: assertTrue("implementationBean MBean must be registered", mejb
259: .isRegistered(new ObjectName(se)));
260: }
261:
262: public void testHandlerMBean() throws Exception {
263: checkMBean(HANDLER_MBEAN_ON, HANDLER_TEST);
264: }
265:
266: private void checkMBean(String onStr, WsTest[] tests)
267: throws Exception {
268: ObjectName on = ObjectName.getInstance(onStr);
269: assertTrue("ObjectName not registered " + on, mejb
270: .isRegistered(on));
271:
272: for (int i = 0; i < tests.length; i++) {
273:
274: boolean isObjectName = tests[i].isObjectName;
275:
276: Object o = mejb.getAttribute(on, tests[i].name);
277: assertNotNull("Cannot retrieve attribute '" + tests[i].name
278: + "' from " + on, o);
279: assertEquals("Expecting type " + tests[i].type
280: + " for attribute " + tests[i].name, tests[i].type,
281: o.getClass());
282: if (tests[i].type.isArray()) {
283: Object[] a1 = (Object[]) o;
284: Object[] a2 = (Object[]) tests[i].expected;
285: assertEquals(
286: "'" + tests[i].name + "' Array size error",
287: a2.length, a1.length);
288:
289: for (int j = 0; j < a1.length; j++) {
290: if (isObjectName) {
291: assertEquals("'" + tests[i].name + "[" + j
292: + "]' mismatch", new ObjectName(
293: (String) a2[j]), new ObjectName(
294: (String) a1[j]));
295: } else {
296: assertEquals("'" + tests[i].name + "[" + j
297: + "]' mismatch", a2[j], a1[j]);
298: }
299: }
300: } else {
301: if (isObjectName) {
302: assertEquals("'" + tests[i].name + "' mismatch",
303: new ObjectName((String) tests[i].expected),
304: new ObjectName((String) o));
305: } else {
306: assertEquals("'" + tests[i].name + "' mismatch",
307: tests[i].expected, o);
308: }
309: }
310: }
311: }
312:
313: }
|