Author: ab Date: Fri Oct 9 13:29:01 2009 New Revision: 823547 URL: http://svn.apache.org/viewvc?rev=823547&view=rev Log: NUTCH-757 RequestUtils getBooleanParameter() always returns false.
Added: lucene/nutch/trunk/src/test/org/apache/nutch/searcher/response/ lucene/nutch/trunk/src/test/org/apache/nutch/searcher/response/TestRequestUtils.java (with props) Modified: lucene/nutch/trunk/CHANGES.txt lucene/nutch/trunk/src/java/org/apache/nutch/searcher/response/RequestUtils.java Modified: lucene/nutch/trunk/CHANGES.txt URL: http://svn.apache.org/viewvc/lucene/nutch/trunk/CHANGES.txt?rev=823547&r1=823546&r2=823547&view=diff ============================================================================== --- lucene/nutch/trunk/CHANGES.txt (original) +++ lucene/nutch/trunk/CHANGES.txt Fri Oct 9 13:29:01 2009 @@ -17,6 +17,9 @@ * NUTCH-731 - Redirection of robots.txt in RobotRulesParser (Julien Nioche via ab) +* NUTCH-757 - RequestUtils getBooleanParameter() always returns false + (Niall Pemberton via ab) + Release 1.0 - 2009-03-23 1. NUTCH-474 - Fetcher2 crawlDelay and blocking fix (Dogacan Guney via ab) Modified: lucene/nutch/trunk/src/java/org/apache/nutch/searcher/response/RequestUtils.java URL: http://svn.apache.org/viewvc/lucene/nutch/trunk/src/java/org/apache/nutch/searcher/response/RequestUtils.java?rev=823547&r1=823546&r2=823547&view=diff ============================================================================== --- lucene/nutch/trunk/src/java/org/apache/nutch/searcher/response/RequestUtils.java (original) +++ lucene/nutch/trunk/src/java/org/apache/nutch/searcher/response/RequestUtils.java Fri Oct 9 13:29:01 2009 @@ -66,9 +66,9 @@ if (parameterExists(request, param)) { String value = request.getParameter(param); if (StringUtils.isNotBlank(value) - && (StringUtils.equals(param, "1") - || StringUtils.equalsIgnoreCase(param, "true") || StringUtils.equalsIgnoreCase( - param, "yes"))) { + && (StringUtils.equals(value, "1") + || StringUtils.equalsIgnoreCase(value, "true") || StringUtils.equalsIgnoreCase( + value, "yes"))) { return true; } } @@ -79,9 +79,9 @@ String param, Boolean def) { if (parameterExists(request, param)) { String value = request.getParameter(param); - return (StringUtils.isNotBlank(value) && (StringUtils.equals(param, "1") - || StringUtils.equalsIgnoreCase(param, "true") || StringUtils.equalsIgnoreCase( - param, "yes"))); + return (StringUtils.isNotBlank(value) && (StringUtils.equals(value, "1") + || StringUtils.equalsIgnoreCase(value, "true") || StringUtils.equalsIgnoreCase( + value, "yes"))); } return def; } Added: lucene/nutch/trunk/src/test/org/apache/nutch/searcher/response/TestRequestUtils.java URL: http://svn.apache.org/viewvc/lucene/nutch/trunk/src/test/org/apache/nutch/searcher/response/TestRequestUtils.java?rev=823547&view=auto ============================================================================== --- lucene/nutch/trunk/src/test/org/apache/nutch/searcher/response/TestRequestUtils.java (added) +++ lucene/nutch/trunk/src/test/org/apache/nutch/searcher/response/TestRequestUtils.java Fri Oct 9 13:29:01 2009 @@ -0,0 +1,140 @@ +/** + * 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. + */ + +package org.apache.nutch.searcher.response; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import junit.framework.TestCase; + +public class TestRequestUtils extends TestCase { + + public TestRequestUtils(String name) { + super(name); + } + + /** + * Test getBooleanParameter() - no default + */ + public void testGetBooleanParameterNoDefault() { + String param = "foo"; + Map parameters = new HashMap(); + HttpServletRequest request = createMockHttpServletRequest(parameters); + + assertFalse("No param", RequestUtils.getBooleanParameter(request, param)); + + parameters.put(param, "0"); + assertFalse("Foo=0", RequestUtils.getBooleanParameter(request, param)); + + parameters.put(param, "no"); + assertFalse("Foo=no", RequestUtils.getBooleanParameter(request, param)); + + parameters.put(param, "false"); + assertFalse("Foo=false", RequestUtils.getBooleanParameter(request, param)); + + parameters.put(param, "abcdef"); + assertFalse("Foo=abcdef", RequestUtils.getBooleanParameter(request, param)); + + parameters.put(param, "1"); + assertTrue("Foo=1", RequestUtils.getBooleanParameter(request, param)); + + parameters.put(param, "yes"); + assertTrue("Foo=yes", RequestUtils.getBooleanParameter(request, param)); + + parameters.put(param, "YES"); + assertTrue("Foo=YES", RequestUtils.getBooleanParameter(request, param)); + + parameters.put(param, "true"); + assertTrue("Foo=true", RequestUtils.getBooleanParameter(request, param)); + + parameters.put(param, "TRUE"); + assertTrue("Foo=TRUE", RequestUtils.getBooleanParameter(request, param)); + } + + /** + * Test getBooleanParameter() - with default + */ + public void testGetBooleanParameterWithoDefault() { + String param = "foo"; + Map parameters = new HashMap(); + HttpServletRequest request = createMockHttpServletRequest(parameters); + + assertTrue("No param - def true", RequestUtils.getBooleanParameter(request, param, true)); + assertFalse("No param - def false", RequestUtils.getBooleanParameter(request, param, false)); + + parameters.put(param, "0"); + assertFalse("Foo=0", RequestUtils.getBooleanParameter(request, param, true)); + + parameters.put(param, "no"); + assertFalse("Foo=no", RequestUtils.getBooleanParameter(request, param, true)); + + parameters.put(param, "false"); + assertFalse("Foo=false", RequestUtils.getBooleanParameter(request, param, true)); + + parameters.put(param, "abcdef"); + assertFalse("Foo=abcdef", RequestUtils.getBooleanParameter(request, param, true)); + + parameters.put(param, "1"); + assertTrue("Foo=1", RequestUtils.getBooleanParameter(request, param, false)); + + parameters.put(param, "yes"); + assertTrue("Foo=yes", RequestUtils.getBooleanParameter(request, param, false)); + + parameters.put(param, "YES"); + assertTrue("Foo=YES", RequestUtils.getBooleanParameter(request, param, false)); + + parameters.put(param, "true"); + assertTrue("Foo=true", RequestUtils.getBooleanParameter(request, param, false)); + + parameters.put(param, "TRUE"); + assertTrue("Foo=TRUE", RequestUtils.getBooleanParameter(request, param, false)); + } + + /** + * Create a mock HttpServletRequest. + */ + private HttpServletRequest createMockHttpServletRequest(Map parameters) { + MockHttpServletRequestInvocationHandler handler = new MockHttpServletRequestInvocationHandler(); + handler.setParameterMap(parameters); + ClassLoader cl = getClass().getClassLoader(); + Class[] interfaces = new Class[] {HttpServletRequest.class}; + return (HttpServletRequest)Proxy.newProxyInstance(cl, interfaces, handler); + } + + /** + * InvocationHandler for mock HttpServletRequest proxy. + */ + private static class MockHttpServletRequestInvocationHandler implements InvocationHandler { + private Map parameters = new HashMap(); + public Object invoke(Object proxy, Method method, Object[] args) + throws Throwable { + if (method.getName().equals("getParameter")) { + return parameters.get((String)args[0]); + } + return null; + } + public void setParameterMap(Map parameters) { + this.parameters = parameters; + } + } +} \ No newline at end of file Propchange: lucene/nutch/trunk/src/test/org/apache/nutch/searcher/response/TestRequestUtils.java ------------------------------------------------------------------------------ svn:eol-style = native