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.Iterator;
022: import java.util.List;
023: import java.util.SortedSet;
024:
025: import javax.xml.namespace.QName;
026:
027: import org.apache.cxf.Bus;
028: import org.apache.cxf.BusFactory;
029: import org.apache.cxf.endpoint.Endpoint;
030: import org.apache.cxf.interceptor.Interceptor;
031: import org.apache.cxf.interceptor.InterceptorChain;
032: import org.apache.cxf.message.Exchange;
033: import org.apache.cxf.message.ExchangeImpl;
034: import org.apache.cxf.message.Message;
035: import org.apache.cxf.phase.Phase;
036: import org.apache.cxf.phase.PhaseManager;
037: import org.apache.cxf.phase.PhaseManagerImpl;
038: import org.apache.cxf.service.Service;
039: import org.apache.cxf.service.model.FaultInfo;
040: import org.apache.cxf.service.model.MessageInfo;
041: import org.apache.cxf.service.model.MessagePartInfo;
042: import org.apache.cxf.service.model.OperationInfo;
043: import org.apache.headers.coloc.types.FaultDetailT;
044: import org.apache.headers.coloc.types.InHeaderT;
045: import org.apache.headers.coloc.types.OutHeaderT;
046: import org.apache.headers.rpc_lit.PingMeFault;
047: import org.easymock.classextension.EasyMock;
048: import org.easymock.classextension.IMocksControl;
049:
050: import org.junit.After;
051: import org.junit.Assert;
052: import org.junit.Before;
053: import org.junit.Test;
054:
055: public class ColocUtilTest extends Assert {
056: private IMocksControl control = EasyMock.createNiceControl();
057: private Bus bus;
058:
059: @Before
060: public void setUp() throws Exception {
061: bus = control.createMock(Bus.class);
062: BusFactory.setDefaultBus(bus);
063: }
064:
065: @After
066: public void tearDown() throws Exception {
067: BusFactory.setDefaultBus(null);
068: }
069:
070: @Test
071: public void testSetColocInPhases() throws Exception {
072: PhaseManagerImpl phaseMgr = new PhaseManagerImpl();
073: SortedSet<Phase> list = phaseMgr.getInPhases();
074: int size1 = list.size();
075: ColocUtil.setPhases(list, Phase.USER_LOGICAL, Phase.INVOKE);
076:
077: assertNotSame("The list size should not be same", size1, list
078: .size());
079: assertEquals("Expecting Phase.USER_LOGICAL", list.first()
080: .getName(), Phase.USER_LOGICAL);
081: assertEquals("Expecting Phase.POST_INVOKE", list.last()
082: .getName(), Phase.INVOKE);
083: }
084:
085: @Test
086: public void testSetColocOutPhases() throws Exception {
087: PhaseManagerImpl phaseMgr = new PhaseManagerImpl();
088:
089: SortedSet<Phase> list = phaseMgr.getOutPhases();
090: int size1 = list.size();
091: ColocUtil.setPhases(list, Phase.SETUP, Phase.POST_LOGICAL);
092:
093: assertNotSame("The list size should not be same", size1, list
094: .size());
095: assertEquals("Expecting Phase.SETUP", list.first().getName(),
096: Phase.SETUP);
097: assertEquals("Expecting Phase.POST_LOGICAL", list.last()
098: .getName(), Phase.POST_LOGICAL);
099:
100: }
101:
102: @Test
103: public void testGetOutInterceptorChain() throws Exception {
104: PhaseManagerImpl phaseMgr = new PhaseManagerImpl();
105: SortedSet<Phase> list = phaseMgr.getInPhases();
106: ColocUtil.setPhases(list, Phase.SETUP, Phase.POST_LOGICAL);
107:
108: Endpoint ep = control.createMock(Endpoint.class);
109: Service srv = control.createMock(Service.class);
110: Exchange ex = new ExchangeImpl();
111:
112: ex.put(Bus.class, bus);
113: ex.put(Endpoint.class, ep);
114: ex.put(Service.class, srv);
115:
116: EasyMock.expect(ep.getOutInterceptors()).andReturn(
117: new ArrayList<Interceptor>()).atLeastOnce();
118: EasyMock.expect(ep.getService()).andReturn(srv).atLeastOnce();
119: EasyMock.expect(srv.getOutInterceptors()).andReturn(
120: new ArrayList<Interceptor>()).atLeastOnce();
121: EasyMock.expect(bus.getOutInterceptors()).andReturn(
122: new ArrayList<Interceptor>()).atLeastOnce();
123:
124: control.replay();
125: InterceptorChain chain = ColocUtil.getOutInterceptorChain(ex,
126: list);
127: control.verify();
128: assertNotNull("Should have chain instance", chain);
129: Iterator<Interceptor<? extends Message>> iter = chain
130: .iterator();
131: assertEquals("Should not have interceptors in chain", false,
132: iter.hasNext());
133: }
134:
135: @Test
136: public void testGetInInterceptorChain() throws Exception {
137: PhaseManagerImpl phaseMgr = new PhaseManagerImpl();
138: SortedSet<Phase> list = phaseMgr.getInPhases();
139: ColocUtil.setPhases(list, Phase.SETUP, Phase.POST_LOGICAL);
140:
141: Endpoint ep = control.createMock(Endpoint.class);
142: Service srv = control.createMock(Service.class);
143: Exchange ex = new ExchangeImpl();
144:
145: ex.put(Bus.class, bus);
146: ex.put(Endpoint.class, ep);
147: ex.put(Service.class, srv);
148:
149: EasyMock.expect(bus.getExtension(PhaseManager.class))
150: .andReturn(phaseMgr);
151: EasyMock.expect(ep.getInInterceptors()).andReturn(
152: new ArrayList<Interceptor>()).atLeastOnce();
153: EasyMock.expect(ep.getService()).andReturn(srv).atLeastOnce();
154: EasyMock.expect(srv.getInInterceptors()).andReturn(
155: new ArrayList<Interceptor>()).atLeastOnce();
156: EasyMock.expect(bus.getInInterceptors()).andReturn(
157: new ArrayList<Interceptor>()).atLeastOnce();
158:
159: control.replay();
160: InterceptorChain chain = ColocUtil.getInInterceptorChain(ex,
161: list);
162: control.verify();
163: assertNotNull("Should have chain instance", chain);
164: Iterator<Interceptor<? extends Message>> iter = chain
165: .iterator();
166: assertEquals("Should not have interceptors in chain", false,
167: iter.hasNext());
168: assertNotNull("OutFaultObserver should be set", chain
169: .getFaultObserver());
170: }
171:
172: @Test
173: public void testIsSameFaultInfo() {
174: OperationInfo oi = control.createMock(OperationInfo.class);
175:
176: boolean match = ColocUtil.isSameFaultInfo(null, null);
177: assertEquals("Should return true", true, match);
178: List<FaultInfo> fil1 = new ArrayList<FaultInfo>();
179: match = ColocUtil.isSameFaultInfo(fil1, null);
180: assertEquals("Should not find a match", false, match);
181: match = ColocUtil.isSameFaultInfo(null, fil1);
182: assertEquals("Should not find a match", false, match);
183:
184: List<FaultInfo> fil2 = new ArrayList<FaultInfo>();
185: match = ColocUtil.isSameFaultInfo(fil1, fil2);
186:
187: QName fn1 = new QName("A", "B");
188: QName fn2 = new QName("C", "D");
189:
190: FaultInfo fi1 = new FaultInfo(fn1, null, oi);
191: fi1.setProperty(Class.class.getName(), PingMeFault.class);
192: fil1.add(fi1);
193: FaultInfo fi2 = new FaultInfo(fn2, null, oi);
194: fi2.setProperty(Class.class.getName(), FaultDetailT.class);
195: match = ColocUtil.isSameFaultInfo(fil1, fil2);
196: assertEquals("Should not find a match", false, match);
197:
198: FaultInfo fi3 = new FaultInfo(fn2, null, oi);
199: fi3.setProperty(Class.class.getName(), PingMeFault.class);
200: fil2.add(fi3);
201: match = ColocUtil.isSameFaultInfo(fil1, fil2);
202: assertEquals("Should find a match", true, match);
203: }
204:
205: @Test
206: public void testIsSameMessageInfo() {
207: OperationInfo oi = control.createMock(OperationInfo.class);
208: boolean match = ColocUtil.isSameMessageInfo(null, null);
209: assertEquals("Should return true", true, match);
210: QName mn1 = new QName("A", "B");
211: QName mn2 = new QName("C", "D");
212:
213: MessageInfo mi1 = new MessageInfo(oi, mn1);
214: MessageInfo mi2 = new MessageInfo(oi, mn2);
215: match = ColocUtil.isSameMessageInfo(mi1, null);
216: assertEquals("Should not find a match", false, match);
217: match = ColocUtil.isSameMessageInfo(null, mi2);
218: assertEquals("Should not find a match", false, match);
219:
220: MessagePartInfo mpi = new MessagePartInfo(new QName("", "B"),
221: null);
222: mpi.setTypeClass(InHeaderT.class);
223: mi1.addMessagePart(mpi);
224:
225: mpi = new MessagePartInfo(new QName("", "D"), null);
226: mpi.setTypeClass(OutHeaderT.class);
227: mi2.addMessagePart(mpi);
228:
229: match = ColocUtil.isSameMessageInfo(mi1, mi2);
230: assertEquals("Should not find a match", false, match);
231:
232: mpi.setTypeClass(InHeaderT.class);
233: match = ColocUtil.isSameMessageInfo(mi1, mi2);
234: assertEquals("Should find a match", true, match);
235: }
236:
237: }
|