Author: sagara Date: Fri Oct 14 11:42:02 2011 New Revision: 1183316 URL: http://svn.apache.org/viewvc?rev=1183316&view=rev Log: AXIS2-3933 : Added basic test framework to test HTTP transport classes.
Added: axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/ axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/MockAxisHttpResponse.java (with props) axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/MockHTTPResponse.java (with props) axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/MockHttpServletResponse.java (with props) Modified: axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/CommonsHTTPTransportSenderTest.java Modified: axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/CommonsHTTPTransportSenderTest.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/CommonsHTTPTransportSenderTest.java?rev=1183316&r1=1183315&r2=1183316&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/CommonsHTTPTransportSenderTest.java (original) +++ axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/CommonsHTTPTransportSenderTest.java Fri Oct 14 11:42:02 2011 @@ -18,31 +18,56 @@ */ package org.apache.axis2.transport.http; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import javax.mail.MessagingException; +import javax.servlet.http.HttpServletResponse; import javax.xml.namespace.QName; +import junit.framework.TestCase; + import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; +import org.apache.axiom.om.OMFactory; +import org.apache.axiom.soap.SOAPBody; +import org.apache.axiom.soap.SOAPEnvelope; +import org.apache.axiom.soap.SOAPFactory; import org.apache.axis2.AxisFault; +import org.apache.axis2.Constants; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; import org.apache.axis2.context.ConfigurationContext; import org.apache.axis2.context.ConfigurationContextFactory; -import org.junit.Test; +import org.apache.axis2.context.MessageContext; +import org.apache.axis2.description.Parameter; +import org.apache.axis2.description.TransportOutDescription; +import org.apache.axis2.engine.Handler.InvocationResponse; +import org.apache.axis2.transport.OutTransportInfo; +import org.apache.axis2.transport.TransportSender; +import org.apache.axis2.transport.http.mock.MockAxisHttpResponse; +import org.apache.axis2.transport.http.mock.MockHttpServletResponse; +import org.apache.axis2.transport.http.mock.MockHTTPResponse; +import org.apache.commons.httpclient.Header; +import org.apache.commons.httpclient.HttpMethod; +import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.http.ProtocolVersion; +import org.apache.http.RequestLine; +import org.apache.http.message.BasicRequestLine; import org.mortbay.http.SocketListener; import org.mortbay.jetty.Server; -public class CommonsHTTPTransportSenderTest { +public class CommonsHTTPTransportSenderTest extends TestCase { + /** * Tests that HTTP connections are properly released when the server returns a 404 error. This * is a regression test for AXIS2-5093. * * @throws Exception */ - @Test + public void testConnectionReleaseWith404() throws Exception { // Create a Jetty server instance without any contexts. It will always return HTTP 404. Server server = new Server(); @@ -72,5 +97,135 @@ public class CommonsHTTPTransportSenderT } finally { server.stop(); } + } + + public void testInvokeWithServletBasedOutTransportInfo() throws Exception { + MockHTTPResponse httpResponse = new MockHttpServletResponse(); + ServletBasedOutTransportInfo info = new ServletBasedOutTransportInfo( + (HttpServletResponse) httpResponse); + SOAPEnvelope envelope = getEnvelope(); + httpResponse = configAndRun(httpResponse, info, null); + + assertEquals("Not the expected Header value", "application/xml", httpResponse.getHeaders() + .get("Content-Type")); + assertEquals("Not the expected Header value", "custom-value", httpResponse.getHeaders() + .get("Custom-header")); + assertEquals("Not the expected body content", envelope.toString().replace("utf", "UTF"), + new String(httpResponse.getByteArrayOutputStream().toByteArray())); + } + + public void testInvokeWithAxisHttpResponseImpl() throws Exception { + RequestLine line = new BasicRequestLine("", "", new ProtocolVersion("http", 1, 0)); + MockHTTPResponse httpResponse = new MockAxisHttpResponse(line); + SOAPEnvelope envelope = getEnvelope(); + httpResponse = (MockAxisHttpResponse) configAndRun(httpResponse, + (OutTransportInfo) httpResponse, null); + + assertEquals("Not the expected Header value", "application/xml", httpResponse.getHeaders() + .get("Content-Type")); + assertEquals("Not the expected Header value", "custom-value", httpResponse.getHeaders() + .get("Custom-header")); + assertEquals("Not the expected body content", envelope.toString().replace("utf", "UTF"), + new String(httpResponse.getByteArrayOutputStream().toByteArray())); + } + + public void testInvokeWithEPR() throws Exception { + RequestLine line = new BasicRequestLine("", "", new ProtocolVersion("http", 1, 0)); + MockHTTPResponse httpResponse = new MockAxisHttpResponse(line); + /* + * TODO - This method used to test client side support of + * CommonsHTTPTransportSender. At the moment this will return Connection + * refused exception because there is no server side support given. It + * is required to complete this test by adding a HTTP server and verify + * data in the server side. + */ + try { + httpResponse = (MockAxisHttpResponse) configAndRun(httpResponse, + (OutTransportInfo) httpResponse, "http://localhost:8080"); + fail("Should raise org.apache.axis2.AxisFault: Connection refused"); + } catch (AxisFault e) { + } + + // assertEquals("Not the expected Header value", "application/xml", + // httpResponse.getHeaders().get("Content-Type")); + // assertEquals("Not the expected Header value", "custom-value", + // httpResponse.getHeaders().get("Custom-header")); + // assertEquals("Not the expected body content", + // envelope.toString().replace("utf", "UTF"), new String(httpResponse + // .getByteArrayOutputStream().toByteArray())); + + } + + public void testCleanup() throws AxisFault { + TransportSender sender = new CommonsHTTPTransportSender(); + MessageContext msgContext = new MessageContext(); + HttpMethod httpMethod = new GetMethod(); + msgContext.setProperty(HTTPConstants.HTTP_METHOD, httpMethod); + assertNotNull("HttpMethod can not be null", + msgContext.getProperty(HTTPConstants.HTTP_METHOD)); + sender.cleanup(msgContext); + assertNull("HttpMethod should be null", msgContext.getProperty(HTTPConstants.HTTP_METHOD)); + + } + + public void testInit() throws AxisFault { + ConfigurationContext confContext = ConfigurationContextFactory + .createEmptyConfigurationContext(); + TransportOutDescription transportOut = new TransportOutDescription("http"); + TransportSender sender = new CommonsHTTPTransportSender(); + sender.init(confContext, transportOut); + + } + + private MockHTTPResponse configAndRun(MockHTTPResponse outResponse, + OutTransportInfo outTransportInfo, String epr) throws Exception { + MockHTTPResponse response = outResponse; + ConfigurationContext confContext = ConfigurationContextFactory + .createEmptyConfigurationContext(); + TransportOutDescription transportOut = new TransportOutDescription("http"); + Parameter param = new Parameter(HTTPConstants.OMIT_SOAP_12_ACTION, false); + SOAPEnvelope envelope = getEnvelope(); + MessageContext msgContext = new MessageContext(); + + TransportSender sender = new CommonsHTTPTransportSender(); + transportOut.addParameter(param); + // create dummy SOAPEnvelope + msgContext.setEnvelope(envelope); + msgContext.setProperty(MessageContext.TRANSPORT_OUT, + ((MockHTTPResponse) response).getByteArrayOutputStream()); + msgContext.setProperty(Constants.OUT_TRANSPORT_INFO, outTransportInfo); + msgContext.setTransportOut(transportOut); + msgContext.setConfigurationContext(confContext); + if (epr != null) { + msgContext.setProperty(Constants.Configuration.TRANSPORT_URL, epr); + } + // set two Headers for testing + List<Header> headerList = new ArrayList<Header>(); + Header header1 = new Header("Content-Type", "application/xml"); + Header header2 = new Header("Custom-header", "custom-value"); + headerList.add(header1); + headerList.add(header2); + msgContext.setProperty(HTTPConstants.HTTP_HEADERS, headerList); + sender.init(confContext, transportOut); + InvocationResponse inResponse = sender.invoke(msgContext); + assertEquals("Not the expected InvocationResponse", InvocationResponse.CONTINUE, inResponse); + return response; + + } + + private SOAPEnvelope getEnvelope() throws IOException, MessagingException { + SOAPFactory soapFac = OMAbstractFactory.getSOAP11Factory(); + OMFactory omFac = OMAbstractFactory.getOMFactory(); + SOAPEnvelope enp = soapFac.createSOAPEnvelope(); + SOAPBody sopaBody = soapFac.createSOAPBody(); + + OMElement content = omFac.createOMElement(new QName("message")); + OMElement data1 = omFac.createOMElement(new QName("part")); + data1.setText("sample data"); + + content.addChild(data1); + sopaBody.addChild(content); + enp.addChild(sopaBody); + return enp; } } Added: axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/MockAxisHttpResponse.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/MockAxisHttpResponse.java?rev=1183316&view=auto ============================================================================== --- axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/MockAxisHttpResponse.java (added) +++ axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/MockAxisHttpResponse.java Fri Oct 14 11:42:02 2011 @@ -0,0 +1,115 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.axis2.transport.http.mock; + +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +import org.apache.axis2.transport.OutTransportInfo; +import org.apache.axis2.transport.http.server.AxisHttpResponse; +import org.apache.http.RequestLine; +import org.apache.http.message.BasicHttpRequest; + +/** + * The Class MockAxisHttpResponse is a mock implementation of AxisHttpResponse + * to used with unit tests. + * + * @since 1.7.0 + */ +public class MockAxisHttpResponse extends BasicHttpRequest implements AxisHttpResponse, + OutTransportInfo, MockHTTPResponse { + + private Map<String, String> headers = new HashMap<String, String>(); + private ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + + public MockAxisHttpResponse(RequestLine requestline) { + super(requestline); + } + + /** + * Gets all the headers as a Map of <Header-Name, Header-Value>. + * + * This method can be used in test cases to retrieve all headers written to + * the HttpServletResponse. + * + * @return the headers + */ + public Map<String, String> getHeaders() { + return headers; + } + + public void setStatus(int sc) { + + } + + public void sendError(int sc, String msg) { + + } + + public void sendError(int sc) { + } + + public void setContentType(String contentType) { + + } + + public OutputStream getOutputStream() { + return null; + } + + public void setDateHeader(String name, long date) { + headers.remove(name); + headers.put(name, new Date(date).toString()); + + } + + public void addDateHeader(String name, long date) { + headers.put(name, new Date(date).toString()); + + } + + public void setHeader(String name, String value) { + headers.remove(name); + headers.put(name, value); + } + + public void addHeader(String name, String value) { + headers.put(name, value); + + } + + public void setIntHeader(String name, int value) { + headers.remove(name); + headers.put(name, String.valueOf(value)); + + } + + public void addIntHeader(String name, int value) { + headers.put(name, String.valueOf(value)); + } + + public ByteArrayOutputStream getByteArrayOutputStream() { + return byteArrayOutputStream; + } + +} \ No newline at end of file Propchange: axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/MockAxisHttpResponse.java ------------------------------------------------------------------------------ svn:eol-style = native Added: axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/MockHTTPResponse.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/MockHTTPResponse.java?rev=1183316&view=auto ============================================================================== --- axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/MockHTTPResponse.java (added) +++ axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/MockHTTPResponse.java Fri Oct 14 11:42:02 2011 @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.axis2.transport.http.mock; + +import java.io.ByteArrayOutputStream; +import java.util.Map; + +/** + * The Interface MockHTTPResponse. + * + * @since 1.7.0 + */ +public interface MockHTTPResponse { + + /** + * Gets all the headers as a Map of <Header-Name, Header-Value>. + * + * This method can be used in test cases to retrieve all headers written to + * the HttpServletResponse. + * + * @return the headers + */ + public Map<String, String> getHeaders(); + + /** + * HTTP response write to a internal ByteArrayOutputStream and possible to + * retrieve written content using this method. + * + * @return tByteArrayOutputStream + */ + public ByteArrayOutputStream getByteArrayOutputStream(); + +} Propchange: axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/MockHTTPResponse.java ------------------------------------------------------------------------------ svn:eol-style = native Added: axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/MockHttpServletResponse.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/MockHttpServletResponse.java?rev=1183316&view=auto ============================================================================== --- axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/MockHttpServletResponse.java (added) +++ axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/MockHttpServletResponse.java Fri Oct 14 11:42:02 2011 @@ -0,0 +1,179 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.axis2.transport.http.mock; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.util.Date; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +import javax.servlet.ServletOutputStream; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletResponse; + +import org.apache.axis2.transport.OutTransportInfo; + +/** + * The Class MockHttpServletResponse is a mock implementation of + * HttpServletResponse to used with unit tests. + * + * @since 1.7.0 + */ +public class MockHttpServletResponse implements HttpServletResponse, OutTransportInfo, MockHTTPResponse { + + private String ContentType; + private int ContentLength; + private OutputStream outStream; + private boolean committed; + private Map<String, String> headers; + private ByteArrayOutputStream byteArrayOutputStream; + + public MockHttpServletResponse() { + headers = new HashMap<String, String>(); + byteArrayOutputStream = new ByteArrayOutputStream(); + } + + public ByteArrayOutputStream getByteArrayOutputStream(){ + return byteArrayOutputStream; + } + + public Map<String, String> getHeaders() { + return headers; + } + + public String getCharacterEncoding() { + return null; + } + + public ServletOutputStream getOutputStream() throws IOException { + return (ServletOutputStream) outStream; + } + + public PrintWriter getWriter() throws IOException { + return null; + } + + public int getBufferSize() { + return 0; + } + + public void flushBuffer() throws IOException { + } + + public boolean isCommitted() { + return committed; + } + + public void reset() { + } + + public Locale getLocale() { + return null; + } + + public void resetBuffer() { + } + + public void setContentLength(int len) { + this.ContentLength = len; + } + + public void setContentType(String type) { + this.ContentType = type; + } + + public void setBufferSize(int size) { + + } + + public void setLocale(Locale loc) { + + } + + public void addCookie(Cookie cookie) { + + } + + public boolean containsHeader(String name) { + return headers.containsKey(name); + } + + public String encodeURL(String url) { + return null; + } + + public String encodeRedirectURL(String url) { + return null; + } + + public String encodeUrl(String url) { + return null; + } + + public String encodeRedirectUrl(String url) { + return null; + } + + public void sendError(int sc, String msg) throws IOException { + } + + public void sendError(int sc) throws IOException { + } + + public void sendRedirect(String location) throws IOException { + } + + public void setDateHeader(String name, long date) { + headers.remove(name); + headers.put(name, new Date(date).toString()); + } + + public void addDateHeader(String name, long date) { + headers.put(name, new Date(date).toString()); + } + + public void setHeader(String name, String value) { + headers.remove(name); + headers.put(name, value); + } + + public void addHeader(String name, String value) { + headers.put(name, value); + } + + public void setIntHeader(String name, int value) { + headers.remove(name); + headers.put(name, String.valueOf(value)); + } + + public void addIntHeader(String name, int value) { + headers.put(name, String.valueOf(value)); + } + + public void setStatus(int sc) { + } + + public void setStatus(int sc, String sm) { + } +} \ No newline at end of file Propchange: axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/MockHttpServletResponse.java ------------------------------------------------------------------------------ svn:eol-style = native