001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestEngineSoapXFire.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.engine;
009:
010: import com.meterware.httpunit.GetMethodWebRequest;
011: import com.meterware.httpunit.WebConversation;
012: import com.meterware.httpunit.WebForm;
013: import com.meterware.httpunit.WebRequest;
014: import com.meterware.httpunit.WebResponse;
015: import com.uwyn.rife.TestCaseServerside;
016: import com.uwyn.rife.engine.testwebservices.soap.xfire.CalculatorApi;
017: import com.uwyn.rife.engine.testwebservices.soap.xfire.EchoApi;
018: import com.uwyn.rife.engine.testwebservices.soap.xfire.XFireElementServiceApi;
019: import com.uwyn.rife.tools.ExceptionUtils;
020: import com.uwyn.rife.tools.HttpUtils;
021: import java.io.ByteArrayOutputStream;
022: import java.io.PrintStream;
023: import java.util.logging.Level;
024: import java.util.logging.Logger;
025: import org.codehaus.xfire.XFire;
026: import org.codehaus.xfire.XFireFactory;
027: import org.codehaus.xfire.XFireRuntimeException;
028: import org.codehaus.xfire.client.XFireProxyFactory;
029: import org.codehaus.xfire.fault.XFireFault;
030: import org.codehaus.xfire.service.Service;
031: import org.codehaus.xfire.service.invoker.ObjectInvoker;
032:
033: public class TestEngineSoapXFire extends TestCaseServerside {
034: public TestEngineSoapXFire(int siteType, String name) {
035: super (siteType, name);
036: }
037:
038: public void testEcho() throws Exception {
039: setupSite("site/soap_xfire.xml");
040:
041: String endpoint = "http://localhost:8181/echo";
042: assertTrue(HttpUtils
043: .retrievePage(endpoint + "?wsdl")
044: .getContent()
045: .startsWith(
046: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
047: + "<wsdl:definitions targetNamespace=\"http://xfire.soap.testwebservices.engine.rife.uwyn.com\" xmlns:tns=\"http://xfire.soap.testwebservices.engine.rife.uwyn.com\" xmlns:wsdlsoap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenc11=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:soapenc12=\"http://www.w3.org/2003/05/soap-encoding\" xmlns:soap11=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\">"));
048:
049: XFire xfire = XFireFactory.newInstance().getXFire();
050: XFireProxyFactory factory = new XFireProxyFactory(xfire);
051: Service service = xfire.getServiceRegistry().getService("Echo");
052: EchoApi echo = (EchoApi) factory.create(service, endpoint);
053: assertEquals("I got : 'the value'", echo.echo("the value"));
054: }
055:
056: public void testCalculator() throws Exception {
057: setupSite("site/soap_xfire.xml");
058:
059: String endpoint = "http://localhost:8181/calculator";
060: assertTrue(HttpUtils
061: .retrievePage(endpoint + "?wsdl")
062: .getContent()
063: .startsWith(
064: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
065: + "<wsdl:definitions targetNamespace=\"http://xfire.soap.testwebservices.engine.rife.uwyn.com\" xmlns:tns=\"http://xfire.soap.testwebservices.engine.rife.uwyn.com\" xmlns:wsdlsoap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenc11=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:soapenc12=\"http://www.w3.org/2003/05/soap-encoding\" xmlns:soap11=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\">"));
066:
067: XFire xfire = XFireFactory.newInstance().getXFire();
068: XFireProxyFactory factory = new XFireProxyFactory(xfire);
069: Service service = xfire.getServiceRegistry().getService(
070: "Calculator");
071: CalculatorApi calculator = (CalculatorApi) factory.create(
072: service, endpoint);
073: assertEquals(112, calculator.add(23, 89));
074: assertEquals(47, calculator.substract(79, 32));
075: }
076:
077: public void testEchoAuth() throws Exception {
078: setupSite("site/soap_xfire.xml");
079:
080: String endpoint = "http://localhost:8181/echoauth";
081:
082: XFire xfire = XFireFactory.newInstance().getXFire();
083: XFireProxyFactory factory = new XFireProxyFactory(xfire);
084: Service service = xfire.getServiceRegistry().getService(
085: "EchoAuth");
086:
087: try {
088: Logger.getLogger("org.codehaus.xfire").setLevel(Level.OFF);
089: EchoApi echo_noauth = (EchoApi) factory.create(service,
090: endpoint);
091: echo_noauth.echo("Yooohoooo!");
092: } catch (Throwable e) {
093: assertTrue(ExceptionUtils.getExceptionStackTrace(e),
094: e instanceof java.lang.NullPointerException);
095: }
096:
097: EchoApi echo_auth = (EchoApi) factory
098: .create(
099: service,
100: endpoint
101: + "?login=gbevin&password=yeolpass&submission=credentials");
102: assertEquals("I got : 'Yooohoooo!'", echo_auth
103: .echo("Yooohoooo!"));
104:
105: WebConversation conversation = new WebConversation();
106: WebRequest request = null;
107: WebResponse response = null;
108: WebForm form = null;
109: String auth_id;
110: request = new GetMethodWebRequest("http://localhost:8181/auth");
111: response = conversation.getResponse(request);
112: form = response.getForms()[0];
113: form.setParameter("login", "guest");
114: form.setParameter("password", "guestpass");
115: response = form.submit();
116: assertEquals(0, response.getForms().length);
117: auth_id = response.getTitle();
118:
119: String url_auth = "http://localhost:8181/echoauth?authid="
120: + auth_id;
121: echo_auth = (EchoApi) factory.create(service, url_auth);
122: assertEquals("I got : 'Yooohoooo!'", echo_auth
123: .echo("Yooohoooo!"));
124: }
125:
126: public void testElementService() throws Exception {
127: setupSite("site/soap_xfire.xml");
128:
129: String endpoint = "http://localhost:8181/elementservice";
130: assertTrue(HttpUtils
131: .retrievePage(endpoint + "?wsdl")
132: .getContent()
133: .startsWith(
134: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
135: + "<wsdl:definitions targetNamespace=\"http://xfire.soap.testwebservices.engine.rife.uwyn.com\" xmlns:tns=\"http://xfire.soap.testwebservices.engine.rife.uwyn.com\" xmlns:wsdlsoap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenc11=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:soapenc12=\"http://www.w3.org/2003/05/soap-encoding\" xmlns:soap11=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\">"));
136:
137: XFire xfire = XFireFactory.newInstance().getXFire();
138: XFireProxyFactory factory = new XFireProxyFactory(xfire);
139: Service service = xfire.getServiceRegistry().getService(
140: "XFireElementService");
141: XFireElementServiceApi element_service = (XFireElementServiceApi) factory
142: .create(service, endpoint
143: + "?input1=value1&input2=value2");
144:
145: assertEquals("value1", element_service
146: .getElementInput("input1"));
147: assertEquals("value2", element_service
148: .getElementInput("input2"));
149: assertNull(element_service.getElementInput("input3"));
150: PrintStream out = System.out;
151: Level orig_level = Logger.getLogger(
152: ObjectInvoker.class.getName()).getLevel();
153: try {
154: System.setOut(new PrintStream(new ByteArrayOutputStream()));
155: Logger.getLogger(ObjectInvoker.class.getName()).setLevel(
156: Level.OFF);
157: element_service.getElementInput("unknown");
158: fail();
159: } catch (XFireRuntimeException e) {
160: assertTrue(e.getCause() instanceof XFireFault);
161: assertEquals(
162: "The element 'rife/soap/xfire.xml' doesn't contain input 'unknown'.",
163: e.getCause().getMessage());
164: } finally {
165: System.setOut(out);
166: Logger.getLogger(ObjectInvoker.class.getName()).setLevel(
167: orig_level);
168: }
169: }
170: }
|