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 3fafd8ebf232850244fd15ede0017851c3f3bfdd Author: Stefan Seifert <[email protected]> AuthorDate: Fri Sep 4 21:22:34 2015 +0000 SLING-5003 Support request dispatcher in MockSlingHttpServletRequest git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/sling-mock@1701328 13f79535-47bb-0310-9956-ffa450edef68 --- .../servlet/MockRequestDispatcherFactory.java | 48 ++++++++++++++++++++++ .../sling/servlet/MockSlingHttpServletRequest.java | 43 +++++++++++++------ .../servlet/MockSlingHttpServletRequestTest.java | 23 +++++++++++ 3 files changed, 101 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockRequestDispatcherFactory.java b/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockRequestDispatcherFactory.java new file mode 100644 index 0000000..08f63c4 --- /dev/null +++ b/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockRequestDispatcherFactory.java @@ -0,0 +1,48 @@ +/* + * 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 javax.servlet.RequestDispatcher; + +import org.apache.sling.api.request.RequestDispatcherOptions; +import org.apache.sling.api.resource.Resource; + +/** + * Interface to create a mock {@link RequestDispatcher} when calling the getRequestDispatcher methods + * on {@link MockSlingHttpServletRequest} instances. + */ +public interface MockRequestDispatcherFactory { + + /** + * Get request dispatcher for given path. + * @param path Path + * @param options Options. Null if no options are provided. + * @return Request dispatcher + */ + RequestDispatcher getRequestDispatcher(String path, RequestDispatcherOptions options); + + /** + * Get request dispatcher for given resource. + * @param resource Resource + * @param options Options. Null if no options are provided. + * @return Request dispatcher + */ + RequestDispatcher getRequestDispatcher(Resource resource, RequestDispatcherOptions options); + +} 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 5fa3068..c7755e2 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 @@ -96,6 +96,7 @@ public class MockSlingHttpServletRequest extends SlingAdaptable implements Sling private String contentType; private String characterEncoding; private byte[] content; + private MockRequestDispatcherFactory requestDispatcherFactory; private static final ResourceBundle EMPTY_RESOURCE_BUNDLE = new ListResourceBundle() { @Override @@ -568,24 +569,45 @@ public class MockSlingHttpServletRequest extends SlingAdaptable implements Sling this.content = content; } - - // --- unsupported operations --- + @Override + public RequestDispatcher getRequestDispatcher(String path) { + if (requestDispatcherFactory == null) { + throw new IllegalStateException("Please provdide a MockRequestDispatcherFactory (setRequestDispatcherFactory)."); + } + return requestDispatcherFactory.getRequestDispatcher(path, null); + } @Override - public RequestDispatcher getRequestDispatcher(Resource dispatcherResource) { - throw new UnsupportedOperationException(); + public RequestDispatcher getRequestDispatcher(String path, RequestDispatcherOptions options) { + if (requestDispatcherFactory == null) { + throw new IllegalStateException("Please provdide a MockRequestDispatcherFactory (setRequestDispatcherFactory)."); + } + return requestDispatcherFactory.getRequestDispatcher(path, options); } @Override - public RequestDispatcher getRequestDispatcher(String dispatcherPath, RequestDispatcherOptions options) { - throw new UnsupportedOperationException(); + public RequestDispatcher getRequestDispatcher(Resource resource) { + if (requestDispatcherFactory == null) { + throw new IllegalStateException("Please provdide a MockRequestDispatcherFactory (setRequestDispatcherFactory)."); + } + return requestDispatcherFactory.getRequestDispatcher(resource, null); } @Override - public RequestDispatcher getRequestDispatcher(Resource dispatcherResource, RequestDispatcherOptions options) { - throw new UnsupportedOperationException(); + public RequestDispatcher getRequestDispatcher(Resource resource, RequestDispatcherOptions options) { + if (requestDispatcherFactory == null) { + throw new IllegalStateException("Please provdide a MockRequestDispatcherFactory (setRequestDispatcherFactory)."); + } + return requestDispatcherFactory.getRequestDispatcher(resource, options); + } + + public void setRequestDispatcherFactory(MockRequestDispatcherFactory requestDispatcherFactory) { + this.requestDispatcherFactory = requestDispatcherFactory; } + + // --- unsupported operations --- + @Override public RequestProgressTracker getRequestProgressTracker() { throw new UnsupportedOperationException(); @@ -722,11 +744,6 @@ public class MockSlingHttpServletRequest extends SlingAdaptable implements Sling } @Override - public RequestDispatcher getRequestDispatcher(String path) { - throw new UnsupportedOperationException(); - } - - @Override public boolean authenticate(HttpServletResponse response) { 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 3f0c52b..2451540 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 @@ -25,6 +25,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -38,11 +39,13 @@ import java.util.Locale; import java.util.Map; import java.util.ResourceBundle; +import javax.servlet.RequestDispatcher; import javax.servlet.http.Cookie; import javax.servlet.http.HttpSession; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.CharEncoding; +import org.apache.sling.api.request.RequestDispatcherOptions; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.servlets.HttpConstants; @@ -296,5 +299,25 @@ public class MockSlingHttpServletRequestTest { assertEquals(data.length, request.getContentLength()); assertArrayEquals(data, IOUtils.toByteArray(request.getInputStream())); } + + @Test + public void testGetRequestDispatcher() { + MockRequestDispatcherFactory requestDispatcherFactory = mock(MockRequestDispatcherFactory.class); + RequestDispatcher requestDispatcher = mock(RequestDispatcher.class); + when(requestDispatcherFactory.getRequestDispatcher(any(Resource.class), any(RequestDispatcherOptions.class))).thenReturn(requestDispatcher); + when(requestDispatcherFactory.getRequestDispatcher(any(String.class), any(RequestDispatcherOptions.class))).thenReturn(requestDispatcher); + + request.setRequestDispatcherFactory(requestDispatcherFactory); + + assertSame(requestDispatcher, request.getRequestDispatcher("/path")); + assertSame(requestDispatcher, request.getRequestDispatcher("/path", new RequestDispatcherOptions())); + assertSame(requestDispatcher, request.getRequestDispatcher(resource)); + assertSame(requestDispatcher, request.getRequestDispatcher(resource, new RequestDispatcherOptions())); + } + + @Test(expected = IllegalStateException.class) + public void testGetRequestDispatcherWithoutFactory() { + request.getRequestDispatcher("/path"); + } } -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
