1   package org.jmage.mapper;
2   
3   import org.jmage.ImageRequest;
4   import org.jmage.JmageException;
5   import org.jmock.Mock;
6   import org.jmock.MockObjectTestCase;
7   
8   import javax.servlet.ServletContext;
9   import javax.servlet.ServletException;
10  import javax.servlet.ServletOutputStream;
11  import javax.servlet.http.HttpServletRequest;
12  import javax.servlet.http.HttpServletResponse;
13  import java.io.IOException;
14  import java.util.Arrays;
15  import java.util.Enumeration;
16  import java.util.Vector;
17  import java.net.URI;
18  
19  /***
20   * ServletRequestMapperTests with relative resource URIs (i.e. servletcontext)
21   */
22  public class ServletRequestMapperServletResourceTests extends MockObjectTestCase {
23  
24      protected ServletMapper servletMapper;
25      protected Mock mockHttpServletRequest;
26      protected Mock mockHttpServletResponse;
27      protected Mock mockServletContext;
28      protected org.jmock.cglib.Mock mockServletOutputStream;
29  
30      protected void setUp() throws Exception {
31          servletMapper = new ServletMapper() {
32              protected ImageRequestMapper imageRequestMapper;
33  
34              public ServletContext getServletContext() {
35                  return (ServletContext) mockServletContext.proxy();
36              }
37  
38              public ImageRequestMapper getImageRequestMapper() {
39                  if (imageRequestMapper == null) {
40                      imageRequestMapper = new ImageRequestMapper() {
41                          public byte[] processRequest() throws JmageException {
42                              return new byte[0];
43                          }
44                      };
45                  }
46                  return imageRequestMapper;
47              }
48          };
49          mockHttpServletRequest = new Mock(HttpServletRequest.class);
50          mockHttpServletResponse = new Mock(HttpServletResponse.class);
51          mockServletOutputStream = new org.jmock.cglib.Mock(ServletOutputStream.class);
52          mockServletContext = new Mock(ServletContext.class);
53  
54          this.setExpectations();
55      }
56  
57      public void testService() throws ServletException, IOException {
58          servletMapper.init();
59          servletMapper.service((HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy());
60  
61          ImageRequest request = servletMapper.getImageRequestMapper().getImageRequest();
62          assertEquals("encodingformat wrong", request.getEncodingFormat(), "jpg");
63          assertTrue("filterchain wrong size", request.getFilterChainURI().length == 1);
64          assertEquals("filterchain wrong filter", request.getFilterChainURI()[0], URI.create("chain:org.jmage.filter.NoOpFilter"));
65          assertEquals("imageuri wrong", request.getImageURI(), URI.create("file:///sample/june_vase.jpg"));
66      }
67  
68      protected void setExpectations() {
69          Enumeration paramNames = new Vector(Arrays.asList(new String[]{"chain", "image", "image_uri", "encode"})).elements();
70          mockHttpServletRequest.expects(once()).method("getParameterNames").withNoArguments().will(returnValue(paramNames));
71          mockHttpServletRequest.expects(atLeastOnce()).method("getParameter").with(eq("chain")).will(returnValue("org.jmage.filter.NoOpFilter"));
72          mockHttpServletRequest.expects(atLeastOnce()).method("getParameter").with(eq("image")).will(returnValue("sample/june_vase.jpg"));
73          mockHttpServletRequest.expects(atLeastOnce()).method("getParameter").with(eq("image_uri")).will(returnValue("sample/june_vase.jpg"));
74          mockHttpServletRequest.expects(atLeastOnce()).method("getParameter").with(eq("encode")).will(returnValue("jpg"));
75          mockHttpServletResponse.expects(once()).method("setContentType").with(eq("image/jpeg")).isVoid();
76          mockHttpServletResponse.expects(once()).method("setHeader").with(eq("Content-disposition"), eq("filename=image_955583717.jpg")).isVoid();
77          mockHttpServletResponse.expects(atLeastOnce()).method("getOutputStream").withNoArguments().will(returnValue((ServletOutputStream) mockServletOutputStream.proxy()));
78          //expected dummy image back
79          mockServletContext.expects(once()).method("getMimeType").with(eq(".jpg")).will(returnValue("image/jpeg"));
80          mockServletOutputStream.expects(once()).method("write").with(eq(new byte[0])).isVoid();
81          mockServletOutputStream.expects(once()).method("flush").withNoArguments().isVoid();
82      }
83  }