From e6bec6cd5ba426113cbb7c2804b9f429c6b098ac Mon Sep 17 00:00:00 2001 From: Brian Demers Date: Wed, 6 Jul 2016 17:16:42 -0400 Subject: [PATCH] SHIRO-425 Ability to set a property value with an enum in shiro.ini file --- .../shiro/config/ReflectionBuilder.java | 13 ++++++ .../shiro/config/ReflectionBuilderTest.groovy | 40 +++++++++++++++++++ .../org/apache/shiro/config/SimpleEnum.groovy | 29 ++++++++++++++ .../config/SimpleEnumPropertyObject.groovy | 34 ++++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 config/ogdl/src/test/groovy/org/apache/shiro/config/SimpleEnum.groovy create mode 100644 config/ogdl/src/test/groovy/org/apache/shiro/config/SimpleEnumPropertyObject.groovy diff --git a/config/ogdl/src/main/java/org/apache/shiro/config/ReflectionBuilder.java b/config/ogdl/src/main/java/org/apache/shiro/config/ReflectionBuilder.java index 22a05f77ef..d29861cdcd 100644 --- a/config/ogdl/src/main/java/org/apache/shiro/config/ReflectionBuilder.java +++ b/config/ogdl/src/main/java/org/apache/shiro/config/ReflectionBuilder.java @@ -422,6 +422,17 @@ protected boolean isTypedProperty(Object object, String propertyName, Class claz throw new ConfigurationException(msg, e); } } + //@since 1.3 : be able to set an enum value for an object property + protected Enum toEnum(Object object, String propertyName, String sValue) { + try { + PropertyDescriptor descriptor = PropertyUtils.getPropertyDescriptor(object, propertyName); + Class propertyClazz = (Class) descriptor.getPropertyType(); + return Enum.valueOf(propertyClazz, sValue); + } catch (Exception e) { + String msg = "Unable to cast property [" + propertyName + "] into an enum for value " + sValue; + throw new ConfigurationException(msg, e); + } + } protected Set toSet(String sValue) { String[] tokens = StringUtils.split(sValue); @@ -698,6 +709,8 @@ protected void applyProperty(Object object, String propertyName, String stringVa } else if (isIndexedPropertyAssignment(propertyName)) { String checked = checkForNullOrEmptyLiteral(stringValue); value = resolveValue(checked); + } else if (isTypedProperty(object, propertyName, Enum.class)) { + value = toEnum(object, propertyName, stringValue); } else if (isTypedProperty(object, propertyName, Set.class)) { value = toSet(stringValue); } else if (isTypedProperty(object, propertyName, Map.class)) { diff --git a/config/ogdl/src/test/groovy/org/apache/shiro/config/ReflectionBuilderTest.groovy b/config/ogdl/src/test/groovy/org/apache/shiro/config/ReflectionBuilderTest.groovy index ffeb31815f..f73e355c06 100644 --- a/config/ogdl/src/test/groovy/org/apache/shiro/config/ReflectionBuilderTest.groovy +++ b/config/ogdl/src/test/groovy/org/apache/shiro/config/ReflectionBuilderTest.groovy @@ -524,4 +524,44 @@ class ReflectionBuilderTest { assertEquals(5, bean.getIntProp()); assertEquals("someString", bean.getStringProp()); } + + @Test + public void testParsingEnum() { + + String objectName = "myObject"; + String objectProperty = objectName + ".enumProperty"; + String listProperty = objectName + ".enumList"; + String mapProperty = objectName + ".enumMap"; + + ReflectionBuilder reflectionBuilder = new ReflectionBuilder(); + // Order matters + Map lines = new LinkedHashMap(); + lines.put(objectName, SimpleEnumPropertyObject.class.getName()); + + lines.put(objectProperty, SimpleEnum.VALUE2.toString()); + lines.put(listProperty, SimpleEnum.VALUE2.toString() +", " + SimpleEnum.VALUE1.toString() ); + lines.put(mapProperty, "two:"+ SimpleEnum.VALUE2.toString() +", one:" + SimpleEnum.VALUE1.toString() ); + + Map results = reflectionBuilder.buildObjects(lines); + + // check that buildObjects returns the expected object + Object obj = results.get(objectName); + assertTrue(obj instanceof SimpleEnumPropertyObject); + SimpleEnumPropertyObject myObject = (SimpleEnumPropertyObject)obj; + assertEquals(SimpleEnum.VALUE2, myObject.getEnumProperty()); + + // now getBean should do the same thing + obj = reflectionBuilder.getBean(objectName); + assertTrue(obj instanceof SimpleEnumPropertyObject); + myObject = (SimpleEnumPropertyObject)obj; + assertEquals(SimpleEnum.VALUE2, myObject.getEnumProperty()); + + // TODO: FIX these + def expectedResultList = [SimpleEnum.VALUE2, SimpleEnum.VALUE1]; + assertEquals(expectedResultList, myObject.getEnumList()); + + def expectedResultMap = [two: SimpleEnum.VALUE2, one: SimpleEnum.VALUE1] + assertEquals(expectedResultMap, myObject.getEnumMap()); + + } } diff --git a/config/ogdl/src/test/groovy/org/apache/shiro/config/SimpleEnum.groovy b/config/ogdl/src/test/groovy/org/apache/shiro/config/SimpleEnum.groovy new file mode 100644 index 0000000000..1f5d0356c2 --- /dev/null +++ b/config/ogdl/src/test/groovy/org/apache/shiro/config/SimpleEnum.groovy @@ -0,0 +1,29 @@ +/* + * 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.config + +/** + * Simple enumeration for tests. + * + * @since 1.3.0 + */ +enum SimpleEnum { + VALUE1, VALUE2 +} \ No newline at end of file diff --git a/config/ogdl/src/test/groovy/org/apache/shiro/config/SimpleEnumPropertyObject.groovy b/config/ogdl/src/test/groovy/org/apache/shiro/config/SimpleEnumPropertyObject.groovy new file mode 100644 index 0000000000..ca6899dfdb --- /dev/null +++ b/config/ogdl/src/test/groovy/org/apache/shiro/config/SimpleEnumPropertyObject.groovy @@ -0,0 +1,34 @@ +/* + * 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.config + +/** + * Simple object with enum property for tests. + * + * @since 1.3.0 + */ +class SimpleEnumPropertyObject { + + SimpleEnum enumProperty; + + List enumList; + + Map enumMap = new LinkedHashMap(); +} \ No newline at end of file