001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)TestMessageService.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.messaging;
030:
031: import java.util.Arrays;
032: import java.util.ArrayList;
033: import java.util.List;
034:
035: import javax.jbi.component.Component;
036: import javax.jbi.messaging.MessageExchange;
037: import javax.jbi.servicedesc.ServiceEndpoint;
038:
039: import javax.management.openmbean.CompositeData;
040: import javax.management.openmbean.CompositeDataSupport;
041: import javax.management.openmbean.CompositeType;
042: import javax.management.openmbean.SimpleType;
043: import javax.management.openmbean.OpenType;
044: import javax.transaction.xa.XAResource;
045:
046: import javax.xml.namespace.QName;
047:
048: /**
049: * Tests for the MessageService class
050: *
051: * @author Sun Microsystems, Inc.
052: */
053: public class TestMessageService extends junit.framework.TestCase {
054: /** Path to endpoint descriptor. */
055: public static final String ENDPOINT_XML_PATH = System
056: .getProperty("junit.srcroot")
057: + "/nms/regress/endpoint.wsdl";
058:
059: /** Component ID for test channel. */
060: private static final String ID_A = "ChannelA";
061: /** Component ID for test channel. */
062: private static final String ID_B = "ChannelB";
063:
064: /** Service and endpoint constants. */
065: private static final QName SERVICE_A = new QName("service-a");
066: private static final String ENDPOINT_A = "endpoint-a";
067: private static final QName SERVICE_B = new QName("service-b");
068: private static final String ENDPOINT_B = "endpoint-b";
069: private static final QName INTERFACE_FB = new QName("foobar");
070: private static final QName SERVICE_FOO = new QName("foo");
071: private static final String ENDPOINT_BAR = "bar";
072:
073: /** NMS impl */
074: private MessageService mMsgSvc;
075: /** NMR Environment Context */
076: private NMRContext mContext;
077: /** Test channel which is created/destroyed for each test. */
078: private DeliveryChannelImpl mChannelA;
079: /** Test channel which is created/destroyed for each test. */
080: private DeliveryChannelImpl mChannelB;
081: /** Endpoint Reference on Channel A */
082: private ServiceEndpoint mEndpointA;
083: /** Endpoint Reference on Channel B */
084: private ServiceEndpoint mEndpointB;
085: /** Exchange factory */
086: private ExchangeFactory mFactory;
087: /** Endpoint registry */
088: private EndpointRegistry mRegistry;
089:
090: /**
091: * The constructor for this testcase, forwards the test name to
092: * the jUnit TestCase base class.
093: * @param aTestName String with the name of this test.
094: */
095: public TestMessageService(String aTestName) {
096: super (aTestName);
097:
098: mMsgSvc = new MessageService();
099: mContext = new NMRContext(mMsgSvc);
100: mFactory = new ExchangeFactory(mMsgSvc);
101: mRegistry = EndpointRegistry.getInstance();
102: }
103:
104: /**
105: * Setup for the test. This creates the ComponentRegistry instance
106: * and other objects needed for the tests.
107: * @throws Exception when set up fails for any reason.
108: */
109: public void setUp() throws Exception {
110: super .setUp();
111:
112: mMsgSvc.initService(mContext);
113: mMsgSvc.startService();
114:
115: // create test channels and add them to the NMS routing table
116: mChannelA = new DeliveryChannelImpl(ID_A, null, mMsgSvc, null);
117: mChannelB = new DeliveryChannelImpl(ID_B, null, mMsgSvc, null);
118: mMsgSvc.addChannel(mChannelA);
119: mMsgSvc.addChannel(mChannelB);
120:
121: mEndpointA = mChannelA.activateEndpoint(SERVICE_A, ENDPOINT_A);
122: mEndpointB = mChannelB.activateEndpoint(SERVICE_B, ENDPOINT_B);
123: }
124:
125: /**
126: * Cleanup for the test.
127: * @throws Exception when tearDown fails for any reason.
128: */
129: public void tearDown() throws Exception {
130: super .tearDown();
131:
132: mChannelA.close();
133: mChannelB.close();
134:
135: mMsgSvc.stopService();
136: mContext.reset();
137: }
138:
139: // ============================= test methods ================================
140:
141: /**
142: * testGetActiveChannelCount
143: * @throws Exception if an unexpected error occurs
144: */
145: public void testGetActiveChannelCount() throws Exception {
146: // we activate two channels in test setup()
147: assertTrue(mMsgSvc.getActiveChannelCount() == 2);
148:
149: // activate a channel and test to see if count is incremented
150: mMsgSvc.activateChannel("foo", null);
151: assertTrue(mMsgSvc.getActiveChannelCount() == 3);
152: }
153:
154: /**
155: * testGetActiveChannels
156: * @throws Exception if an unexpected error occurs
157: */
158: public void testGetActiveChannels() throws Exception {
159: String[] channels;
160: List list;
161:
162: channels = mMsgSvc.getActiveChannels();
163:
164: // verify size
165: assertTrue(channels.length == 2);
166:
167: // sort the array and search for the endpoints that should be there
168: list = Arrays.asList(channels);
169: assertTrue(list.contains(ID_A));
170: assertTrue(list.contains(ID_B));
171: }
172:
173: /**
174: * testGetActiveEndpointCount
175: * @throws Exception if an unexpected error occurs
176: */
177: public void testGetActiveEndpointCount() throws Exception {
178: // we activate two endpoints in test setup()
179: assertTrue(mMsgSvc.getActiveEndpointCount() == 2);
180:
181: // activate a channel and test to see if count is incremented
182: mChannelA.activateEndpoint(SERVICE_FOO, ENDPOINT_BAR);
183: assertTrue(mMsgSvc.getActiveEndpointCount() == 3);
184: }
185:
186: /**
187: * testGetActiveEndpoints
188: * @throws Exception if an unexpected error occurs
189: */
190: public void testGetActiveEndpoints() throws Exception {
191: String[] endpoints;
192: List list;
193:
194: list = new ArrayList();
195: endpoints = mMsgSvc.getActiveEndpoints();
196:
197: // index 1 = endpoint name
198: for (int i = 0; i < endpoints.length; i++) {
199: list.add(endpoints[i]);
200: }
201:
202: // verify size
203: assertTrue(list.size() == 2);
204:
205: // verify contents
206: assertTrue(list.contains(((RegisteredEndpoint) mEndpointA)
207: .toExternalName()));
208: assertTrue(list.contains(((RegisteredEndpoint) mEndpointB)
209: .toExternalName()));
210: }
211:
212: /** Send using a interface connection. */
213: public void testSendInterfaceConnection() throws Exception {
214: MessageExchange exchange;
215:
216: // point interface at endpoint on channel A
217: mRegistry.addInterfaceConnection(INTERFACE_FB, SERVICE_A,
218: ENDPOINT_A);
219:
220: exchange = mFactory.createInOnlyExchange();
221: exchange.setInterfaceName(INTERFACE_FB);
222: exchange.setOperation(new QName("op"));
223:
224: mChannelB.send(exchange);
225:
226: assertTrue(mChannelA.accept(1000) != null);
227: }
228:
229: /** Send using an endpoint connection with service name. */
230: public void testSendEndpointConnection() throws Exception {
231: MessageExchange exchange;
232:
233: // point interface at endpoint on channel A
234: mRegistry.addEndpointConnection(SERVICE_FOO, ENDPOINT_BAR,
235: SERVICE_A, ENDPOINT_A, Link.STANDARD);
236:
237: exchange = mFactory.createInOnlyExchange();
238: exchange.setService(SERVICE_FOO);
239: exchange.setOperation(new QName("op"));
240:
241: mChannelB.send(exchange);
242:
243: assertTrue(mChannelA.accept(1000) != null);
244: }
245:
246: /** Send using an endpoint connection using service endpoint. */
247: public void testSendEndpointConnection2() throws Exception {
248: ServiceEndpoint endpoint;
249: MessageExchange consumerEx;
250: MessageExchange providerEx;
251:
252: // point interface at endpoint on channel A
253: mRegistry.addEndpointConnection(SERVICE_FOO, ENDPOINT_BAR,
254: SERVICE_A, ENDPOINT_A, Link.STANDARD);
255:
256: endpoint = mRegistry.getInternalEndpoint(SERVICE_FOO,
257: ENDPOINT_BAR);
258:
259: consumerEx = mFactory.createInOnlyExchange();
260: consumerEx.setEndpoint(endpoint);
261: consumerEx.setOperation(new QName("op"));
262:
263: mChannelB.send(consumerEx);
264:
265: // make sure it got to the other side
266: providerEx = mChannelA.accept(1000);
267: assertTrue(providerEx != null);
268:
269: // verify that send() didn't overwrite the endpoint link for consumer
270: assertEquals(endpoint, consumerEx.getEndpoint());
271: assertEquals(mEndpointA, providerEx.getEndpoint());
272: assertFalse(endpoint.equals(mEndpointA));
273:
274: }
275:
276: /** Send using an endpoint connection which does not have a corresponding
277: * activated endpoint.
278: */
279: public void testSendEndpointConnection3() throws Exception {
280: ServiceEndpoint endpoint;
281: MessageExchange consumerEx;
282: MessageExchange providerEx;
283:
284: // point interface at endpoint on channel A
285: mRegistry.addEndpointConnection(SERVICE_FOO, ENDPOINT_BAR,
286: SERVICE_A, "blahblah", Link.STANDARD);
287:
288: endpoint = mRegistry.getInternalEndpoint(SERVICE_FOO,
289: ENDPOINT_BAR);
290:
291: consumerEx = mFactory.createInOnlyExchange();
292: consumerEx.setEndpoint(endpoint);
293: consumerEx.setOperation(new QName("op"));
294:
295: try {
296: mChannelB.send(consumerEx);
297: fail("Able to send using unlinked service connection");
298: } catch (javax.jbi.messaging.MessagingException msgEx) {
299: // we should end up here
300: }
301: }
302:
303: /** Test eh XAResource registration primitives.
304: */
305: public void testXAResourceRegistration() throws Exception {
306: XAResource res1 = new XAtest();
307: XAResource res2 = new XAtest();
308: XAResource[] ress;
309:
310: mMsgSvc.addXAResource(res1);
311: mMsgSvc.addXAResource(res2);
312: ress = mMsgSvc.getXAResources();
313: assertTrue(ress.length == 2);
314: assertTrue(ress[0] == res1 || ress[1] == res1);
315: assertTrue(ress[0] == res2 || ress[1] == res2);
316: mMsgSvc.purgeXAResources();
317: ress = mMsgSvc.getXAResources();
318: assertTrue(ress.length == 0);
319: }
320:
321: public void testMessageServiceStatistics() throws Exception {
322: CompositeData cd = mMsgSvc.getStatistics();
323: assertTrue(cd != null);
324: assertTrue(cd.values().size() == 40);
325: mMsgSvc.enableStatistics();
326: assertTrue(mMsgSvc.statisticsEnabled());
327: mMsgSvc.disableStatistics();
328: assertTrue(!mMsgSvc.statisticsEnabled());
329:
330: }
331:
332: public void testDeliveryChannelStatistics() throws Exception {
333: MessageExchange exchange;
334:
335: exchange = mFactory.createInOnlyExchange();
336: exchange.setService(SERVICE_A);
337: exchange.setOperation(new QName("op"));
338:
339: mChannelB.send(exchange);
340:
341: assertTrue(mChannelA.accept(1000) != null);
342:
343: String[] c = mMsgSvc.getChannelNames();
344: for (int i = 0; i < c.length; i++) {
345: ChannelStatistics cs = mMsgSvc.getChannelStatistics(c[i]);
346: assertTrue(cs != null);
347: CompositeData cd = cs.getStatistics();
348: assertTrue(cd != null);
349: assertTrue(cd.values().size() == 37);
350: }
351: }
352:
353: public void testEndpointStatistics() throws Exception {
354: MessageExchange exchange;
355:
356: exchange = mFactory.createInOnlyExchange();
357: exchange.setService(SERVICE_A);
358: exchange.setOperation(new QName("op"));
359:
360: mChannelB.send(exchange);
361:
362: assertTrue(mChannelA.accept(1000) != null);
363:
364: String[] c = mMsgSvc.getEndpointNames();
365: for (int i = 0; i < c.length; i++) {
366: EndpointStatistics es = mMsgSvc.getEndpointStatistics(c[i]);
367: assertTrue(es != null);
368: CompositeData cd = es.getStatistics();
369: assertTrue(cd != null);
370: assertTrue(cd.values().size() == 30);
371: }
372: }
373: }
374:
375: class XAtest implements XAResource {
376: public void start(javax.transaction.xa.Xid tx, int id) {
377:
378: }
379:
380: public boolean setTransactionTimeout(int timeout) {
381: return (false);
382: }
383:
384: public void rollback(javax.transaction.xa.Xid tx) {
385:
386: }
387:
388: public javax.transaction.xa.Xid[] recover(int id) {
389: return (null);
390: }
391:
392: public int prepare(javax.transaction.xa.Xid id) {
393: return (0);
394: }
395:
396: public boolean isSameRM(javax.transaction.xa.XAResource resource) {
397: return (false);
398: }
399:
400: public int getTransactionTimeout() {
401: return (0);
402: }
403:
404: public void forget(javax.transaction.xa.Xid id) {
405:
406: }
407:
408: public void end(javax.transaction.xa.Xid xid, int id) {
409:
410: }
411:
412: public void commit(javax.transaction.xa.Xid xid, boolean x) {
413:
414: }
415: }
|