1   /*
2    * Copyright 2006 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.soap.soap12;
18  
19  import java.util.Iterator;
20  import java.util.Locale;
21  import javax.xml.namespace.QName;
22  
23  import org.springframework.ws.soap.AbstractSoapBodyTestCase;
24  import org.springframework.ws.soap.SoapFault;
25  import org.springframework.ws.soap.SoapFaultDetail;
26  import org.springframework.ws.soap.SoapFaultDetailElement;
27  import org.springframework.ws.soap.SoapVersion;
28  import org.springframework.xml.transform.StringResult;
29  import org.springframework.xml.transform.StringSource;
30  
31  public abstract class AbstractSoap12BodyTestCase extends AbstractSoapBodyTestCase {
32  
33      public void testGetType() {
34          assertTrue("Invalid type returned", soapBody instanceof Soap12Body);
35      }
36  
37      public void testGetName() throws Exception {
38          assertEquals("Invalid qualified name", new QName(SoapVersion.SOAP_12.getEnvelopeNamespaceUri(), "Body"),
39                  soapBody.getName());
40      }
41  
42      public void testGetSource() throws Exception {
43          StringResult result = new StringResult();
44          transformer.transform(soapBody.getSource(), result);
45          assertXMLEqual("Invalid contents of body", "<Body xmlns='http://www.w3.org/2003/05/soap-envelope' />",
46                  result.toString());
47      }
48  
49      public void testAddMustUnderstandFault() throws Exception {
50          SoapFault fault = soapBody.addMustUnderstandFault("SOAP Must Understand Error", Locale.ENGLISH);
51          assertEquals("Invalid fault code", new QName("http://www.w3.org/2003/05/soap-envelope", "MustUnderstand"),
52                  fault.getFaultCode());
53          StringResult result = new StringResult();
54          transformer.transform(fault.getSource(), result);
55          assertXMLEqual("Invalid contents of body",
56                  "<soapenv:Fault xmlns:soapenv='http://www.w3.org/2003/05/soap-envelope'>" +
57                          "<soapenv:Code><soapenv:Value>" + soapBody.getName().getPrefix() +
58                          ":MustUnderstand</soapenv:Value></soapenv:Code>" +
59                          "<soapenv:Reason><soapenv:Text xml:lang='en'>SOAP Must Understand Error</soapenv:Text>" +
60                          "</soapenv:Reason></soapenv:Fault>", result.toString());
61      }
62  
63      public void testAddSenderFault() throws Exception {
64          SoapFault fault = soapBody.addClientOrSenderFault("faultString", Locale.ENGLISH);
65          assertEquals("Invalid fault code", new QName("http://www.w3.org/2003/05/soap-envelope", "Sender"),
66                  fault.getFaultCode());
67          StringResult result = new StringResult();
68          transformer.transform(fault.getSource(), result);
69          assertXMLEqual("Invalid contents of body",
70                  "<soapenv:Fault xmlns:soapenv='http://www.w3.org/2003/05/soap-envelope'>" +
71                          "<soapenv:Code><soapenv:Value>" + soapBody.getName().getPrefix() +
72                          ":Sender</soapenv:Value></soapenv:Code>" +
73                          "<soapenv:Reason><soapenv:Text xml:lang='en'>faultString</soapenv:Text></soapenv:Reason>" +
74                          "</soapenv:Fault>", result.toString());
75      }
76  
77      public void testAddReceiverFault() throws Exception {
78          SoapFault fault = soapBody.addServerOrReceiverFault("faultString", Locale.ENGLISH);
79          assertEquals("Invalid fault code", new QName("http://www.w3.org/2003/05/soap-envelope", "Receiver"),
80                  fault.getFaultCode());
81          StringResult result = new StringResult();
82          transformer.transform(fault.getSource(), result);
83          assertXMLEqual("Invalid contents of body",
84                  "<soapenv:Fault xmlns:soapenv='http://www.w3.org/2003/05/soap-envelope'>" +
85                          "<soapenv:Code><soapenv:Value>" + soapBody.getName().getPrefix() +
86                          ":Receiver</soapenv:Value></soapenv:Code>" +
87                          "<soapenv:Reason><soapenv:Text xml:lang='en'>faultString</soapenv:Text></soapenv:Reason>" +
88                          "</soapenv:Fault>", result.toString());
89      }
90  
91      public void testAddFaultWithDetail() throws Exception {
92          SoapFault fault = soapBody.addServerOrReceiverFault("faultString", Locale.ENGLISH);
93          SoapFaultDetail detail = fault.addFaultDetail();
94          SoapFaultDetailElement detailElement =
95                  detail.addFaultDetailElement(new QName("namespace", "localPart", "prefix"));
96          StringSource detailContents = new StringSource("<detailContents xmlns='namespace'/>");
97          transformer.transform(detailContents, detailElement.getResult());
98          StringResult result = new StringResult();
99          transformer.transform(fault.getSource(), result);
100         assertXMLEqual("Invalid source for body",
101                 "<soapenv:Fault xmlns:soapenv='http://www.w3.org/2003/05/soap-envelope'>" +
102                         "<soapenv:Code><soapenv:Value>" + soapBody.getName().getPrefix() + ":Receiver</soapenv:Value>" +
103                         "</soapenv:Code>" +
104                         "<soapenv:Reason><soapenv:Text xml:lang='en'>faultString</soapenv:Text></soapenv:Reason>" +
105                         "<soapenv:Detail><prefix:localPart xmlns:prefix='namespace'><detailContents xmlns='namespace'/>" +
106                         "</prefix:localPart></soapenv:Detail></soapenv:Fault>", result.toString());
107     }
108 
109     public void testAddFaultWithDetailResult() throws Exception {
110         SoapFault fault = soapBody.addServerOrReceiverFault("faultString", Locale.ENGLISH);
111         SoapFaultDetail detail = fault.addFaultDetail();
112         transformer.transform(new StringSource("<detailContents xmlns='namespace'/>"), detail.getResult());
113         transformer.transform(new StringSource("<detailContents xmlns='namespace'/>"), detail.getResult());
114         StringResult result = new StringResult();
115         transformer.transform(fault.getSource(), result);
116         assertXMLEqual("Invalid source for body",
117                 "<soapenv:Fault xmlns:soapenv='http://www.w3.org/2003/05/soap-envelope'>" +
118                         "<soapenv:Code><soapenv:Value>" + soapBody.getName().getPrefix() + ":Receiver</soapenv:Value>" +
119                         "</soapenv:Code>" +
120                         "<soapenv:Reason><soapenv:Text xml:lang='en'>faultString</soapenv:Text></soapenv:Reason>" +
121                         "<soapenv:Detail>" + "<detailContents xmlns='namespace'/>" +
122                         "<detailContents xmlns='namespace'/>" + "</soapenv:Detail></soapenv:Fault>", result.toString());
123     }
124 
125     public void testAddFaultWithSubcode() throws Exception {
126         Soap12Fault fault = (Soap12Fault) soapBody.addServerOrReceiverFault("faultString", Locale.ENGLISH);
127         QName subcode1 = new QName("http://www.springframework.org", "Subcode1", "spring-ws");
128         fault.addFaultSubcode(subcode1);
129         QName subcode2 = new QName("http://www.springframework.org", "Subcode2", "spring-ws");
130         fault.addFaultSubcode(subcode2);
131         Iterator iterator = fault.getFaultSubcodes();
132         assertTrue("No subcode found", iterator.hasNext());
133         assertEquals("Invalid subcode", subcode1, iterator.next());
134         assertTrue("No subcode found", iterator.hasNext());
135         assertEquals("Invalid subcode", subcode2, iterator.next());
136         assertFalse("Subcode found", iterator.hasNext());
137         StringResult result = new StringResult();
138         transformer.transform(fault.getSource(), result);
139         assertXMLEqual("Invalid source for body",
140                 "<soapenv:Fault xmlns:soapenv='http://www.w3.org/2003/05/soap-envelope'>" +
141                         "<soapenv:Code><soapenv:Value>" + soapBody.getName().getPrefix() + ":Receiver</soapenv:Value>" +
142                         "<soapenv:Subcode><soapenv:Value xmlns:spring-ws='http://www.springframework.org'>spring-ws:Subcode1</soapenv:Value>" +
143                         "<soapenv:Subcode><soapenv:Value xmlns:spring-ws='http://www.springframework.org'>spring-ws:Subcode2</soapenv:Value>" +
144                         "</soapenv:Subcode></soapenv:Subcode></soapenv:Code>" +
145                         "<soapenv:Reason><soapenv:Text xml:lang='en'>faultString</soapenv:Text></soapenv:Reason>" +
146                         "</soapenv:Fault>", result.toString());
147     }
148 
149 }