View Javadoc

1   package org.apache.velocity.tools.view;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Tests {@link VelocityView}.
41   *
42   */
43  public class VelocityViewTest
44  {
45  
46      /**
47       * Test method for {@link org.apache.velocity.tools.view.VelocityView#getTemplate(javax.servlet.http.HttpServletRequest)}.
48       * Tests VELTOOLS-119
49       * @throws IOException If something goes wrong.
50       * @throws MethodInvocationException If something goes wrong.
51       * @throws ParseErrorException If something goes wrong.
52       * @throws ResourceNotFoundException If something goes wrong.
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          // This was necessary to verify the bug, now it is not called at all.
80          // expect(response.getCharacterEncoding()).andReturn("ISO-8859-1");
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  }