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.soap11;
18  
19  import java.util.Locale;
20  import javax.xml.namespace.QName;
21  
22  import org.springframework.ws.soap.AbstractSoapBodyTestCase;
23  import org.springframework.ws.soap.SoapFault;
24  import org.springframework.ws.soap.SoapFaultDetail;
25  import org.springframework.ws.soap.SoapFaultDetailElement;
26  import org.springframework.ws.soap.SoapVersion;
27  import org.springframework.xml.transform.StringResult;
28  import org.springframework.xml.transform.StringSource;
29  
30  public abstract class AbstractSoap11BodyTestCase extends AbstractSoapBodyTestCase {
31  
32      public void testGetType() {
33          assertTrue("Invalid type returned", soapBody instanceof Soap11Body);
34      }
35  
36      public void testGetName() throws Exception {
37          assertEquals("Invalid qualified name", SoapVersion.SOAP_11.getBodyName(), soapBody.getName());
38      }
39  
40      public void testGetSource() throws Exception {
41          StringResult result = new StringResult();
42          transformer.transform(soapBody.getSource(), result);
43          assertXMLEqual("Invalid contents of body", "<Body xmlns='http://schemas.xmlsoap.org/soap/envelope/' />",
44                  result.toString());
45      }
46  
47      public void testAddMustUnderstandFault() throws Exception {
48          SoapFault fault = soapBody.addMustUnderstandFault("SOAP Must Understand Error", null);
49          assertEquals("Invalid fault code", new QName("http://schemas.xmlsoap.org/soap/envelope/", "MustUnderstand"),
50                  fault.getFaultCode());
51          assertPayloadEqual("<SOAP-ENV:Fault xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>" +
52                  "<faultcode>" + soapBody.getName().getPrefix() + ":MustUnderstand</faultcode>" +
53                  "<faultstring>SOAP Must Understand Error</faultstring></SOAP-ENV:Fault>");
54      }
55  
56      public void testAddClientFault() throws Exception {
57          SoapFault fault = soapBody.addClientOrSenderFault("faultString", null);
58          assertEquals("Invalid fault code", new QName("http://schemas.xmlsoap.org/soap/envelope/", "Client"),
59                  fault.getFaultCode());
60          assertPayloadEqual("<SOAP-ENV:Fault xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>" +
61                  "<faultcode>" + soapBody.getName().getPrefix() + ":Client</faultcode>" +
62                  "<faultstring>faultString</faultstring>" + "</SOAP-ENV:Fault>");
63      }
64  
65      public void testAddServerFault() throws Exception {
66          SoapFault fault = soapBody.addServerOrReceiverFault("faultString", null);
67          assertEquals("Invalid fault code", new QName("http://schemas.xmlsoap.org/soap/envelope/", "Server"),
68                  fault.getFaultCode());
69          assertPayloadEqual("<SOAP-ENV:Fault xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>" +
70                  "<faultcode>" + soapBody.getName().getPrefix() + ":Server</faultcode>" +
71                  "<faultstring>faultString</faultstring>" + "</SOAP-ENV:Fault>");
72      }
73  
74      public void testAddFault() throws Exception {
75          QName faultCode = new QName("http://www.springframework.org", "fault", "spring");
76          String faultString = "faultString";
77          Soap11Fault fault = ((Soap11Body) soapBody).addFault(faultCode, faultString, Locale.ENGLISH);
78          assertNotNull("Null returned", fault);
79          assertTrue("SoapBody has no fault", soapBody.hasFault());
80          assertNotNull("SoapBody has no fault", soapBody.getFault());
81          assertEquals("Invalid fault code", faultCode, fault.getFaultCode());
82          assertEquals("Invalid fault string", faultString, fault.getFaultStringOrReason());
83          assertEquals("Invalid fault string locale", Locale.ENGLISH, fault.getFaultStringLocale());
84          String actor = "http://www.springframework.org/actor";
85          fault.setFaultActorOrRole(actor);
86          assertEquals("Invalid fault actor", actor, fault.getFaultActorOrRole());
87          assertPayloadEqual("<SOAP-ENV:Fault xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' " +
88                  "xmlns:spring='http://www.springframework.org'>" + "<faultcode>spring:fault</faultcode>" +
89                  "<faultstring xml:lang='en'>" + faultString + "</faultstring>" + "<faultactor>" + actor +
90                  "</faultactor>" + "</SOAP-ENV:Fault>");
91      }
92  
93      public void testAddFaultNoPrefix() throws Exception {
94          QName faultCode = new QName("http://www.springframework.org", "fault");
95          String faultString = "faultString";
96          Soap11Fault fault = ((Soap11Body) soapBody).addFault(faultCode, faultString, Locale.ENGLISH);
97          assertNotNull("Null returned", fault);
98          assertTrue("SoapBody has no fault", soapBody.hasFault());
99          assertNotNull("SoapBody has no fault", soapBody.getFault());
100         assertEquals("Invalid fault code", faultCode, fault.getFaultCode());
101         assertEquals("Invalid fault string", faultString, fault.getFaultStringOrReason());
102         assertEquals("Invalid fault string locale", Locale.ENGLISH, fault.getFaultStringLocale());
103         String actor = "http://www.springframework.org/actor";
104         fault.setFaultActorOrRole(actor);
105         assertEquals("Invalid fault actor", actor, fault.getFaultActorOrRole());
106     }
107 
108     public void testAddFaultWithDetail() throws Exception {
109         QName faultCode = new QName("http://www.springframework.org", "fault", "spring");
110         String faultString = "faultString";
111         SoapFault fault = ((Soap11Body) soapBody).addFault(faultCode, faultString, null);
112         SoapFaultDetail detail = fault.addFaultDetail();
113         QName detailName = new QName("http://www.springframework.org", "detailEntry", "spring");
114         SoapFaultDetailElement detailElement1 = detail.addFaultDetailElement(detailName);
115         StringSource detailContents = new StringSource("<detailContents xmlns='namespace'/>");
116         transformer.transform(detailContents, detailElement1.getResult());
117         SoapFaultDetailElement detailElement2 = detail.addFaultDetailElement(detailName);
118         detailContents = new StringSource("<detailContents xmlns='namespace'/>");
119         transformer.transform(detailContents, detailElement2.getResult());
120         assertPayloadEqual(
121                 "<SOAP-ENV:Fault xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' xmlns:spring='http://www.springframework.org'>" +
122                         "<faultcode>spring:fault</faultcode>" + "<faultstring>" + faultString + "</faultstring>" +
123                         "<detail>" +
124                         "<spring:detailEntry xmlns:spring='http://www.springframework.org'><detailContents xmlns='namespace'/></spring:detailEntry>" +
125                         "<spring:detailEntry xmlns:spring='http://www.springframework.org'><detailContents xmlns='namespace'/></spring:detailEntry>" +
126                         "</detail>" + "</SOAP-ENV:Fault>");
127 
128     }
129 
130     public void testAddFaultWithDetailResult() throws Exception {
131         SoapFault fault = ((Soap11Body) soapBody)
132                 .addFault(new QName("namespace", "localPart", "prefix"), "Fault", null);
133         SoapFaultDetail detail = fault.addFaultDetail();
134         transformer.transform(new StringSource("<detailContents xmlns='namespace'/>"), detail.getResult());
135         transformer.transform(new StringSource("<detailContents xmlns='namespace'/>"), detail.getResult());
136         assertPayloadEqual("<SOAP-ENV:Fault xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>" +
137                 "<faultcode xmlns:prefix='namespace'>prefix:localPart</faultcode>" +
138                 "<faultstring>Fault</faultstring>" + "<detail>" + "<detailContents xmlns='namespace'/>" +
139                 "<detailContents xmlns='namespace'/>" + "</detail></SOAP-ENV:Fault>");
140     }
141 
142 }