You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ss...@apache.org on 2015/09/04 23:22:35 UTC

svn commit: r1701328 - in /sling/trunk/testing/mocks/sling-mock/src: main/java/org/apache/sling/testing/mock/sling/servlet/ test/java/org/apache/sling/testing/mock/sling/servlet/

Author: sseifert
Date: Fri Sep  4 21:22:34 2015
New Revision: 1701328

URL: http://svn.apache.org/r1701328
Log:
SLING-5003 Support request dispatcher in MockSlingHttpServletRequest

Added:
    sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockRequestDispatcherFactory.java   (with props)
Modified:
    sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequest.java
    sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequestTest.java

Added: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockRequestDispatcherFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockRequestDispatcherFactory.java?rev=1701328&view=auto
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockRequestDispatcherFactory.java (added)
+++ sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockRequestDispatcherFactory.java Fri Sep  4 21:22:34 2015
@@ -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);
+
+}

Propchange: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockRequestDispatcherFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockRequestDispatcherFactory.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Fri Sep  4 21:22:34 2015
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockRequestDispatcherFactory.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequest.java?rev=1701328&r1=1701327&r2=1701328&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequest.java (original)
+++ sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequest.java Fri Sep  4 21:22:34 2015
@@ -96,6 +96,7 @@ public class MockSlingHttpServletRequest
     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
         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();
@@ -721,11 +743,6 @@ public class MockSlingHttpServletRequest
         throw new UnsupportedOperationException();
     }
 
-    @Override
-    public RequestDispatcher getRequestDispatcher(String path) {
-        throw new UnsupportedOperationException();
-    }
-
     @Override
     public boolean authenticate(HttpServletResponse response) {
         throw new UnsupportedOperationException();

Modified: sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequestTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequestTest.java?rev=1701328&r1=1701327&r2=1701328&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequestTest.java (original)
+++ sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequestTest.java Fri Sep  4 21:22:34 2015
@@ -25,6 +25,7 @@ import static org.junit.Assert.assertNot
 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 MockSlingHttpServletRequest
         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");
+    }
     
 }