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.soap;
019:
020: import java.math.BigInteger;
021:
022: import javax.xml.namespace.QName;
023:
024: import org.w3c.dom.Element;
025:
026: import org.apache.cxf.binding.soap.Soap11;
027: import org.apache.cxf.binding.soap.Soap12;
028: import org.apache.cxf.binding.soap.SoapBinding;
029: import org.apache.cxf.binding.soap.SoapFault;
030: import org.apache.cxf.ws.rm.Identifier;
031: import org.apache.cxf.ws.rm.RMConstants;
032: import org.apache.cxf.ws.rm.SequenceAcknowledgement;
033: import org.apache.cxf.ws.rm.SequenceFault;
034: import org.apache.cxf.ws.rm.SequenceFaultType;
035: import org.easymock.classextension.EasyMock;
036: import org.easymock.classextension.IMocksControl;
037: import org.junit.Assert;
038: import org.junit.Before;
039: import org.junit.Test;
040:
041: /**
042: *
043: */
044: public class SoapFaultFactoryTest extends Assert {
045:
046: private IMocksControl control;
047: private SequenceFault sf;
048:
049: @Before
050: public void setUp() {
051: control = EasyMock.createNiceControl();
052: }
053:
054: SequenceFault setupSequenceFault(boolean isSender, QName code,
055: Object detail) {
056: sf = control.createMock(SequenceFault.class);
057: EasyMock.expect(sf.getReason()).andReturn("reason");
058: EasyMock.expect(sf.isSender()).andReturn(isSender);
059: EasyMock.expect(sf.getSubCode()).andReturn(code);
060: if (null != detail) {
061: EasyMock.expect(sf.getDetail()).andReturn(detail);
062: SequenceFaultType sft = new SequenceFaultType();
063: sft.setFaultCode(RMConstants.getUnknownSequenceFaultCode());
064: }
065: return sf;
066: }
067:
068: @Test
069: public void createSoap11Fault() {
070: SoapBinding sb = control.createMock(SoapBinding.class);
071: EasyMock.expect(sb.getSoapVersion()).andReturn(
072: Soap11.getInstance());
073: setupSequenceFault(false, RMConstants
074: .getSequenceTerminatedFaultCode(), null);
075: control.replay();
076: SoapFaultFactory factory = new SoapFaultFactory(sb);
077: SoapFault fault = (SoapFault) factory.createFault(sf);
078: assertEquals("reason", fault.getReason());
079: assertEquals(Soap11.getInstance().getReceiver(), fault
080: .getFaultCode());
081: assertEquals(RMConstants.getSequenceTerminatedFaultCode(),
082: fault.getSubCode());
083: assertNull(fault.getDetail());
084: assertSame(sf, fault.getCause());
085: control.verify();
086: }
087:
088: @Test
089: public void createSoap12Fault() {
090: SoapBinding sb = control.createMock(SoapBinding.class);
091: EasyMock.expect(sb.getSoapVersion()).andReturn(
092: Soap12.getInstance());
093: Identifier id = new Identifier();
094: id.setValue("sid");
095: setupSequenceFault(true, RMConstants
096: .getUnknownSequenceFaultCode(), id);
097: control.replay();
098: SoapFaultFactory factory = new SoapFaultFactory(sb);
099: SoapFault fault = (SoapFault) factory.createFault(sf);
100: assertEquals("reason", fault.getReason());
101: assertEquals(Soap12.getInstance().getSender(), fault
102: .getFaultCode());
103: assertEquals(RMConstants.getUnknownSequenceFaultCode(), fault
104: .getSubCode());
105: Element elem = fault.getDetail();
106: assertEquals(RMConstants.getNamespace(), elem.getNamespaceURI());
107: assertEquals("Identifier", elem.getLocalName());
108: assertNull(fault.getCause());
109: control.verify();
110: }
111:
112: @Test
113: public void createSoap12FaultWithIdentifierDetail() {
114: SoapBinding sb = control.createMock(SoapBinding.class);
115: EasyMock.expect(sb.getSoapVersion()).andReturn(
116: Soap12.getInstance());
117: Identifier id = new Identifier();
118: id.setValue("sid");
119: setupSequenceFault(true, RMConstants
120: .getUnknownSequenceFaultCode(), id);
121: control.replay();
122: SoapFaultFactory factory = new SoapFaultFactory(sb);
123: SoapFault fault = (SoapFault) factory.createFault(sf);
124: assertEquals("reason", fault.getReason());
125: assertEquals(Soap12.getInstance().getSender(), fault
126: .getFaultCode());
127: assertEquals(RMConstants.getUnknownSequenceFaultCode(), fault
128: .getSubCode());
129: Element elem = fault.getDetail();
130: assertEquals(RMConstants.getNamespace(), elem.getNamespaceURI());
131: assertEquals("Identifier", elem.getLocalName());
132: control.verify();
133: }
134:
135: @Test
136: public void createSoap12FaultWithAcknowledgementDetail() {
137: SoapBinding sb = control.createMock(SoapBinding.class);
138: EasyMock.expect(sb.getSoapVersion()).andReturn(
139: Soap12.getInstance());
140: SequenceAcknowledgement ack = new SequenceAcknowledgement();
141: Identifier id = new Identifier();
142: id.setValue("sid");
143: ack.setIdentifier(id);
144: SequenceAcknowledgement.AcknowledgementRange range = new SequenceAcknowledgement.AcknowledgementRange();
145: range.setLower(BigInteger.ONE);
146: range.setUpper(BigInteger.TEN);
147: ack.getAcknowledgementRange().add(range);
148: setupSequenceFault(true, RMConstants
149: .getInvalidAcknowledgmentFaultCode(), ack);
150: control.replay();
151: SoapFaultFactory factory = new SoapFaultFactory(sb);
152: SoapFault fault = (SoapFault) factory.createFault(sf);
153: assertEquals("reason", fault.getReason());
154: assertEquals(Soap12.getInstance().getSender(), fault
155: .getFaultCode());
156: assertEquals(RMConstants.getInvalidAcknowledgmentFaultCode(),
157: fault.getSubCode());
158: Element elem = fault.getDetail();
159: assertEquals(RMConstants.getNamespace(), elem.getNamespaceURI());
160: assertEquals("SequenceAcknowledgement", elem.getLocalName());
161: control.verify();
162: }
163:
164: @Test
165: public void createSoap12FaultWithoutDetail() {
166: SoapBinding sb = control.createMock(SoapBinding.class);
167: EasyMock.expect(sb.getSoapVersion()).andReturn(
168: Soap12.getInstance());
169: setupSequenceFault(true, RMConstants
170: .getCreateSequenceRefusedFaultCode(), null);
171: control.replay();
172: SoapFaultFactory factory = new SoapFaultFactory(sb);
173: SoapFault fault = (SoapFault) factory.createFault(sf);
174: assertEquals("reason", fault.getReason());
175: assertEquals(Soap12.getInstance().getSender(), fault
176: .getFaultCode());
177: assertEquals(RMConstants.getCreateSequenceRefusedFaultCode(),
178: fault.getSubCode());
179: assertNull(fault.getDetail());
180:
181: control.verify();
182: }
183:
184: @Test
185: public void testToString() {
186: SoapBinding sb = control.createMock(SoapBinding.class);
187: EasyMock.expect(sb.getSoapVersion()).andReturn(
188: Soap11.getInstance());
189: SoapFault fault = control.createMock(SoapFault.class);
190: EasyMock.expect(fault.getReason()).andReturn("r");
191: EasyMock.expect(fault.getFaultCode()).andReturn(
192: new QName("ns", "code"));
193: EasyMock.expect(fault.getSubCode()).andReturn(
194: new QName("ns", "subcode"));
195: control.replay();
196: SoapFaultFactory factory = new SoapFaultFactory(sb);
197: assertEquals("Reason: r, code: {ns}code, subCode: {ns}subcode",
198: factory.toString(fault));
199: control.verify();
200:
201: }
202: }
|