shiro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lhazlew...@apache.org
Subject svn commit: r1225047 - in /shiro/trunk/web/src: main/java/org/apache/shiro/web/config/ main/java/org/apache/shiro/web/env/ test/groovy/org/apache/shiro/web/config/ test/groovy/org/apache/shiro/web/env/ test/java/org/apache/shiro/web/config/
Date Tue, 27 Dec 2011 23:33:34 GMT
Author: lhazlewood
Date: Tue Dec 27 23:33:34 2011
New Revision: 1225047

URL: http://svn.apache.org/viewvc?rev=1225047&view=rev
Log:
SHIRO-306: Fix implemented and accompanied with test cases

Added:
    shiro/trunk/web/src/test/groovy/org/apache/shiro/web/config/
    shiro/trunk/web/src/test/groovy/org/apache/shiro/web/config/IniFilterChainResolverFactoryTest.groovy
    shiro/trunk/web/src/test/groovy/org/apache/shiro/web/env/
    shiro/trunk/web/src/test/groovy/org/apache/shiro/web/env/IniWebEnvironmentTest.groovy
Removed:
    shiro/trunk/web/src/test/java/org/apache/shiro/web/config/IniFilterChainResolverFactoryTest.java
Modified:
    shiro/trunk/web/src/main/java/org/apache/shiro/web/config/IniFilterChainResolverFactory.java
    shiro/trunk/web/src/main/java/org/apache/shiro/web/env/IniWebEnvironment.java

Modified: shiro/trunk/web/src/main/java/org/apache/shiro/web/config/IniFilterChainResolverFactory.java
URL: http://svn.apache.org/viewvc/shiro/trunk/web/src/main/java/org/apache/shiro/web/config/IniFilterChainResolverFactory.java?rev=1225047&r1=1225046&r2=1225047&view=diff
==============================================================================
--- shiro/trunk/web/src/main/java/org/apache/shiro/web/config/IniFilterChainResolverFactory.java
(original)
+++ shiro/trunk/web/src/main/java/org/apache/shiro/web/config/IniFilterChainResolverFactory.java
Tue Dec 27 23:33:34 2011
@@ -140,14 +140,20 @@ public class IniFilterChainResolverFacto
 
     protected Map<String, Filter> getFilters(Map<String, String> section, Map<String,
?> defaults) {
 
-        Map<String, Filter> filters;
+        Map<String, Filter> filters = extractFilters(defaults);
 
         if (!CollectionUtils.isEmpty(section)) {
             ReflectionBuilder builder = new ReflectionBuilder(defaults);
             Map<String, ?> built = builder.buildObjects(section);
-            filters = extractFilters(built);
-        } else {
-            filters = extractFilters(defaults);
+            Map<String,Filter> sectionFilters = extractFilters(built);
+
+            if (CollectionUtils.isEmpty(filters)) {
+                filters = sectionFilters;
+            } else {
+                if (!CollectionUtils.isEmpty(sectionFilters)) {
+                    filters.putAll(sectionFilters);
+                }
+            }
         }
 
         return filters;

Modified: shiro/trunk/web/src/main/java/org/apache/shiro/web/env/IniWebEnvironment.java
URL: http://svn.apache.org/viewvc/shiro/trunk/web/src/main/java/org/apache/shiro/web/env/IniWebEnvironment.java?rev=1225047&r1=1225046&r2=1225047&view=diff
==============================================================================
--- shiro/trunk/web/src/main/java/org/apache/shiro/web/env/IniWebEnvironment.java (original)
+++ shiro/trunk/web/src/main/java/org/apache/shiro/web/env/IniWebEnvironment.java Tue Dec
27 23:33:34 2011
@@ -200,14 +200,16 @@ public class IniWebEnvironment extends R
             factory = new WebIniSecurityManagerFactory(ini);
         }
 
+        WebSecurityManager wsm = (WebSecurityManager)factory.getInstance();
+
+        //SHIRO-306 - get beans after they've been created (the call was before the factory.getInstance()
call,
+        //which always returned null.
         Map<String, ?> beans = factory.getBeans();
         if (!CollectionUtils.isEmpty(beans)) {
             this.objects.putAll(beans);
         }
 
-        // Create the security manager and check that it implements WebSecurityManager.
-        // Otherwise, it can't be used with the filter.
-        return (WebSecurityManager) factory.getInstance();
+        return wsm;
     }
 
     /**

Added: shiro/trunk/web/src/test/groovy/org/apache/shiro/web/config/IniFilterChainResolverFactoryTest.groovy
URL: http://svn.apache.org/viewvc/shiro/trunk/web/src/test/groovy/org/apache/shiro/web/config/IniFilterChainResolverFactoryTest.groovy?rev=1225047&view=auto
==============================================================================
--- shiro/trunk/web/src/test/groovy/org/apache/shiro/web/config/IniFilterChainResolverFactoryTest.groovy
(added)
+++ shiro/trunk/web/src/test/groovy/org/apache/shiro/web/config/IniFilterChainResolverFactoryTest.groovy
Tue Dec 27 23:33:34 2011
@@ -0,0 +1,160 @@
+/*
+ * 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.config
+
+import javax.servlet.Filter
+import javax.servlet.FilterConfig
+import javax.servlet.ServletContext
+import org.apache.shiro.config.Ini
+import org.apache.shiro.web.filter.authc.FormAuthenticationFilter
+import org.apache.shiro.web.filter.authz.SslFilter
+import org.apache.shiro.web.filter.mgt.FilterChainResolver
+import static org.easymock.EasyMock.*
+
+/**
+ * Unit tests for the {@link IniFilterChainResolverFactory} implementation.
+ *
+ * @since 1.2
+ */
+class IniFilterChainResolverFactoryTest extends GroovyTestCase {
+
+    private IniFilterChainResolverFactory factory;
+
+    protected FilterConfig createNiceMockFilterConfig() {
+        FilterConfig mock = createNiceMock(FilterConfig)
+        ServletContext mockServletContext = createNiceMock(ServletContext)
+        expect(mock.servletContext).andReturn(mockServletContext)
+        return mock
+    }
+
+    void setUp() {
+        this.factory = new IniFilterChainResolverFactory()
+    }
+
+    void testNewInstance() {
+        assertNull factory.filterConfig
+        factory.filterConfig = null
+        assertNull factory.filterConfig
+    }
+
+    void testGetInstanceNoIni() {
+        assertNotNull factory.getInstance()
+    }
+
+    void testNewInstanceWithIni() {
+        Ini ini = new Ini()
+        ini.load("""
+        [urls]
+        /index.html = anon
+        """)
+        factory = new IniFilterChainResolverFactory(ini)
+        FilterChainResolver resolver = factory.getInstance()
+        assertNotNull resolver
+    }
+
+    void testGetFiltersWithNullOrEmptySection() {
+        Map<String, Filter> filters = factory.getFilters(null, null);
+        assertNull(filters);
+    }
+
+    void testCreateChainsWithNullUrlsSection() {
+        //should do nothing (return immediately, no exceptions):
+        factory.createChains(null, null);
+    }
+
+    void testNewInstanceWithNonFilter() {
+        Ini ini = new Ini()
+        ini.load("""
+        [filters]
+        # any non filter will do:
+        test = org.apache.shiro.web.servlet.SimpleCookie
+        [urls]
+        /index.html = anon
+        """)
+        factory = new IniFilterChainResolverFactory(ini)
+        assertNotNull factory.getInstance()
+    }
+
+    void testNewInstanceWithFilterConfig() {
+        Ini ini = new Ini()
+        ini.load("""
+        [urls]
+        /index.html = anon
+        """)
+        factory = new IniFilterChainResolverFactory(ini)
+        FilterConfig config = createNiceMockFilterConfig()
+        factory.setFilterConfig(config)
+        
+        replay config
+        
+        FilterChainResolver resolver = factory.getInstance();
+        
+        assertNotNull resolver
+        
+        verify config
+    }
+
+    //asserts SHIRO-306
+    void testGetFilters() {
+        def extractedFilters = factory.getFilters(null, null)
+        assertNull extractedFilters
+    }
+
+    //asserts SHIRO-306
+    void testGetFiltersWithoutSectionWithDefaults() {
+        def factory = new IniFilterChainResolverFactory()
+
+        def defaults = ['filter': new FormAuthenticationFilter()]
+
+        def extractedFilters = factory.getFilters(null, defaults)
+        
+        assertNotNull extractedFilters
+        assertEquals 1, extractedFilters.size()
+        assertTrue extractedFilters['filter'] instanceof FormAuthenticationFilter
+    }
+
+    //asserts SHIRO-306
+    void testGetFiltersWithSectionWithoutDefaults() {
+        def factory = new IniFilterChainResolverFactory()
+
+        def section = ['filter': FormAuthenticationFilter.class.name]
+
+        def extractedFilters = factory.getFilters(section, null)
+
+        assertNotNull extractedFilters
+        assertEquals 1, extractedFilters.size()
+        assertTrue extractedFilters['filter'] instanceof FormAuthenticationFilter
+    }
+
+    //asserts SHIRO-306
+    void testGetFiltersWithSectionAndDefaults() {
+        def factory = new IniFilterChainResolverFactory()
+
+        def section = ['filtersSectionFilter': SslFilter.class.name]
+
+        def defaults = ['mainSectionFilter': new FormAuthenticationFilter()]
+
+        def extractedFilters = factory.getFilters(section, defaults)
+
+        assertNotNull extractedFilters
+        assertEquals 2, extractedFilters.size()
+        assertTrue extractedFilters['filtersSectionFilter'] instanceof SslFilter
+        assertTrue extractedFilters['mainSectionFilter'] instanceof FormAuthenticationFilter
+    }
+}

Added: shiro/trunk/web/src/test/groovy/org/apache/shiro/web/env/IniWebEnvironmentTest.groovy
URL: http://svn.apache.org/viewvc/shiro/trunk/web/src/test/groovy/org/apache/shiro/web/env/IniWebEnvironmentTest.groovy?rev=1225047&view=auto
==============================================================================
--- shiro/trunk/web/src/test/groovy/org/apache/shiro/web/env/IniWebEnvironmentTest.groovy
(added)
+++ shiro/trunk/web/src/test/groovy/org/apache/shiro/web/env/IniWebEnvironmentTest.groovy
Tue Dec 27 23:33:34 2011
@@ -0,0 +1,51 @@
+/*
+ * 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.env
+
+import org.apache.shiro.config.Ini
+import org.apache.shiro.web.filter.mgt.DefaultFilter
+
+/**
+ * Unit tests for the {@link IniWebEnvironment} implementation.
+ * 
+ * @since 1.2
+ */
+class IniWebEnvironmentTest extends GroovyTestCase {
+    
+    
+    //asserts SHIRO-306
+    void testObjectsAfterSecurityManagerCreation() {
+        
+        def ini = new Ini()
+        ini.load("""
+        [main]
+        compositeBean = org.apache.shiro.config.CompositeBean
+        """)
+        
+        def env = new IniWebEnvironment(ini:  ini)
+        env.init()
+
+        assertNotNull env.objects
+        //asserts that the objects size = securityManager (1) + num custom objects + num
default filters
+        def expectedSize = 2 + DefaultFilter.values().length
+        assertEquals expectedSize, env.objects.size()
+        assertNotNull env.objects['securityManager']
+        assertNotNull env.objects['compositeBean']
+    }
+}



Mime
View raw message