Author: henning Date: Mon Oct 21 21:14:18 2013 New Revision: 1534376 URL: http://svn.apache.org/r1534376 Log: Backport CONFIGURATION-550 from r1502844.
Modified: commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/RELEASE-NOTES.txt commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/changes/changes.xml commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/main/java/org/apache/commons/configuration/PropertyConverter.java commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/java/org/apache/commons/configuration/TestPropertyConverter.java Modified: commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/RELEASE-NOTES.txt URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/RELEASE-NOTES.txt?rev=1534376&r1=1534375&r2=1534376&view=diff ============================================================================== --- commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/RELEASE-NOTES.txt (original) +++ commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/RELEASE-NOTES.txt Mon Oct 21 21:14:18 2013 @@ -36,6 +36,10 @@ BUG FIXES IN 1.10 XMLConfiguration now adds attributes of elements defining a list to all list nodes. +* [CONFIGURATION-550] Missing conversion to char + + Conversion to Character is now supported. + * [CONFIGURATION-555] XMLConfiguration doesn't seem to be preserving whitespace for the current node where xml:space="preserve" is set. Modified: commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/changes/changes.xml?rev=1534376&r1=1534375&r2=1534376&view=diff ============================================================================== --- commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/changes/changes.xml (original) +++ commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/changes/changes.xml Mon Oct 21 21:14:18 2013 @@ -31,11 +31,14 @@ XMLConfiguration now adds attributes of elements defining a list to all list nodes. </action> - <action dev="oheger" type="update" issue="CONFIGURATION-555"> - Fixed a bug in the handling of the xml:space attribute in - XMLConfiguration. The attribute is now also applied to the current - element, not only to sub elements. - </action> + <action dev="oheger" type="add" issue="CONFIGURATION-550"> + Conversion to Character is now supported. + </action> + <action dev="oheger" type="update" issue="CONFIGURATION-555"> + Fixed a bug in the handling of the xml:space attribute in + XMLConfiguration. The attribute is now also applied to the current + element, not only to sub elements. + </action> <action dev="henning" type="fix" issue="CONFIGURATION-556"> In 1.7 and before, any change to the system properties was immediately reflected in a SystemConfiguration object. This Modified: commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/main/java/org/apache/commons/configuration/PropertyConverter.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/main/java/org/apache/commons/configuration/PropertyConverter.java?rev=1534376&r1=1534375&r2=1534376&view=diff ============================================================================== --- commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/main/java/org/apache/commons/configuration/PropertyConverter.java (original) +++ commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/main/java/org/apache/commons/configuration/PropertyConverter.java Mon Oct 21 21:14:18 2013 @@ -106,6 +106,10 @@ public final class PropertyConverter { return toBoolean(value); } + else if (Character.class.equals(cls) || Character.TYPE.equals(cls)) + { + return toCharacter(value); + } else if (Number.class.isAssignableFrom(cls) || cls.isPrimitive()) { if (Integer.class.equals(cls) || Integer.TYPE.equals(cls)) @@ -213,6 +217,32 @@ public final class PropertyConverter } /** + * Converts the specified value object to a {@code Character}. This method + * converts the passed in object to a string. If the string has exactly one + * character, this character is returned as result. Otherwise, conversion + * fails. + * + * @param value the value to be converted + * @return the resulting {@code Character} object + * @throws ConversionException if the conversion is not possible + */ + public static Character toCharacter(Object value) throws ConversionException + { + String strValue = String.valueOf(value); + if (strValue.length() == 1) + { + return Character.valueOf(strValue.charAt(0)); + } + else + { + throw new ConversionException( + String.format( + "The value '%s' cannot be converted to a Character object!", + strValue)); + } + } + + /** * Convert the specified object into a Byte. * * @param value the value to convert Modified: commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/java/org/apache/commons/configuration/TestPropertyConverter.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/java/org/apache/commons/configuration/TestPropertyConverter.java?rev=1534376&r1=1534375&r2=1534376&view=diff ============================================================================== --- commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/java/org/apache/commons/configuration/TestPropertyConverter.java (original) +++ commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/java/org/apache/commons/configuration/TestPropertyConverter.java Mon Oct 21 21:14:18 2013 @@ -349,4 +349,42 @@ public class TestPropertyConverter assertEquals("Wrong conversion result", value, PropertyConverter.to(String.class, value, null)); } + + /** + * Tests whether a conversion to character is possible. + */ + @Test + public void testToCharSuccess() + { + assertEquals("Wrong conversion result", Character.valueOf('t'), + PropertyConverter.to(Character.class, "t", null)); + } + + /** + * Tests whether other objects implementing a toString() method can be + * converted to character. + */ + @Test + public void testToCharViaToString() + { + Object value = new Object() + { + @Override + public String toString() + { + return "X"; + } + }; + assertEquals("Wrong conversion result", Character.valueOf('X'), + PropertyConverter.to(Character.TYPE, value, null)); + } + + /** + * Tests a failed conversion to character. + */ + @Test(expected = ConversionException.class) + public void testToCharFailed() + { + PropertyConverter.to(Character.TYPE, "FF", null); + } }