This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.testing.sling-mock-1.5.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-sling-mock.git
commit 602f4a99fe4cfc530f43e6eef9b402d3ff02648b Author: Stefan Seifert <[email protected]> AuthorDate: Thu Sep 3 08:36:21 2015 +0000 SLING-4993 Support RequestParameter in MockSlingHttpServlerRequest git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/sling-mock@1700953 13f79535-47bb-0310-9956-ffa450edef68 --- .../mock/sling/servlet/MockRequestParameter.java | 105 +++++++++++++++++++++ .../sling/servlet/MockRequestParameterMap.java | 101 ++++++++++++++++++++ .../sling/servlet/MockSlingHttpServletRequest.java | 55 +++++++---- .../servlet/MockSlingHttpServletRequestTest.java | 16 ++++ 4 files changed, 261 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockRequestParameter.java b/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockRequestParameter.java new file mode 100644 index 0000000..f69d5af --- /dev/null +++ b/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockRequestParameter.java @@ -0,0 +1,105 @@ +/* + * 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.sling.testing.mock.sling.servlet; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; + +import org.apache.sling.api.request.RequestParameter; + +/** + * Mock implementation of {@link RequestParameter}. + */ +class MockRequestParameter implements RequestParameter { + + private String name; + private String encoding = "UTF-8"; + private String value; + + private byte[] content; + + public MockRequestParameter(String name, String value) { + this.value = value; + this.content = null; + } + void setName(String name) { + this.name = name; + } + + public String getName() { + return this.name; + } + + public void setEncoding(String encoding) { + this.encoding = encoding; + } + + public String getEncoding() { + return this.encoding; + } + + public byte[] get() { + if (content == null) { + try { + content = getString().getBytes(getEncoding()); + } catch (Exception e) { + // UnsupportedEncodingException, IllegalArgumentException + content = getString().getBytes(); + } + } + return content; + } + + public String getContentType() { + // none known for www-form-encoded parameters + return null; + } + + public InputStream getInputStream() { + return new ByteArrayInputStream(this.get()); + } + + public String getFileName() { + // no original file name + return null; + } + + public long getSize() { + return this.get().length; + } + + public String getString() { + return value; + } + + public String getString(String encoding) throws UnsupportedEncodingException { + return new String(this.get(), encoding); + } + + public boolean isFormField() { + // www-form-encoded are always form fields + return true; + } + + public String toString() { + return this.getString(); + } + +} diff --git a/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockRequestParameterMap.java b/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockRequestParameterMap.java new file mode 100644 index 0000000..3578dd1 --- /dev/null +++ b/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockRequestParameterMap.java @@ -0,0 +1,101 @@ +/* + * 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.sling.testing.mock.sling.servlet; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import org.apache.sling.api.request.RequestParameter; +import org.apache.sling.api.request.RequestParameterMap; + +/** + * Mock implementation of {@link RequestParameterMap}. + */ +class MockRequestParameterMap implements RequestParameterMap { + + private final Map<String,RequestParameter[]> delegate = new HashMap<String, RequestParameter[]>(); + + public RequestParameter getValue(String name) { + RequestParameter[] params = getValues(name); + return (params != null && params.length > 0) ? params[0] : null; + } + + public RequestParameter[] getValues(String name) { + return delegate.get(name); + } + + public int size() { + return delegate.size(); + } + + public boolean isEmpty() { + return delegate.isEmpty(); + } + + public boolean containsKey(Object key) { + return delegate.containsKey(key); + } + + public boolean containsValue(Object value) { + return delegate.containsValue(value); + } + + public RequestParameter[] get(Object key) { + return delegate.get(key); + } + + public RequestParameter[] put(String key, RequestParameter[] value) { + return delegate.put(key, value); + } + + public RequestParameter[] remove(Object key) { + return delegate.remove(key); + } + + public void putAll(Map<? extends String, ? extends RequestParameter[]> m) { + delegate.putAll(m); + } + + public void clear() { + delegate.clear(); + } + + public Set<String> keySet() { + return delegate.keySet(); + } + + public Collection<RequestParameter[]> values() { + return delegate.values(); + } + + public Set<java.util.Map.Entry<String, RequestParameter[]>> entrySet() { + return delegate.entrySet(); + } + + public boolean equals(Object o) { + return delegate.equals(o); + } + + public int hashCode() { + return delegate.hashCode(); + } + +} diff --git a/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequest.java b/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequest.java index b776bf4..b7b8f21 100644 --- a/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequest.java +++ b/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequest.java @@ -66,6 +66,8 @@ import org.apache.sling.testing.mock.sling.MockSling; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; +import com.google.common.collect.ImmutableList; + /** * Mock {@link SlingHttpServletRequest} implementation. */ @@ -463,36 +465,62 @@ public class MockSlingHttpServletRequest extends SlingAdaptable implements Sling } return resourceBundle; } - - // --- unsupported operations --- @Override - public RequestDispatcher getRequestDispatcher(Resource dispatcherResource) { - throw new UnsupportedOperationException(); + public RequestParameter getRequestParameter(String name) { + String value = getParameter(name); + if (value != null) { + return new MockRequestParameter(name, value); + } + return null; } @Override - public RequestDispatcher getRequestDispatcher(String dispatcherPath, RequestDispatcherOptions options) { - throw new UnsupportedOperationException(); + public RequestParameterMap getRequestParameterMap() { + MockRequestParameterMap map = new MockRequestParameterMap(); + for (Map.Entry<String,String[]> entry : getParameterMap().entrySet()) { + map.put(entry.getKey(), getRequestParameters(entry.getKey())); + } + return map; } @Override - public RequestDispatcher getRequestDispatcher(Resource dispatcherResource, RequestDispatcherOptions options) { - throw new UnsupportedOperationException(); + public RequestParameter[] getRequestParameters(String name) { + String[] values = getParameterValues(name); + if (values == null) { + return null; + } + RequestParameter[] requestParameters = new RequestParameter[values.length]; + for (int i = 0; i < values.length; i++) { + requestParameters[i] = new MockRequestParameter(name, values[i]); + } + return requestParameters; } + // part of Sling API 2.7 + public List<RequestParameter> getRequestParameterList() { + List<RequestParameter> params = new ArrayList<RequestParameter>(); + for (RequestParameter[] requestParameters : getRequestParameterMap().values()) { + params.addAll(ImmutableList.copyOf(requestParameters)); + } + return params; + } + + + // --- unsupported operations --- + @Override - public RequestParameter getRequestParameter(String name) { + public RequestDispatcher getRequestDispatcher(Resource dispatcherResource) { throw new UnsupportedOperationException(); } @Override - public RequestParameterMap getRequestParameterMap() { + public RequestDispatcher getRequestDispatcher(String dispatcherPath, RequestDispatcherOptions options) { throw new UnsupportedOperationException(); } @Override - public RequestParameter[] getRequestParameters(String name) { + public RequestDispatcher getRequestDispatcher(Resource dispatcherResource, RequestDispatcherOptions options) { throw new UnsupportedOperationException(); } @@ -721,9 +749,4 @@ public class MockSlingHttpServletRequest extends SlingAdaptable implements Sling throw new UnsupportedOperationException(); } - // part of Sling API 2.7 - public List<RequestParameter> getRequestParameterList() { - throw new UnsupportedOperationException(); - } - } diff --git a/src/test/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequestTest.java b/src/test/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequestTest.java index 8c868b7..4c379e4 100644 --- a/src/test/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequestTest.java +++ b/src/test/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequestTest.java @@ -250,4 +250,20 @@ public class MockSlingHttpServletRequestTest { assertFalse(bundle2.getKeys().hasMoreElements()); } + @Test + public void testRequestParameter() throws Exception { + request.setQueryString("param1=123¶m2=" + URLEncoder.encode("äöü߀!:!", CharEncoding.UTF_8) + + "¶m3=a¶m3=b"); + + assertEquals(3, request.getRequestParameterMap().size()); + assertEquals(4, request.getRequestParameterList().size()); + assertEquals("123", request.getRequestParameter("param1").getString()); + assertEquals("äöü߀!:!", request.getRequestParameter("param2").getString()); + assertEquals("a",request.getRequestParameters("param3")[0].getString()); + assertEquals("b",request.getRequestParameters("param3")[1].getString()); + + assertNull(request.getRequestParameter("unknown")); + assertNull(request.getRequestParameters("unknown")); + } + } -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
