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