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.HashMap;
022: import java.util.Map;
023:
024: import javax.xml.datatype.Duration;
025:
026: import org.apache.cxf.Bus;
027: import org.apache.cxf.endpoint.Client;
028: import org.apache.cxf.endpoint.ConduitSelector;
029: import org.apache.cxf.endpoint.Endpoint;
030: import org.apache.cxf.jaxb.DatatypeFactory;
031: import org.apache.cxf.message.Message;
032: import org.apache.cxf.service.model.BindingInfo;
033: import org.apache.cxf.service.model.BindingOperationInfo;
034: import org.apache.cxf.service.model.EndpointInfo;
035: import org.apache.cxf.service.model.InterfaceInfo;
036: import org.apache.cxf.service.model.OperationInfo;
037: import org.apache.cxf.service.model.ServiceInfo;
038: import org.apache.cxf.transport.Conduit;
039: import org.apache.cxf.workqueue.SynchronousExecutor;
040: import org.apache.cxf.ws.addressing.RelatesToType;
041: import org.apache.cxf.ws.addressing.v200408.AttributedURI;
042: import org.apache.cxf.ws.addressing.v200408.EndpointReferenceType;
043: import org.apache.cxf.ws.rm.manager.SourcePolicyType;
044: import org.easymock.classextension.EasyMock;
045: import org.easymock.classextension.IMocksControl;
046: import org.junit.After;
047: import org.junit.Assert;
048: import org.junit.Before;
049: import org.junit.Test;
050:
051: /**
052: *
053: */
054: public class ProxyTest extends Assert {
055:
056: private IMocksControl control;
057: private RMEndpoint rme;
058:
059: @Before
060: public void setUp() {
061: control = EasyMock.createNiceControl();
062: rme = control.createMock(RMEndpoint.class);
063: }
064:
065: @After
066: public void tearDown() {
067: control.verify();
068: }
069:
070: @Test
071: public void testCtor() {
072: Proxy proxy = new Proxy(rme);
073: assertSame(rme, proxy.getReliableEndpoint());
074: control.replay();
075: }
076:
077: @Test
078: public void testOfferedIdentifier() {
079: OfferType offer = control.createMock(OfferType.class);
080: Identifier id = control.createMock(Identifier.class);
081: EasyMock.expect(offer.getIdentifier()).andReturn(id);
082: control.replay();
083: Proxy proxy = new Proxy(rme);
084: assertNull(proxy.getOfferedIdentifier());
085: proxy.setOfferedIdentifier(offer);
086: assertSame(id, proxy.getOfferedIdentifier());
087: }
088:
089: @Test
090: public void testAcknowledgeNotSupported() throws RMException {
091: DestinationSequence ds = control
092: .createMock(DestinationSequence.class);
093: EndpointReferenceType acksToEPR = control
094: .createMock(EndpointReferenceType.class);
095: EasyMock.expect(ds.getAcksTo()).andReturn(acksToEPR);
096: AttributedURI acksToURI = control
097: .createMock(AttributedURI.class);
098: EasyMock.expect(acksToEPR.getAddress()).andReturn(acksToURI);
099: String acksToAddress = RMConstants.getAnonymousAddress();
100: EasyMock.expect(acksToURI.getValue()).andReturn(acksToAddress);
101: control.replay();
102: Proxy proxy = new Proxy(rme);
103: proxy.acknowledge(ds);
104: }
105:
106: @Test
107: public void testAcknowledge() throws NoSuchMethodException,
108: RMException {
109: Method m = Proxy.class.getDeclaredMethod("invoke", new Class[] {
110: OperationInfo.class, Object[].class, Map.class });
111: Proxy proxy = control.createMock(Proxy.class,
112: new Method[] { m });
113: proxy.setReliableEndpoint(rme);
114: DestinationSequence ds = control
115: .createMock(DestinationSequence.class);
116: EndpointReferenceType acksToEPR = control
117: .createMock(EndpointReferenceType.class);
118: EasyMock.expect(ds.getAcksTo()).andReturn(acksToEPR);
119: AttributedURI acksToURI = control
120: .createMock(AttributedURI.class);
121: EasyMock.expect(acksToEPR.getAddress()).andReturn(acksToURI);
122: String acksToAddress = "acksTo";
123: EasyMock.expect(acksToURI.getValue()).andReturn(acksToAddress);
124: Endpoint endpoint = control.createMock(Endpoint.class);
125: EasyMock.expect(rme.getEndpoint()).andReturn(endpoint);
126: EndpointInfo epi = control.createMock(EndpointInfo.class);
127: EasyMock.expect(endpoint.getEndpointInfo()).andReturn(epi);
128: ServiceInfo si = control.createMock(ServiceInfo.class);
129: EasyMock.expect(epi.getService()).andReturn(si);
130: InterfaceInfo ii = control.createMock(InterfaceInfo.class);
131: EasyMock.expect(si.getInterface()).andReturn(ii);
132: OperationInfo oi = control.createMock(OperationInfo.class);
133: EasyMock.expect(
134: ii.getOperation(RMConstants
135: .getSequenceAckOperationName())).andReturn(oi);
136: expectInvoke(proxy, oi, null);
137: control.replay();
138:
139: proxy.acknowledge(ds);
140: }
141:
142: @Test
143: public void testLastMessage() throws NoSuchMethodException,
144: RMException {
145: Method m = Proxy.class.getDeclaredMethod("invoke", new Class[] {
146: OperationInfo.class, Object[].class, Map.class });
147: Proxy proxy = control.createMock(Proxy.class,
148: new Method[] { m });
149: proxy.setReliableEndpoint(rme);
150: SourceSequence ss = control.createMock(SourceSequence.class);
151: EasyMock.expect(ss.getTarget()).andReturn(null);
152: control.replay();
153: proxy.lastMessage(ss);
154: control.verify();
155:
156: control.reset();
157: org.apache.cxf.ws.addressing.EndpointReferenceType target = RMUtils
158: .createAnonymousReference();
159: EasyMock.expect(ss.getTarget()).andReturn(target);
160: control.replay();
161: proxy.lastMessage(ss);
162: control.verify();
163:
164: control.reset();
165: target = RMUtils
166: .createReference("http://localhost:9000/greeterPort");
167: EasyMock.expect(ss.getTarget()).andReturn(target);
168: Endpoint endpoint = control.createMock(Endpoint.class);
169: EasyMock.expect(rme.getEndpoint()).andReturn(endpoint);
170: EndpointInfo epi = control.createMock(EndpointInfo.class);
171: EasyMock.expect(endpoint.getEndpointInfo()).andReturn(epi);
172: ServiceInfo si = control.createMock(ServiceInfo.class);
173: EasyMock.expect(epi.getService()).andReturn(si);
174: InterfaceInfo ii = control.createMock(InterfaceInfo.class);
175: EasyMock.expect(si.getInterface()).andReturn(ii);
176: OperationInfo oi = control.createMock(OperationInfo.class);
177: EasyMock.expect(
178: ii.getOperation(RMConstants
179: .getLastMessageOperationName())).andReturn(oi);
180: expectInvokeWithContext(proxy, oi, null);
181: control.replay();
182:
183: proxy.lastMessage(ss);
184:
185: }
186:
187: @Test
188: public void testTerminate() throws NoSuchMethodException,
189: RMException {
190: Method m = Proxy.class.getDeclaredMethod("invoke", new Class[] {
191: OperationInfo.class, Object[].class, Map.class });
192: Proxy proxy = control.createMock(Proxy.class,
193: new Method[] { m });
194: proxy.setReliableEndpoint(rme);
195: Endpoint endpoint = control.createMock(Endpoint.class);
196: EasyMock.expect(rme.getEndpoint()).andReturn(endpoint);
197: EndpointInfo epi = control.createMock(EndpointInfo.class);
198: EasyMock.expect(endpoint.getEndpointInfo()).andReturn(epi);
199: ServiceInfo si = control.createMock(ServiceInfo.class);
200: EasyMock.expect(epi.getService()).andReturn(si);
201: InterfaceInfo ii = control.createMock(InterfaceInfo.class);
202: EasyMock.expect(si.getInterface()).andReturn(ii);
203: OperationInfo oi = control.createMock(OperationInfo.class);
204: EasyMock.expect(
205: ii.getOperation(RMConstants
206: .getTerminateSequenceOperationName()))
207: .andReturn(oi);
208: SourceSequence ss = control.createMock(SourceSequence.class);
209: Identifier id = control.createMock(Identifier.class);
210: EasyMock.expect(ss.getIdentifier()).andReturn(id);
211: expectInvoke(proxy, oi, null);
212: control.replay();
213: proxy.terminate(ss);
214: }
215:
216: @Test
217: public void testCreateSequenceResponse()
218: throws NoSuchMethodException, RMException {
219: Method m = Proxy.class.getDeclaredMethod("invoke", new Class[] {
220: OperationInfo.class, Object[].class, Map.class });
221: Proxy proxy = control.createMock(Proxy.class,
222: new Method[] { m });
223: proxy.setReliableEndpoint(rme);
224: Endpoint endpoint = control.createMock(Endpoint.class);
225: EasyMock.expect(rme.getEndpoint()).andReturn(endpoint);
226: EndpointInfo epi = control.createMock(EndpointInfo.class);
227: EasyMock.expect(endpoint.getEndpointInfo()).andReturn(epi);
228: ServiceInfo si = control.createMock(ServiceInfo.class);
229: EasyMock.expect(epi.getService()).andReturn(si);
230: InterfaceInfo ii = control.createMock(InterfaceInfo.class);
231: EasyMock.expect(si.getInterface()).andReturn(ii);
232: OperationInfo oi = control.createMock(OperationInfo.class);
233: EasyMock
234: .expect(
235: ii
236: .getOperation(RMConstants
237: .getCreateSequenceResponseOnewayOperationName()))
238: .andReturn(oi);
239: CreateSequenceResponseType csr = control
240: .createMock(CreateSequenceResponseType.class);
241: expectInvoke(proxy, oi, null);
242: control.replay();
243: proxy.createSequenceResponse(csr);
244: }
245:
246: @Test
247: public void testCreateSequenceOnClient()
248: throws NoSuchMethodException, RMException {
249: testCreateSequence(false);
250: }
251:
252: @Test
253: public void testCreateSequenceOnServer()
254: throws NoSuchMethodException, RMException {
255: testCreateSequence(true);
256: }
257:
258: @Test
259: public void testInvoke() throws Exception {
260: Method m = Proxy.class
261: .getDeclaredMethod(
262: "createClient",
263: new Class[] {
264: Bus.class,
265: Endpoint.class,
266: Conduit.class,
267: org.apache.cxf.ws.addressing.EndpointReferenceType.class });
268: Proxy proxy = control.createMock(Proxy.class,
269: new Method[] { m });
270: proxy.setReliableEndpoint(rme);
271:
272: RMManager manager = control.createMock(RMManager.class);
273: EasyMock.expect(rme.getManager()).andReturn(manager);
274: Bus bus = control.createMock(Bus.class);
275: EasyMock.expect(manager.getBus()).andReturn(bus);
276: Endpoint endpoint = control.createMock(Endpoint.class);
277: EasyMock.expect(rme.getEndpoint()).andReturn(endpoint);
278: BindingInfo bi = control.createMock(BindingInfo.class);
279: EasyMock.expect(rme.getBindingInfo()).andReturn(bi);
280:
281: Conduit conduit = control.createMock(Conduit.class);
282: EasyMock.expect(rme.getConduit()).andReturn(conduit);
283: org.apache.cxf.ws.addressing.EndpointReferenceType replyTo = control
284: .createMock(org.apache.cxf.ws.addressing.EndpointReferenceType.class);
285: EasyMock.expect(rme.getReplyTo()).andReturn(replyTo);
286:
287: OperationInfo oi = control.createMock(OperationInfo.class);
288: BindingOperationInfo boi = control
289: .createMock(BindingOperationInfo.class);
290: EasyMock.expect(bi.getOperation(oi)).andReturn(boi);
291: Client client = control.createMock(Client.class);
292: EasyMock.expect(
293: proxy.createClient(bus, endpoint, conduit, replyTo))
294: .andReturn(client);
295: Object[] args = new Object[] {};
296: Map<String, Object> context = new HashMap<String, Object>();
297: Object[] results = new Object[] { "a", "b", "c" };
298: EasyMock.expect(client.invoke(boi, args, context)).andReturn(
299: results);
300:
301: control.replay();
302: assertEquals("a", proxy.invoke(oi, args, context));
303: }
304:
305: @Test
306: public void testRMClientConstruction() {
307: Proxy proxy = new Proxy(rme);
308: Bus bus = control.createMock(Bus.class);
309: Endpoint endpoint = control.createMock(Endpoint.class);
310: Conduit conduit = control.createMock(Conduit.class);
311: org.apache.cxf.ws.addressing.EndpointReferenceType address = control
312: .createMock(org.apache.cxf.ws.addressing.EndpointReferenceType.class);
313: control.replay();
314: assertNotNull(proxy.createClient(bus, endpoint, conduit,
315: address));
316: }
317:
318: @Test
319: public void testRMClientGetConduit() {
320: Proxy proxy = new Proxy(rme);
321: Bus bus = control.createMock(Bus.class);
322: Endpoint endpoint = control.createMock(Endpoint.class);
323: Conduit conduit = control.createMock(Conduit.class);
324: ConduitSelector cs = control.createMock(ConduitSelector.class);
325: EasyMock.expect(cs.selectConduit(EasyMock.isA(Message.class)))
326: .andReturn(conduit);
327: control.replay();
328: Proxy.RMClient client = proxy.new RMClient(bus, endpoint, cs);
329: assertSame(conduit, client.getConduit());
330: }
331:
332: private void testCreateSequence(boolean isServer)
333: throws NoSuchMethodException, RMException {
334: Method m = Proxy.class.getDeclaredMethod("invoke", new Class[] {
335: OperationInfo.class, Object[].class, Map.class });
336: Proxy proxy = control.createMock(Proxy.class,
337: new Method[] { m });
338: proxy.setReliableEndpoint(rme);
339:
340: RMManager manager = control.createMock(RMManager.class);
341: EasyMock.expect(rme.getManager()).andReturn(manager);
342: SourcePolicyType sp = control
343: .createMock(SourcePolicyType.class);
344: EasyMock.expect(manager.getSourcePolicy()).andReturn(sp);
345: EasyMock.expect(sp.getAcksTo()).andReturn(null);
346: Duration d = DatatypeFactory.createDuration("PT12H");
347: EasyMock.expect(sp.getSequenceExpiration()).andReturn(d);
348: EasyMock.expect(sp.isIncludeOffer()).andReturn(true);
349: Duration dOffered = DatatypeFactory.createDuration("PT24H");
350: EasyMock.expect(sp.getOfferedSequenceExpiration()).andReturn(
351: dOffered);
352: Source source = control.createMock(Source.class);
353: EasyMock.expect(rme.getSource()).andReturn(source);
354: Identifier offeredId = control.createMock(Identifier.class);
355: EasyMock.expect(source.generateSequenceIdentifier()).andReturn(
356: offeredId);
357:
358: Endpoint endpoint = control.createMock(Endpoint.class);
359: EasyMock.expect(rme.getEndpoint()).andReturn(endpoint);
360: EndpointInfo epi = control.createMock(EndpointInfo.class);
361: EasyMock.expect(endpoint.getEndpointInfo()).andReturn(epi);
362: ServiceInfo si = control.createMock(ServiceInfo.class);
363: EasyMock.expect(epi.getService()).andReturn(si);
364: InterfaceInfo ii = control.createMock(InterfaceInfo.class);
365: EasyMock.expect(si.getInterface()).andReturn(ii);
366: OperationInfo oi = control.createMock(OperationInfo.class);
367: CreateSequenceResponseType csr = control
368: .createMock(CreateSequenceResponseType.class);
369: if (isServer) {
370: EasyMock.expect(
371: ii.getOperation(RMConstants
372: .getCreateSequenceOnewayOperationName()))
373: .andReturn(oi);
374: Endpoint ae = control.createMock(Endpoint.class);
375: EasyMock.expect(rme.getApplicationEndpoint()).andReturn(ae);
376: EasyMock.expect(ae.getExecutor()).andReturn(
377: SynchronousExecutor.getInstance());
378: expectInvoke(proxy, oi, null);
379: } else {
380: EasyMock.expect(
381: ii.getOperation(RMConstants
382: .getCreateSequenceOperationName()))
383: .andReturn(oi);
384: expectInvoke(proxy, oi, csr);
385: }
386:
387: EndpointReferenceType defaultAcksTo = control
388: .createMock(EndpointReferenceType.class);
389: RelatesToType relatesTo = control
390: .createMock(RelatesToType.class);
391: control.replay();
392: if (isServer) {
393: assertNull(proxy.createSequence(defaultAcksTo, relatesTo,
394: isServer));
395: } else {
396: assertSame(csr, proxy.createSequence(defaultAcksTo,
397: relatesTo, isServer));
398: }
399: }
400:
401: @SuppressWarnings("unchecked")
402: private void expectInvoke(Proxy proxy, OperationInfo oi,
403: Object expectedReturn) throws RMException {
404: EasyMock.expect(
405: proxy.invoke(EasyMock.same(oi), EasyMock
406: .isA(Object[].class), (Map) EasyMock.isNull()))
407: .andReturn(expectedReturn);
408: }
409:
410: @SuppressWarnings("unchecked")
411: private void expectInvokeWithContext(Proxy proxy, OperationInfo oi,
412: Object expectedReturn) throws RMException {
413: EasyMock.expect(
414: proxy.invoke(EasyMock.same(oi), EasyMock
415: .isA(Object[].class), EasyMock.isA(Map.class)))
416: .andReturn(expectedReturn);
417: }
418: }
|