1 package org.apache.velocity.tools.view;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.junit.Assert.*;
23 import static org.easymock.EasyMock.*;
24
25 import java.io.IOException;
26 import java.io.StringWriter;
27
28 import javax.servlet.ServletContext;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31
32 import org.apache.velocity.Template;
33 import org.apache.velocity.context.Context;
34 import org.apache.velocity.exception.MethodInvocationException;
35 import org.apache.velocity.exception.ParseErrorException;
36 import org.apache.velocity.exception.ResourceNotFoundException;
37 import org.junit.Test;
38
39
40
41
42
43 public class VelocityViewTest
44 {
45
46
47
48
49
50
51
52
53
54 @Test
55 public void testGetTemplateHttpServletRequestHttpServletResponse() throws ResourceNotFoundException, ParseErrorException, MethodInvocationException, IOException
56 {
57 JeeConfig config = createMock(JeeConfig.class);
58 ServletContext servletContext = createMock(ServletContext.class);
59 HttpServletRequest request = createMock(HttpServletRequest.class);
60 HttpServletResponse response = createMock(HttpServletResponse.class);
61 Context context = createMock(Context.class);
62
63 expect(config.getServletContext()).andReturn(servletContext);
64 expect(config.findInitParameter(VelocityView.DEPRECATION_SUPPORT_MODE_KEY)).andReturn("false");
65 expect(config.findInitParameter(VelocityView.USER_OVERWRITE_KEY)).andReturn(null);
66 expect(config.findInitParameter(VelocityView.LOAD_DEFAULTS_KEY)).andReturn("false");
67 expect(servletContext.getInitParameter(VelocityView.PROPERTIES_KEY)).andReturn(null);
68 expect(config.getInitParameter(VelocityView.PROPERTIES_KEY)).andReturn(null);
69 expect(config.findInitParameter(VelocityView.CLEAN_CONFIGURATION_KEY)).andReturn(null);
70 expect(servletContext.getInitParameter(VelocityView.TOOLS_KEY)).andReturn(null);
71 expect(config.getInitParameter(VelocityView.TOOLS_KEY)).andReturn(null);
72 expect(servletContext.getAttribute(ServletUtils.CONFIGURATION_KEY)).andReturn(null);
73 expect(servletContext.getResourceAsStream(VelocityView.USER_TOOLS_PATH))
74 .andReturn(getClass().getResourceAsStream("/org/apache/velocity/tools/view/tools.xml"));
75
76 expect(request.getAttribute("javax.servlet.include.servlet_path")).andReturn("/charset-test.vm");
77 expect(request.getAttribute("javax.servlet.include.path_info")).andReturn(null);
78
79
80
81
82 replay(config, servletContext, request, response, context);
83 VelocityView view = new VelocityView(config);
84 Template template = view.getTemplate(request);
85 StringWriter writer = new StringWriter();
86 template.merge(context, writer);
87 writer.close();
88 assertTrue(writer.toString().startsWith("Questo è il momento della verità"));
89
90 verify(config, servletContext, request, response, context);
91 }
92
93 }