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.Endpoint;
029: import org.apache.cxf.interceptor.Interceptor;
030: import org.apache.cxf.message.Exchange;
031: import org.apache.cxf.message.ExchangeImpl;
032: import org.apache.cxf.message.Message;
033: import org.apache.cxf.message.MessageImpl;
034: import org.apache.cxf.phase.PhaseManager;
035: import org.apache.cxf.phase.PhaseManagerImpl;
036: import org.apache.cxf.service.Service;
037: import org.apache.cxf.service.model.BindingInfo;
038: import org.apache.cxf.service.model.BindingOperationInfo;
039: import org.apache.cxf.service.model.EndpointInfo;
040: import org.apache.cxf.service.model.MessageInfo;
041: import org.apache.cxf.service.model.OperationInfo;
042: import org.easymock.classextension.EasyMock;
043: import org.easymock.classextension.IMocksControl;
044:
045: import org.junit.After;
046: import org.junit.Assert;
047: import org.junit.Before;
048: import org.junit.Test;
049:
050: public class ColocMessageObserverTest extends Assert {
051: private IMocksControl control = EasyMock.createNiceControl();
052: private ColocMessageObserver observer;
053: private Message msg;
054: private Exchange ex;
055: private Service srv;
056: private Endpoint ep;
057: private Bus bus;
058: private OperationInfo oi;
059:
060: @Before
061: public void setUp() throws Exception {
062: ep = control.createMock(Endpoint.class);
063: bus = control.createMock(Bus.class);
064: srv = control.createMock(Service.class);
065: oi = control.createMock(OperationInfo.class);
066: BusFactory.setDefaultBus(bus);
067: msg = new MessageImpl();
068: ex = new ExchangeImpl();
069: //msg.setExchange(ex);
070: }
071:
072: @After
073: public void tearDown() throws Exception {
074: BusFactory.setDefaultBus(null);
075: }
076:
077: @Test
078: public void testSetExchangeProperties() throws Exception {
079: observer = new ColocMessageObserver(ep, bus);
080: QName opName = new QName("A", "B");
081: msg.put(Message.WSDL_OPERATION, opName);
082:
083: EasyMock.expect(ep.getService()).andReturn(srv);
084: Binding binding = control.createMock(Binding.class);
085: EasyMock.expect(ep.getBinding()).andReturn(binding);
086: EndpointInfo ei = control.createMock(EndpointInfo.class);
087: EasyMock.expect(ep.getEndpointInfo()).andReturn(ei);
088: BindingInfo bi = control.createMock(BindingInfo.class);
089: EasyMock.expect(ei.getBinding()).andReturn(bi);
090: BindingOperationInfo boi = control
091: .createMock(BindingOperationInfo.class);
092: EasyMock.expect(bi.getOperation(opName)).andReturn(boi);
093: EasyMock.expect(boi.getOperationInfo()).andReturn(oi);
094:
095: control.replay();
096: observer.setExchangeProperties(ex, msg);
097: control.verify();
098:
099: assertNotNull("Bus should be set", ex.get(Bus.class));
100: assertNotNull("Endpoint should be set", ex.get(Endpoint.class));
101: assertNotNull("Binding should be set", ex.get(Binding.class));
102: assertNotNull("Service should be set", ex.get(Service.class));
103: assertNotNull("BindingOperationInfo should be set", ex
104: .get(BindingOperationInfo.class));
105: assertNotNull("OperationInfo should be set", ex
106: .get(OperationInfo.class));
107: }
108:
109: @Test
110: public void testObserverOnMessage() throws Exception {
111: observer = new TestColocMessageObserver(ep, bus);
112: msg.setExchange(ex);
113:
114: Binding binding = control.createMock(Binding.class);
115: EasyMock.expect(ep.getBinding()).andReturn(binding);
116:
117: Message inMsg = new MessageImpl();
118: EasyMock.expect(binding.createMessage()).andReturn(inMsg);
119:
120: MessageInfo mi = control.createMock(MessageInfo.class);
121: EasyMock.expect(oi.getInput()).andReturn(mi);
122:
123: EasyMock.expect(ep.getService()).andReturn(srv).anyTimes();
124: EasyMock.expect(bus.getExtension(PhaseManager.class))
125: .andReturn(new PhaseManagerImpl()).times(2);
126: EasyMock.expect(bus.getInInterceptors()).andReturn(
127: new ArrayList<Interceptor>());
128: EasyMock.expect(ep.getInInterceptors()).andReturn(
129: new ArrayList<Interceptor>());
130: EasyMock.expect(srv.getInInterceptors()).andReturn(
131: new ArrayList<Interceptor>());
132:
133: control.replay();
134: observer.onMessage(msg);
135: control.verify();
136:
137: Exchange inEx = inMsg.getExchange();
138: assertNotNull("Should Have a valid Exchange", inEx);
139: assertEquals("Message.REQUESTOR_ROLE should be false",
140: Boolean.FALSE, inMsg.get(Message.REQUESTOR_ROLE));
141: assertEquals("Message.INBOUND_MESSAGE should be true",
142: Boolean.TRUE, inMsg.get(Message.INBOUND_MESSAGE));
143: assertNotNull(
144: "MessageInfo should be present in the Message instance",
145: inMsg.get(MessageInfo.class));
146: assertNotNull("Chain should be set", inMsg
147: .getInterceptorChain());
148: Exchange ex1 = msg.getExchange();
149: assertNotNull("Exchange should be set", ex1);
150: }
151:
152: class TestColocMessageObserver extends ColocMessageObserver {
153: public TestColocMessageObserver(Endpoint endpoint, Bus bus) {
154: super (endpoint, bus);
155: }
156:
157: public void setExchangeProperties(Exchange exchange, Message m) {
158: exchange.put(Bus.class, bus);
159: exchange.put(Endpoint.class, ep);
160: exchange.put(Service.class, srv);
161: exchange.put(OperationInfo.class, oi);
162: }
163:
164: protected List<Interceptor> addColocInterceptors() {
165: return new ArrayList<Interceptor>();
166: }
167: }
168: }
|