You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by ap...@apache.org on 2011/01/11 17:09:10 UTC

svn commit: r1057711 - in /velocity/tools/trunk/velocity-tools-view: ./ src/main/java/org/apache/velocity/tools/view/ src/test/java/org/apache/velocity/tools/view/ src/test/resources/ src/test/resources/WEB-INF/

Author: apetrelli
Date: Tue Jan 11 16:09:10 2011
New Revision: 1057711

URL: http://svn.apache.org/viewvc?rev=1057711&view=rev
Log:
VELTOOLS-119
Discarding the response character encoding and using the default behaviour (read input.encoding property).

Added:
    velocity/tools/trunk/velocity-tools-view/src/test/java/org/apache/velocity/tools/view/
    velocity/tools/trunk/velocity-tools-view/src/test/java/org/apache/velocity/tools/view/VelocityViewTest.java
    velocity/tools/trunk/velocity-tools-view/src/test/resources/VM_global_library.vm
    velocity/tools/trunk/velocity-tools-view/src/test/resources/WEB-INF/
    velocity/tools/trunk/velocity-tools-view/src/test/resources/WEB-INF/velocity.properties
    velocity/tools/trunk/velocity-tools-view/src/test/resources/charset-test.vm
Modified:
    velocity/tools/trunk/velocity-tools-view/pom.xml
    velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/VelocityView.java
    velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/VelocityViewServlet.java

Modified: velocity/tools/trunk/velocity-tools-view/pom.xml
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-view/pom.xml?rev=1057711&r1=1057710&r2=1057711&view=diff
==============================================================================
--- velocity/tools/trunk/velocity-tools-view/pom.xml (original)
+++ velocity/tools/trunk/velocity-tools-view/pom.xml Tue Jan 11 16:09:10 2011
@@ -45,5 +45,11 @@
 			<version>4.8.1</version>
 			<scope>test</scope>
 		</dependency>
+		<dependency>
+			<groupId>org.easymock</groupId>
+			<artifactId>easymock</artifactId>
+			<version>3.0</version>
+			<scope>test</scope>
+		</dependency>
 	</dependencies>
 </project>

Modified: velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/VelocityView.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/VelocityView.java?rev=1057711&r1=1057710&r2=1057711&view=diff
==============================================================================
--- velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/VelocityView.java (original)
+++ velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/VelocityView.java Tue Jan 11 16:09:10 2011
@@ -428,7 +428,7 @@ public class VelocityView extends ViewTo
      * Remember that as these configurations are added on top of each other,
      * the newer values will always override the older ones.  Also, once they
      * are all loaded, this method can "clean" your configuration of all invalid
-     * tool, toolbox or data configurations if you set the 
+     * tool, toolbox or data configurations if you set the
      * {@code org.apache.velocity.tools.cleanConfiguration} init-param to true in
      * either your servlet or servletContext init-params.
      */
@@ -732,7 +732,7 @@ public class VelocityView extends ViewTo
     /******************* REQUEST PROCESSING ****************************/
 
     /**
-     * 
+     *
      *
      * @param request  HttpServletRequest object containing client request
      * @param response HttpServletResponse object for the response
@@ -746,7 +746,7 @@ public class VelocityView extends ViewTo
         Context context = createContext(request, response);
 
         // get the template
-        Template template = getTemplate(request, response);
+        Template template = getTemplate(request);
 
         // merge the template and context into the response
         merge(template, context, response.getWriter());
@@ -805,9 +805,18 @@ public class VelocityView extends ViewTo
      */
     public Template getTemplate(HttpServletRequest request)
     {
-        return getTemplate(request, null);
+        String path = ServletUtils.getPath(request);
+        return getTemplate(path);
     }
 
+    /**
+     * <p>Gets the requested template.</p>
+     *
+     * @param request client request
+     * @param response client response.
+     * @return Velocity Template object or null
+     * @deprecated Use {@link #getTemplate(HttpServletRequest)}.
+     */
     public Template getTemplate(HttpServletRequest request,
                                    HttpServletResponse response)
     {
@@ -918,7 +927,7 @@ public class VelocityView extends ViewTo
                 }
                 catch (Exception e)
                 {
-                    getLog().error("Trouble releasing VelocityWriter: " + 
+                    getLog().error("Trouble releasing VelocityWriter: " +
                                    e.getMessage(), e);
                 }
             }

Modified: velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/VelocityViewServlet.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/VelocityViewServlet.java?rev=1057711&r1=1057710&r2=1057711&view=diff
==============================================================================
--- velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/VelocityViewServlet.java (original)
+++ velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/VelocityViewServlet.java Tue Jan 11 16:09:10 2011
@@ -68,7 +68,7 @@ import org.apache.velocity.runtime.log.L
  *   <dt>org.apache.velocity.tools.shared.config</dt>
  *   <dd>By default, this is {@code true}. If set to {@code false}, then
  *     the {@link VelocityView} used by this servlet will not be shared
- *     with {@link VelocityViewFilter}s, other VelocityViewServlets or 
+ *     with {@link VelocityViewFilter}s, other VelocityViewServlets or
  *     {@link org.apache.velocity.tools.view.jsp.VelocityViewTag}s in the
  *     application.</dd>
  *   <dt>org.apache.velocity.tools.loadDefaults</dt>
@@ -293,7 +293,7 @@ public class VelocityViewServlet extends
     protected Template getTemplate(HttpServletRequest request,
                                    HttpServletResponse response)
     {
-        return getVelocityView().getTemplate(request, response);
+        return getVelocityView().getTemplate(request);
     }
 
     protected Template getTemplate(String name)
@@ -344,7 +344,7 @@ public class VelocityViewServlet extends
             getLog().error("Error processing a template for path '" + path + "'", e);
             return;
         }
-        
+
         try
         {
             getLog().error("Error processing a template for path '" + path + "'", e);
@@ -398,7 +398,7 @@ public class VelocityViewServlet extends
     /**
      * Manages the {@link ResourceNotFoundException} to send an HTTP 404 result
      * when needed.
-     * 
+     *
      * @param request The request object.
      * @param response The response object.
      * @param e The exception to check.

Added: velocity/tools/trunk/velocity-tools-view/src/test/java/org/apache/velocity/tools/view/VelocityViewTest.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-view/src/test/java/org/apache/velocity/tools/view/VelocityViewTest.java?rev=1057711&view=auto
==============================================================================
--- velocity/tools/trunk/velocity-tools-view/src/test/java/org/apache/velocity/tools/view/VelocityViewTest.java (added)
+++ velocity/tools/trunk/velocity-tools-view/src/test/java/org/apache/velocity/tools/view/VelocityViewTest.java Tue Jan 11 16:09:10 2011
@@ -0,0 +1,93 @@
+package org.apache.velocity.tools.view;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import static org.junit.Assert.*;
+import static org.easymock.EasyMock.*;
+
+import java.io.IOException;
+import java.io.StringWriter;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.velocity.Template;
+import org.apache.velocity.context.Context;
+import org.apache.velocity.exception.MethodInvocationException;
+import org.apache.velocity.exception.ParseErrorException;
+import org.apache.velocity.exception.ResourceNotFoundException;
+import org.junit.Test;
+
+/**
+ * Tests {@link VelocityView}.
+ *
+ */
+public class VelocityViewTest
+{
+
+    /**
+     * Test method for {@link org.apache.velocity.tools.view.VelocityView#getTemplate(javax.servlet.http.HttpServletRequest)}.
+     * Tests VELTOOLS-119
+     * @throws IOException If something goes wrong.
+     * @throws MethodInvocationException If something goes wrong.
+     * @throws ParseErrorException If something goes wrong.
+     * @throws ResourceNotFoundException If something goes wrong.
+     */
+    @Test
+    public void testGetTemplateHttpServletRequestHttpServletResponse() throws ResourceNotFoundException, ParseErrorException, MethodInvocationException, IOException
+    {
+        JeeConfig config = createMock(JeeConfig.class);
+        ServletContext servletContext = createMock(ServletContext.class);
+        HttpServletRequest request = createMock(HttpServletRequest.class);
+        HttpServletResponse response = createMock(HttpServletResponse.class);
+        Context context = createMock(Context.class);
+
+        expect(config.getServletContext()).andReturn(servletContext);
+        expect(config.findInitParameter(VelocityView.DEPRECATION_SUPPORT_MODE_KEY)).andReturn("false");
+        expect(config.findInitParameter(VelocityView.USER_OVERWRITE_KEY)).andReturn(null);
+        expect(config.findInitParameter(VelocityView.LOAD_DEFAULTS_KEY)).andReturn("false");
+        expect(servletContext.getInitParameter(VelocityView.PROPERTIES_KEY)).andReturn(null);
+        expect(config.getInitParameter(VelocityView.PROPERTIES_KEY)).andReturn(null);
+        expect(config.findInitParameter(VelocityView.CLEAN_CONFIGURATION_KEY)).andReturn(null);
+        expect(servletContext.getInitParameter(VelocityView.TOOLS_KEY)).andReturn(null);
+        expect(config.getInitParameter(VelocityView.TOOLS_KEY)).andReturn(null);
+        expect(servletContext.getAttribute(ServletUtils.CONFIGURATION_KEY)).andReturn(null);
+        expect(servletContext.getResourceAsStream(VelocityView.USER_TOOLS_PATH))
+            .andReturn(getClass().getResourceAsStream("/org/apache/velocity/tools/view/tools.xml"));
+
+        expect(request.getAttribute("javax.servlet.include.servlet_path")).andReturn("/charset-test.vm");
+        expect(request.getAttribute("javax.servlet.include.path_info")).andReturn(null);
+
+        // This was necessary to verify the bug, now it is not called at all.
+        // expect(response.getCharacterEncoding()).andReturn("ISO-8859-1");
+
+        replay(config, servletContext, request, response, context);
+        VelocityView view = new VelocityView(config);
+        Template template = view.getTemplate(request);
+        StringWriter writer = new StringWriter();
+        template.merge(context, writer);
+        writer.close();
+        assertTrue(writer.toString().startsWith("Questo è il momento della verità"));
+
+        verify(config, servletContext, request, response, context);
+    }
+
+}

Added: velocity/tools/trunk/velocity-tools-view/src/test/resources/VM_global_library.vm
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-view/src/test/resources/VM_global_library.vm?rev=1057711&view=auto
==============================================================================
--- velocity/tools/trunk/velocity-tools-view/src/test/resources/VM_global_library.vm (added)
+++ velocity/tools/trunk/velocity-tools-view/src/test/resources/VM_global_library.vm Tue Jan 11 16:09:10 2011
@@ -0,0 +1,27 @@
+#macro( quietnull $a)
+#if($a)$a#end
+#end
+
+#macro( recurse $a )
+  global recurse $a
+  #set( $a = $a - 1)
+  #if ($a > 0)
+    #recurse( $a )
+  #end
+#end
+
+#macro( callrecurse )
+   #set( $count = 5)
+   #recurse( $count )
+#end
+
+#macro( testbool $b )
+ #if($b)
+  arg true
+ #end
+ #if( ! $b )
+  arg false
+ #end
+#end
+
+

Added: velocity/tools/trunk/velocity-tools-view/src/test/resources/WEB-INF/velocity.properties
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-view/src/test/resources/WEB-INF/velocity.properties?rev=1057711&view=auto
==============================================================================
--- velocity/tools/trunk/velocity-tools-view/src/test/resources/WEB-INF/velocity.properties (added)
+++ velocity/tools/trunk/velocity-tools-view/src/test/resources/WEB-INF/velocity.properties Tue Jan 11 16:09:10 2011
@@ -0,0 +1,21 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+input.encoding=UTF-8
+runtime.log.logsystem.class = org.apache.velocity.runtime.log.NullLogChute
+resource.loader = clazz
+clazz.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader

Added: velocity/tools/trunk/velocity-tools-view/src/test/resources/charset-test.vm
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-view/src/test/resources/charset-test.vm?rev=1057711&view=auto
==============================================================================
--- velocity/tools/trunk/velocity-tools-view/src/test/resources/charset-test.vm (added)
+++ velocity/tools/trunk/velocity-tools-view/src/test/resources/charset-test.vm Tue Jan 11 16:09:10 2011
@@ -0,0 +1 @@
+Questo è il momento della verità.