commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ebo...@apache.org
Subject svn commit: r1183533 - in /commons/proper/configuration/branches/configuration2_experimental/src: main/java/org/apache/commons/configuration2/converter/ test/java/org/apache/commons/configuration2/converter/ test/java/org/apache/commons/configuration2/...
Date Fri, 14 Oct 2011 22:31:31 GMT
Author: ebourg
Date: Fri Oct 14 22:31:31 2011
New Revision: 1183533

URL: http://svn.apache.org/viewvc?rev=1183533&view=rev
Log:
Added support for binary literals (CONFIGURATION-466) (ported from the trunk)

Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/NumberConverter.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/converter/TestDefaultPropertyConverter.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestBaseConfiguration.java

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/NumberConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/NumberConverter.java?rev=1183533&r1=1183532&r2=1183533&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/NumberConverter.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/converter/NumberConverter.java
Fri Oct 14 22:31:31 2011
@@ -38,6 +38,12 @@ abstract class NumberConverter<T extends
     /** Constant for the radix of hex numbers.*/
     private static final int HEX_RADIX = 16;
 
+    /** Constant for the prefix of binary numbers.*/
+    private static final String BIN_PREFIX = "0b";
+
+    /** Constant for the radix of binary numbers.*/
+    private static final int BIN_RADIX = 2;
+
     /**
      * Tries to convert the specified object into a number object. This method
      * is used by the conversion methods for number types. Note that the return
@@ -72,7 +78,21 @@ abstract class NumberConverter<T extends
                             + "! Invalid hex number.", nex);
                 }
             }
-
+            
+            if (str.startsWith(BIN_PREFIX))
+            {
+                try
+                {
+                    return new BigInteger(str.substring(BIN_PREFIX.length()), BIN_RADIX);
+                }
+                catch (NumberFormatException nex)
+                {
+                    throw new ConversionException("Could not convert " + str
+                            + " to " + targetClass.getName()
+                            + "! Invalid binary number.", nex);
+                }
+            }
+            
             try
             {
                 Constructor<? extends Number> constr = targetClass.getConstructor(String.class);

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/converter/TestDefaultPropertyConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/converter/TestDefaultPropertyConverter.java?rev=1183533&r1=1183532&r2=1183533&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/converter/TestDefaultPropertyConverter.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/converter/TestDefaultPropertyConverter.java
Fri Oct 14 22:31:31 2011
@@ -71,8 +71,8 @@ public class TestDefaultPropertyConverte
     }
 
     /**
-     * Tests conversion to numbers when an invalid Hex value is passed in. This
-     * should cause an exception.
+     * Tests conversion to numbers when an invalid Hex value is passed in.
+     * This should cause an exception.
      */
     public void testToNumberFromInvalidHexString()
     {
@@ -88,6 +88,33 @@ public class TestDefaultPropertyConverte
     }
 
     /**
+     * Tests conversion to numbers when the passed in objects are strings with
+     * prefixes for special radices.
+     */
+    public void testToNumberFromBinaryString()
+    {
+        Number n = converter.convert(Integer.class, "0b1111");
+        assertEquals("Incorrect Integer value", 15, n.intValue());
+    }
+
+    /**
+     * Tests conversion to numbers when an invalid binary value is passed in.
+     * This should cause an exception.
+     */
+    public void testToNumberFromInvalidBinaryString()
+    {
+        try
+        {
+            converter.convert(Integer.class, "0bNotABinValue");
+            fail("Could convert invalid binary value!");
+        }
+        catch (ConversionException cex)
+        {
+            // ok
+        }
+    }
+
+    /**
      * Tests conversion to numbers when the passed in objects have no numeric
      * String representation. This should cause an exception.
      */

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestBaseConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestBaseConfiguration.java?rev=1183533&r1=1183532&r2=1183533&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestBaseConfiguration.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestBaseConfiguration.java
Fri Oct 14 22:31:31 2011
@@ -694,6 +694,23 @@ public class TestBaseConfiguration exten
         assertEquals("long value", 0xFFFFFFFFFFFFFFFFL, config.getBigInteger("number").longValue());
     }
 
+    public void testGetBinaryValue()
+    {
+        config.setProperty("number", "0b11111111");
+        assertEquals("byte value", (byte) 0xFF, config.getByte("number"));
+
+        config.setProperty("number", "0b1111111111111111");
+        assertEquals("short value", (short) 0xFFFF, config.getShort("number"));
+
+        config.setProperty("number", "0b11111111111111111111111111111111");
+        assertEquals("int value", 0xFFFFFFFF, config.getInt("number"));
+
+        config.setProperty("number", "0b1111111111111111111111111111111111111111111111111111111111111111");
+        assertEquals("long value", 0xFFFFFFFFFFFFFFFFL, config.getLong("number"));
+
+        assertEquals("long value", 0xFFFFFFFFFFFFFFFFL, config.getBigInteger("number").longValue());
+    }
+
     /**
      * Tests if conversion between number types is possible.
      */



Mime
View raw message