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:24:46 UTC
svn commit: r590811 - in /velocity/tools/trunk/src:
java/org/apache/velocity/tools/view/tools/LinkTool.java
test/org/apache/velocity/tools/test/blackbox/LinkToolTests.java
test/org/apache/velocity/tools/test/blackbox/ServletAdaptor.java
Author: nbubna
Date: Wed Oct 31 13:24:45 2007
New Revision: 590811
URL: http://svn.apache.org/viewvc?rev=590811&view=rev
Log:
VELTOOLS-88 - add addIgnore(), addAllParameters() and tests for LinkTool (thx to Chrisopher Schultz)
Added:
velocity/tools/trunk/src/test/org/apache/velocity/tools/test/blackbox/LinkToolTests.java (with props)
velocity/tools/trunk/src/test/org/apache/velocity/tools/test/blackbox/ServletAdaptor.java (with props)
Modified:
velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/LinkTool.java
Modified: velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/LinkTool.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/LinkTool.java?rev=590811&r1=590810&r2=590811&view=diff
==============================================================================
--- velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/LinkTool.java (original)
+++ velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/LinkTool.java Wed Oct 31 13:24:45 2007
@@ -23,6 +23,7 @@
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -111,6 +112,14 @@
/** 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 parametersToIgnore;
/** Java 1.4 encode method to use instead of deprecated 1.3 version. */
private static Method encode = null;
@@ -286,6 +295,38 @@
/**
+ * 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();
+ }
+ else
+ {
+ copy.parametersToIgnore = (ArrayList)parametersToIgnore.clone();
+ }
+
+ copy.parametersToIgnore.add(parameterName);
+
+ return copy;
+ }
+
+
+ /**
* This is just to avoid duplicating this code for both copyWith() methods
*/
protected LinkTool duplicate()
@@ -319,6 +360,8 @@
copy.queryDataDelim = this.queryDataDelim;
copy.selfAbsolute = this.selfAbsolute;
copy.selfParams = this.selfParams;
+ copy.parametersToIgnore = this.parametersToIgnore;
+
return copy;
}
}
@@ -726,6 +769,7 @@
* @see #configure(Map params)
* @see #setSelfAbsolute(boolean selfAbsolute)
* @see #setSelfIncludeParameters(boolean selfParams)
+ * @see #addAllParameters()
* @since VelocityTools 1.3
*/
public LinkTool getSelf()
@@ -744,7 +788,7 @@
// then add the params (if so configured)
if (this.selfParams)
{
- dupe.params(request.getParameterMap());
+ dupe = dupe.addAllParameters();
}
return dupe;
}
@@ -841,6 +885,50 @@
return URLEncoder.encode(url);
}
+ /**
+ * 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);
+ }
+
+ /**
+ * 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()
+ {
+ // Since we're adding all these parameters at once, there's no
+ // reason to make a copy of each LinkTool along the way, as might
+ // be done if we wrapped a call to addQueryData in a loop over
+ // all request parameters.
+ //
+ // Instead, we copy the current parameters, filter out those we
+ // want to ignore, and then use the copyWith(Map) method to
+ // copy all the parameters into this LinkTool.
+ HashMap params = new HashMap(request.getParameterMap());
+
+ if (parametersToIgnore != null && parametersToIgnore.size() > 0)
+ {
+ for (Iterator i = parametersToIgnore.iterator(); i.hasNext();)
+ {
+ params.remove(i.next());
+ }
+ }
+ return copyWith(params);
+ }
// --------------------------------------------- Internal Class -----------
Added: velocity/tools/trunk/src/test/org/apache/velocity/tools/test/blackbox/LinkToolTests.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/test/org/apache/velocity/tools/test/blackbox/LinkToolTests.java?rev=590811&view=auto
==============================================================================
--- velocity/tools/trunk/src/test/org/apache/velocity/tools/test/blackbox/LinkToolTests.java (added)
+++ velocity/tools/trunk/src/test/org/apache/velocity/tools/test/blackbox/LinkToolTests.java Wed Oct 31 13:24:45 2007
@@ -0,0 +1,170 @@
+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.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.velocity.tools.view.tools.LinkTool;
+import org.apache.velocity.tools.view.context.ViewContext;
+import org.apache.velocity.tools.view.context.ChainedContext;
+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
+{
+ public @Test void testAddAllParameters()
+ {
+ HashMap params = new HashMap();
+ params.put("a", "b");
+ InvocationHandler handler = new ServletAdaptor("/test", params);
+ Object proxy
+ = Proxy.newProxyInstance(this.getClass().getClassLoader(),
+ new Class[] { HttpServletRequest.class,
+ HttpServletResponse.class },
+ handler);
+
+ HttpServletRequest request = (HttpServletRequest)proxy;
+ HttpServletResponse response = (HttpServletResponse)proxy;
+ ViewContext vc = new ChainedContext(null, // Velocity
+ request, // Request
+ response, // Response
+ null // application
+ );
+
+ LinkTool link = new LinkTool();
+ link.init(vc);
+
+ String url = link.setRelative("/target")
+ .addQueryData("foo", "bar")
+ .addQueryData("bar", "baz")
+ .addAllParameters()
+ .toString();
+
+ Assert.assertEquals("/test/target?foo=bar&bar=baz&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);
+ Object proxy
+ = Proxy.newProxyInstance(this.getClass().getClassLoader(),
+ new Class[] { HttpServletRequest.class,
+ HttpServletResponse.class },
+ handler);
+
+ HttpServletRequest request = (HttpServletRequest)proxy;
+ HttpServletResponse response = (HttpServletResponse)proxy;
+ ViewContext vc = new ChainedContext(null, // Velocity
+ request, // Request
+ response, // Response
+ null // application
+ );
+
+ LinkTool link = new LinkTool();
+ link.init(vc);
+
+ String url = link.setRelative("/target")
+ .addQueryData("foo", "bar")
+ .addQueryData("bar", "baz")
+ .addAllParameters()
+ .toString();
+
+ Assert.assertEquals("/test/target?foo=bar&bar=baz&a=a&a=b&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);
+ Object proxy
+ = Proxy.newProxyInstance(this.getClass().getClassLoader(),
+ new Class[] { HttpServletRequest.class,
+ HttpServletResponse.class },
+ handler);
+
+ HttpServletRequest request = (HttpServletRequest)proxy;
+ HttpServletResponse response = (HttpServletResponse)proxy;
+ ViewContext vc = new ChainedContext(null, // Velocity
+ request, // Request
+ response, // Response
+ null // application
+ );
+
+ LinkTool link = new LinkTool();
+ link.init(vc);
+
+ String url = link.setRelative("/target")
+ .addQueryData("foo", "bar")
+ .addQueryData("bar", "baz")
+ .addIgnore("b")
+ .addAllParameters()
+ .toString();
+
+ Assert.assertEquals("/test/target?foo=bar&bar=baz&a=b", url);
+ }
+
+ public @Test void testAddAllParametersFirst()
+ {
+ HashMap params = new HashMap();
+ params.put("a", "b");
+ InvocationHandler handler = new ServletAdaptor("/test", params);
+ Object proxy
+ = Proxy.newProxyInstance(this.getClass().getClassLoader(),
+ new Class[] { HttpServletRequest.class,
+ HttpServletResponse.class },
+ handler);
+
+ HttpServletRequest request = (HttpServletRequest)proxy;
+ HttpServletResponse response = (HttpServletResponse)proxy;
+ ViewContext vc = new ChainedContext(null, // Velocity
+ request, // Request
+ response, // Response
+ null // application
+ );
+
+ LinkTool link = new LinkTool();
+ link.init(vc);
+
+ String url = link.setRelative("/target")
+ .addAllParameters()
+ .addQueryData("foo", "bar")
+ .addQueryData("bar", "baz")
+ .toString();
+
+ Assert.assertEquals("/test/target?a=b&foo=bar&bar=baz", url);
+ }
+
+}
Propchange: velocity/tools/trunk/src/test/org/apache/velocity/tools/test/blackbox/LinkToolTests.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: velocity/tools/trunk/src/test/org/apache/velocity/tools/test/blackbox/LinkToolTests.java
------------------------------------------------------------------------------
svn:keywords = Revision
Propchange: velocity/tools/trunk/src/test/org/apache/velocity/tools/test/blackbox/LinkToolTests.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: velocity/tools/trunk/src/test/org/apache/velocity/tools/test/blackbox/ServletAdaptor.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/test/org/apache/velocity/tools/test/blackbox/ServletAdaptor.java?rev=590811&view=auto
==============================================================================
--- velocity/tools/trunk/src/test/org/apache/velocity/tools/test/blackbox/ServletAdaptor.java (added)
+++ velocity/tools/trunk/src/test/org/apache/velocity/tools/test/blackbox/ServletAdaptor.java Wed Oct 31 13:24:45 2007
@@ -0,0 +1,199 @@
+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 org.apache.velocity.tools.view.context.ViewContext;
+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(ViewContext.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/trunk/src/test/org/apache/velocity/tools/test/blackbox/ServletAdaptor.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: velocity/tools/trunk/src/test/org/apache/velocity/tools/test/blackbox/ServletAdaptor.java
------------------------------------------------------------------------------
svn:keywords = Revision
Propchange: velocity/tools/trunk/src/test/org/apache/velocity/tools/test/blackbox/ServletAdaptor.java
------------------------------------------------------------------------------
svn:mime-type = text/plain