View Javadoc

1   /*
2    * $Id: CachedServletRequestTestCase.java 22387 2011-07-12 03:53:36Z 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.transport.servlet;
12  
13  import org.mule.api.MuleException;
14  import org.mule.api.config.MuleProperties;
15  import org.mule.api.endpoint.EndpointException;
16  import org.mule.api.transport.MessageReceiver;
17  import org.mule.tck.junit4.AbstractMuleContextTestCase;
18  
19  import java.io.IOException;
20  
21  import javax.servlet.ServletConfig;
22  import javax.servlet.ServletContext;
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.junit.Test;
27  
28  import static org.junit.Assert.assertTrue;
29  import static org.mockito.Matchers.eq;
30  import static org.mockito.Mockito.mock;
31  import static org.mockito.Mockito.when;
32  
33  public class CachedServletRequestTestCase extends AbstractMuleContextTestCase
34  {
35      private static final String CONNECTOR_NAME = "ServletConnector";
36  
37      private ServletContext mockServletContext;
38      private ServletConfig mockServletConfig;
39  
40      @Override
41      protected void doSetUp() throws Exception
42      {
43          super.doSetUp();
44  
45          mockServletContext = mock(ServletContext.class);
46          when(mockServletContext.getAttribute(eq(MuleProperties.MULE_CONTEXT_PROPERTY))).thenReturn(muleContext);
47  
48          mockServletConfig = mock(ServletConfig.class);
49          when(mockServletConfig.getServletContext()).thenReturn(mockServletContext);
50          when(mockServletConfig.getInitParameter(eq(AbstractReceiverServlet.SERVLET_CONNECTOR_NAME_PROPERTY))).thenReturn(CONNECTOR_NAME);
51      }
52  
53      @Test
54      public void testReceiverServletUsingCachedServletRequest() throws Exception
55      {
56          registerServletConnector();
57  
58          SensingMuleReceiverServlet servlet = new SensingMuleReceiverServlet();
59          servlet.init(mockServletConfig);
60  
61          HttpServletRequest mockRequest = createMockRequest();
62          servlet.doGet(mockRequest, null);
63          assertTrue(servlet.isUsingCachedServletRequest());
64      }
65  
66      private void registerServletConnector() throws MuleException
67      {
68          ServletConnector connector = new ServletConnector(muleContext);
69          connector.setName(CONNECTOR_NAME);
70          connector.setUseCachedHttpServletRequest(true);
71          muleContext.getRegistry().registerConnector(connector);
72      }
73  
74      private HttpServletRequest createMockRequest() throws IOException
75      {
76          HttpServletRequest mockRequest = mock(HttpServletRequest.class);
77          when(mockRequest.getInputStream()).thenReturn(new MockServletInputStream());
78          when(mockRequest.getPathInfo()).thenReturn("/foo");
79          return mockRequest;
80      }
81  
82      private static class SensingMuleReceiverServlet extends MuleReceiverServlet
83      {
84          private boolean usingCachedServletRequest = false;
85  
86          public SensingMuleReceiverServlet()
87          {
88              super();
89          }
90  
91          @Override
92          protected MessageReceiver getReceiverForURI(HttpServletRequest request)
93              throws EndpointException
94          {
95              usingCachedServletRequest = (request instanceof CachedHttpServletRequest);
96  
97              // throw an exception here to bypass all the code dealing with Mule internals
98              // (i.e. message receiver, endpoints, routing messages etc.). We have had access to
99              // the object we wanted to verify and that's good enough.
100             throw new AbortControlFlowException();
101         }
102 
103         @Override
104         protected void handleException(Throwable exception, String message, HttpServletResponse response)
105         {
106             assertTrue(exception instanceof AbortControlFlowException);
107         }
108 
109         public boolean isUsingCachedServletRequest()
110         {
111             return usingCachedServletRequest;
112         }
113     }
114 
115     /**
116      * Use a specialized exception to abort the control flow in the test to ensure that no other,
117      * accidentially thrown exception is masked.
118      */
119     private static class AbortControlFlowException extends RuntimeException
120     {
121         public AbortControlFlowException()
122         {
123             super();
124         }
125     }
126 }