shiro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lhazlew...@apache.org
Subject svn commit: r1479225 - in /shiro/trunk/core/src: main/java/org/apache/shiro/config/ReflectionBuilder.java test/groovy/org/apache/shiro/config/ReflectionBuilderTest.groovy
Date Sun, 05 May 2013 01:24:32 GMT
Author: lhazlewood
Date: Sun May  5 01:24:32 2013
New Revision: 1479225

URL: http://svn.apache.org/r1479225
Log:
SHIRO-423: implemented fix w/ accompanying tests

Modified:
    shiro/trunk/core/src/main/java/org/apache/shiro/config/ReflectionBuilder.java
    shiro/trunk/core/src/test/groovy/org/apache/shiro/config/ReflectionBuilderTest.groovy

Modified: shiro/trunk/core/src/main/java/org/apache/shiro/config/ReflectionBuilder.java
URL: http://svn.apache.org/viewvc/shiro/trunk/core/src/main/java/org/apache/shiro/config/ReflectionBuilder.java?rev=1479225&r1=1479224&r2=1479225&view=diff
==============================================================================
--- shiro/trunk/core/src/main/java/org/apache/shiro/config/ReflectionBuilder.java (original)
+++ shiro/trunk/core/src/main/java/org/apache/shiro/config/ReflectionBuilder.java Sun May
 5 01:24:32 2013
@@ -428,6 +428,15 @@ public class ReflectionBuilder {
         if (tokens == null || tokens.length <= 0) {
             return null;
         }
+
+        //SHIRO-423: check to see if the value is a referenced Set already, and if so, return
it immediately:
+        if (tokens.length == 1 && isReference(tokens[0])) {
+            Object reference = resolveReference(tokens[0]);
+            if (reference instanceof Set) {
+                return (Set)reference;
+            }
+        }
+
         Set<String> setTokens = new LinkedHashSet<String>(Arrays.asList(tokens));
 
         //now convert into correct values and/or references:
@@ -446,6 +455,14 @@ public class ReflectionBuilder {
             return null;
         }
 
+        //SHIRO-423: check to see if the value is a referenced Map already, and if so, return
it immediately:
+        if (tokens.length == 1 && isReference(tokens[0])) {
+            Object reference = resolveReference(tokens[0]);
+            if (reference instanceof Map) {
+                return (Map)reference;
+            }
+        }
+
         Map<String, String> mapTokens = new LinkedHashMap<String, String>(tokens.length);
         for (String token : tokens) {
             String[] kvPair = StringUtils.split(token, MAP_KEY_VALUE_DELIMITER);
@@ -468,6 +485,30 @@ public class ReflectionBuilder {
         return map;
     }
 
+    // @since 1.2.2
+    protected Collection<?> toCollection(String sValue) {
+
+        String[] tokens = StringUtils.split(sValue);
+        if (tokens == null || tokens.length <= 0) {
+            return null;
+        }
+
+        //SHIRO-423: check to see if the value is a referenced Collection already, and if
so, return it immediately:
+        if (tokens.length == 1 && isReference(tokens[0])) {
+            Object reference = resolveReference(tokens[0]);
+            if (reference instanceof Collection) {
+                return (Collection)reference;
+            }
+        }
+
+        //now convert into correct values and/or references:
+        List<Object> values = new ArrayList<Object>(tokens.length);
+        for (String token : tokens) {
+            Object value = resolveValue(token);
+            values.add(value);
+        }
+        return values;
+    }
 
     protected List<?> toList(String sValue) {
         String[] tokens = StringUtils.split(sValue);
@@ -475,6 +516,14 @@ public class ReflectionBuilder {
             return null;
         }
 
+        //SHIRO-423: check to see if the value is a referenced List already, and if so, return
it immediately:
+        if (tokens.length == 1 && isReference(tokens[0])) {
+            Object reference = resolveReference(tokens[0]);
+            if (reference instanceof List) {
+                return (List)reference;
+            }
+        }
+
         //now convert into correct values and/or references:
         List<Object> values = new ArrayList<Object>(tokens.length);
         for (String token : tokens) {
@@ -653,9 +702,10 @@ public class ReflectionBuilder {
             value = toSet(stringValue);
         } else if (isTypedProperty(object, propertyName, Map.class)) {
             value = toMap(stringValue);
-        } else if (isTypedProperty(object, propertyName, List.class) ||
-                isTypedProperty(object, propertyName, Collection.class)) {
+        } else if (isTypedProperty(object, propertyName, List.class)) {
             value = toList(stringValue);
+        } else if (isTypedProperty(object, propertyName, Collection.class)) {
+            value = toCollection(stringValue);
         } else if (isTypedProperty(object, propertyName, byte[].class)) {
             value = toBytes(stringValue);
         } else if (isTypedProperty(object, propertyName, ByteSource.class)) {

Modified: shiro/trunk/core/src/test/groovy/org/apache/shiro/config/ReflectionBuilderTest.groovy
URL: http://svn.apache.org/viewvc/shiro/trunk/core/src/test/groovy/org/apache/shiro/config/ReflectionBuilderTest.groovy?rev=1479225&r1=1479224&r2=1479225&view=diff
==============================================================================
--- shiro/trunk/core/src/test/groovy/org/apache/shiro/config/ReflectionBuilderTest.groovy
(original)
+++ shiro/trunk/core/src/test/groovy/org/apache/shiro/config/ReflectionBuilderTest.groovy
Sun May  5 01:24:32 2013
@@ -21,7 +21,6 @@ package org.apache.shiro.config
 import org.apache.shiro.codec.Base64
 import org.apache.shiro.codec.CodecSupport
 import org.apache.shiro.codec.Hex
-import org.apache.shiro.config.event.BeanEvent
 import org.apache.shiro.realm.ldap.JndiLdapRealm
 import org.apache.shiro.util.CollectionUtils
 import org.junit.Test
@@ -43,7 +42,7 @@ class ReflectionBuilderTest {
         builder.applyProperty(cBean, 'booleanProp', true)
         builder.applyProperty(cBean, 'intProp', 42)
         builder.applyProperty(cBean, 'simpleBean', new SimpleBean())
-        
+
         assertTrue cBean.stringProp == 'hello world'
         assertTrue cBean.booleanProp
         assertTrue cBean.intProp == 42
@@ -56,9 +55,9 @@ class ReflectionBuilderTest {
 
         CompositeBean cBean = new CompositeBean();
         cBean.simpleBeanMap = ['simpleBean1': new SimpleBean()]
-        
+
         builder.applyProperty(cBean, 'simpleBeanMap[simpleBean2]', new SimpleBean())
-        
+
         assertTrue cBean.simpleBeanMap['simpleBean2'] instanceof SimpleBean
     }
 
@@ -81,7 +80,7 @@ class ReflectionBuilderTest {
         CompositeBean cbean1 = new CompositeBean('cbean1');
         cbean1.compositeBeanMap = ['cbean2': new CompositeBean('cbean2')]
         cbean1.compositeBeanMap['cbean2'].compositeBeanArray = new CompositeBean[2];
-        
+
         builder.applyProperty(cbean1, "compositeBeanMap[cbean2].compositeBeanArray[0]", new
CompositeBean('cbean3'))
         builder.applyProperty(cbean1, "compositeBeanMap[cbean2].compositeBeanArray[0].simpleBean",
new SimpleBean('sbean1'))
 
@@ -89,8 +88,8 @@ class ReflectionBuilderTest {
         assertTrue cbean1.compositeBeanMap['cbean2'].compositeBeanArray[0].simpleBean.name
== 'sbean1'
     }
 
-    //asserts SHIRO-305: https://issues.apache.org/jira/browse/SHIRO-305
     @Test
+    //asserts SHIRO-305: https://issues.apache.org/jira/browse/SHIRO-305
     void testNestedMapAssignmentWithPeriodDelimitedKeys() {
         def ini = new Ini()
         ini.load('''
@@ -101,7 +100,7 @@ class ReflectionBuilderTest {
         ''')
         def builder = new ReflectionBuilder()
         def objects = builder.buildObjects(ini.getSections().iterator().next())
-        
+
         assertFalse objects.isEmpty()
         def ldapRealm = objects['ldapRealm'] as JndiLdapRealm
         assertEquals 'ssl', ldapRealm.contextFactory.environment['java.naming.security.protocol']
@@ -240,24 +239,32 @@ class ReflectionBuilderTest {
         assertEquals(simpleBean.getIntProp(), 101);
     }
 
-    @Test(expected=ConfigurationException)
+    @Test
     void testObjectReferenceConfigWithTypeMismatch() {
         Map<String, String> defs = new LinkedHashMap<String, String>();
         defs.put("simpleBean", "org.apache.shiro.config.SimpleBean");
         defs.put("compositeBean", "org.apache.shiro.config.CompositeBean");
         defs.put("compositeBean.simpleBean", "simpleBean");
         ReflectionBuilder builder = new ReflectionBuilder();
-        builder.buildObjects(defs);
+        try {
+            builder.buildObjects(defs);
+            "Should have encountered an " + ConfigurationException.class.name
+        } catch (ConfigurationException expected) {
+        }
     }
 
-    @Test(expected=UnresolveableReferenceException)
+    @Test
     void testObjectReferenceConfigWithInvalidReference() {
         Map<String, String> defs = new LinkedHashMap<String, String>();
         defs.put("simpleBean", "org.apache.shiro.config.SimpleBean");
         defs.put("compositeBean", "org.apache.shiro.config.CompositeBean");
         defs.put("compositeBean.simpleBean", '$foo');
         ReflectionBuilder builder = new ReflectionBuilder();
-        builder.buildObjects(defs);
+        try {
+            builder.buildObjects(defs);
+            fail "should have encountered an " + UnresolveableReferenceException.class.name
+        } catch (UnresolveableReferenceException expected) {
+        }
     }
 
     @Test
@@ -278,6 +285,30 @@ class ReflectionBuilderTest {
     }
 
     @Test
+    //SHIRO-423
+    void testSetPropertyWithReferencedSet() {
+        def set = [new SimpleBean('foo'), new SimpleBean('bar')] as Set
+
+        def defs = [
+                compositeBean: 'org.apache.shiro.config.CompositeBean',
+                'compositeBean.simpleBeanSet': '$set'
+        ]
+
+        ReflectionBuilder builder = new ReflectionBuilder(['set': set]);
+        Map objects = builder.buildObjects(defs);
+        assertFalse(CollectionUtils.isEmpty(objects));
+        CompositeBean cBean = (CompositeBean) objects.get("compositeBean");
+        assertNotNull(cBean);
+        Set<SimpleBean> simpleBeans = cBean.getSimpleBeanSet();
+        assertNotNull(simpleBeans);
+        assertSame set, simpleBeans
+        assertEquals(2, simpleBeans.size());
+        def i = simpleBeans.iterator()
+        assertEquals 'foo', i.next().name
+        assertEquals 'bar', i.next().name
+    }
+
+    @Test
     void testListProperty() {
         Map<String, String> defs = new LinkedHashMap<String, String>();
         defs.put("simpleBean1", "org.apache.shiro.config.SimpleBean");
@@ -295,6 +326,29 @@ class ReflectionBuilderTest {
     }
 
     @Test
+    //SHIRO-423
+    void testListPropertyWithReferencedList() {
+        List list = [new SimpleBean('foo'), new SimpleBean('bar')] as List
+
+        def defs = [
+                compositeBean: 'org.apache.shiro.config.CompositeBean',
+                'compositeBean.simpleBeanList': '$list'
+        ]
+
+        ReflectionBuilder builder = new ReflectionBuilder(['list': list]);
+        Map objects = builder.buildObjects(defs);
+        assertFalse(CollectionUtils.isEmpty(objects));
+        CompositeBean cBean = (CompositeBean) objects.get("compositeBean");
+        assertNotNull(cBean);
+        def simpleBeans = cBean.getSimpleBeanList();
+        assertNotNull(simpleBeans);
+        assertSame list, simpleBeans
+        assertEquals(2, simpleBeans.size());
+        assertEquals 'foo', simpleBeans[0].name
+        assertEquals 'bar', simpleBeans[1].name
+    }
+
+    @Test
     void testCollectionProperty() {
         Map<String, String> defs = new LinkedHashMap<String, String>();
         defs.put("simpleBean1", "org.apache.shiro.config.SimpleBean");
@@ -313,6 +367,30 @@ class ReflectionBuilderTest {
     }
 
     @Test
+    //SHIRO-423
+    void testCollectionPropertyWithReferencedCollection() {
+        def c = [new SimpleBean('foo'), new SimpleBean('bar')]
+
+        def defs = [
+                compositeBean: 'org.apache.shiro.config.CompositeBean',
+                'compositeBean.simpleBeanCollection': '$collection'
+        ]
+
+        ReflectionBuilder builder = new ReflectionBuilder(['collection': c]);
+        Map objects = builder.buildObjects(defs);
+        assertFalse(CollectionUtils.isEmpty(objects));
+        CompositeBean cBean = (CompositeBean) objects.get("compositeBean");
+        assertNotNull(cBean);
+        def simpleBeans = cBean.getSimpleBeanCollection();
+        assertNotNull(simpleBeans);
+        assertSame c, simpleBeans
+        assertEquals(2, simpleBeans.size());
+        def i  = simpleBeans.iterator()
+        assertEquals 'foo', i.next().name
+        assertEquals 'bar', i.next().name
+    }
+
+    @Test
     void testByteArrayHexProperty() {
         String source = "Hello, world.";
         byte[] bytes = CodecSupport.toBytes(source);
@@ -374,6 +452,29 @@ class ReflectionBuilderTest {
     }
 
     @Test
+    //SHIRO-423
+    void testMapPropertyWithReferencedMap() {
+        def map = ['foo': new SimpleBean('foo'), 'bar': new SimpleBean('bar')]
+
+        def defs = [
+                compositeBean: 'org.apache.shiro.config.CompositeBean',
+                'compositeBean.simpleBeanMap': '$map'
+        ]
+
+        ReflectionBuilder builder = new ReflectionBuilder(['map': map]);
+        Map objects = builder.buildObjects(defs);
+        assertFalse(CollectionUtils.isEmpty(objects));
+        CompositeBean cBean = (CompositeBean) objects.get("compositeBean");
+        assertNotNull(cBean);
+        def simpleBeansMap = cBean.getSimpleBeanMap();
+        assertNotNull(simpleBeansMap);
+        assertSame map, simpleBeansMap
+        assertEquals(2, simpleBeansMap.size());
+        assertEquals 'foo', simpleBeansMap['foo'].name
+        assertEquals 'bar', simpleBeansMap['bar'].name
+    }
+
+    @Test
     void testNestedListProperty() {
         Map<String, String> defs = new LinkedHashMap<String, String>();
         defs.put("simpleBean1", "org.apache.shiro.config.SimpleBean");
@@ -394,8 +495,8 @@ class ReflectionBuilderTest {
         assertEquals(2, children.size());
     }
 
-    //asserts SHIRO-413
     @Test
+    //asserts SHIRO-413
     void testInitializable() {
         def defs = [
                 initializableBean: 'org.apache.shiro.config.InitializableBean'
@@ -424,93 +525,4 @@ class ReflectionBuilderTest {
         assertEquals(5, bean.getIntProp());
         assertEquals("someString", bean.getStringProp());
     }
-
-    @Test
-    void testBeanListeners() {
-
-        def ini = new Ini();
-        ini.load '''
-            loggingListener = org.apache.shiro.config.event.LoggingBeanEventListener
-            listenerOne = org.apache.shiro.config.RecordingBeanListener
-            listenerTwo = org.apache.shiro.config.RecordingBeanListener
-
-            simpleBeanFactory = org.apache.shiro.config.SimpleBeanFactory
-            simpleBeanFactory.factoryInt = 5
-            simpleBeanFactory.factoryString = someString
-
-            compositeBean = org.apache.shiro.config.CompositeBean
-            compositeBean.simpleBean = $simpleBeanFactory
-        '''
-
-        ReflectionBuilder builder = new ReflectionBuilder();
-        Map<String, ?> objects = builder.buildObjects(ini.getSections().iterator().next());
-        assertFalse(CollectionUtils.isEmpty(objects));
-
-        assertInstantiatedEvents("listenerOne", objects, 4) //3 beans following + its own
instantiated event
-        assertConfiguredEvents("listenerOne", objects, 4) //3 beans following + its own configured
event
-        assertInitializedEvents("listenerOne", objects, 4) //3 beans following + its own
initialized event
-
-        assertInstantiatedEvents("listenerTwo", objects, 3) //2 beans following + its own
instantiated event
-        assertConfiguredEvents("listenerTwo", objects, 3); //2 beans following + its own
configured event
-        assertInitializedEvents("listenerTwo", objects, 3); //2 beans following + its own
initialized event
-
-        builder.destroy();
-
-        assertDestroyedEvents("listenerOne", objects, 4); //3 beans defined after it + its
own destroyed event
-        assertDestroyedEvents("listenerTwo", objects, 3); //2 beans defined after it + its
own destroyed event
-    }
-
-    void assertInstantiatedEvents(String name, Map<String, ?> objects, int expected)
{
-        def bean = objects.get(name) as RecordingBeanListener
-        def events = bean.getInstantiatedEvents()
-        assertEquals(expected, events.size())
-
-        checkType(name, events, "simpleBeanFactory", SimpleBeanFactory);
-        checkType(name, events, "compositeBean", CompositeBean);
-    }
-
-    void assertConfiguredEvents(String name, Map<String, ?> objects, int expected)
{
-        def bean = objects.get(name) as RecordingBeanListener
-        def events = bean.getConfiguredEvents();
-        assertEquals(expected, events.size())
-
-        checkType(name, events, "listenerTwo", RecordingBeanListener);
-        checkType(name, events, "simpleBeanFactory", SimpleBeanFactory);
-        checkType(name, events, "compositeBean", CompositeBean);
-    }
-
-    void assertInitializedEvents(String name, Map<String, ?> objects, int expected)
{
-        def bean = objects.get(name) as RecordingBeanListener
-        def events = bean.getInitializedEvents();
-        assertEquals(expected, events.size())
-
-        checkType(name, events, "listenerTwo", RecordingBeanListener);
-        checkType(name, events, "simpleBeanFactory", SimpleBeanFactory);
-        checkType(name, events, "compositeBean", CompositeBean);
-    }
-
-    void assertDestroyedEvents(String name, Map<String, ?> objects, int expected) {
-        def bean = objects.get(name) as RecordingBeanListener
-        def events = bean.getDestroyedEvents();
-        assertEquals(expected, events.size())
-
-        if (expected > 3) {
-            checkType(name, events, "listenerOne", RecordingBeanListener);
-        }
-        checkType(name, events, "listenerTwo", RecordingBeanListener);
-        checkType(name, events, "simpleBeanFactory", SimpleBeanFactory);
-        checkType(name, events, "compositeBean", CompositeBean);
-    }
-
-    void checkType(String instanceName, List<? extends BeanEvent> events, String name,
Class<?> expectedType) {
-        for(BeanEvent event: events) {
-            if(event.getBeanName().equals(name)) {
-                assertTrue("Notification for bean " + name + " did not provide an instance
of " + expectedType
-                        + " to listener " + instanceName,
-                expectedType.isInstance(event.getBean()))
-                return;
-            }
-        }
-        fail("No bean named " + name + " was ever notified to listener " + instanceName +
".");
-    }
 }



Mime
View raw message