1 package org.jmage.mapper;
2
3 import org.jmage.JmageException;
4 import org.jmage.ImageRequest;
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.io.ByteArrayInputStream;
15 import java.util.Arrays;
16 import java.util.Enumeration;
17 import java.util.Vector;
18 import java.net.URI;
19
20 /***
21 * ServletRequestMapperTests
22 */
23 public class ServletRequestMapperTests extends MockObjectTestCase {
24
25 protected ServletMapper servletMapper;
26 protected Mock mockHttpServletRequest;
27 protected Mock mockHttpServletResponse;
28 protected Mock mockServletContext;
29 protected org.jmock.cglib.Mock mockServletOutputStream;
30
31 protected void setUp() throws Exception {
32 servletMapper = new ServletMapper() {
33 protected ImageRequestMapper imageRequestMapper;
34
35 public ServletContext getServletContext() {
36 return (ServletContext) mockServletContext.proxy();
37 }
38
39 public ImageRequestMapper getImageRequestMapper() {
40 if (imageRequestMapper == null) {
41 imageRequestMapper = new ImageRequestMapper() {
42 public byte[] processRequest() throws JmageException {
43 return new byte[0];
44 }
45 };
46 }
47 return imageRequestMapper;
48 }
49 };
50 mockHttpServletRequest = new Mock(HttpServletRequest.class);
51 mockHttpServletResponse = new Mock(HttpServletResponse.class);
52 mockServletOutputStream = new org.jmock.cglib.Mock(ServletOutputStream.class);
53 mockServletContext = new Mock(ServletContext.class);
54
55 this.setExpectations();
56 }
57
58 public void testService() throws ServletException, IOException {
59 servletMapper.init();
60 servletMapper.service((HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy());
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
69 protected void setExpectations() {
70 Enumeration paramNames = new Vector(Arrays.asList(new String[]{"chain", "image", "image_uri", "encode", "noop__Property"})).elements();
71 mockHttpServletRequest.expects(once()).method("getParameterNames").withNoArguments().will(returnValue(paramNames));
72 mockHttpServletRequest.expects(atLeastOnce()).method("getParameter").with(eq("noop__Property")).will(returnValue("noopValue"));
73 mockHttpServletRequest.expects(atLeastOnce()).method("getParameter").with(eq("chain")).will(returnValue("org.jmage.filter.NoOpFilter"));
74 mockHttpServletRequest.expects(atLeastOnce()).method("getParameter").with(eq("image")).will(returnValue("file:///sample/june_vase.jpg"));
75 mockHttpServletRequest.expects(atLeastOnce()).method("getParameter").with(eq("image_uri")).will(returnValue("file:///sample/june_vase.jpg"));
76 mockHttpServletRequest.expects(atLeastOnce()).method("getParameter").with(eq("encode")).will(returnValue("jpg"));
77 mockHttpServletResponse.expects(once()).method("setContentType").with(eq("image/jpeg")).isVoid();
78 mockHttpServletResponse.expects(once()).method("setHeader").with(eq("Content-disposition"), eq("filename=image_-875714787.jpg")).isVoid();
79 mockHttpServletResponse.expects(atLeastOnce()).method("getOutputStream").withNoArguments().will(returnValue((ServletOutputStream) mockServletOutputStream.proxy()));
80
81 mockServletContext.expects(once()).method("getMimeType").with(eq(".jpg")).will(returnValue("image/jpeg"));
82 mockServletOutputStream.expects(once()).method("write").with(eq(new byte[0])).isVoid();
83 mockServletOutputStream.expects(once()).method("flush").withNoArguments().isVoid();
84 }
85 }