shiro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lhazlew...@apache.org
Subject svn commit: r816704 - in /incubator/shiro/trunk/web/src: main/java/org/apache/shiro/web/filter/mgt/DefaultFilterChainManager.java test/java/org/apache/shiro/web/filter/mgt/ test/java/org/apache/shiro/web/filter/mgt/DefaultFilterChainManagerTest.java
Date Fri, 18 Sep 2009 16:32:22 GMT
Author: lhazlewood
Date: Fri Sep 18 16:32:22 2009
New Revision: 816704

URL: http://svn.apache.org/viewvc?rev=816704&view=rev
Log:
Created DefaultFilterChainManagerTest - 100% code coverage - yay!

Added:
    incubator/shiro/trunk/web/src/test/java/org/apache/shiro/web/filter/mgt/
    incubator/shiro/trunk/web/src/test/java/org/apache/shiro/web/filter/mgt/DefaultFilterChainManagerTest.java
Modified:
    incubator/shiro/trunk/web/src/main/java/org/apache/shiro/web/filter/mgt/DefaultFilterChainManager.java

Modified: incubator/shiro/trunk/web/src/main/java/org/apache/shiro/web/filter/mgt/DefaultFilterChainManager.java
URL: http://svn.apache.org/viewvc/incubator/shiro/trunk/web/src/main/java/org/apache/shiro/web/filter/mgt/DefaultFilterChainManager.java?rev=816704&r1=816703&r2=816704&view=diff
==============================================================================
--- incubator/shiro/trunk/web/src/main/java/org/apache/shiro/web/filter/mgt/DefaultFilterChainManager.java
(original)
+++ incubator/shiro/trunk/web/src/main/java/org/apache/shiro/web/filter/mgt/DefaultFilterChainManager.java
Fri Sep 18 16:32:22 2009
@@ -19,6 +19,7 @@
 package org.apache.shiro.web.filter.mgt;
 
 import org.apache.shiro.config.ConfigurationException;
+import org.apache.shiro.util.ClassUtils;
 import org.apache.shiro.util.CollectionUtils;
 import org.apache.shiro.util.Nameable;
 import org.apache.shiro.util.StringUtils;
@@ -32,7 +33,6 @@
 import org.apache.shiro.web.filter.authz.PortFilter;
 import org.apache.shiro.web.filter.authz.RolesAuthorizationFilter;
 import org.apache.shiro.web.filter.authz.SslFilter;
-import org.apache.shiro.web.servlet.ProxiedFilterChain;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -40,7 +40,10 @@
 import javax.servlet.FilterChain;
 import javax.servlet.FilterConfig;
 import javax.servlet.ServletException;
-import java.util.*;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
 
 /**
  * @since 1.0
@@ -138,7 +141,7 @@
         //
         //     { "authc", "roles[admin,user]", "perms[file:edit]" }
         //
-        String[] filterTokens = split(chainDefinition, ',', '[', ']', true, true);
+        String[] filterTokens = split(chainDefinition);
 
         //each token is specific to each filter.
         //strip the name and extract any filter-specific config between brackets [ ]
@@ -176,6 +179,9 @@
     }
 
     public void addToChain(String chainName, String filterName, String chainSpecificFilterConfig)
{
+        if (!StringUtils.hasText(chainName)) {
+            throw new IllegalArgumentException("chainName cannot be null or empty.");
+        }
         Filter filter = getFilter(filterName);
         if (filter == null) {
             throw new IllegalArgumentException("There is no filter with name '" + filterName
+
@@ -241,48 +247,6 @@
     }
 
     /**
-     * Returns the {@code FilterChain} to use for the specified application path, or {@code
null} if there
-     * was not a configured chain for the specified path.
-     * <p/>
-     * The default implementation simply calls <code>this.chains.get(chainUrl)</code>
to acquire the configured
-     * {@code List&lt;Filter&gt;} filter chain.  If that configured chain is non-null
and not empty, it is
-     * returned, otherwise {@code null} is returned to indicate that the {@code originalChain}
should be
-     * used instead.
-     *
-     * @param chainUrl      the configured filter chain url
-     * @param originalChain the original FilterChain given by the Servlet container.
-     * @return the {@code FilterChain} to use for the specified application path, or {@code
null} if the
-     *         original {@code FilterChain} should be used.
-     */
-    public FilterChain getChain(String chainUrl, FilterChain originalChain) {
-        Map<String, NamedFilterList> filterChains = getFilterChains();
-        List<Filter> pathFilters = filterChains != null ? filterChains.get(chainUrl)
: null;
-        if (!CollectionUtils.isEmpty(pathFilters)) {
-            return createChain(pathFilters, originalChain);
-        }
-        return null;
-    }
-
-    /**
-     * Creates a new FilterChain based on the specified configured url filter chain and original
chain.
-     * <p/>
-     * The input arguments are expected be be non-null and non-empty, since these conditions
are accounted for in the
-     * {@link #getChain(String, javax.servlet.FilterChain) getChain(chainUrl,originalChain)}
implementation that
-     * calls this method.
-     * <p/>
-     * The default implementation merely returns
-     * <code>new {@link org.apache.shiro.web.servlet.ProxiedFilterChain FilterChainWrapper(filters,
originalChain)}</code>,
-     * and can be overridden by subclasses for custom creation.
-     *
-     * @param filters       the configured filter chain for the incoming request application
path
-     * @param originalChain the original FilterChain given by the Servlet container.
-     * @return a new FilterChain based on the specified configured url filter chain and original
chain.
-     */
-    protected FilterChain createChain(List<Filter> filters, FilterChain originalChain)
{
-        return new ProxiedFilterChain(originalChain, filters);
-    }
-
-    /**
      * Initializes the filter by calling <code>filter.init( {@link #getFilterConfig()
getFilterConfig()} );</code>.
      *
      * @param filter the filter to initialize with the {@code FilterConfig}.
@@ -301,13 +265,33 @@
     }
 
     protected void addDefaultFilters(boolean init) {
-        addFilter("anon", new AnonymousFilter(), init, false);
-        addFilter("user", new UserFilter(), init, false);
-        addFilter("authc", new FormAuthenticationFilter(), init, false);
-        addFilter("authcBasic", new BasicHttpAuthenticationFilter(), init, false);
-        addFilter("roles", new RolesAuthorizationFilter(), init, false);
-        addFilter("perms", new PermissionsAuthorizationFilter(), init, false);
-        addFilter("port", new PortFilter(), init, false);
-        addFilter("ssl", new SslFilter(), init, false);
+        for (DefaultFilter defaultFilter : DefaultFilter.values()) {
+            addFilter(defaultFilter.name(), defaultFilter.newInstance(), init, false);
+        }
+    }
+
+    protected static enum DefaultFilter {
+        anon(AnonymousFilter.class),
+        user(UserFilter.class),
+        authc(FormAuthenticationFilter.class),
+        authcBasic(BasicHttpAuthenticationFilter.class),
+        roles(RolesAuthorizationFilter.class),
+        perms(PermissionsAuthorizationFilter.class),
+        port(PortFilter.class),
+        ssl(SslFilter.class);
+
+        private final Class<? extends Filter> filterClass;
+
+        private DefaultFilter(Class<? extends Filter> filterClass) {
+            this.filterClass = filterClass;
+        }
+
+        public Filter newInstance() {
+            return (Filter) ClassUtils.newInstance(this.filterClass);
+        }
+
+        public Class<? extends Filter> getFilterClass() {
+            return this.filterClass;
+        }
     }
 }

Added: incubator/shiro/trunk/web/src/test/java/org/apache/shiro/web/filter/mgt/DefaultFilterChainManagerTest.java
URL: http://svn.apache.org/viewvc/incubator/shiro/trunk/web/src/test/java/org/apache/shiro/web/filter/mgt/DefaultFilterChainManagerTest.java?rev=816704&view=auto
==============================================================================
--- incubator/shiro/trunk/web/src/test/java/org/apache/shiro/web/filter/mgt/DefaultFilterChainManagerTest.java
(added)
+++ incubator/shiro/trunk/web/src/test/java/org/apache/shiro/web/filter/mgt/DefaultFilterChainManagerTest.java
Fri Sep 18 16:32:22 2009
@@ -0,0 +1,213 @@
+/*
+ * 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.shiro.web.filter.mgt;
+
+import org.apache.shiro.config.ConfigurationException;
+import org.apache.shiro.web.filter.authz.SslFilter;
+import org.apache.shiro.web.servlet.ShiroFilter;
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.*;
+import org.junit.Before;
+import org.junit.Test;
+
+import javax.servlet.*;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Test case for the {@link DefaultFilterChainManager} implementation.
+ *
+ * @since 1.0
+ */
+public class DefaultFilterChainManagerTest {
+
+    DefaultFilterChainManager manager;
+
+    @Before
+    public void setUp() {
+        this.manager = new DefaultFilterChainManager();
+    }
+
+    @Test
+    public void testNewInstanceDefaultFilters() {
+        for (DefaultFilterChainManager.DefaultFilter defaultFilter : DefaultFilterChainManager.DefaultFilter.values())
{
+            assertNotNull(manager.getFilter(defaultFilter.name()));
+        }
+        assertFalse(manager.hasChains());
+    }
+
+    protected FilterConfig createNiceMockFilterConfig() {
+        FilterConfig mock = createNiceMock(FilterConfig.class);
+        ServletContext mockServletContext = createNiceMock(ServletContext.class);
+        expect(mock.getServletContext()).andReturn(mockServletContext);
+        return mock;
+    }
+
+    @Test
+    public void testNewInstanceWithFilterConfig() {
+        FilterConfig mock = createNiceMockFilterConfig();
+        replay(mock);
+        this.manager = new DefaultFilterChainManager(mock);
+        for (DefaultFilterChainManager.DefaultFilter defaultFilter : DefaultFilterChainManager.DefaultFilter.values())
{
+            assertNotNull(manager.getFilter(defaultFilter.name()));
+        }
+        assertFalse(manager.hasChains());
+        verify(mock);
+    }
+
+    @Test
+    public void testCreateChain() {
+        try {
+            manager.createChain(null, null);
+        } catch (NullPointerException expected) {
+        }
+        try {
+            manager.createChain("test", null);
+        } catch (NullPointerException expected) {
+        }
+
+        manager.createChain("test", "authc, roles[manager], perms[\"user:read,write:12345\"");
+
+        assertTrue(manager.hasChains());
+
+        Set<String> chainNames = manager.getChainNames();
+        assertNotNull(chainNames);
+        assertEquals(1, chainNames.size());
+        assertTrue(chainNames.contains("test"));
+
+        Map<String, NamedFilterList> chains = manager.getFilterChains();
+        assertEquals(1, chains.size());
+        assertTrue(chains.containsKey("test"));
+        manager.setFilterChains(chains);
+
+        NamedFilterList chain = manager.getChain("test");
+        assertNotNull(chain);
+
+        Filter filter = chain.get(0);
+        assertNotNull(filter);
+        assertEquals(DefaultFilterChainManager.DefaultFilter.authc.getFilterClass(), filter.getClass());
+
+        filter = chain.get(1);
+        assertNotNull(filter);
+        assertEquals(DefaultFilterChainManager.DefaultFilter.roles.getFilterClass(), filter.getClass());
+
+        filter = chain.get(2);
+        assertNotNull(filter);
+        assertEquals(DefaultFilterChainManager.DefaultFilter.perms.getFilterClass(), filter.getClass());
+    }
+
+    @Test
+    public void testBeanMethods() {
+        Map<String, Filter> filters = manager.getFilters();
+        assertEquals(filters.size(), DefaultFilterChainManager.DefaultFilter.values().length);
+        manager.setFilters(filters);
+    }
+
+    @Test
+    public void testAddFilter() {
+        FilterConfig mockFilterConfig = createNiceMockFilterConfig();
+        replay(mockFilterConfig);
+        this.manager = new DefaultFilterChainManager(mockFilterConfig);
+        manager.addFilter("test", new SslFilter());
+        Filter filter = manager.getFilter("test");
+        assertNotNull(filter);
+        assertEquals(SslFilter.class, filter.getClass());
+        verify(mockFilterConfig);
+    }
+
+    @Test(expected = ConfigurationException.class)
+    public void testAddFilterInitThrowsException() {
+        FilterConfig mockFilterConfig = createNiceMockFilterConfig();
+        Filter mockFilter = createNiceMock(Filter.class);
+
+        try {
+            mockFilter.init(isA(FilterConfig.class));
+        } catch (ServletException e) {
+            fail("test setup failure.");
+        }
+        //noinspection ThrowableInstanceNeverThrown
+        expectLastCall().andThrow(new ServletException());
+
+        replay(mockFilterConfig);
+        replay(mockFilter);
+
+        this.manager = new DefaultFilterChainManager(mockFilterConfig);
+
+        this.manager.addFilter("blah", mockFilter);
+
+        verify(mockFilterConfig);
+        verify(mockFilter);
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void testAddFilterNoFilterConfig() {
+        manager.addFilter("test", new SslFilter());
+    }
+
+    @Test
+    public void testAddToChain() {
+        FilterConfig mockFilterConfig = createNiceMockFilterConfig();
+        replay(mockFilterConfig);
+        this.manager = new DefaultFilterChainManager(mockFilterConfig);
+
+        manager.addFilter("testSsl", new SslFilter());
+        manager.createChain("test", "anon");
+
+        try {
+            manager.addToChain("test", null);
+        } catch (IllegalArgumentException expected) {
+        }
+        try {
+            manager.addToChain(null, "testSsl");
+        } catch (IllegalArgumentException expected) {
+        }
+    }
+
+    @Test
+    public void testAddToChainNotPathProcessor() {
+        FilterConfig mockFilterConfig = createNiceMockFilterConfig();
+        replay(mockFilterConfig);
+        this.manager = new DefaultFilterChainManager(mockFilterConfig);
+
+        manager.addFilter("nonPathProcessor", new ShiroFilter());
+        manager.createChain("test", "nonPathProcessor");
+
+        try {
+            manager.addToChain("test", "nonPathProcessor", "dummyConfig");
+        } catch (ConfigurationException expected) {
+        }
+    }
+
+    @Test
+    public void testProxy() {
+        FilterChain mock = createNiceMock(FilterChain.class);
+        replay(mock);
+        manager.createChain("test", "anon");
+        this.manager.proxy(mock, "test");
+        verify(mock);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testProxyNoChain() {
+        FilterChain mock = createNiceMock(FilterChain.class);
+        replay(mock);
+        this.manager.proxy(mock, "blah");
+        verify(mock);
+    }
+}



Mime
View raw message