View Javadoc

1   /*
2    * $Id: AbstractServletTestCase.java 19270 2010-09-01 10:37:41Z dirk.olmes $
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 org.mule.api.MuleMessage;
14  import org.mule.api.config.MuleProperties;
15  import org.mule.module.client.MuleClient;
16  import org.mule.tck.FunctionalTestCase;
17  import org.mule.transport.http.HttpConnector;
18  import org.mule.transport.http.HttpConstants;
19  import org.mule.transport.servlet.MuleReceiverServlet;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import javax.servlet.ServletContext;
25  
26  import org.mortbay.jetty.Server;
27  import org.mortbay.jetty.servlet.Context;
28  import org.mortbay.jetty.servlet.ServletHolder;
29  
30  public abstract class AbstractServletTestCase extends FunctionalTestCase
31  {
32      public static final int HTTP_PORT = 63088;
33  
34      private Server httpServer;
35      private String context;
36  
37      public AbstractServletTestCase(String context)
38      {
39          super();
40          this.context = context;
41      }
42  
43      @Override
44      protected void doSetUp() throws Exception
45      {
46          super.doSetUp();
47  
48          httpServer = new Server(HTTP_PORT);
49  
50          Context root = new Context(httpServer,"/",Context.SESSIONS);
51          ServletHolder holder = new ServletHolder(MuleReceiverServlet.class);
52          root.addServlet(holder, context);
53  
54          ServletContext context = root.getServletContext();
55          context.setAttribute(MuleProperties.MULE_CONTEXT_PROPERTY, muleContext);
56          
57          httpServer.start();
58          
59  
60      }
61  
62      @Override
63      protected void doTearDown() throws Exception
64      {
65          super.doTearDown();
66          if (httpServer != null && httpServer.isStarted())
67          {
68              httpServer.stop();
69          }
70      }
71  
72      public void testBasic(String root) throws Exception
73      {
74          MuleClient client = new MuleClient(muleContext);
75  
76          MuleMessage result = client.send(root + "/helloworld", "", null);
77          assertEquals((Integer)200, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
78          assertEquals("Hello World", result.getPayloadAsString());
79  
80          result = client.send(root + "/hello", "", null);
81          assertEquals((Integer)404, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
82  
83          Map<String, String> props = new HashMap<String, String>();
84          props.put(HttpConnector.HTTP_METHOD_PROPERTY, HttpConstants.METHOD_GET);
85          result = client.send(root + "/helloworld", "", props);
86          assertEquals((Integer)405, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
87  
88          props.put(HttpConnector.HTTP_METHOD_PROPERTY, HttpConstants.METHOD_DELETE);
89          result = client.send(root + "/helloworld", "", props);
90          assertEquals("Hello World Delete", result.getPayloadAsString());
91          assertEquals((Integer)200, result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0));
92      }
93  
94  }