shiro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lhazlew...@apache.org
Subject svn commit: r1479224 - in /shiro/branches/1.2.x/core/src: main/java/org/apache/shiro/config/ReflectionBuilder.java test/groovy/org/apache/shiro/config/ReflectionBuilderTest.groovy
Date Sun, 05 May 2013 01:23:59 GMT
Author: lhazlewood
Date: Sun May  5 01:23:59 2013
New Revision: 1479224

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

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

Modified: shiro/branches/1.2.x/core/src/main/java/org/apache/shiro/config/ReflectionBuilder.java
URL: http://svn.apache.org/viewvc/shiro/branches/1.2.x/core/src/main/java/org/apache/shiro/config/ReflectionBuilder.java?rev=1479224&r1=1479223&r2=1479224&view=diff
==============================================================================
--- shiro/branches/1.2.x/core/src/main/java/org/apache/shiro/config/ReflectionBuilder.java
(original)
+++ shiro/branches/1.2.x/core/src/main/java/org/apache/shiro/config/ReflectionBuilder.java
Sun May  5 01:23:59 2013
@@ -270,6 +270,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:
@@ -288,6 +297,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);
@@ -310,6 +327,31 @@ public class ReflectionBuilder {
         return map;
     }
 
+    // @since 1.2.2
+    // TODO: make protected in 1.3+
+    private 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);
@@ -317,6 +359,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) {
@@ -495,9 +545,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/branches/1.2.x/core/src/test/groovy/org/apache/shiro/config/ReflectionBuilderTest.groovy
URL: http://svn.apache.org/viewvc/shiro/branches/1.2.x/core/src/test/groovy/org/apache/shiro/config/ReflectionBuilderTest.groovy?rev=1479224&r1=1479223&r2=1479224&view=diff
==============================================================================
--- shiro/branches/1.2.x/core/src/test/groovy/org/apache/shiro/config/ReflectionBuilderTest.groovy
(original)
+++ shiro/branches/1.2.x/core/src/test/groovy/org/apache/shiro/config/ReflectionBuilderTest.groovy
Sun May  5 01:23:59 2013
@@ -23,12 +23,15 @@ import org.apache.shiro.codec.CodecSuppo
 import org.apache.shiro.codec.Hex
 import org.apache.shiro.realm.ldap.JndiLdapRealm
 import org.apache.shiro.util.CollectionUtils
+import org.junit.Test
+import static org.junit.Assert.*
 
 /**
  * Unit tests for the {@link ReflectionBuilder} implementation.
  */
-class ReflectionBuilderTest extends GroovyTestCase {
+class ReflectionBuilderTest {
 
+    @Test
     void testStandardPropertyAssignment() {
         ReflectionBuilder builder = new ReflectionBuilder();
 
@@ -43,7 +46,8 @@ class ReflectionBuilderTest extends Groo
         assertTrue cBean.intProp == 42
         assertTrue cBean.simpleBean instanceof SimpleBean
     }
-    
+
+    @Test
     void testMapEntryAssignment() {
         ReflectionBuilder builder = new ReflectionBuilder();
 
@@ -55,6 +59,7 @@ class ReflectionBuilderTest extends Groo
         assertTrue cBean.simpleBeanMap['simpleBean2'] instanceof SimpleBean
     }
 
+    @Test
     void testArrayEntryAssignment() {
         ReflectionBuilder builder = new ReflectionBuilder();
 
@@ -66,6 +71,7 @@ class ReflectionBuilderTest extends Groo
         assertTrue cBean.compositeBeanArray[0] instanceof CompositeBean
     }
 
+    @Test
     void testNestedPathAssignment() {
         ReflectionBuilder builder = new ReflectionBuilder();
 
@@ -80,6 +86,7 @@ class ReflectionBuilderTest extends Groo
         assertTrue cbean1.compositeBeanMap['cbean2'].compositeBeanArray[0].simpleBean.name
== 'sbean1'
     }
 
+    @Test
     //asserts SHIRO-305: https://issues.apache.org/jira/browse/SHIRO-305
     void testNestedMapAssignmentWithPeriodDelimitedKeys() {
         def ini = new Ini()
@@ -99,6 +106,7 @@ class ReflectionBuilderTest extends Groo
         assertEquals 'plain ssl', ldapRealm.contextFactory.environment['com.sun.jndi.ldap.connect.pool.protocol']
     }
 
+    @Test
     void testSimpleConfig() {
         Map<String, String> defs = new LinkedHashMap<String, String>();
         defs.put("compositeBean", "org.apache.shiro.config.CompositeBean");
@@ -116,6 +124,7 @@ class ReflectionBuilderTest extends Groo
         assertEquals(compositeBean.getIntProp(), 42);
     }
 
+    @Test
     void testWithConfiguredNullValue() {
         Map<String,Object> defaults = new LinkedHashMap<String,Object>();
         CompositeBean cBean = new CompositeBean();
@@ -139,6 +148,7 @@ class ReflectionBuilderTest extends Groo
         assertNull(compositeBean.getSimpleBean());
     }
 
+    @Test
     void testWithConfiguredNullLiteralValue() {
         Map<String, String> defs = new LinkedHashMap<String, String>();
         defs.put("compositeBean", "org.apache.shiro.config.CompositeBean");
@@ -156,6 +166,7 @@ class ReflectionBuilderTest extends Groo
         assertEquals("null", compositeBean.getStringProp());
     }
 
+    @Test
     void testWithConfiguredEmptyStringValue() {
         Map<String, String> defs = new LinkedHashMap<String, String>();
         defs.put("compositeBean", "org.apache.shiro.config.CompositeBean");
@@ -173,6 +184,7 @@ class ReflectionBuilderTest extends Groo
         assertEquals("", compositeBean.getStringProp());
     }
 
+    @Test
     void testWithConfiguredEmptyStringLiteralValue() {
         Map<String, String> defs = new LinkedHashMap<String, String>();
         defs.put("compositeBean", "org.apache.shiro.config.CompositeBean");
@@ -190,6 +202,7 @@ class ReflectionBuilderTest extends Groo
         assertEquals("\"\"", compositeBean.getStringProp());
     }
 
+    @Test
     void testSimpleConfigWithDollarSignStringValue() {
         Map<String, String> defs = new LinkedHashMap<String, String>();
         defs.put("compositeBean", "org.apache.shiro.config.CompositeBean");
@@ -202,6 +215,7 @@ class ReflectionBuilderTest extends Groo
         assertEquals(compositeBean.getStringProp(), '$500');
     }
 
+    @Test
     void testObjectReferenceConfig() {
         Map<String, String> defs = new LinkedHashMap<String, String>();
         defs.put("simpleBean", "org.apache.shiro.config.SimpleBean");
@@ -223,6 +237,7 @@ class ReflectionBuilderTest extends Groo
         assertEquals(simpleBean.getIntProp(), 101);
     }
 
+    @Test
     void testObjectReferenceConfigWithTypeMismatch() {
         Map<String, String> defs = new LinkedHashMap<String, String>();
         defs.put("simpleBean", "org.apache.shiro.config.SimpleBean");
@@ -236,6 +251,7 @@ class ReflectionBuilderTest extends Groo
         }
     }
 
+    @Test
     void testObjectReferenceConfigWithInvalidReference() {
         Map<String, String> defs = new LinkedHashMap<String, String>();
         defs.put("simpleBean", "org.apache.shiro.config.SimpleBean");
@@ -249,6 +265,7 @@ class ReflectionBuilderTest extends Groo
         }
     }
 
+    @Test
     void testSetProperty() {
         Map<String, String> defs = new LinkedHashMap<String, String>();
         defs.put("simpleBean1", "org.apache.shiro.config.SimpleBean");
@@ -265,6 +282,31 @@ class ReflectionBuilderTest extends Groo
         assertEquals(2, simpleBeans.size());
     }
 
+    @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");
@@ -281,6 +323,30 @@ class ReflectionBuilderTest extends Groo
         assertEquals(3, simpleBeans.size());
     }
 
+    @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");
@@ -298,6 +364,31 @@ class ReflectionBuilderTest extends Groo
         assertEquals(3, simpleBeans.size());
     }
 
+    @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);
@@ -318,6 +409,7 @@ class ReflectionBuilderTest extends Groo
         assertEquals(source, reconstituted);
     }
 
+    @Test
     void testByteArrayBase64Property() {
         String source = "Hello, world.";
         byte[] bytes = CodecSupport.toBytes(source);
@@ -336,6 +428,7 @@ class ReflectionBuilderTest extends Groo
         assertEquals(reconstituted, source);
     }
 
+    @Test
     void testMapProperty() {
         Map<String, String> defs = new LinkedHashMap<String, String>();
         defs.put("simpleBean1", "org.apache.shiro.config.SimpleBean");
@@ -356,6 +449,30 @@ class ReflectionBuilderTest extends Groo
         assertTrue(value instanceof SimpleBean);
     }
 
+    @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");
@@ -376,6 +493,7 @@ class ReflectionBuilderTest extends Groo
         assertEquals(2, children.size());
     }
 
+    @Test
     //asserts SHIRO-413
     void testInitializable() {
         def defs = [
@@ -387,6 +505,7 @@ class ReflectionBuilderTest extends Groo
         assertTrue bean.isInitialized()
     }
 
+    @Test
     void testFactoryInstantiation() {
         Map<String, String> defs = new LinkedHashMap<String, String>();
         defs.put("simpleBeanFactory", "org.apache.shiro.config.SimpleBeanFactory");



Mime
View raw message