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.systest.soap12;
019:
020: import java.io.InputStream;
021: import java.net.HttpURLConnection;
022: import java.net.URL;
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: import javax.xml.namespace.QName;
027: import javax.xml.xpath.XPathConstants;
028:
029: import org.w3c.dom.Document;
030: import org.w3c.dom.Node;
031:
032: import org.apache.cxf.binding.soap.Soap12;
033: import org.apache.cxf.helpers.XMLUtils;
034: import org.apache.cxf.helpers.XPathUtils;
035: import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
036: import org.apache.hello_world_soap12_http.Greeter;
037: import org.apache.hello_world_soap12_http.PingMeFault;
038: import org.apache.hello_world_soap12_http.SOAPService;
039: import org.apache.hello_world_soap12_http.types.FaultDetail;
040: import org.junit.BeforeClass;
041: import org.junit.Test;
042:
043: public class Soap12ClientServerTest extends
044: AbstractBusClientServerTestBase {
045:
046: private final QName serviceName = new QName(
047: "http://apache.org/hello_world_soap12_http", "SOAPService");
048: private final QName portName = new QName(
049: "http://apache.org/hello_world_soap12_http", "SoapPort");
050:
051: @BeforeClass
052: public static void startServers() throws Exception {
053: assertTrue("server did not launch correctly",
054: launchServer(Server.class));
055: }
056:
057: @Test
058: public void testBasicConnection() throws Exception {
059: Greeter greeter = getGreeter();
060: for (int i = 0; i < 5; i++) {
061: String echo = greeter.sayHi();
062: assertEquals("Bonjour", echo);
063: }
064:
065: }
066:
067: @Test
068: public void testPingMeFault() throws Exception {
069: Greeter greeter = getGreeter();
070: try {
071: greeter.pingMe();
072: fail("Should throw Exception!");
073: } catch (PingMeFault ex) {
074: FaultDetail detail = ex.getFaultInfo();
075: assertEquals((short) 2, detail.getMajor());
076: assertEquals((short) 1, detail.getMinor());
077: assertEquals("PingMeFault raised by server", ex
078: .getMessage());
079: }
080: }
081:
082: @Test
083: public void testGetSayHi() throws Exception {
084: HttpURLConnection httpConnection = getHttpConnection("http://localhost:9012/SoapContext/SoapPort/sayHi");
085: httpConnection.connect();
086:
087: InputStream in = httpConnection.getInputStream();
088: assertNotNull(in);
089: assertEquals("application/soap+xml; charset=utf-8",
090: httpConnection.getContentType().toLowerCase());
091:
092: Document doc = XMLUtils.parse(in);
093: assertNotNull(doc);
094: Map<String, String> ns = new HashMap<String, String>();
095: ns.put("soap12", Soap12.SOAP_NAMESPACE);
096: ns
097: .put("ns2",
098: "http://apache.org/hello_world_soap12_http/types");
099: XPathUtils xu = new XPathUtils(ns);
100: Node body = (Node) xu.getValue("/soap12:Envelope/soap12:Body",
101: doc, XPathConstants.NODE);
102: assertNotNull(body);
103: String response = (String) xu.getValue(
104: "//ns2:sayHiResponse/ns2:responseType/text()", body,
105: XPathConstants.STRING);
106: assertEquals("Bonjour", response);
107: }
108:
109: @Test
110: public void testGetPingMe() throws Exception {
111: HttpURLConnection httpConnection = getHttpConnection("http://localhost:9012/SoapContext/SoapPort/pingMe");
112: httpConnection.connect();
113:
114: assertEquals(500, httpConnection.getResponseCode());
115:
116: assertEquals("Internal Server Error", httpConnection
117: .getResponseMessage());
118:
119: InputStream in = httpConnection.getErrorStream();
120: assertNotNull(in);
121: assertEquals("application/soap+xml; charset=utf-8",
122: httpConnection.getContentType().toLowerCase());
123:
124: Document doc = XMLUtils.parse(in);
125: assertNotNull(doc);
126:
127: Map<String, String> ns = new HashMap<String, String>();
128: ns.put("s", Soap12.SOAP_NAMESPACE);
129: ns
130: .put("ns2",
131: "http://apache.org/hello_world_soap12_http/types");
132: XPathUtils xu = new XPathUtils(ns);
133: String codeValue = (String) xu.getValue(
134: "/s:Envelope/s:Body/s:Fault/s:Code/s:Value/text()",
135: doc, XPathConstants.STRING);
136:
137: assertEquals("soap:Receiver", codeValue);
138: String reason = (String) xu.getValue("//s:Reason//text()", doc,
139: XPathConstants.STRING);
140: assertEquals("PingMeFault raised by server", reason);
141:
142: String minor = (String) xu.getValue(
143: "//s:Detail//ns2:faultDetail/ns2:minor/text()", doc,
144: XPathConstants.STRING);
145: assertEquals("1", minor);
146: String major = (String) xu.getValue(
147: "//s:Detail//ns2:faultDetail/ns2:major/text()", doc,
148: XPathConstants.STRING);
149: assertEquals("2", major);
150: }
151:
152: @Test
153: public void testGetMethodNotExist() throws Exception {
154: HttpURLConnection httpConnection = getHttpConnection("http://localhost:9012/SoapContext/SoapPort/greetMe");
155: httpConnection.connect();
156:
157: assertEquals(500, httpConnection.getResponseCode());
158:
159: assertEquals("application/soap+xml; charset=utf-8",
160: httpConnection.getContentType().toLowerCase());
161:
162: assertEquals("Internal Server Error", httpConnection
163: .getResponseMessage());
164:
165: InputStream in = httpConnection.getErrorStream();
166: assertNotNull(in);
167:
168: Document doc = XMLUtils.parse(in);
169: assertNotNull(doc);
170: Map<String, String> ns = new HashMap<String, String>();
171: ns.put("s", Soap12.SOAP_NAMESPACE);
172: ns
173: .put("ns2",
174: "http://apache.org/hello_world_soap12_http/types");
175: XPathUtils xu = new XPathUtils(ns);
176: String codeValue = (String) xu.getValue(
177: "/s:Envelope/s:Body/s:Fault/s:Code/s:Value/text()",
178: doc, XPathConstants.STRING);
179:
180: assertEquals("soap:Receiver", codeValue);
181: String reason = (String) xu.getValue("//s:Reason//text()", doc,
182: XPathConstants.STRING);
183: assertEquals("No such operation: greetMe", reason);
184: }
185:
186: private Greeter getGreeter() {
187: URL wsdl = getClass().getResource(
188: "/wsdl/hello_world_soap12.wsdl");
189: assertNotNull("WSDL is null", wsdl);
190:
191: SOAPService service = new SOAPService(wsdl, serviceName);
192: assertNotNull("Service is ull ", service);
193:
194: return service.getPort(portName, Greeter.class);
195: }
196:
197: }
|