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.util.ArrayList;
021: import java.util.Collection;
022:
023: import org.apache.cxf.Bus;
024: import org.apache.cxf.binding.Binding;
025: import org.apache.cxf.endpoint.Endpoint;
026: import org.apache.cxf.interceptor.Fault;
027: import org.apache.cxf.message.Exchange;
028: import org.apache.cxf.message.Message;
029: import org.apache.cxf.phase.Phase;
030: import org.apache.cxf.ws.policy.AssertionInfo;
031: import org.apache.cxf.ws.policy.AssertionInfoMap;
032: import org.apache.neethi.Assertion;
033: import org.easymock.classextension.EasyMock;
034: import org.easymock.classextension.IMocksControl;
035: import org.junit.After;
036: import org.junit.Assert;
037: import org.junit.Before;
038: import org.junit.Test;
039:
040: /**
041: *
042: */
043: public class AbstractRMInterceptorTest extends Assert {
044:
045: private IMocksControl control;
046:
047: @Before
048: public void setUp() {
049: control = EasyMock.createNiceControl();
050: }
051:
052: @After
053: public void tearDown() {
054: control.verify();
055: }
056:
057: @Test
058: public void testAccessors() {
059: RMInterceptor interceptor = new RMInterceptor();
060: assertEquals(Phase.PRE_LOGICAL, interceptor.getPhase());
061: Bus bus = control.createMock(Bus.class);
062: RMManager busMgr = control.createMock(RMManager.class);
063: EasyMock.expect(bus.getExtension(RMManager.class)).andReturn(
064: busMgr);
065: RMManager mgr = control.createMock(RMManager.class);
066:
067: control.replay();
068: assertNull(interceptor.getBus());
069: interceptor.setBus(bus);
070: assertSame(bus, interceptor.getBus());
071: assertSame(busMgr, interceptor.getManager());
072: interceptor.setManager(mgr);
073: assertSame(mgr, interceptor.getManager());
074: }
075:
076: @Test
077: public void testHandleMessageSequenceFaultNoBinding() {
078: RMInterceptor interceptor = new RMInterceptor();
079: Message message = control.createMock(Message.class);
080: SequenceFault sf = control.createMock(SequenceFault.class);
081: interceptor.setSequenceFault(sf);
082: Exchange ex = control.createMock(Exchange.class);
083: EasyMock.expect(message.getExchange()).andReturn(ex);
084: Endpoint e = control.createMock(Endpoint.class);
085: EasyMock.expect(ex.get(Endpoint.class)).andReturn(e);
086: EasyMock.expect(e.getBinding()).andReturn(null);
087: control.replay();
088: try {
089: interceptor.handleMessage(message);
090: fail("Expected Fault not thrown.");
091: } catch (Fault f) {
092: assertSame(sf, f.getCause());
093: }
094: }
095:
096: @Test
097: public void testHandleMessageSequenceFault() {
098: RMInterceptor interceptor = new RMInterceptor();
099: Message message = control.createMock(Message.class);
100: SequenceFault sf = control.createMock(SequenceFault.class);
101: interceptor.setSequenceFault(sf);
102: Exchange ex = control.createMock(Exchange.class);
103: EasyMock.expect(message.getExchange()).andReturn(ex);
104: Endpoint e = control.createMock(Endpoint.class);
105: EasyMock.expect(ex.get(Endpoint.class)).andReturn(e);
106: Binding b = control.createMock(Binding.class);
107: EasyMock.expect(e.getBinding()).andReturn(b);
108: RMManager mgr = control.createMock(RMManager.class);
109: interceptor.setManager(mgr);
110: BindingFaultFactory bff = control
111: .createMock(BindingFaultFactory.class);
112: EasyMock.expect(mgr.getBindingFaultFactory(b)).andReturn(bff);
113: Fault fault = control.createMock(Fault.class);
114: EasyMock.expect(bff.createFault(sf)).andReturn(fault);
115: EasyMock.expect(bff.toString(fault)).andReturn("f");
116: control.replay();
117: try {
118: interceptor.handleMessage(message);
119: fail("Expected Fault not thrown.");
120: } catch (Fault f) {
121: assertSame(f, fault);
122: }
123: }
124:
125: @Test
126: public void testHandleMessageRMException() {
127: RMInterceptor interceptor = new RMInterceptor();
128: Message message = control.createMock(Message.class);
129: RMException rme = control.createMock(RMException.class);
130: interceptor.setRMException(rme);
131: control.replay();
132: try {
133: interceptor.handleMessage(message);
134: fail("Expected Fault not thrown.");
135: } catch (Fault f) {
136: assertSame(rme, f.getCause());
137: }
138: }
139:
140: @Test
141: public void testAssertReliability() {
142: RMInterceptor interceptor = new RMInterceptor();
143: Message message = control.createMock(Message.class);
144: EasyMock.expect(message.get(AssertionInfoMap.class)).andReturn(
145: null);
146: AssertionInfoMap aim = control
147: .createMock(AssertionInfoMap.class);
148: Collection<AssertionInfo> ais = new ArrayList<AssertionInfo>();
149: EasyMock.expect(message.get(AssertionInfoMap.class)).andReturn(
150: aim).times(2);
151: Assertion a = control.createMock(Assertion.class);
152: AssertionInfo ai = new AssertionInfo(a);
153: EasyMock.expectLastCall();
154: control.replay();
155: interceptor.assertReliability(message);
156: assertTrue(!ai.isAsserted());
157: aim.put(RMConstants.getRMAssertionQName(), ais);
158: interceptor.assertReliability(message);
159: assertTrue(!ai.isAsserted());
160: ais.add(ai);
161: interceptor.assertReliability(message);
162:
163: }
164:
165: class RMInterceptor extends AbstractRMInterceptor {
166:
167: private SequenceFault sequenceFault;
168: private RMException rmException;
169:
170: void setSequenceFault(SequenceFault sf) {
171: sequenceFault = sf;
172: }
173:
174: void setRMException(RMException rme) {
175: rmException = rme;
176: }
177:
178: @Override
179: protected void handle(Message msg) throws SequenceFault,
180: RMException {
181: if (null != sequenceFault) {
182: throw sequenceFault;
183: } else if (null != rmException) {
184: throw rmException;
185: }
186: }
187: }
188: }
|