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.binding.coloc;
019:
020: import java.util.ArrayList;
021: import java.util.List;
022:
023: import javax.xml.namespace.QName;
024:
025: import org.apache.cxf.Bus;
026: import org.apache.cxf.BusFactory;
027: import org.apache.cxf.binding.Binding;
028: import org.apache.cxf.endpoint.ClientImpl;
029: import org.apache.cxf.endpoint.Endpoint;
030: import org.apache.cxf.endpoint.Server;
031: import org.apache.cxf.endpoint.ServerRegistry;
032: import org.apache.cxf.interceptor.Fault;
033: import org.apache.cxf.interceptor.Interceptor;
034: import org.apache.cxf.interceptor.InterceptorChain;
035: import org.apache.cxf.message.Exchange;
036: import org.apache.cxf.message.ExchangeImpl;
037: import org.apache.cxf.message.Message;
038: import org.apache.cxf.message.MessageImpl;
039: import org.apache.cxf.phase.Phase;
040: import org.apache.cxf.phase.PhaseManager;
041: import org.apache.cxf.phase.PhaseManagerImpl;
042: import org.apache.cxf.service.Service;
043: import org.apache.cxf.service.model.BindingInfo;
044: import org.apache.cxf.service.model.BindingOperationInfo;
045: import org.apache.cxf.service.model.EndpointInfo;
046: import org.apache.cxf.service.model.InterfaceInfo;
047: import org.apache.cxf.service.model.ServiceInfo;
048: import org.easymock.classextension.EasyMock;
049: import org.easymock.classextension.IMocksControl;
050:
051: import org.junit.After;
052: import org.junit.Assert;
053: import org.junit.Before;
054: import org.junit.Test;
055:
056: public class ColocOutInterceptorTest extends Assert {
057: private static final String COLOCATED = Message.class.getName()
058: + ".COLOCATED";
059: private IMocksControl control = EasyMock.createNiceControl();
060: private ColocOutInterceptor colocOut;
061: private Message msg;
062: private Exchange ex;
063:
064: @Before
065: public void setUp() throws Exception {
066: colocOut = new ColocOutInterceptor();
067: msg = new MessageImpl();
068: ex = new ExchangeImpl();
069: msg.setExchange(ex);
070: }
071:
072: @After
073: public void tearDown() throws Exception {
074: colocOut.setBus(null);
075: BusFactory.setDefaultBus(null);
076: }
077:
078: @Test
079: public void testColocOutPhase() throws Exception {
080: assertEquals(Phase.POST_LOGICAL, colocOut.getPhase());
081: }
082:
083: @Test
084: public void testColocOutInvalidBus() throws Exception {
085: try {
086: colocOut.handleMessage(msg);
087: fail("Should have thrown a fault");
088: } catch (Fault f) {
089: assertEquals("Bus not created or not set as default bus.",
090: f.getMessage());
091: }
092: }
093:
094: @Test
095: public void testColocOutInvalidServiceRegistry() throws Exception {
096:
097: setupBus();
098: try {
099: colocOut.handleMessage(msg);
100: fail("Should have thrown a fault");
101: } catch (Fault f) {
102: assertEquals("Server Registry not registered with bus.", f
103: .getMessage());
104: }
105: }
106:
107: @Test
108: public void testColocOutInvalidEndpoint() throws Exception {
109:
110: Bus bus = setupBus();
111: ServerRegistry sr = control.createMock(ServerRegistry.class);
112: EasyMock.expect(bus.getExtension(ServerRegistry.class))
113: .andReturn(sr);
114:
115: control.replay();
116: try {
117: colocOut.handleMessage(msg);
118: fail("Should have thrown a fault");
119: } catch (Fault f) {
120: assertEquals("Consumer Endpoint not found in exchange.", f
121: .getMessage());
122: }
123: }
124:
125: @Test
126: public void testColocOutInvalidOperation() throws Exception {
127:
128: Bus bus = setupBus();
129: ServerRegistry sr = control.createMock(ServerRegistry.class);
130: EasyMock.expect(bus.getExtension(ServerRegistry.class))
131: .andReturn(sr);
132:
133: Endpoint ep = control.createMock(Endpoint.class);
134: ex.put(Endpoint.class, ep);
135:
136: control.replay();
137: try {
138: colocOut.handleMessage(msg);
139: fail("Should have thrown a fault");
140: } catch (Fault f) {
141: assertEquals("Operation not found in exchange.", f
142: .getMessage());
143: }
144: }
145:
146: @Test
147: public void testColocOutIsColocated() throws Exception {
148: verifyIsColocatedWithNullList();
149: verifyIsColocatedWithEmptyList();
150: verifyIsColocatedWithDifferentService();
151: verifyIsColocatedWithDifferentEndpoint();
152: verifyIsColocatedWithDifferentOperation();
153: verifyIsColocatedWithSameOperation();
154: }
155:
156: @Test
157: public void testColocOutIsColocatedPropertySet() throws Exception {
158: colocOut = new TestColocOutInterceptor1();
159:
160: Bus bus = setupBus();
161: ServerRegistry sr = control.createMock(ServerRegistry.class);
162: EasyMock.expect(bus.getExtension(ServerRegistry.class))
163: .andReturn(sr);
164:
165: //Funtion Param
166: Server s1 = control.createMock(Server.class);
167: List<Server> list = new ArrayList<Server>();
168: list.add(s1);
169: Endpoint sep = control.createMock(Endpoint.class);
170: ex.put(Endpoint.class, sep);
171: BindingInfo sbi = control.createMock(BindingInfo.class);
172: InterfaceInfo sii = control.createMock(InterfaceInfo.class);
173: BindingOperationInfo sboi = control
174: .createMock(BindingOperationInfo.class);
175:
176: ex.put(BindingOperationInfo.class, sboi);
177: //Local var
178: Service ses = control.createMock(Service.class);
179: EndpointInfo sei = control.createMock(EndpointInfo.class);
180:
181: Endpoint rep = control.createMock(Endpoint.class);
182: Service res = control.createMock(Service.class);
183: BindingInfo rbi = control.createMock(BindingInfo.class);
184: EndpointInfo rei = control.createMock(EndpointInfo.class);
185:
186: EasyMock.expect(sr.getServers()).andReturn(list);
187: EasyMock.expect(sep.getService()).andReturn(ses);
188: EasyMock.expect(sep.getEndpointInfo()).andReturn(sei);
189: EasyMock.expect(s1.getEndpoint()).andReturn(rep);
190: EasyMock.expect(rep.getService()).andReturn(res);
191: EasyMock.expect(rep.getEndpointInfo()).andReturn(rei);
192: EasyMock.expect(ses.getName()).andReturn(new QName("A", "B"));
193: EasyMock.expect(res.getName()).andReturn(new QName("A", "B"));
194: EasyMock.expect(rei.getName()).andReturn(new QName("C", "D"));
195: EasyMock.expect(sei.getName()).andReturn(new QName("C", "D"));
196: EasyMock.expect(rei.getBinding()).andReturn(rbi);
197:
198: QName intf = new QName("G", "H");
199: QName op = new QName("E", "F");
200: EasyMock.expect(sboi.getName()).andReturn(op).anyTimes();
201: EasyMock.expect(rbi.getOperation(op)).andReturn(sboi);
202:
203: InterceptorChain chain = control
204: .createMock(InterceptorChain.class);
205: msg.setInterceptorChain(chain);
206: EasyMock.expect(sboi.getBinding()).andReturn(sbi);
207: EasyMock.expect(sbi.getInterface()).andReturn(sii);
208: EasyMock.expect(sii.getName()).andReturn(intf);
209:
210: control.replay();
211: colocOut.handleMessage(msg);
212: assertEquals("COLOCATED property should be set", Boolean.TRUE,
213: msg.get(COLOCATED));
214: assertEquals("Message.WSDL_OPERATION property should be set",
215: op, msg.get(Message.WSDL_OPERATION));
216: assertEquals("Message.WSDL_INTERFACE property should be set",
217: intf, msg.get(Message.WSDL_INTERFACE));
218:
219: control.verify();
220: }
221:
222: @Test
223: public void testInvokeInboundChain() {
224: //Reset Exchange on msg
225: msg.setExchange(null);
226: Bus bus = setupBus();
227: colocOut.setBus(bus);
228: PhaseManager pm = new PhaseManagerImpl();
229: EasyMock.expect(bus.getExtension(PhaseManager.class))
230: .andReturn(pm).times(2);
231:
232: Endpoint ep = control.createMock(Endpoint.class);
233: Binding bd = control.createMock(Binding.class);
234: Service srv = control.createMock(Service.class);
235: ex.setInMessage(msg);
236: ex.put(Bus.class, bus);
237: ex.put(Endpoint.class, ep);
238: ex.put(Service.class, srv);
239:
240: EasyMock.expect(ep.getBinding()).andReturn(bd);
241: EasyMock.expect(bd.createMessage())
242: .andReturn(new MessageImpl());
243: EasyMock.expect(ep.getInInterceptors()).andReturn(
244: new ArrayList<Interceptor>()).atLeastOnce();
245: EasyMock.expect(ep.getService()).andReturn(srv).atLeastOnce();
246: EasyMock.expect(srv.getInInterceptors()).andReturn(
247: new ArrayList<Interceptor>()).atLeastOnce();
248: EasyMock.expect(bus.getInInterceptors()).andReturn(
249: new ArrayList<Interceptor>()).atLeastOnce();
250:
251: control.replay();
252: colocOut.invokeInboundChain(ex, ep);
253: Message inMsg = ex.getInMessage();
254: assertNotSame(msg, inMsg);
255: assertEquals("Requestor role should be set to true.",
256: Boolean.TRUE, inMsg.get(Message.REQUESTOR_ROLE));
257: assertEquals("Inbound Message should be set to true.",
258: Boolean.TRUE, inMsg.get(Message.INBOUND_MESSAGE));
259: assertNotNull(
260: "Inbound Message should have interceptor chain set.",
261: inMsg.getInterceptorChain());
262: assertEquals("Client Invoke state should be FINISHED",
263: Boolean.TRUE, ex.get(ClientImpl.FINISHED));
264: control.verify();
265: }
266:
267: private void verifyIsColocatedWithNullList() {
268: Server val = colocOut.isColocated(null, null, null);
269: assertEquals("Is not a colocated call", null, val);
270: control.reset();
271: }
272:
273: private void verifyIsColocatedWithEmptyList() {
274: List<Server> list = new ArrayList<Server>();
275: //Local var
276: Endpoint sep = control.createMock(Endpoint.class);
277: Service ses = control.createMock(Service.class);
278: EndpointInfo sei = control.createMock(EndpointInfo.class);
279:
280: EasyMock.expect(sep.getService()).andReturn(ses);
281: EasyMock.expect(sep.getEndpointInfo()).andReturn(sei);
282:
283: control.replay();
284: Server val = colocOut.isColocated(list, sep, null);
285: assertEquals("Is not a colocated call", null, val);
286: control.reset();
287: }
288:
289: private void verifyIsColocatedWithDifferentService() {
290: //Funtion Param
291: Server s1 = control.createMock(Server.class);
292: List<Server> list = new ArrayList<Server>();
293: list.add(s1);
294: Endpoint sep = control.createMock(Endpoint.class);
295: //Local var
296: Service ses = control.createMock(Service.class);
297:
298: Endpoint rep = control.createMock(Endpoint.class);
299: Service res = control.createMock(Service.class);
300:
301: EasyMock.expect(sep.getService()).andReturn(ses);
302: EasyMock.expect(s1.getEndpoint()).andReturn(rep);
303: EasyMock.expect(rep.getService()).andReturn(res);
304: EasyMock.expect(ses.getName()).andReturn(new QName("A", "C"));
305: EasyMock.expect(res.getName()).andReturn(new QName("A", "B"));
306:
307: control.replay();
308: Server val = colocOut.isColocated(list, sep, null);
309: assertEquals("Is not a colocated call", null, val);
310: control.reset();
311: }
312:
313: private void verifyIsColocatedWithDifferentEndpoint() {
314: //Funtion Param
315: Server s1 = control.createMock(Server.class);
316: List<Server> list = new ArrayList<Server>();
317: list.add(s1);
318: Endpoint sep = control.createMock(Endpoint.class);
319: BindingOperationInfo sboi = control
320: .createMock(BindingOperationInfo.class);
321: //Local var
322: Service ses = control.createMock(Service.class);
323: EndpointInfo sei = control.createMock(EndpointInfo.class);
324:
325: Endpoint rep = control.createMock(Endpoint.class);
326: Service res = control.createMock(Service.class);
327: EndpointInfo rei = control.createMock(EndpointInfo.class);
328:
329: EasyMock.expect(sep.getService()).andReturn(ses);
330: EasyMock.expect(sep.getEndpointInfo()).andReturn(sei);
331: EasyMock.expect(s1.getEndpoint()).andReturn(rep);
332: EasyMock.expect(rep.getService()).andReturn(res);
333: EasyMock.expect(rep.getEndpointInfo()).andReturn(rei);
334: EasyMock.expect(ses.getName()).andReturn(new QName("A", "B"));
335: EasyMock.expect(res.getName()).andReturn(new QName("A", "B"));
336: EasyMock.expect(rei.getName()).andReturn(new QName("C", "D"));
337: EasyMock.expect(sei.getName()).andReturn(new QName("C", "E"));
338:
339: control.replay();
340: Server val = colocOut.isColocated(list, sep, sboi);
341: assertEquals("Is not a colocated call", null, val);
342: control.reset();
343: }
344:
345: private void verifyIsColocatedWithDifferentOperation() {
346: //Funtion Param
347: Server s1 = control.createMock(Server.class);
348: List<Server> list = new ArrayList<Server>();
349: list.add(s1);
350: Endpoint sep = control.createMock(Endpoint.class);
351: BindingOperationInfo sboi = control
352: .createMock(BindingOperationInfo.class);
353: //Local var
354: Service ses = control.createMock(Service.class);
355: ServiceInfo ssi = control.createMock(ServiceInfo.class);
356: EndpointInfo sei = control.createMock(EndpointInfo.class);
357: TestBindingInfo rbi = new TestBindingInfo(ssi, "testBinding");
358: Endpoint rep = control.createMock(Endpoint.class);
359: Service res = control.createMock(Service.class);
360: EndpointInfo rei = control.createMock(EndpointInfo.class);
361:
362: EasyMock.expect(sep.getService()).andReturn(ses);
363: EasyMock.expect(sep.getEndpointInfo()).andReturn(sei);
364: EasyMock.expect(s1.getEndpoint()).andReturn(rep);
365: EasyMock.expect(rep.getService()).andReturn(res);
366: EasyMock.expect(rep.getEndpointInfo()).andReturn(rei);
367: EasyMock.expect(ses.getName()).andReturn(new QName("A", "B"));
368: EasyMock.expect(res.getName()).andReturn(new QName("A", "B"));
369: EasyMock.expect(rei.getName()).andReturn(new QName("C", "D"));
370: EasyMock.expect(sei.getName()).andReturn(new QName("C", "D"));
371: EasyMock.expect(rei.getBinding()).andReturn(rbi);
372: EasyMock.expect(sboi.getName()).andReturn(new QName("E", "F"));
373: //Causes ConcurrentModification intermittently
374: //QName op = new QName("E", "F");
375: //EasyMock.expect(rbi.getOperation(op).andReturn(null);
376:
377: control.replay();
378: Server val = colocOut.isColocated(list, sep, sboi);
379: assertEquals("Is not a colocated call", null, val);
380: assertEquals("BindingOperation.getOperation was not called", 1,
381: rbi.getOpCount());
382: control.reset();
383: }
384:
385: private void verifyIsColocatedWithSameOperation() {
386: colocOut = new TestColocOutInterceptor1();
387: //Funtion Param
388: Server s1 = control.createMock(Server.class);
389: List<Server> list = new ArrayList<Server>();
390: list.add(s1);
391: Endpoint sep = control.createMock(Endpoint.class);
392: BindingOperationInfo sboi = control
393: .createMock(BindingOperationInfo.class);
394: //Local var
395: Service ses = control.createMock(Service.class);
396: EndpointInfo sei = control.createMock(EndpointInfo.class);
397: BindingInfo rbi = control.createMock(BindingInfo.class);
398: Endpoint rep = control.createMock(Endpoint.class);
399: Service res = control.createMock(Service.class);
400: EndpointInfo rei = control.createMock(EndpointInfo.class);
401:
402: EasyMock.expect(sep.getService()).andReturn(ses);
403: EasyMock.expect(sep.getEndpointInfo()).andReturn(sei);
404: EasyMock.expect(s1.getEndpoint()).andReturn(rep);
405: EasyMock.expect(rep.getService()).andReturn(res);
406: EasyMock.expect(rep.getEndpointInfo()).andReturn(rei);
407: EasyMock.expect(ses.getName()).andReturn(new QName("A", "B"));
408: EasyMock.expect(res.getName()).andReturn(new QName("A", "B"));
409: EasyMock.expect(rei.getName()).andReturn(new QName("C", "D"));
410: EasyMock.expect(sei.getName()).andReturn(new QName("C", "D"));
411: EasyMock.expect(rei.getBinding()).andReturn(rbi);
412: QName op = new QName("E", "F");
413: EasyMock.expect(sboi.getName()).andReturn(op);
414: EasyMock.expect(rbi.getOperation(op)).andReturn(sboi);
415:
416: control.replay();
417: Server val = colocOut.isColocated(list, sep, sboi);
418: assertEquals("Expecting a colocated call", s1, val);
419: control.reset();
420: }
421:
422: private Bus setupBus() {
423: Bus bus = control.createMock(Bus.class);
424: BusFactory.setDefaultBus(bus);
425: return bus;
426: }
427:
428: class TestColocOutInterceptor1 extends ColocOutInterceptor {
429: public void invokeColocObserver(Message outMsg,
430: Endpoint inboundEndpoint) {
431: //No Op
432: }
433:
434: public void invokeInboundChain(Exchange exchange, Endpoint ep) {
435: //No Op
436: }
437:
438: protected boolean isSameOperationInfo(BindingOperationInfo s,
439: BindingOperationInfo r) {
440: return true;
441: }
442: }
443:
444: class TestBindingInfo extends BindingInfo {
445: private int opCount;
446:
447: TestBindingInfo(ServiceInfo si, String bindingId) {
448: super (si, bindingId);
449: }
450:
451: public int getOpCount() {
452: return opCount;
453: }
454:
455: public BindingOperationInfo getOperation(QName opName) {
456: BindingOperationInfo boi = super.getOperation(opName);
457: ++opCount;
458: return boi;
459: }
460:
461: }
462: }
|