You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by nb...@apache.org on 2007/10/31 21:58:22 UTC

svn commit: r590826 - in /velocity/tools/branches/2.x: ./ src/main/java/org/apache/velocity/tools/view/ src/test/java/org/apache/velocity/tools/test/blackbox/

Author: nbubna
Date: Wed Oct 31 13:58:21 2007
New Revision: 590826

URL: http://svn.apache.org/viewvc?rev=590826&view=rev
Log:
VELTOOLS-88 - port changes to 2.x branch

Added:
    velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/blackbox/LinkToolTests.java   (with props)
    velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/blackbox/ServletAdaptor.java   (with props)
Modified:
    velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/view/LinkTool.java
    velocity/tools/branches/2.x/test.xml

Modified: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/view/LinkTool.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/view/LinkTool.java?rev=590826&r1=590825&r2=590826&view=diff
==============================================================================
--- velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/view/LinkTool.java (original)
+++ velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/view/LinkTool.java Wed Oct 31 13:58:21 2007
@@ -22,6 +22,7 @@
 import java.io.UnsupportedEncodingException;
 import java.net.URLEncoder;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import javax.servlet.ServletContext;
@@ -99,6 +100,15 @@
     /** The self-include-parameters status */
     private boolean selfParams;
 
+    /**
+     * List of parameters that should be ignored when the current request's
+     * parameters are copied.
+     *
+     * @see #addIgnore(String)
+     * @see #addAllParameters()
+     */
+    private ArrayList<String> parametersToIgnore;
+
 
     /**
      * Default constructor. Tool must be initialized before use.
@@ -300,6 +310,36 @@
 
 
     /**
+     * For internal use.
+     *
+     * Copies this LinkTool and adds the specified parameter name to the
+     * ignore list in the copy.
+     *
+     * @param parameterName The name of the parameter to ignore when
+     *                      copying all parameters from the current request.
+     * @return A new LinkTool with the specified parameterName added to the
+     *         ignore list.
+     * @see #addAllParameters()
+     * @see #addIgnore()
+     */
+    protected LinkTool copyWithIgnore(String parameterName)
+    {
+        LinkTool copy = duplicate();
+        if(copy.parametersToIgnore == null)
+        {
+            copy.parametersToIgnore = new ArrayList<String>();
+        }
+        else
+        {
+            copy.parametersToIgnore =
+                (ArrayList<String>)parametersToIgnore.clone();
+        }
+        copy.parametersToIgnore.add(parameterName);
+        return copy;
+    }
+
+
+    /**
      * This is just to avoid duplicating this code for both copyWith() methods
      */
     protected LinkTool duplicate()
@@ -336,6 +376,7 @@
             copy.queryDataDelim = this.queryDataDelim;
             copy.selfAbsolute = this.selfAbsolute;
             copy.selfParams = this.selfParams;
+            copy.parametersToIgnore = this.parametersToIgnore;
             return copy;
         }
     }
@@ -610,6 +651,64 @@
         return getQueryData();
     }
 
+    /**
+     * Convenience method equivalent to
+     * {@link #addIgnore(String parameterName)}.
+     * @since VelocityTools 2.0
+     */
+    public LinkTool ignore(String paramName)
+    {
+        return addIgnore(paramName);
+    }
+
+    /**
+     * Instructs this LinkTool to ignore the specified parameter when
+     * copying the current request's parameters.
+     *
+     * @param parameterName The name of the parameter to ignore.
+     * @see #addAllParameters()
+     */
+    public LinkTool addIgnore(String parameterName)
+    {
+        return copyWithIgnore(parameterName);
+    }
+
+    /**
+     * Convenience method equivalent to
+     * {@link #addAllParameters()}.
+     * @since VelocityTools 2.0
+     */
+    public LinkTool selfParams()
+    {
+        return addAllParameters();
+    }
+
+    /**
+     * Adds all of the current request's parameters to this link's
+     * "query data". Any parameters that have been set to be ignored
+     * will be ignored.
+     *
+     * @return A LinkTool object with all of the current request's parameters
+     *         added to it.
+     * @see #addIgnore(String)
+     */
+    public LinkTool addAllParameters()
+    {
+        if (this.parametersToIgnore != null)
+        {
+            Map params = new HashMap(request.getParameterMap());
+            for (String name : this.parametersToIgnore)
+            {
+                params.remove(name);
+            }
+            return copyWith(params);
+        }
+        else
+        {
+            return copyWith(request.getParameterMap());
+        }
+    }
+
 
     /**
      * <p>Returns the URI that addresses this web application. E.g.
@@ -694,6 +793,7 @@
      * @see #configure(Map params)
      * @see #setSelfAbsolute(boolean selfAbsolute)
      * @see #setSelfIncludeParameters(boolean selfParams)
+     * @see #addAllParameters()
      * @since VelocityTools 1.3
      */
     public LinkTool getSelf()
@@ -712,7 +812,7 @@
         // then add the params (if so configured)
         if (this.selfParams)
         {
-            dupe.params(request.getParameterMap());
+            dupe = dupe.addAllParameters();
         }
         return dupe;
     }

Added: velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/blackbox/LinkToolTests.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/blackbox/LinkToolTests.java?rev=590826&view=auto
==============================================================================
--- velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/blackbox/LinkToolTests.java (added)
+++ velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/blackbox/LinkToolTests.java Wed Oct 31 13:58:21 2007
@@ -0,0 +1,124 @@
+package org.apache.velocity.tools.test.blackbox;
+
+/*
+ * 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 java.lang.reflect.Proxy;
+import java.lang.reflect.InvocationHandler;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.velocity.tools.view.LinkTool;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * <p>LinkTool tests.</p>
+ *
+ * @author Christopher Schultz
+ * @version $Id$
+ */
+public class LinkToolTests
+{
+    private LinkTool newLinkTool(InvocationHandler handler)
+    {
+        Object proxy
+            = Proxy.newProxyInstance(this.getClass().getClassLoader(),
+                                     new Class[] { HttpServletRequest.class,
+                                                   HttpServletResponse.class },
+                                     handler);
+
+        HttpServletRequest request = (HttpServletRequest)proxy;
+        HttpServletResponse response = (HttpServletResponse)proxy;
+
+        LinkTool link = new LinkTool();
+        link.setRequest(request);
+        link.setResponse(response);
+        return link;
+    }
+
+    public @Test void testAddAllParameters()
+    {
+        HashMap params = new HashMap();
+        params.put("a", "b");
+        InvocationHandler handler = new ServletAdaptor("/test", params);
+        LinkTool link = newLinkTool(handler);
+
+        String url = link.setRelative("/target")
+            .addQueryData("foo", "bar")
+            .addQueryData("bar", "baz")
+            .addAllParameters()
+            .toString();
+
+        Assert.assertEquals("/test/target?foo=bar&amp;bar=baz&amp;a=b", url);
+    }
+
+    public @Test void testAddMultiValueParameters()
+    {
+        HashMap params = new HashMap();
+        params.put("a", new String[] { "a", "b", "c" });
+        InvocationHandler handler = new ServletAdaptor("/test", params);
+        LinkTool link = newLinkTool(handler);
+
+        String url = link.setRelative("/target")
+            .addQueryData("foo", "bar")
+            .addQueryData("bar", "baz")
+            .addAllParameters()
+            .toString();
+
+        Assert.assertEquals("/test/target?foo=bar&amp;bar=baz&amp;a=a&amp;a=b&amp;a=c", url);
+    }
+
+    public @Test void testAddIgnoreParameters()
+    {
+        HashMap params = new HashMap();
+        params.put("a", "b");
+        params.put("b", "c");
+        InvocationHandler handler = new ServletAdaptor("/test", params);
+        LinkTool link = newLinkTool(handler);
+
+        String url = link.setRelative("/target")
+            .addQueryData("foo", "bar")
+            .addQueryData("bar", "baz")
+            .addIgnore("b")
+            .addAllParameters()
+            .toString();
+
+        Assert.assertEquals("/test/target?foo=bar&amp;bar=baz&amp;a=b", url);
+    }
+
+    public @Test void testAddAllParametersFirst()
+    {
+        HashMap params = new HashMap();
+        params.put("a", "b");
+        InvocationHandler handler = new ServletAdaptor("/test", params);
+        LinkTool link = newLinkTool(handler);
+
+        String url = link.setRelative("/target")
+            .addAllParameters()
+            .addQueryData("foo", "bar")
+            .addQueryData("bar", "baz")
+            .toString();
+
+        Assert.assertEquals("/test/target?a=b&amp;foo=bar&amp;bar=baz", url);
+    }
+
+}

Propchange: velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/blackbox/LinkToolTests.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/blackbox/LinkToolTests.java
------------------------------------------------------------------------------
    svn:keywords = Revision

Propchange: velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/blackbox/LinkToolTests.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/blackbox/ServletAdaptor.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/blackbox/ServletAdaptor.java?rev=590826&view=auto
==============================================================================
--- velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/blackbox/ServletAdaptor.java (added)
+++ velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/blackbox/ServletAdaptor.java Wed Oct 31 13:58:21 2007
@@ -0,0 +1,198 @@
+package org.apache.velocity.tools.test.blackbox;
+
+/*
+ * 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 java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.util.Collections;
+import java.util.Map;
+import org.apache.commons.collections.iterators.IteratorEnumeration;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * <p>Helper class for LinkToolTests class</p>
+ *
+ * @author Christopher Schultz
+ * @version $Id$
+ */
+public class ServletAdaptor implements InvocationHandler
+{
+    private Map _params;
+    private String _contextPath;
+
+    public ServletAdaptor(String contextPath,
+                          Map params)
+    {
+        _contextPath = contextPath;
+        if(null == _contextPath)
+        {
+            _contextPath = "";
+        }
+
+        _params = params;
+
+        if(null == _params)
+        {
+            _params = Collections.EMPTY_MAP;
+        }
+    }
+
+    public Object invoke(Object proxy,
+                         Method method,
+                         Object[] args)
+    {
+        Class clazz = method.getDeclaringClass();
+
+        if(clazz.isAssignableFrom(HttpServletRequest.class))
+        {
+            return request(proxy, method, args);
+        }
+        else if(clazz.isAssignableFrom(HttpServletResponse.class))
+        {
+            return response(proxy, method, args);
+        }
+        else if(clazz.isAssignableFrom(ServletContext.class))
+        {
+            return context(proxy, method, args);
+        }
+        else
+        {
+            throw new IllegalStateException("Unexpected proxy interface: "
+                                            + clazz.getName());
+        }
+    }
+
+    protected Object response(Object proxy,
+                              Method method,
+                              Object[] args)
+    {
+        String methodName = method.getName();
+
+        if("encodeURL".equals(methodName)
+           || "encodeUrl".equals(methodName))
+        {
+            // Don't worry about adding ";jsessionid" or anything.
+            return args[0];
+        }
+        else
+        {
+            throw new IllegalStateException("Unexpected method call: "
+                                            + method);
+        }
+    }
+
+    protected Object request(Object proxy,
+                             Method method,
+                             Object[] args)
+    {
+        String methodName = method.getName();
+
+        if("getContextPath".equals(methodName))
+        {
+            return _contextPath;
+        }
+        else if("getParameter".equals(methodName))
+        {
+            Object value = _params.get(args[0]);
+
+            if(value instanceof String)
+            {
+                return value;
+            }
+            else if (value instanceof String[])
+            {
+                return ((String[])value)[0];
+            }
+            else
+            {
+                throw new IllegalStateException("Parameter value must be either String or String[].");
+            }
+        }
+        else if("getParameterValues".equals(methodName))
+        {
+            Object value = _params.get(args[0]);
+
+            if(value instanceof String)
+            {
+                return new String[] { (String)value };
+            }
+            else if (value instanceof String[])
+            {
+                return value;
+            }
+            else
+            {
+                throw new IllegalStateException("Parameter value must be either String or String[].");
+            }
+        }
+        else if("getParameterMap".equals(methodName))
+        {
+            return Collections.unmodifiableMap(_params);
+        }
+        else if("getParameterNames".equals(methodName))
+        {
+            return new IteratorEnumeration(_params.keySet().iterator());
+        }
+        else if("getSession".equals(methodName))
+        {
+            return null;
+        }
+        else if("getAttribute".equals(methodName))
+        {
+            if(((String)args[0]).equals("XHTML"))
+            {
+                return Boolean.TRUE; // xhtml = true
+            }
+            else
+            {
+                return null;
+            }
+        }
+        else if("getCharacterEncoding".equals(methodName))
+        {
+            return "UTF-8";
+        }
+        else
+        {
+            throw new IllegalStateException("Unexpected method call: "
+                                            + method);
+        }
+    }
+
+    protected Object context(Object proxy,
+                             Method method,
+                             Object[] args)
+    {
+        String methodName = method.getName();
+
+        if("getContextPath".equals(methodName))
+        {
+            return _contextPath;
+        }
+        else
+        {
+            throw new IllegalStateException("Unexpected method call: "
+                                            + methodName);
+        }
+    }
+
+}

Propchange: velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/blackbox/ServletAdaptor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/blackbox/ServletAdaptor.java
------------------------------------------------------------------------------
    svn:keywords = Revision

Propchange: velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/blackbox/ServletAdaptor.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: velocity/tools/branches/2.x/test.xml
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/test.xml?rev=590826&r1=590825&r2=590826&view=diff
==============================================================================
--- velocity/tools/branches/2.x/test.xml (original)
+++ velocity/tools/branches/2.x/test.xml Wed Oct 31 13:58:21 2007
@@ -167,7 +167,7 @@
     </junit>
   </target>
 
-  <!-- vew tools (blackbox) tests -->
+  <!-- view tools (blackbox) tests -->
   <target name="test.view">
     <antcall target="start-showcase-webapp"/>
     <junit fork="false"
@@ -179,7 +179,9 @@
         <pathelement location="${test.classes.dir}"/>
       </classpath>
       <batchtest todir="${test.rst.dir}">
-        <fileset dir="${test.classes.dir}" includes="org/apache/velocity/tools/test/blackbox/**/*"/>
+        <fileset dir="${test.classes.dir}">
+            <include name="**/blackbox/*Test*"/>
+        </fileset>
       </batchtest>
       <formatter type="plain"/>
     </junit>