You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-commits@axis.apache.org by sa...@apache.org on 2011/10/18 13:19:16 UTC

svn commit: r1185577 - in /axis/axis2/java/core/trunk/modules/transport/http: ./ test/org/apache/axis2/transport/http/ test/org/apache/axis2/transport/http/mock/server/

Author: sagara
Date: Tue Oct 18 11:19:15 2011
New Revision: 1185577

URL: http://svn.apache.org/viewvc?rev=1185577&view=rev
Log:
AXIS2-3933 : 
Completed HTTP test framework  so that it can be used to test features of CommonHTTTPTransportSender and HTTPSender. 
Ported Jetty based test case to use new test framework and removed Jetty dependency.   

Added:
    axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/CommonsHTTPTransportSenderClientSideTest.java   (with props)
    axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/HTTPSenderTest.java   (with props)
    axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/
    axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/AbstractHTTPServerTest.java   (with props)
    axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/BasicHttpServer.java   (with props)
    axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/BasicHttpServerImpl.java   (with props)
Modified:
    axis/axis2/java/core/trunk/modules/transport/http/pom.xml
    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/pom.xml
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/transport/http/pom.xml?rev=1185577&r1=1185576&r2=1185577&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/transport/http/pom.xml (original)
+++ axis/axis2/java/core/trunk/modules/transport/http/pom.xml Tue Oct 18 11:19:15 2011
@@ -105,10 +105,5 @@
             <groupId>org.apache.httpcomponents</groupId>
             <artifactId>httpcore</artifactId>
         </dependency>
-        <dependency>
-            <groupId>jetty</groupId>
-            <artifactId>jetty</artifactId>
-            <scope>test</scope>
-        </dependency>
     </dependencies>
 </project>

Added: axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/CommonsHTTPTransportSenderClientSideTest.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/CommonsHTTPTransportSenderClientSideTest.java?rev=1185577&view=auto
==============================================================================
--- axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/CommonsHTTPTransportSenderClientSideTest.java (added)
+++ axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/CommonsHTTPTransportSenderClientSideTest.java Tue Oct 18 11:19:15 2011
@@ -0,0 +1,79 @@
+package org.apache.axis2.transport.http;
+
+import javax.xml.namespace.QName;
+
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMElement;
+import org.apache.axis2.AxisFault;
+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.apache.axis2.transport.OutTransportInfo;
+import org.apache.axis2.transport.http.mock.MockAxisHttpResponse;
+import org.apache.axis2.transport.http.mock.MockHTTPResponse;
+import org.apache.axis2.transport.http.mock.server.AbstractHTTPServerTest;
+import org.apache.axis2.transport.http.mock.server.BasicHttpServer;
+import org.apache.http.ProtocolVersion;
+import org.apache.http.RequestLine;
+import org.apache.http.message.BasicRequestLine;
+
+public class CommonsHTTPTransportSenderClientSideTest extends AbstractHTTPServerTest {
+
+    public void testInvokeWithEPR() throws Exception {
+        RequestLine line = new BasicRequestLine("", "", new ProtocolVersion("http", 1, 0));
+        MockHTTPResponse httpResponse = new MockAxisHttpResponse(line);
+        getBasicHttpServer().setResponseTemplate(BasicHttpServer.RESPONSE_HTTP_OK_LOOP_BACK);
+
+        // We only interested on HTTP message sent to the server side by this
+        // client hence ignore the processing of response at client side.
+        try {
+            httpResponse = (MockAxisHttpResponse) CommonsHTTPTransportSenderTest.configAndRun(
+                    httpResponse, (OutTransportInfo) httpResponse, "http://localhost:8080");
+
+        } catch (Exception e) {
+        }
+        assertEquals("Not the expected HTTP Method", "POST", getHTTPMethod());
+        assertEquals("Not the expected Header value", "application/xml",
+                getHeaders().get("Content-Type"));
+        assertEquals("Not the expected Header value", "custom-value",
+                getHeaders().get("Custom-header"));
+        assertEquals("Not the expected body content", getEnvelope().toString()
+                .replace("utf", "UTF"), getStringContent());
+    }
+     
+    /*
+     * Tests that HTTP connections are properly released when the server returns
+     * a 404 error. This is a regression test for AXIS2-5093.
+     */
+    public void testConnectionReleaseWith404() throws Exception {
+        getBasicHttpServer().setResponseTemplate(BasicHttpServer.RESPONSE_HTTP_404);
+        // If connections are not properly released then we will end up with a
+        // ConnectionPoolTimeoutException here.
+
+        ConfigurationContext configurationContext = ConfigurationContextFactory
+                .createConfigurationContextFromURIs(
+                        CommonsHTTPTransportSenderClientSideTest.class.getResource("axis2.xml"),
+                        null);
+        ServiceClient serviceClient = new ServiceClient(configurationContext, null);
+        Options options = serviceClient.getOptions();
+        options.setTo(new EndpointReference("http://localhost:8080//nonexisting"));
+        OMElement request = OMAbstractFactory.getOMFactory().createOMElement(
+                new QName("urn:test", "test"));
+        // If connections are not properly released then we will end up with a
+        // ConnectionPoolTimeoutException here.
+        for (int i = 0; i < 200; i++) {
+            try {
+                serviceClient.sendReceive(request);
+            } catch (AxisFault ex) {
+                // Check that this is a 404 error
+                assertNull(ex.getCause());
+                assertTrue(ex.getMessage().contains("404"));
+            }
+            serviceClient.cleanupTransport();
+        }
+
+    }
+
+}

Propchange: axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/CommonsHTTPTransportSenderClientSideTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

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=1185577&r1=1185576&r2=1185577&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 Tue Oct 18 11:19:15 2011
@@ -36,9 +36,6 @@ import org.apache.axiom.soap.SOAPEnvelop
 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.apache.axis2.context.MessageContext;
@@ -56,49 +53,9 @@ import org.apache.commons.httpclient.met
 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 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
-     */
-    
-    public void testConnectionReleaseWith404() throws Exception {
-        // Create a Jetty server instance without any contexts. It will always return HTTP 404.
-        Server server = new Server();
-        SocketListener listener = new SocketListener();
-        server.addListener(listener);
-        server.start();
-        try {
-            ConfigurationContext configurationContext =
-                    ConfigurationContextFactory.createConfigurationContextFromURIs(
-                            CommonsHTTPTransportSenderTest.class.getResource("axis2.xml"), null);
-            ServiceClient serviceClient = new ServiceClient(configurationContext, null);
-            Options options = serviceClient.getOptions();
-            options.setTo(new EndpointReference("http://localhost:" + listener.getPort() + "/nonexisting"));
-            OMElement request = OMAbstractFactory.getOMFactory().createOMElement(new QName("urn:test", "test"));
-            // If connections are not properly released then we will end up with a
-            // ConnectionPoolTimeoutException here.
-            for (int i=0; i<200; i++) {
-                try {
-                    serviceClient.sendReceive(request);
-                } catch (AxisFault ex) {
-                    // Check that this is a 404 error
-                    assertNull(ex.getCause());
-                    assertTrue(ex.getMessage().contains("404"));
-                }
-                serviceClient.cleanupTransport();
-            }
-        } finally {
-            server.stop();
-        }
-    }    
-
     public void testInvokeWithServletBasedOutTransportInfo() throws Exception {
         MockHTTPResponse httpResponse = new MockHttpServletResponse();
         ServletBasedOutTransportInfo info = new ServletBasedOutTransportInfo(
@@ -128,34 +85,7 @@ public class CommonsHTTPTransportSenderT
         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();
@@ -176,8 +106,8 @@ public class CommonsHTTPTransportSenderT
         sender.init(confContext, transportOut);
 
     }
-    
-    private MockHTTPResponse configAndRun(MockHTTPResponse outResponse,
+
+    public static MockHTTPResponse configAndRun(MockHTTPResponse outResponse,
             OutTransportInfo outTransportInfo, String epr) throws Exception {
         MockHTTPResponse response = outResponse;
         ConfigurationContext confContext = ConfigurationContextFactory
@@ -213,7 +143,7 @@ public class CommonsHTTPTransportSenderT
 
     }
     
-    private SOAPEnvelope getEnvelope() throws IOException, MessagingException {
+    static SOAPEnvelope getEnvelope() throws IOException, MessagingException {
         SOAPFactory soapFac = OMAbstractFactory.getSOAP11Factory();
         OMFactory omFac = OMAbstractFactory.getOMFactory();
         SOAPEnvelope enp = soapFac.createSOAPEnvelope();

Added: axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/HTTPSenderTest.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/HTTPSenderTest.java?rev=1185577&view=auto
==============================================================================
--- axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/HTTPSenderTest.java (added)
+++ axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/HTTPSenderTest.java Tue Oct 18 11:19:15 2011
@@ -0,0 +1,240 @@
+/*
+ * 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;
+
+import java.io.IOException;
+import java.net.URL;
+
+import javax.mail.MessagingException;
+import javax.ws.rs.core.HttpHeaders;
+
+import org.apache.axis2.Constants;
+import org.apache.axis2.context.ConfigurationContext;
+import org.apache.axis2.context.ConfigurationContextFactory;
+import org.apache.axis2.context.MessageContext;
+import org.apache.axis2.context.OperationContext;
+import org.apache.axis2.transport.http.mock.server.AbstractHTTPServerTest;
+
+/**
+ * The Class HTTPSenderTest.
+ */
+public class HTTPSenderTest extends AbstractHTTPServerTest {
+   
+    private HTTPSender httpSender;
+
+    /**
+     * Send via http.
+     *
+     * @param httpMethod the http method
+     * @param soapAction the soap action
+     * @param address the address
+     * @param rest the rest
+     * @throws IOException Signals that an I/O exception has occurred.
+     * @throws MessagingException the messaging exception
+     */
+    private void sendViaHTTP(String httpMethod, String soapAction, String address, boolean rest)
+            throws IOException, MessagingException {
+        httpSender = new HTTPSender();
+        MessageContext msgContext = new MessageContext();
+        ConfigurationContext configContext = ConfigurationContextFactory
+                .createEmptyConfigurationContext();
+        OperationContext opContext = new OperationContext();
+        
+        msgContext.setConfigurationContext(configContext);
+        msgContext.setEnvelope(getEnvelope());
+        msgContext.setDoingREST(rest);
+        msgContext.setProperty(Constants.Configuration.HTTP_METHOD, httpMethod);      
+        msgContext.setOperationContext(opContext );
+        URL url = new URL(address);        
+        httpSender.send(msgContext, url, soapAction);
+       
+
+    }
+
+    /**
+     * Test send via get.
+     *
+     * @throws Exception the exception
+     */
+    public void testSendViaGet() throws Exception {
+        sendViaHTTP(Constants.Configuration.HTTP_METHOD_GET, "urn:getService",
+                "http://localhost:8080/getService", true);
+        assertEquals("Not the expected HTTP Method", Constants.Configuration.HTTP_METHOD_GET,
+                getHTTPMethod());
+        assertEquals("Not the expected content", "/getService?part=sample%20data",
+                getStringContent());
+        assertEquals("Not the expected HTTP Header value", "urn:getService",
+                getHeaders().get("SOAPAction"));
+        assertEquals("Not the expected HTTP Header value",
+                "application/x-www-form-urlencoded;action=\"urn:getService\";",
+                getHeaders().get(HttpHeaders.CONTENT_TYPE));
+        assertEquals("Not the expected HTTP Header value", "localhost:8080",
+                getHeaders().get(HttpHeaders.HOST));
+        assertEquals("Not the expected HTTP Header value", "Axis2",
+                getHeaders().get(HttpHeaders.USER_AGENT));
+    }
+    
+    /**
+     * Test send via post.
+     *
+     * @throws Exception the exception
+     */
+    public void testSendViaPost() throws Exception {
+        // test with REST payload
+        sendViaHTTP(Constants.Configuration.HTTP_METHOD_POST, "urn:postService",
+                "http://localhost:8080/postService", true);
+        assertEquals("Not the expected HTTP Method", Constants.Configuration.HTTP_METHOD_POST,
+                getHTTPMethod());
+        assertEquals("Not the expected content", getEnvelope().getFirstElement().getFirstElement()
+                .toString(), getStringContent());
+        assertEquals("Not the expected HTTP Header value", "urn:postService",
+                getHeaders().get("SOAPAction"));
+        assertEquals("Not the expected HTTP Header value", "application/xml",
+                getHeaders().get(HttpHeaders.CONTENT_TYPE));
+        assertEquals("Not the expected HTTP Header value", "localhost:8080",
+                getHeaders().get(HttpHeaders.HOST));
+        assertEquals("Not the expected HTTP Header value", "Axis2",
+                getHeaders().get(HttpHeaders.USER_AGENT));
+
+        // test with SOAP payload.
+        sendViaHTTP(Constants.Configuration.HTTP_METHOD_POST, "urn:postService",
+                "http://localhost:8080/postService", false);
+        assertEquals("Not the expected HTTP Method", Constants.Configuration.HTTP_METHOD_POST,
+                getHTTPMethod());
+        assertEquals("Not the expected content", getEnvelope().toString(), getStringContent());
+        assertEquals("Not the expected HTTP Header value", "urn:postService",
+                getHeaders().get("SOAPAction").replace("\"", ""));
+        assertEquals("Not the expected HTTP Header value", "text/xml",
+                getHeaders().get(HttpHeaders.CONTENT_TYPE));
+        assertEquals("Not the expected HTTP Header value", "localhost:8080",
+                getHeaders().get(HttpHeaders.HOST));
+        assertEquals("Not the expected HTTP Header value", "Axis2",
+                getHeaders().get(HttpHeaders.USER_AGENT));
+    }
+
+    /**
+     * Test send via put.
+     *
+     * @throws Exception the exception
+     */
+    public void testSendViaPut() throws Exception {
+        // test with REST payload
+        sendViaHTTP(Constants.Configuration.HTTP_METHOD_PUT, "urn:putService",
+                "http://localhost:8080/putService", true);
+        assertEquals("Not the expected HTTP Method", Constants.Configuration.HTTP_METHOD_PUT,
+                getHTTPMethod());
+        assertEquals("Not the expected content", getEnvelope().getFirstElement().getFirstElement()
+                .toString(), getStringContent());
+        assertEquals("Not the expected HTTP Header value", "urn:putService",
+                getHeaders().get("SOAPAction"));
+        assertEquals("Not the expected HTTP Header value", "application/xml",
+                getHeaders().get(HttpHeaders.CONTENT_TYPE));
+        assertEquals("Not the expected HTTP Header value", "localhost:8080",
+                getHeaders().get(HttpHeaders.HOST));
+        assertEquals("Not the expected HTTP Header value", "Axis2",
+                getHeaders().get(HttpHeaders.USER_AGENT));
+
+        // test with SOAP payload.
+        sendViaHTTP(Constants.Configuration.HTTP_METHOD_PUT, "urn:putService",
+                "http://localhost:8080/putService", false);
+        assertEquals("Not the expected HTTP Method", Constants.Configuration.HTTP_METHOD_PUT,
+                getHTTPMethod());
+        assertEquals("Not the expected content", getEnvelope().toString(), getStringContent());
+        assertEquals("Not the expected HTTP Header value", "urn:putService",
+                getHeaders().get("SOAPAction").replace("\"", ""));
+        assertEquals("Not the expected HTTP Header value", "text/xml",
+                getHeaders().get(HttpHeaders.CONTENT_TYPE));
+        assertEquals("Not the expected HTTP Header value", "localhost:8080",
+                getHeaders().get(HttpHeaders.HOST));
+        assertEquals("Not the expected HTTP Header value", "Axis2",
+                getHeaders().get(HttpHeaders.USER_AGENT));
+    }
+
+    /**
+     * Test send via delete.
+     *
+     * @throws Exception the exception
+     */
+    public void testSendViaDelete() throws Exception {
+        // test with REST payload
+        sendViaHTTP(Constants.Configuration.HTTP_METHOD_DELETE, "urn:deleteService",
+                "http://localhost:8080/deleteService", true);
+        assertEquals("Not the expected HTTP Method", Constants.Configuration.HTTP_METHOD_DELETE,
+                getHTTPMethod());
+        assertEquals("Not the expected content", "/deleteService?part=sample%20data",
+                getStringContent());
+        assertEquals("Not the expected HTTP Header value",
+                "application/x-www-form-urlencoded;action=\"urn:deleteService\";", getHeaders()
+                        .get(HttpHeaders.CONTENT_TYPE));
+        assertEquals("Not the expected HTTP Header value", "localhost:8080",
+                getHeaders().get(HttpHeaders.HOST));
+        assertEquals("Not the expected HTTP Header value", "Axis2",
+                getHeaders().get(HttpHeaders.USER_AGENT));
+
+    }
+
+    /**
+     * Test send via head.
+     *
+     * @throws Exception the exception
+     */
+    public void testSendViaHead() throws Exception {
+
+        sendViaHTTP(Constants.Configuration.HTTP_METHOD_HEAD, "urn:deleteService",
+                "http://localhost:8080/deleteService", true);
+        assertEquals("Not the expected HTTP Method", Constants.Configuration.HTTP_METHOD_POST,
+                getHTTPMethod());
+        assertEquals("Not the expected content", getEnvelope().getFirstElement().getFirstElement()
+                .toString(), getStringContent());
+        assertEquals("Not the expected HTTP Header value", "application/xml",
+                getHeaders().get(HttpHeaders.CONTENT_TYPE));
+        assertEquals("Not the expected HTTP Header value", "localhost:8080",
+                getHeaders().get(HttpHeaders.HOST));
+        assertEquals("Not the expected HTTP Header value", "Axis2",
+                getHeaders().get(HttpHeaders.USER_AGENT));
+
+    }
+
+    /**
+     * Test send nohttp method.
+     *
+     * @throws Exception the exception
+     */
+    public void testSendNOHTTPMethod() throws Exception {
+
+        sendViaHTTP(null, "urn:noService", "http://localhost:8080/noService", true);
+        assertEquals("Not the expected HTTP Method", Constants.Configuration.HTTP_METHOD_POST,
+                getHTTPMethod());
+        assertEquals("Not the expected content", getEnvelope().getFirstElement().getFirstElement()
+                .toString(), getStringContent());
+        assertEquals("Not the expected HTTP Header value", "urn:noService",
+                getHeaders().get("SOAPAction"));
+        assertEquals("Not the expected HTTP Header value", "application/xml",
+                getHeaders().get(HttpHeaders.CONTENT_TYPE));
+        assertEquals("Not the expected HTTP Header value", "localhost:8080",
+                getHeaders().get(HttpHeaders.HOST));
+        assertEquals("Not the expected HTTP Header value", "Axis2",
+                getHeaders().get(HttpHeaders.USER_AGENT));
+
+    }   
+    
+
+   
+}

Propchange: axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/HTTPSenderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/AbstractHTTPServerTest.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/AbstractHTTPServerTest.java?rev=1185577&view=auto
==============================================================================
--- axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/AbstractHTTPServerTest.java (added)
+++ axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/AbstractHTTPServerTest.java Tue Oct 18 11:19:15 2011
@@ -0,0 +1,166 @@
+/*
+ * 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.server;
+
+import java.io.IOException;
+import java.util.Map;
+
+import javax.mail.MessagingException;
+import javax.xml.namespace.QName;
+
+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 junit.framework.TestCase;
+
+/**
+ * The Class AbstractHTTPServerTest can be used to write any test case that
+ * require HTTP back end server features. This class take care about start the
+ * server at each test run and stop after the test execution.
+ * AbstractHTTPServerTest also provide number of utility method to query server
+ * side details. By default this use HTTPcore based BasicHttpServerImpl as the
+ * HTTP server but it is possible to provide other Implementations using
+ * setBasicHttpServer() method.
+ * 
+ * @since 1.7.0
+ */
+public abstract class AbstractHTTPServerTest extends TestCase {
+
+    private BasicHttpServer basicHttpServer;
+
+    /**
+     * Gets the basic http server.
+     * 
+     * @return the basic http server
+     */
+    protected BasicHttpServer getBasicHttpServer() {
+        return basicHttpServer;
+    }
+
+    /**
+     * Sets the basic http server.
+     * 
+     * @param basicHttpServer
+     *            the new basic http server
+     */
+    protected void setBasicHttpServer(BasicHttpServer basicHttpServer) {
+        this.basicHttpServer = basicHttpServer;
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        if (basicHttpServer == null) {
+            setBasicHttpServer(new BasicHttpServerImpl());
+            basicHttpServer.start();
+        }
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see junit.framework.TestCase#tearDown()
+     */
+    @Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+        basicHttpServer.stop();
+        basicHttpServer = null;
+    }
+
+    /**
+     * Return all the HTTP Headers received by the server as a Java Map
+     * instance.
+     * 
+     * @return the headers
+     */
+    protected Map<String, String> getHeaders() {
+        return basicHttpServer.getHeaders();
+
+    }
+
+    /**
+     * Gets the string content.
+     * 
+     * @return the string content
+     */
+    protected String getStringContent() {
+        return new String(basicHttpServer.getContent());
+    }
+
+    /**
+     * Gets the bytes content.
+     * 
+     * @return the bytes content
+     */
+    protected byte[] getBytesContent() {
+        return basicHttpServer.getContent();
+    }
+
+    /**
+     * Gets the hTTP method.
+     * 
+     * @return the hTTP method
+     */
+    protected String getHTTPMethod() {
+        return basicHttpServer.getMethod();
+    }
+
+    /**
+     * Gets the request url.
+     * 
+     * @return the request url
+     */
+    protected String getRequestURL() {
+        return basicHttpServer.getUrl();
+
+    }
+
+    /**
+     * Gets the envelope with sample data.
+     * 
+     * @return the envelope
+     * @throws IOException
+     *             Signals that an I/O exception has occurred.
+     * @throws MessagingException
+     *             the messaging exception
+     */
+    public static 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;
+    }
+
+}

Propchange: axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/AbstractHTTPServerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/BasicHttpServer.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/BasicHttpServer.java?rev=1185577&view=auto
==============================================================================
--- axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/BasicHttpServer.java (added)
+++ axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/BasicHttpServer.java Tue Oct 18 11:19:15 2011
@@ -0,0 +1,145 @@
+/*
+ * 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.server;
+
+import java.util.Map;
+
+/**
+ * The interface BasicHttpServer defines basic operations for a HTTPServer that
+ * can be used in test cases. Implementation of this interface can be real
+ * HTTPserver or can be a mock implementation or wrap 3rd party HTTP server
+ * based on test cases requirements.
+ * 
+ * @since 1.7.0
+ */
+public interface BasicHttpServer {
+
+    /**
+     * Start the HTTP server.
+     * 
+     * @throws Exception
+     *             the exception
+     */
+    public void start() throws Exception;
+
+    /**
+     * Stop the HTTP server.
+     * 
+     * @throws Exception
+     *             the exception
+     */
+    public void stop() throws Exception;
+
+    /**
+     * Return all the HTTP Headers received by the server as a Java Map
+     * instance.
+     * 
+     * @return the headers
+     */
+    public Map<String, String> getHeaders();
+
+    /**
+     * Return HTTP message content received by server as a byte array.
+     * 
+     * @return the content
+     */
+    public byte[] getContent();
+
+    /**
+     * Gets the HTTP method.
+     * 
+     * @return the method
+     */
+    public String getMethod();
+
+    /**
+     * Gets the request url.
+     * 
+     * @return the url
+     */
+    public String getUrl();
+
+    /**
+     * Sets the headers.
+     * 
+     * @param headers
+     *            the headers
+     */
+    public void setHeaders(Map<String, String> headers);
+
+    /**
+     * Sets the content.
+     * 
+     * @param entityContent
+     *            the new content
+     */
+    public void setContent(byte[] entityContent);
+
+    /**
+     * Sets the method.
+     * 
+     * @param method
+     *            the new method
+     */
+    public void setMethod(String method);
+
+    /**
+     * Sets the url.
+     * 
+     * @param url
+     *            the new url
+     */
+    public void setUrl(String url);
+
+    /**
+     * Gets the entity content length.
+     * 
+     * @return the entity content length
+     */
+    public int getEntityContentLength();
+
+    /**
+     * Sets the response template.
+     * 
+     * @param responseTemplate
+     *            the new response template
+     */
+    public void setResponseTemplate(String responseTemplate);
+
+    /**
+     * Gets the response template.
+     * 
+     * @return the response template
+     */
+    public String getResponseTemplate();
+
+    /**
+     * Sets the close manully.
+     * 
+     * @param close
+     *            the new close manully
+     */
+    public void setCloseManully(boolean close);
+
+    public static final String RESPONSE_HTTP_404 = "response.http.404";
+    public static final String RESPONSE_HTTP_OK_XML = "response.http.ok.xml";
+    public static final String RESPONSE_HTTP_OK_LOOP_BACK = "response.http.ok.loop.back";
+
+}

Propchange: axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/BasicHttpServer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/BasicHttpServerImpl.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/BasicHttpServerImpl.java?rev=1185577&view=auto
==============================================================================
--- axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/BasicHttpServerImpl.java (added)
+++ axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/BasicHttpServerImpl.java Tue Oct 18 11:19:15 2011
@@ -0,0 +1,373 @@
+/*
+ * 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.server;
+
+import java.io.IOException;
+import java.io.InterruptedIOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+
+import org.apache.http.ConnectionClosedException;
+import org.apache.http.Header;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpEntityEnclosingRequest;
+import org.apache.http.HttpException;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpServerConnection;
+import org.apache.http.HttpStatus;
+import org.apache.http.entity.ContentProducer;
+import org.apache.http.entity.EntityTemplate;
+import org.apache.http.impl.DefaultConnectionReuseStrategy;
+import org.apache.http.impl.DefaultHttpResponseFactory;
+import org.apache.http.impl.DefaultHttpServerConnection;
+import org.apache.http.message.BasicHttpRequest;
+import org.apache.http.params.BasicHttpParams;
+import org.apache.http.params.CoreConnectionPNames;
+import org.apache.http.params.HttpParams;
+import org.apache.http.params.CoreProtocolPNames;
+import org.apache.http.protocol.BasicHttpProcessor;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.BasicHttpContext;
+import org.apache.http.protocol.HttpRequestHandler;
+import org.apache.http.protocol.HttpRequestHandlerRegistry;
+import org.apache.http.protocol.HttpService;
+import org.apache.http.protocol.ResponseConnControl;
+import org.apache.http.protocol.ResponseContent;
+import org.apache.http.protocol.ResponseDate;
+import org.apache.http.protocol.ResponseServer;
+import org.apache.http.util.EntityUtils;
+
+/**
+ * The purpose of this server application is facilitate to HTTP related test
+ * cases as a back end server based on httpcore. Original code copied from
+ * ElementalHttpServer class from httpcore component of Apache HTTPComponents
+ * project.
+ * 
+ * @see http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/httpcore/src
+ *      /examples/org/apache/http/examples/ElementalHttpServer.java
+ * @since 1.7.0
+ * 
+ */
+public class BasicHttpServerImpl implements BasicHttpServer {
+
+    private Thread serverThread;
+    private Map<String, String> headers;
+    private byte[] content;
+    private String method;
+    private String url;
+    private String responseTemplate;
+    boolean close;
+
+    public BasicHttpServerImpl() {
+        headers = new HashMap<String, String>();
+        content = null;
+        close = true;
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see org.apache.axis2.transport.http.mock.server.BasicHttpServer#start()
+     */
+    public void start() throws Exception {
+        serverThread = new RequestListenerThread(8080, this);
+        serverThread.setDaemon(false);
+        serverThread.start();
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see org.apache.axis2.transport.http.mock.server.BasicHttpServer#stop()
+     */
+    public void stop() throws Exception {
+        if (close) {
+            ((RequestListenerThread) serverThread).getServersocket().close();
+        }
+
+    }
+
+    public Map<String, String> getHeaders() {
+        return headers;
+    }
+
+    public byte[] getContent() {
+        return content;
+    }
+
+    public String getMethod() {
+        return method;
+    }
+
+    public String getUrl() {
+        return url;
+    }
+
+    public void setHeaders(Map<String, String> headers) {
+    }
+
+    public void setContent(byte[] content) {
+        this.content = content;
+    }
+
+    public void setMethod(String method) {
+        this.method = method;
+    }
+
+    public void setUrl(String url) {
+        this.url = url;
+    }
+
+    public int getEntityContentLength() {
+        return content.length;
+    }
+
+    public String getResponseTemplate() {
+        return responseTemplate;
+    }
+
+    public void setResponseTemplate(String responseTemplate) {
+        this.responseTemplate = responseTemplate;
+    }
+
+    public void setCloseManully(boolean close) {
+
+        this.close = close;
+
+    }
+
+    static class HttpServiceHandler implements HttpRequestHandler {
+
+        BasicHttpServer server;
+
+        public HttpServiceHandler(BasicHttpServer server) {
+            this.server = server;
+        }
+
+        /*
+         * (non-Javadoc)
+         * 
+         * @see
+         * org.apache.http.protocol.HttpRequestHandler#handle(org.apache.http
+         * .HttpRequest, org.apache.http.HttpResponse,
+         * org.apache.http.protocol.HttpContext)
+         */
+        public void handle(final HttpRequest request, final HttpResponse response,
+                final HttpContext context) throws HttpException, IOException {
+
+            server.setMethod(request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH));
+            server.setUrl(request.getRequestLine().getUri());
+
+            // process HTTP Headers
+            for (Header header : request.getAllHeaders()) {
+                server.getHeaders().put(header.getName(), header.getValue());
+            }
+
+            // TODO implement processing for other Entity types
+            if (request instanceof HttpEntityEnclosingRequest) {
+                HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
+                byte[] entityContent = EntityUtils.toByteArray(entity);
+                server.setContent(entityContent);
+            } else if (request instanceof BasicHttpRequest) {
+                BasicHttpRequest bhr = (BasicHttpRequest) request;
+                server.setContent(bhr.getRequestLine().getUri().getBytes());
+            }
+
+            // Handle response based on "responseTemplate"
+            EntityTemplate body = null;
+            if (server.getResponseTemplate() == null
+                    || server.getResponseTemplate().equals(BasicHttpServer.RESPONSE_HTTP_OK_XML)) {
+                response.setStatusCode(HttpStatus.SC_OK);
+                body = new EntityTemplate(new ContentProducer() {
+                    public void writeTo(final OutputStream outstream) throws IOException {
+                        OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
+                        writer.write("<Response>ok<Response>");
+                        writer.flush();
+                    }
+                });
+
+                response.setEntity(body);
+
+            } else if (server.getResponseTemplate().equals(
+                    BasicHttpServer.RESPONSE_HTTP_OK_LOOP_BACK)) {
+                response.setStatusCode(HttpStatus.SC_OK);
+                body = new EntityTemplate(new ContentProducer() {
+                    public void writeTo(final OutputStream outstream) throws IOException {
+                        OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
+                        writer.write(new String(server.getContent()));
+                        writer.flush();
+                    }
+                });
+            } else if (server.getResponseTemplate().equals(BasicHttpServer.RESPONSE_HTTP_404)) {
+                response.setStatusCode(HttpStatus.SC_NOT_FOUND);
+                body = new EntityTemplate(new ContentProducer() {
+
+                    public void writeTo(final OutputStream outstream) throws IOException {
+                        OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
+                        writer.write("<html><body><h1>");
+                        writer.write(" not found - 404");
+                        writer.write("</h1></body></html>");
+                        writer.flush();
+                    }
+
+                });
+            }
+            // TODO - customize to send content type depend on expectations.
+            body.setContentType("text/html; charset=UTF-8");
+            response.setEntity(body);
+        }
+
+    }
+
+    static class RequestListenerThread extends Thread {
+
+        private final ServerSocket serversocket;
+        private final HttpParams params;
+        private final HttpService httpService;
+
+        /**
+         * Instantiates a new request listener thread.
+         * 
+         * @param port
+         *            the port
+         * @param server
+         * @throws IOException
+         *             Signals that an I/O exception has occurred.
+         */
+        public RequestListenerThread(int port, BasicHttpServer server) throws IOException {
+            this.serversocket = new ServerSocket(port);
+            this.params = new BasicHttpParams();
+            // Basic configuration.
+            this.params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)
+                    .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
+                    .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
+                    .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
+                    .setParameter(CoreProtocolPNames.ORIGIN_SERVER, "HttpComponents/1.1");
+
+            BasicHttpProcessor httpproc = new BasicHttpProcessor();
+            httpproc.addInterceptor(new ResponseDate());
+            httpproc.addInterceptor(new ResponseServer());
+            httpproc.addInterceptor(new ResponseContent());
+            httpproc.addInterceptor(new ResponseConnControl());
+
+            // Set up request handlers
+            HttpRequestHandlerRegistry reqistry = new HttpRequestHandlerRegistry();
+            reqistry.register("*", new HttpServiceHandler(server));
+
+            // Set up the HTTP service
+            this.httpService = new HttpService(httpproc, new DefaultConnectionReuseStrategy(),
+                    new DefaultHttpResponseFactory());
+            this.httpService.setParams(this.params);
+            this.httpService.setHandlerResolver(reqistry);
+        }
+
+        /**
+         * Gets the serversocket.
+         * 
+         * @return the serversocket
+         */
+        public ServerSocket getServersocket() {
+            return serversocket;
+        }
+
+        /*
+         * (non-Javadoc)
+         * 
+         * @see java.lang.Thread#run()
+         */
+        public void run() {
+            System.out.println("Listening on port " + this.serversocket.getLocalPort());
+            while (!Thread.interrupted()) {
+                try {
+                    // Set up HTTP connection
+                    Socket socket = this.serversocket.accept();
+                    DefaultHttpServerConnection conn = new DefaultHttpServerConnection();
+                    System.out.println("Incoming connection from " + socket.getInetAddress());
+                    conn.bind(socket, this.params);
+
+                    // Start worker thread
+                    Thread t = new WorkerThread(this.httpService, conn);
+                    t.setDaemon(false);
+                    t.start();
+                } catch (InterruptedIOException ex) {
+                    break;
+                } catch (IOException e) {
+                    System.err.println("I/O error initialising connection thread: "
+                            + e.getMessage());
+                    break;
+                }
+            }
+        }
+    }
+
+    static class WorkerThread extends Thread {
+
+        private final HttpService httpservice;
+        private final HttpServerConnection conn;
+
+        /**
+         * Instantiates a new worker thread.
+         * 
+         * @param httpservice
+         *            the httpservice
+         * @param conn
+         *            the conn
+         */
+        public WorkerThread(final HttpService httpservice, final HttpServerConnection conn) {
+            super();
+            this.httpservice = httpservice;
+            this.conn = conn;
+        }
+
+        /*
+         * (non-Javadoc)
+         * 
+         * @see java.lang.Thread#run()
+         */
+        public void run() {
+            System.out.println("New connection thread");
+            HttpContext context = new BasicHttpContext(null);
+            try {
+                while (!Thread.interrupted() && this.conn.isOpen()) {
+                    this.httpservice.handleRequest(this.conn, context);
+                }
+            } catch (ConnectionClosedException ex) {
+                System.err.println("Client closed connection");
+            } catch (IOException ex) {
+                System.err.println("I/O error: " + ex.getMessage());
+            } catch (HttpException ex) {
+                System.err.println("Unrecoverable HTTP protocol violation: " + ex.getMessage());
+            } finally {
+                try {
+                    this.conn.shutdown();
+                } catch (IOException ignore) {
+                }
+            }
+        }
+
+    }
+
+}

Propchange: axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/BasicHttpServerImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native