You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by js...@apache.org on 2019/05/13 13:47:21 UTC

[sling-org-apache-sling-engine] branch master updated: SLING-8416 - Improve test coverage for ServletFilterManager

This is an automated email from the ASF dual-hosted git repository.

jsedding pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-engine.git


The following commit(s) were added to refs/heads/master by this push:
     new 82d2b42  SLING-8416 - Improve test coverage for ServletFilterManager
82d2b42 is described below

commit 82d2b4208b01e0384110af48ee0225adcf5e0fe5
Author: Julian Sedding <js...@apache.org>
AuthorDate: Mon May 13 15:46:52 2019 +0200

    SLING-8416 - Improve test coverage for ServletFilterManager
---
 pom.xml                                            |  12 ++
 .../impl/filter/ServletFilterManagerTest.java      | 123 +++++++++++++++++++++
 2 files changed, 135 insertions(+)

diff --git a/pom.xml b/pom.xml
index b2a32e5..ab667a2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -191,6 +191,18 @@
             <version>1.0</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.testing.osgi-mock.junit4</artifactId>
+            <version>2.4.8</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>osgi.cmpn</artifactId>
+            <version>5.0.0</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
     <profiles>
         <profile>
diff --git a/src/test/java/org/apache/sling/engine/impl/filter/ServletFilterManagerTest.java b/src/test/java/org/apache/sling/engine/impl/filter/ServletFilterManagerTest.java
new file mode 100644
index 0000000..7274d2f
--- /dev/null
+++ b/src/test/java/org/apache/sling/engine/impl/filter/ServletFilterManagerTest.java
@@ -0,0 +1,123 @@
+package org.apache.sling.engine.impl.filter;
+
+import org.apache.commons.lang3.ArrayUtils;
+import org.apache.sling.engine.EngineConstants;
+import org.apache.sling.engine.impl.filter.ServletFilterManager.FilterChainType;
+import org.apache.sling.testing.mock.osgi.context.ContextCallback;
+import org.apache.sling.testing.mock.osgi.context.OsgiContextImpl;
+import org.apache.sling.testing.mock.osgi.junit.OsgiContext;
+import org.apache.sling.testing.mock.osgi.junit.OsgiContextBuilder;
+import org.junit.Rule;
+import org.junit.Test;
+import org.osgi.framework.BundleContext;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import java.io.IOException;
+import java.util.Hashtable;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class ServletFilterManagerTest {
+
+    @Rule
+    public OsgiContext osgiContext = new OsgiContextBuilder()
+            .afterSetUp(new ContextCallback<OsgiContextImpl>() {
+                @Override
+                public void execute(OsgiContextImpl context) throws Exception {
+                    servletFilterManager = new ServletFilterManager(context.bundleContext(), null);
+                    servletFilterManager.open();
+                }
+            })
+            .build();
+
+    private ServletFilterManager servletFilterManager;
+
+    @Test
+    public void registerFilterWithMultipleScopes() throws Exception {
+
+        FilterChainType[] scopes = {FilterChainType.REQUEST, FilterChainType.INCLUDE};
+
+        TestFilter testFilter = registerFilterForScopes(osgiContext.bundleContext(), scopes);
+
+        assertFilterInScopes(servletFilterManager, testFilter, scopes);
+    }
+
+    @Test
+    public void registerFilterWithComponentScope() throws Exception {
+
+        TestFilter testFilter = registerFilterForScopes(osgiContext.bundleContext(), FilterChainType.COMPONENT);
+
+        // COMPONENT implies INCLUDE ande FORWARD
+        assertFilterInScopes(servletFilterManager, testFilter,
+                FilterChainType.COMPONENT, FilterChainType.INCLUDE, FilterChainType.FORWARD);
+    }
+
+    @Test
+    public void registerFilterWithAllScopes() throws Exception {
+
+        FilterChainType[] allScopes = FilterChainType.values();
+
+        TestFilter testFilter = registerFilterForScopes(osgiContext.bundleContext(), allScopes);
+
+        assertFilterInScopes(servletFilterManager, testFilter, allScopes);
+    }
+
+
+    private static TestFilter registerFilterForScopes(BundleContext bundleContext, FilterChainType... scopes) {
+        String[] scopeNames = new String[scopes.length];
+        for (int i = 0; i < scopes.length; i++) {
+            scopeNames[i] = scopes[i].name();
+        }
+
+        TestFilter testFilter = new TestFilter();
+        Hashtable<String, Object> properties = new Hashtable<>();
+        properties.put(EngineConstants.SLING_FILTER_SCOPE, scopeNames);
+        bundleContext.registerService(Filter.class, testFilter, properties);
+        return testFilter;
+    }
+
+    private static void assertFilterInScopes(ServletFilterManager mgr, Filter filterInstance, FilterChainType... scopes) {
+        for (FilterChainType scope : FilterChainType.values()) {
+            if (ArrayUtils.contains(scopes, scope)) {
+                assertTrue("Expected filter in scope " + scope.name(), hasFilterInScope(mgr, filterInstance, scope));
+            } else {
+                assertFalse("Unexpected filter in scope " + scope.name(), hasFilterInScope(mgr, filterInstance, scope));
+            }
+        }
+    }
+
+    private static boolean hasFilterInScope(ServletFilterManager mgr, Filter instance, FilterChainType scope) {
+        FilterHandle[] filterHandles = mgr.getFilters(scope);
+        for (FilterHandle filterHandle : filterHandles) {
+            if (filterHandle.getFilter() == instance) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+
+    private static class TestFilter implements Filter {
+
+        @Override
+        public void init(FilterConfig filterConfig) throws ServletException {
+
+        }
+
+        @Override
+        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
+            chain.doFilter(request, response);
+        }
+
+        @Override
+        public void destroy() {
+
+        }
+    }
+}
\ No newline at end of file