View Javadoc

1   /*
2    * $Id: AbstractServletTestCase.java 22477 2011-07-20 15:27:35Z justin.calleja $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.module.jersey;
12  
13  import static org.junit.Assert.assertEquals;
14  
15  import java.util.HashMap;
16  import java.util.Map;
17  
18  import javax.servlet.ServletContext;
19  
20  import org.mortbay.jetty.Server;
21  import org.mortbay.jetty.servlet.Context;
22  import org.mortbay.jetty.servlet.ServletHolder;
23  import org.mule.api.MuleMessage;
24  import org.mule.api.config.MuleProperties;
25  import org.mule.module.client.MuleClient;
26  import org.mule.tck.AbstractServiceAndFlowTestCase;
27  import org.mule.transport.http.HttpConnector;
28  import org.mule.transport.http.HttpConstants;
29  import org.mule.transport.servlet.MuleReceiverServlet;
30  
31  public abstract class AbstractServletTestCase extends AbstractServiceAndFlowTestCase
32  {
33  
34      //TODO(pablo.kraan): replace with a dynamic endpoint
35      public static final int HTTP_PORT = 63088;
36  
37      private Server httpServer;
38      private String context;
39      
40      public AbstractServletTestCase(ConfigVariant variant, String configResources, String context)
41      {
42          super(variant, configResources);
43          this.context = context;
44      }
45  
46      @Override
47      protected void doSetUp() throws Exception
48      {
49          super.doSetUp();
50  
51          httpServer = new Server(HTTP_PORT);
52  
53          Context root = new Context(httpServer,"/",Context.SESSIONS);
54          ServletHolder holder = new ServletHolder(MuleReceiverServlet.class);
55          root.addServlet(holder, context);
56  
57          ServletContext context = root.getServletContext();
58          context.setAttribute(MuleProperties.MULE_CONTEXT_PROPERTY, muleContext);
59          
60          httpServer.start();
61      }
62  
63      @Override
64      protected void doTearDown() throws Exception
65      {
66          super.doTearDown();
67          if (httpServer != null && httpServer.isStarted())
68          {
69              httpServer.stop();
70          }
71      }
72  
73      public void doTestBasic(String root) throws Exception
74      {
75          MuleClient client = new MuleClient(muleContext);
76  
77          MuleMessage result = client.send(root + "/helloworld", "", null);
78          assertEquals((Integer)200, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
79          assertEquals("Hello World", result.getPayloadAsString());
80  
81          result = client.send(root + "/hello", "", null);
82          assertEquals((Integer)404, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
83  
84          Map<String, String> props = new HashMap<String, String>();
85          props.put(HttpConnector.HTTP_METHOD_PROPERTY, HttpConstants.METHOD_GET);
86          result = client.send(root + "/helloworld", "", props);
87          assertEquals((Integer)405, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
88  
89          props.put(HttpConnector.HTTP_METHOD_PROPERTY, HttpConstants.METHOD_DELETE);
90          result = client.send(root + "/helloworld", "", props);
91          assertEquals("Hello World Delete", result.getPayloadAsString());
92          assertEquals((Integer)200, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
93      }
94  
95  }