001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.ws.rm;
019:
020: import java.lang.reflect.Method;
021: import java.util.ArrayList;
022: import java.util.Collection;
023: import java.util.Collections;
024: import java.util.List;
025:
026: import javax.wsdl.extensions.ExtensibilityElement;
027: import javax.xml.namespace.QName;
028:
029: import org.apache.cxf.Bus;
030: import org.apache.cxf.endpoint.Endpoint;
031: import org.apache.cxf.interceptor.Interceptor;
032: import org.apache.cxf.service.Service;
033: import org.apache.cxf.service.model.BindingInfo;
034: import org.apache.cxf.service.model.BindingOperationInfo;
035: import org.apache.cxf.service.model.EndpointInfo;
036: import org.apache.cxf.service.model.InterfaceInfo;
037: import org.apache.cxf.service.model.OperationInfo;
038: import org.apache.cxf.service.model.ServiceInfo;
039: import org.apache.cxf.transport.Conduit;
040: import org.apache.cxf.ws.addressing.Names;
041: import org.apache.cxf.ws.policy.EffectivePolicy;
042: import org.apache.cxf.ws.policy.EndpointPolicy;
043: import org.apache.cxf.ws.policy.PolicyEngine;
044: import org.apache.cxf.ws.policy.PolicyInterceptorProviderRegistry;
045: import org.apache.neethi.Assertion;
046: import org.apache.neethi.Policy;
047: import org.easymock.classextension.EasyMock;
048: import org.easymock.classextension.IMocksControl;
049: import org.junit.After;
050: import org.junit.Assert;
051: import org.junit.Before;
052: import org.junit.Test;
053:
054: public class RMEndpointTest extends Assert {
055:
056: private IMocksControl control;
057: private RMManager manager;
058: private Endpoint ae;
059: private RMEndpoint rme;
060:
061: @Before
062: public void setUp() {
063: control = EasyMock.createNiceControl();
064: manager = control.createMock(RMManager.class);
065: ae = control.createMock(Endpoint.class);
066: rme = new RMEndpoint(manager, ae);
067: }
068:
069: @After
070: public void tearDown() {
071: control.verify();
072: }
073:
074: @Test
075: public void testConstructor() {
076: control.replay();
077: assertNotNull(rme);
078: assertNull(rme.getEndpoint());
079: assertNull(rme.getService());
080: assertNull(rme.getConduit());
081: assertNull(rme.getReplyTo());
082: }
083:
084: @Test
085: public void testGetManager() {
086: control.replay();
087: assertSame(manager, rme.getManager());
088: }
089:
090: @Test
091: public void testGetApplicationEndpoint() {
092: control.replay();
093: assertSame(ae, rme.getApplicationEndpoint());
094: }
095:
096: @Test
097: public void testGetProxy() {
098: control.replay();
099: assertSame(rme, rme.getProxy().getReliableEndpoint());
100: }
101:
102: @Test
103: public void testGetServant() {
104: control.replay();
105: assertNotNull(rme.getServant());
106: }
107:
108: @Test
109: public void testGetSetDestination() {
110: Destination d = control.createMock(Destination.class);
111: control.replay();
112: assertSame(rme, rme.getDestination().getReliableEndpoint());
113: rme.setDestination(d);
114: assertSame(d, rme.getDestination());
115: }
116:
117: @Test
118: public void testGetSetSource() {
119: Source s = control.createMock(Source.class);
120: control.replay();
121: assertSame(rme, rme.getSource().getReliableEndpoint());
122: rme.setSource(s);
123: assertSame(s, rme.getSource());
124: }
125:
126: @Test
127: public void testInitialise() throws NoSuchMethodException {
128: Method m1 = RMEndpoint.class.getDeclaredMethod("createService",
129: new Class[] {});
130: Method m2 = RMEndpoint.class.getDeclaredMethod(
131: "createEndpoint", new Class[] {});
132: Method m3 = RMEndpoint.class.getDeclaredMethod("setPolicies",
133: new Class[] {});
134:
135: rme = control.createMock(RMEndpoint.class, new Method[] { m1,
136: m2, m3 });
137: rme.createService();
138: EasyMock.expectLastCall();
139: rme.createEndpoint();
140: EasyMock.expectLastCall();
141: rme.setPolicies();
142: EasyMock.expectLastCall();
143: Conduit c = control.createMock(Conduit.class);
144: org.apache.cxf.ws.addressing.EndpointReferenceType epr = control
145: .createMock(org.apache.cxf.ws.addressing.EndpointReferenceType.class);
146: control.replay();
147: rme.initialise(c, epr);
148: assertSame(c, rme.getConduit());
149: assertSame(epr, rme.getReplyTo());
150: }
151:
152: @Test
153: public void testCreateService() {
154: Service as = control.createMock(Service.class);
155: EasyMock.expect(ae.getService()).andReturn(as);
156: control.replay();
157: rme.createService();
158: Service s = rme.getService();
159: assertNotNull(s);
160: WrappedService ws = (WrappedService) s;
161: assertSame(as, ws.getWrappedService());
162: assertSame(rme.getServant(), s.getInvoker());
163: verifyService();
164: }
165:
166: @Test
167: public void testCreateEndpoint() throws NoSuchMethodException {
168: Method m = RMEndpoint.class.getDeclaredMethod(
169: "getUsingAddressing",
170: new Class[] { EndpointInfo.class });
171: rme = control.createMock(RMEndpoint.class, new Method[] { m });
172: rme.setAplicationEndpoint(ae);
173: rme.setManager(manager);
174: Service as = control.createMock(Service.class);
175: EasyMock.expect(ae.getService()).andReturn(as);
176: EndpointInfo aei = control.createMock(EndpointInfo.class);
177: EasyMock.expect(ae.getEndpointInfo()).andReturn(aei).times(2);
178: BindingInfo bi = control.createMock(BindingInfo.class);
179: EasyMock.expect(aei.getBinding()).andReturn(bi);
180: String ns = "http://schemas.xmlsoap.org/wsdl/soap/";
181: EasyMock.expect(bi.getBindingId()).andReturn(ns);
182: EasyMock.expect(aei.getTransportId()).andReturn(ns);
183: String addr = "addr";
184: EasyMock.expect(aei.getAddress()).andReturn(addr);
185: Object ua = new Object();
186: EasyMock.expect(rme.getUsingAddressing(aei)).andReturn(ua);
187: control.replay();
188: rme.createService();
189: rme.createEndpoint();
190: Endpoint e = rme.getEndpoint();
191: WrappedEndpoint we = (WrappedEndpoint) e;
192: assertSame(ae, we.getWrappedEndpoint());
193: Service s = rme.getService();
194: assertEquals(1, s.getEndpoints().size());
195: assertSame(e, s.getEndpoints().get(RMConstants.getPortName()));
196: }
197:
198: @Test
199: public void testGetUsingAddressing() {
200: EndpointInfo ei = null;
201: control.replay();
202: assertNull(rme.getUsingAddressing(ei));
203: control.verify();
204:
205: control.reset();
206: ExtensibilityElement ua = control
207: .createMock(ExtensibilityElement.class);
208: ei = control.createMock(EndpointInfo.class);
209: List<ExtensibilityElement> noExts = new ArrayList<ExtensibilityElement>();
210: List<ExtensibilityElement> exts = new ArrayList<ExtensibilityElement>();
211: exts.add(ua);
212: EasyMock.expect(ei.getExtensors(ExtensibilityElement.class))
213: .andReturn(noExts);
214: BindingInfo bi = control.createMock(BindingInfo.class);
215: EasyMock.expect(ei.getBinding()).andReturn(bi).times(2);
216: EasyMock.expect(bi.getExtensors(ExtensibilityElement.class))
217: .andReturn(noExts);
218: ServiceInfo si = control.createMock(ServiceInfo.class);
219: EasyMock.expect(ei.getService()).andReturn(si).times(2);
220: EasyMock.expect(si.getExtensors(ExtensibilityElement.class))
221: .andReturn(exts);
222: EasyMock.expect(ua.getElementType()).andReturn(
223: Names.WSAW_USING_ADDRESSING_QNAME);
224: control.replay();
225: assertSame(ua, rme.getUsingAddressing(ei));
226: }
227:
228: @Test
229: public void testGetUsingAddressingFromExtensions() {
230: List<ExtensibilityElement> exts = new ArrayList<ExtensibilityElement>();
231: ExtensibilityElement ua = control
232: .createMock(ExtensibilityElement.class);
233: exts.add(ua);
234: EasyMock.expect(ua.getElementType()).andReturn(
235: Names.WSAW_USING_ADDRESSING_QNAME);
236: control.replay();
237: assertSame(ua, rme.getUsingAddressing(exts));
238: }
239:
240: @Test
241: public void testMessageArrivals() {
242: assertEquals(0L, rme.getLastApplicationMessage());
243: assertEquals(0L, rme.getLastControlMessage());
244: rme.receivedControlMessage();
245: assertEquals(0L, rme.getLastApplicationMessage());
246: assertTrue(rme.getLastControlMessage() > 0);
247: rme.receivedApplicationMessage();
248: assertTrue(rme.getLastApplicationMessage() > 0);
249: assertTrue(rme.getLastControlMessage() > 0);
250: control.replay();
251: }
252:
253: @Test
254: public void testSetPoliciesNoEngine() {
255: Bus bus = control.createMock(Bus.class);
256: EasyMock.expect(manager.getBus()).andReturn(bus);
257: EasyMock.expect(bus.getExtension(PolicyEngine.class))
258: .andReturn(null);
259: control.replay();
260: rme.setPolicies();
261: }
262:
263: @Test
264: public void testSetPoliciesEngineDisabled() {
265: Bus bus = control.createMock(Bus.class);
266: EasyMock.expect(manager.getBus()).andReturn(bus);
267: PolicyEngine pe = control.createMock(PolicyEngine.class);
268: EasyMock.expect(bus.getExtension(PolicyEngine.class))
269: .andReturn(pe);
270: EasyMock.expect(pe.isEnabled()).andReturn(false);
271: control.replay();
272: rme.setPolicies();
273: }
274:
275: @Test
276: public void testSetPolicies() throws NoSuchMethodException {
277: Method m = RMEndpoint.class.getDeclaredMethod("getEndpoint",
278: new Class[] {});
279: rme = control.createMock(RMEndpoint.class, new Method[] { m });
280: rme.setAplicationEndpoint(ae);
281: rme.setManager(manager);
282: Endpoint e = control.createMock(Endpoint.class);
283: EasyMock.expect(rme.getEndpoint()).andReturn(e);
284: EndpointInfo ei = control.createMock(EndpointInfo.class);
285: EasyMock.expect(e.getEndpointInfo()).andReturn(ei);
286: Bus bus = control.createMock(Bus.class);
287: EasyMock.expect(manager.getBus()).andReturn(bus).times(2);
288: PolicyEngine pe = control.createMock(PolicyEngine.class);
289: EasyMock.expect(bus.getExtension(PolicyEngine.class))
290: .andReturn(pe);
291: EasyMock.expect(pe.isEnabled()).andReturn(true);
292: PolicyInterceptorProviderRegistry reg = control
293: .createMock(PolicyInterceptorProviderRegistry.class);
294: EasyMock
295: .expect(
296: bus
297: .getExtension(PolicyInterceptorProviderRegistry.class))
298: .andReturn(reg);
299: EndpointInfo aei = control.createMock(EndpointInfo.class);
300: EasyMock.expect(ae.getEndpointInfo()).andReturn(aei);
301: EndpointPolicy epi = control.createMock(EndpointPolicy.class);
302: EasyMock.expect(pe.getServerEndpointPolicy(aei, null))
303: .andReturn(epi);
304: EasyMock.expect(epi.getChosenAlternative()).andReturn(
305: new ArrayList<Assertion>());
306:
307: pe.setEndpointPolicy(ei, epi);
308: EasyMock.expectLastCall();
309: BindingInfo bi = control.createMock(BindingInfo.class);
310: EasyMock.expect(ei.getBinding()).andReturn(bi);
311: BindingOperationInfo boi = control
312: .createMock(BindingOperationInfo.class);
313: EasyMock.expect(bi.getOperations()).andReturn(
314: Collections.singletonList(boi));
315: pe.setEffectiveServerRequestPolicy(EasyMock.eq(ei), EasyMock
316: .eq(boi), EasyMock.isA(EffectivePolicy.class));
317: EasyMock.expectLastCall();
318: pe.setEffectiveServerResponsePolicy(EasyMock.eq(ei), EasyMock
319: .eq(boi), EasyMock.isA(EffectivePolicy.class));
320: EasyMock.expectLastCall();
321: pe.setEffectiveClientRequestPolicy(EasyMock.eq(ei), EasyMock
322: .eq(boi), EasyMock.isA(EffectivePolicy.class));
323: EasyMock.expectLastCall();
324: pe.setEffectiveClientResponsePolicy(EasyMock.eq(ei), EasyMock
325: .eq(boi), EasyMock.isA(EffectivePolicy.class));
326: EasyMock.expectLastCall();
327: control.replay();
328: rme.setPolicies();
329: }
330:
331: @Test
332: public void testShutdown() {
333: DestinationSequence ds = control
334: .createMock(DestinationSequence.class);
335: Identifier did = control.createMock(Identifier.class);
336: EasyMock.expect(ds.getIdentifier()).andReturn(did);
337: String d = "d";
338: EasyMock.expect(did.getValue()).andReturn(d);
339: SourceSequence ss = control.createMock(SourceSequence.class);
340: Identifier sid = control.createMock(Identifier.class);
341: EasyMock.expect(ss.getIdentifier()).andReturn(sid);
342: String s = "s";
343: EasyMock.expect(sid.getValue()).andReturn(s);
344: ds.cancelDeferredAcknowledgments();
345: EasyMock.expectLastCall();
346: ds.cancelTermination();
347: EasyMock.expectLastCall();
348: RetransmissionQueue queue = control
349: .createMock(RetransmissionQueue.class);
350: EasyMock.expect(manager.getRetransmissionQueue()).andReturn(
351: queue);
352: queue.stop(ss);
353: EasyMock.expectLastCall();
354: control.replay();
355: rme.getDestination().addSequence(ds, false);
356: rme.getSource().addSequence(ss, false);
357: rme.shutdown();
358: }
359:
360: @Test
361: public void testEffectivePolicyImpl() {
362: EndpointPolicy ep = control.createMock(EndpointPolicy.class);
363: Collection<Assertion> alt = new ArrayList<Assertion>();
364: EasyMock.expect(ep.getChosenAlternative()).andReturn(alt)
365: .times(2);
366: PolicyInterceptorProviderRegistry reg = control
367: .createMock(PolicyInterceptorProviderRegistry.class);
368: List<Interceptor> li = new ArrayList<Interceptor>();
369: EasyMock.expect(reg.getInterceptors(alt, true, false))
370: .andReturn(li);
371: Policy p = control.createMock(Policy.class);
372: EasyMock.expect(ep.getPolicy()).andReturn(p);
373: control.replay();
374: EffectivePolicy effective = rme.new EffectivePolicyImpl(ep,
375: reg, true, false);
376: assertSame(alt, effective.getChosenAlternative());
377: assertSame(li, effective.getInterceptors());
378: assertSame(p, effective.getPolicy());
379: }
380:
381: private void verifyService() {
382: Service service = rme.getService();
383: ServiceInfo si = service.getServiceInfos().get(0);
384: assertNotNull("service info is null", si);
385:
386: InterfaceInfo intf = si.getInterface();
387:
388: assertEquals(7, intf.getOperations().size());
389:
390: String ns = si.getName().getNamespaceURI();
391: ns = RMConstants.getNamespace();
392: OperationInfo oi = intf.getOperation(new QName(ns,
393: "CreateSequence"));
394: assertNotNull("No operation info.", oi);
395: assertTrue("Operation is oneway.", !oi.isOneWay());
396: assertTrue("Operation is unwrapped.", !oi.isUnwrapped());
397: assertTrue("Operation is unwrappedCapable.", !oi
398: .isUnwrappedCapable());
399: assertNull("Unexpected unwrapped operation.", oi
400: .getUnwrappedOperation());
401:
402: oi = intf.getOperation(new QName(ns, "TerminateSequence"));
403: assertNotNull("No operation info.", oi);
404: assertTrue("Operation is toway.", oi.isOneWay());
405:
406: oi = intf
407: .getOperation(new QName(ns, "SequenceAcknowledgement"));
408: assertNotNull("No operation info.", oi);
409: assertTrue("Operation is toway.", oi.isOneWay());
410:
411: oi = intf.getOperation(new QName(ns, "LastMessage"));
412: assertNotNull("No operation info.", oi);
413: assertTrue("Operation is toway.", oi.isOneWay());
414:
415: oi = intf.getOperation(new QName(ns, "AckRequested"));
416: assertNotNull("No operation info.", oi);
417: assertTrue("Operation is toway.", oi.isOneWay());
418:
419: oi = intf.getOperation(new QName(ns, "CreateSequenceOneway"));
420: assertNotNull("No operation info.", oi);
421: assertTrue("Operation is toway.", oi.isOneWay());
422:
423: oi = intf.getOperation(new QName(ns,
424: "CreateSequenceResponseOneway"));
425: assertNotNull("No operation info.", oi);
426: assertTrue("Operation is toway.", oi.isOneWay());
427: }
428:
429: }
|