1   package org.jmage.mapper;
2   
3   import org.jmock.Mock;
4   import org.jmock.MockObjectTestCase;
5   
6   import javax.servlet.*;
7   import javax.servlet.http.HttpServletRequest;
8   import javax.servlet.http.HttpServletResponse;
9   import java.io.IOException;
10  import java.util.Arrays;
11  import java.util.Enumeration;
12  import java.util.Vector;
13  
14  /***
15   * InterceptorMapperTests
16   */
17  public class InterceptorMapperMatchingTest extends MockObjectTestCase {
18      protected InterceptorMapper mapper;
19      protected Mock mockHttpServletRequest;
20      protected Mock mockHttpServletResponse;
21      protected Mock mockFilterConfig;
22      protected Mock mockFilterChain;
23      protected Mock mockServletContext;
24      protected Mock mockRequestDispatcher;
25  
26      protected void setUp() throws Exception {
27          mapper = new InterceptorMapper();
28          mockHttpServletRequest = new Mock(HttpServletRequest.class);
29          mockHttpServletResponse = new Mock(HttpServletResponse.class);
30          mockFilterConfig = new Mock(FilterConfig.class);
31          mockFilterChain = new Mock(FilterChain.class);
32          mockServletContext = new Mock(ServletContext.class);
33          mockRequestDispatcher = new Mock(RequestDispatcher.class);
34  
35          this.setExpectations();
36  
37          mapper.init((FilterConfig) mockFilterConfig.proxy());
38      }
39  
40      public void testMatch() throws IOException, ServletException {
41          mapper.doFilter((ServletRequest) mockHttpServletRequest.proxy(), (ServletResponse) mockHttpServletResponse.proxy(), (FilterChain) mockFilterChain.proxy());
42      }
43  
44      protected void setExpectations() {
45          mockFilterConfig.expects(once()).method("getServletContext").withNoArguments().will(returnValue((ServletContext) mockServletContext.proxy()));
46  
47          mockHttpServletRequest.expects(once()).method("getRequestURL").withNoArguments().will(returnValue(new StringBuffer("http://host.org/context/elisha.gif")));
48          mockHttpServletRequest.expects(once()).method("getHeader").with(eq("JMAGE_INTERNAL")).will(returnValue("FALSE"));
49          mockHttpServletRequest.expects(once()).method("getAttribute").with(eq("JMAGE_FILTERED")).will(returnValue("FALSE"));
50          mockHttpServletRequest.expects(once()).method("setAttribute").with(eq("JMAGE_FILTERED"), eq(Boolean.TRUE)).isVoid();
51          mockHttpServletRequest.expects(once()).method("getAttribute").with(eq("JMAGE_MATCHED")).will(returnValue(Boolean.FALSE));
52          mockHttpServletRequest.expects(once()).method("setAttribute").with(eq("JMAGE_MATCHED"), eq(Boolean.TRUE)).isVoid();
53  
54          //ugh
55          Enumeration filterNames = new Vector(Arrays.asList(new String[]{"context"})).elements();
56          mockFilterConfig.expects(once()).method("getInitParameterNames").withNoArguments().will(returnValue(filterNames));
57  
58          mockFilterConfig.expects(once()).method("getInitParameter").with(eq("context")).will(returnValue("org.jmage.filter.NoOpFilter"));
59          String requestString = "/jmage?image=http://host.org/context/elisha.gif&chain=chain:org.jmage.filter.NoOpFilter";
60          mockServletContext.expects(once()).method("getRequestDispatcher").with(eq(requestString)).will(returnValue((RequestDispatcher) mockRequestDispatcher.proxy()));
61  
62          mockRequestDispatcher.expects(once()).method("forward").with(eq((ServletRequest) mockHttpServletRequest.proxy()), eq((ServletResponse) mockHttpServletResponse.proxy()));
63      }
64  }