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: * @(#)TestEndpointRegistry.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.ArrayList;
032: import javax.jbi.servicedesc.ServiceEndpoint;
033: import javax.xml.namespace.QName;
034:
035: /**
036: * Tests for the EndpointRegistry class
037: *
038: * @author Sun Microsystems, Inc.
039: */
040: public class TestEndpointRegistry extends junit.framework.TestCase {
041: private static final String OWNER_A = "ownerA";
042: private static final String OWNER_B = "ownerB";
043: private static final QName SERVICE_A = new QName("serviceA");
044: private static final QName SERVICE_B = new QName("serviceB");
045: private static final String ENDPOINT_A = "endpointA";
046: private static final String ENDPOINT_B = "endpointB";
047: private static final QName INTERFACE_A = new QName("interfaceA");
048: private static final QName INTERFACE_B = new QName("interfaceB");
049:
050: private EndpointRegistry mRegistry;
051:
052: /**
053: * The constructor for this testcase, forwards the test name to
054: * the jUnit TestCase base class.
055: * @param aTestName String with the name of this test.
056: */
057: public TestEndpointRegistry(String aTestName) {
058: super (aTestName);
059: mRegistry = EndpointRegistry.getInstance();
060: }
061:
062: /**
063: * Setup for the test. This creates the ComponentRegistry instance
064: * and other objects needed for the tests.
065: * @throws Exception when set up fails for any reason.
066: */
067: public void setUp() throws Exception {
068: super .setUp();
069: }
070:
071: /**
072: * Cleanup for the test.
073: * @throws Exception when tearDown fails for any reason.
074: */
075: public void tearDown() throws Exception {
076: // Make sure that toString() works after any type of change.
077: System.out.println(mRegistry.toString());
078:
079: super .tearDown();
080: mRegistry.clear();
081: }
082:
083: // ============================= test methods ================================
084:
085: /**
086: * Verify registration and check for pollution.
087: */
088: public void testRegisterInternalEndpoint() throws Exception {
089: RegisteredEndpoint re1;
090: RegisteredEndpoint re2;
091:
092: re1 = mRegistry.registerInternalEndpoint(SERVICE_A, ENDPOINT_A,
093: OWNER_A);
094:
095: assertTrue(mRegistry
096: .countEndpoints(RegisteredEndpoint.INTERNAL) == 1);
097: assertTrue(mRegistry
098: .countEndpoints(RegisteredEndpoint.EXTERNAL) == 0);
099:
100: // If the same owner attempts to register the same endpoint, the
101: // same RegisteredEndpoint instance should be returned.
102:
103: re2 = mRegistry.registerInternalEndpoint(SERVICE_A, ENDPOINT_A,
104: OWNER_A);
105: assertTrue(mRegistry
106: .countEndpoints(RegisteredEndpoint.INTERNAL) == 1);
107: assertTrue(mRegistry
108: .countEndpoints(RegisteredEndpoint.EXTERNAL) == 0);
109: assertSame(re1, re2);
110:
111: // If a different owner attempts to register the same endpoint, an
112: // exception should be thrown.
113:
114: try {
115: re2 = mRegistry.registerInternalEndpoint(SERVICE_A,
116: ENDPOINT_A, OWNER_B);
117: fail("Expected exception not received.");
118: } catch (javax.jbi.messaging.MessagingException mEx) {
119: assertTrue(mRegistry
120: .countEndpoints(RegisteredEndpoint.INTERNAL) == 1);
121: assertTrue(mRegistry
122: .countEndpoints(RegisteredEndpoint.EXTERNAL) == 0);
123: }
124: }
125:
126: /**
127: * Verify registration and check for pollution.
128: */
129: public void testRegisterExternalEndpoint() throws Exception {
130: ServiceEndpoint external;
131: RegisteredEndpoint re1;
132: RegisteredEndpoint re2;
133:
134: external = new Endpoint(SERVICE_A, ENDPOINT_A);
135: re1 = mRegistry.registerExternalEndpoint(external, OWNER_A);
136:
137: assertTrue(mRegistry
138: .countEndpoints(RegisteredEndpoint.EXTERNAL) == 1);
139: assertTrue(mRegistry
140: .countEndpoints(RegisteredEndpoint.INTERNAL) == 0);
141:
142: // If the same owner attempts to register the same endpoint, an
143: // exception should be thrown.
144:
145: try {
146: re2 = mRegistry.registerExternalEndpoint(external, OWNER_A);
147: fail("Expected exception not received.");
148: } catch (javax.jbi.messaging.MessagingException mEx) {
149: assertTrue(mRegistry
150: .countEndpoints(RegisteredEndpoint.INTERNAL) == 0);
151: assertTrue(mRegistry
152: .countEndpoints(RegisteredEndpoint.EXTERNAL) == 1);
153: }
154:
155: // If a different owner attempts to register the same endpoint, an
156: // exception should be thrown.
157:
158: try {
159: re2 = mRegistry.registerExternalEndpoint(external, OWNER_B);
160: fail("Expected exception not received.");
161: } catch (javax.jbi.messaging.MessagingException mEx) {
162: assertTrue(mRegistry
163: .countEndpoints(RegisteredEndpoint.INTERNAL) == 0);
164: assertTrue(mRegistry
165: .countEndpoints(RegisteredEndpoint.EXTERNAL) == 1);
166: }
167: }
168:
169: /**
170: * Verify get succeeds on internal endpoint and check for pollution.
171: */
172: public void testGetInternalEndpoint() throws Exception {
173: mRegistry.registerInternalEndpoint(SERVICE_A, ENDPOINT_A,
174: OWNER_A);
175:
176: assertTrue(mRegistry.getInternalEndpoint(SERVICE_A, ENDPOINT_A) != null);
177: assertTrue(mRegistry.getExternalEndpoint(SERVICE_A, ENDPOINT_A) == null);
178: }
179:
180: /**
181: * Verify get succeeds on external endpoint and check for pollution.
182: */
183: public void testGetExternalEndpoint() throws Exception {
184: ServiceEndpoint external;
185:
186: external = new Endpoint(SERVICE_A, ENDPOINT_A);
187: mRegistry.registerExternalEndpoint(external, OWNER_A);
188:
189: assertTrue(mRegistry.getExternalEndpoint(SERVICE_A, ENDPOINT_A) != null);
190: assertTrue(mRegistry.getInternalEndpoint(SERVICE_A, ENDPOINT_A) == null);
191: }
192:
193: /**
194: * Verify get succeeds on internal endpoints and check for pollution.
195: */
196: public void testGetInternalEndpointsForService() throws Exception {
197: ServiceEndpoint[] internal;
198: ServiceEndpoint external;
199:
200: external = new Endpoint(SERVICE_A, ENDPOINT_B);
201:
202: mRegistry.registerInternalEndpoint(SERVICE_A, ENDPOINT_A,
203: OWNER_A);
204: mRegistry.registerInternalEndpoint(SERVICE_B, ENDPOINT_B,
205: OWNER_A);
206: mRegistry.registerExternalEndpoint(external, OWNER_A);
207:
208: internal = mRegistry.getInternalEndpointsForService(SERVICE_A,
209: true);
210:
211: assertTrue(internal.length == 1);
212:
213: internal = mRegistry
214: .getInternalEndpointsForService(null, false);
215:
216: assertTrue(internal.length == 2);
217: }
218:
219: /**
220: * Verify get succeeds on external endpoints and check for pollution.
221: */
222: public void testGetExternalEndpointsForService() throws Exception {
223: ServiceEndpoint[] external;
224:
225: mRegistry.registerInternalEndpoint(SERVICE_A, ENDPOINT_A,
226: OWNER_A);
227: mRegistry.registerExternalEndpoint(new Endpoint(SERVICE_A,
228: ENDPOINT_B), OWNER_A);
229:
230: external = mRegistry.getExternalEndpointsForService(SERVICE_A);
231:
232: assertTrue(external.length == 1);
233:
234: }
235:
236: /**
237: * Verify removal and check for pollution.
238: */
239: public void testRemoveEndpoint() throws Exception {
240: ServiceEndpoint external;
241: ServiceEndpoint internal;
242:
243: external = new Endpoint(SERVICE_A, ENDPOINT_B);
244: internal = mRegistry.registerInternalEndpoint(SERVICE_A,
245: ENDPOINT_A, OWNER_A);
246: mRegistry.registerExternalEndpoint(external, OWNER_B);
247:
248: assertTrue(mRegistry
249: .countEndpoints(RegisteredEndpoint.EXTERNAL) == 1);
250: assertTrue(mRegistry
251: .countEndpoints(RegisteredEndpoint.INTERNAL) == 1);
252:
253: mRegistry.removeEndpoint(internal);
254: assertTrue(mRegistry
255: .countEndpoints(RegisteredEndpoint.EXTERNAL) == 1);
256: assertTrue(mRegistry
257: .countEndpoints(RegisteredEndpoint.INTERNAL) == 0);
258:
259: mRegistry.removeEndpoint(external);
260: assertTrue(mRegistry
261: .countEndpoints(RegisteredEndpoint.EXTERNAL) == 0);
262: assertTrue(mRegistry
263: .countEndpoints(RegisteredEndpoint.INTERNAL) == 0);
264: }
265:
266: /**######################################################################
267: * #################### ConnectionManager Tests ########################
268: * ####################################################################*/
269:
270: /**
271: * Verify addition and check for pollution.
272: */
273: public void testAddInterfaceConnectionGood() throws Exception {
274: RegisteredEndpoint[] endpoints;
275: ServiceEndpoint a;
276: ServiceEndpoint b;
277:
278: a = mRegistry.registerInternalEndpoint(SERVICE_A, ENDPOINT_A,
279: "boo");
280: b = mRegistry.registerInternalEndpoint(SERVICE_B, ENDPOINT_B,
281: "hoo");
282: mRegistry.addInterfaceConnection(INTERFACE_A, SERVICE_A,
283: ENDPOINT_A);
284:
285: endpoints = mRegistry.getInternalEndpointsForInterface(
286: INTERFACE_A, null);
287: assertTrue(endpoints.length == 1);
288: assertTrue(endpoints[0].getServiceName().equals(SERVICE_A));
289: assertTrue(endpoints[0].getEndpointName().equals(ENDPOINT_A));
290:
291: endpoints = mRegistry.getInternalEndpointsForInterface(null,
292: null);
293: assertTrue(endpoints.length == 2);
294:
295: mRegistry.registerExternalEndpoint(a, "boo");
296: mRegistry.registerExternalEndpoint(b, "hoo");
297: mRegistry.addInterfaceConnection(INTERFACE_B, SERVICE_A,
298: ENDPOINT_A);
299:
300: endpoints = mRegistry.getExternalEndpointsForInterface(
301: INTERFACE_B, null);
302: assertTrue(endpoints.length == 1);
303: assertTrue(endpoints[0].getServiceName().equals(SERVICE_A));
304: assertTrue(endpoints[0].getEndpointName().equals(ENDPOINT_A));
305:
306: endpoints = mRegistry.getExternalEndpointsForInterface(null,
307: null);
308: assertTrue(endpoints.length == 2);
309:
310: }
311:
312: /**
313: * Make sure duplicates are rejected
314: */
315: public void testAddInterfaceConnectionBad() throws Exception {
316: mRegistry.addInterfaceConnection(INTERFACE_A, SERVICE_A,
317: ENDPOINT_A);
318:
319: try {
320: mRegistry.addInterfaceConnection(INTERFACE_A, SERVICE_A,
321: ENDPOINT_A);
322: } catch (javax.jbi.messaging.MessagingException ex) {
323: }
324: }
325:
326: /**
327: * Make sure identical duplicates are permitted.
328: */
329: public void testAddInterfaceConnectionDupeGood() throws Exception {
330: mRegistry.addInterfaceConnection(INTERFACE_A, SERVICE_A,
331: ENDPOINT_A);
332: mRegistry.addInterfaceConnection(INTERFACE_A, SERVICE_A,
333: ENDPOINT_A);
334: }
335:
336: /**
337: * Make sure non-identical duplicates are rejected.
338: */
339: public void testAddInterfaceConnectionDupeBad() throws Exception {
340: mRegistry.addInterfaceConnection(INTERFACE_A, SERVICE_A,
341: ENDPOINT_A);
342: try {
343: mRegistry.addInterfaceConnection(INTERFACE_A, SERVICE_A,
344: "foo");
345: fail("Able to add non-identical duplicate service connection");
346: } catch (javax.jbi.messaging.MessagingException ex) {
347: }
348: }
349:
350: /**
351: * Make sure identical duplicates are permitted.
352: */
353: public void testAddEndpointConnectionDupeGood() throws Exception {
354: mRegistry.addEndpointConnection(SERVICE_A, ENDPOINT_A,
355: SERVICE_B, ENDPOINT_B, Link.STANDARD);
356: mRegistry.addEndpointConnection(SERVICE_A, ENDPOINT_A,
357: SERVICE_B, ENDPOINT_B, Link.STANDARD);
358: }
359:
360: /**
361: * Make sure non-identical duplicates are rejected.
362: */
363: public void testAddEndpointConnectionDupeBad() throws Exception {
364: mRegistry.addEndpointConnection(SERVICE_A, ENDPOINT_A,
365: SERVICE_B, ENDPOINT_B, Link.STANDARD);
366:
367: try {
368: mRegistry.addEndpointConnection(SERVICE_A, ENDPOINT_A,
369: SERVICE_B, "foo", Link.STANDARD);
370:
371: fail("Able to add non-identical duplicate service connection");
372: } catch (javax.jbi.messaging.MessagingException ex) {
373: }
374: }
375:
376: /**
377: * Verify addition and check for pollution.
378: */
379: public void testAddEndpointConnectionGood() throws Exception {
380: mRegistry.addEndpointConnection(SERVICE_A, ENDPOINT_A,
381: SERVICE_B, ENDPOINT_B, Link.STANDARD);
382:
383: assertTrue(mRegistry
384: .countEndpoints(RegisteredEndpoint.INTERNAL) == 0);
385: assertTrue(mRegistry
386: .countEndpoints(RegisteredEndpoint.EXTERNAL) == 0);
387: assertTrue(mRegistry.countEndpoints(RegisteredEndpoint.LINKED) == 1);
388:
389: // make sure that a mapped endpoint is returned
390: RegisteredEndpoint re;
391:
392: re = mRegistry.getInternalEndpoint(SERVICE_A, ENDPOINT_A);
393: assertTrue(re instanceof LinkedEndpoint);
394: }
395:
396: /**
397: * Test conversion of service connections to internal endpoints.
398: */
399: public void testGetInternalEndpointsForServiceConversion()
400: throws Exception {
401: mRegistry.addEndpointConnection(SERVICE_A, ENDPOINT_A,
402: SERVICE_B, ENDPOINT_B, Link.STANDARD);
403: mRegistry.registerInternalEndpoint(SERVICE_B, ENDPOINT_B,
404: OWNER_B);
405:
406: RegisteredEndpoint[] re;
407:
408: // try without conversion
409: re = mRegistry.getInternalEndpointsForService(SERVICE_A, false);
410: assertTrue(re.length == 1);
411: assertTrue(re[0] instanceof LinkedEndpoint);
412: assertEquals(re[0].getServiceName(), SERVICE_A);
413:
414: // try with conversion
415: re = mRegistry.getInternalEndpointsForService(SERVICE_A, true);
416: assertTrue(re.length == 1);
417: assertTrue(re[0] instanceof InternalEndpoint);
418: assertEquals(re[0].getServiceName(), SERVICE_B);
419: }
420:
421: /**
422: * Test for mixed results, with linked and internal endpoints available
423: * for a given service.
424: */
425: public void testGetInternalEndpointsForServiceMixed()
426: throws Exception {
427: mRegistry.addEndpointConnection(SERVICE_A, "linked1",
428: SERVICE_B, ENDPOINT_B, Link.STANDARD);
429: mRegistry.addEndpointConnection(SERVICE_A, "linked2",
430: SERVICE_B, ENDPOINT_B, Link.STANDARD);
431:
432: mRegistry.registerInternalEndpoint(SERVICE_A, "internal1",
433: OWNER_B);
434: mRegistry.registerInternalEndpoint(SERVICE_A, "internal2",
435: OWNER_B);
436:
437: RegisteredEndpoint[] re;
438: ArrayList endpointNames = new ArrayList();
439:
440: // peform a query for the service SERVICE_A
441: re = mRegistry.getInternalEndpointsForService(SERVICE_A, false);
442: assertTrue(re.length == 4);
443:
444: // build a list of endpoint names and check 'em
445: for (int i = 0; i < re.length; i++) {
446: endpointNames.add(re[i].getEndpointName());
447: }
448:
449: assertTrue(endpointNames.contains("linked1"));
450: assertTrue(endpointNames.contains("linked2"));
451: assertTrue(endpointNames.contains("internal1"));
452: assertTrue(endpointNames.contains("internal2"));
453: }
454:
455: /**
456: * Test for mixed results, with linked and external endpoints available
457: * for a given service.
458: */
459: public void testGetExternalEndpointsForServiceMixed()
460: throws Exception {
461: mRegistry.registerExternalEndpoint(new Endpoint(SERVICE_A,
462: "internal1"), OWNER_B);
463: mRegistry.registerExternalEndpoint(new Endpoint(SERVICE_A,
464: "internal2"), OWNER_B);
465:
466: RegisteredEndpoint[] re;
467: ArrayList endpointNames = new ArrayList();
468:
469: // peform a query for the service SERVICE_A
470: re = mRegistry.getExternalEndpointsForService(SERVICE_A);
471: assertEquals(2, re.length);
472:
473: // build a list of endpoint names and check 'em
474: for (int i = 0; i < re.length; i++) {
475: endpointNames.add(re[i].getEndpointName());
476: }
477:
478: assertTrue(endpointNames.contains("internal1"));
479: assertTrue(endpointNames.contains("internal2"));
480: }
481:
482: /**
483: * Make sure duplicates are rejected
484: */
485: public void testAddEndpointConnectionBad() throws Exception {
486: mRegistry.addEndpointConnection(SERVICE_A, ENDPOINT_A,
487: SERVICE_B, ENDPOINT_B, Link.STANDARD);
488:
489: try {
490: mRegistry.addEndpointConnection(SERVICE_A, ENDPOINT_A,
491: SERVICE_B, ENDPOINT_B, Link.STANDARD);
492: } catch (javax.jbi.messaging.MessagingException ex) {
493: }
494: }
495:
496: /**
497: * Verify removal and check for pollution.
498: */
499: public void testRemoveInterfaceConnection() throws Exception {
500: mRegistry.addInterfaceConnection(INTERFACE_A, SERVICE_A,
501: ENDPOINT_A);
502:
503: // verify that the endpoint was removed
504: assertTrue(mRegistry.removeInterfaceConnection(INTERFACE_A,
505: SERVICE_A, ENDPOINT_A));
506:
507: // verify that subsequent calls fail
508: assertFalse(mRegistry.removeInterfaceConnection(INTERFACE_A,
509: SERVICE_A, ENDPOINT_A));
510: }
511:
512: /**
513: * Verify removal and check for pollution.
514: */
515: public void testRemoveEndpointConnection() throws Exception {
516: mRegistry.addEndpointConnection(SERVICE_A, ENDPOINT_A,
517: SERVICE_B, ENDPOINT_B, Link.STANDARD);
518:
519: // verify that the endpoint was removed
520: assertTrue(mRegistry.removeEndpointConnection(SERVICE_A,
521: ENDPOINT_A, SERVICE_B, ENDPOINT_B));
522:
523: // verify that subsequent calls fail
524: assertFalse(mRegistry.removeEndpointConnection(SERVICE_A,
525: ENDPOINT_A, SERVICE_B, ENDPOINT_B));
526: }
527:
528: class Endpoint implements ServiceEndpoint {
529: private QName mService;
530: private String mEndpoint;
531:
532: Endpoint(QName service, String endpoint) {
533: mService = service;
534: mEndpoint = endpoint;
535: }
536:
537: public org.w3c.dom.DocumentFragment getAsReference(
538: QName operationName) {
539: return null;
540: }
541:
542: public String getEndpointName() {
543: return mEndpoint;
544: }
545:
546: public QName[] getInterfaces() {
547: return null;
548: }
549:
550: public javax.xml.namespace.QName getServiceName() {
551: return mService;
552: }
553:
554: }
555: }
|