ebourg 2004/10/18 03:44:31
Modified: configuration/xdocs changes.xml
configuration/src/java/org/apache/commons/configuration
PropertyConverter.java
configuration/src/test/org/apache/commons/configuration
TestBaseConfiguration.java
Log:
Numeric properties can now be specified in hexadecimal format (bug 28026)
Revision Changes Path
1.58 +4 -0 jakarta-commons/configuration/xdocs/changes.xml
Index: changes.xml
===================================================================
RCS file: /home/cvs/jakarta-commons/configuration/xdocs/changes.xml,v
retrieving revision 1.57
retrieving revision 1.58
diff -u -r1.57 -r1.58
--- changes.xml 18 Oct 2004 10:19:26 -0000 1.57
+++ changes.xml 18 Oct 2004 10:44:31 -0000 1.58
@@ -8,6 +8,10 @@
<body>
<release version="1.1-dev" date="in CVS">
+ <action dev="ebourg" type="add" issue="28026">
+ Numeric properties can now be specified in hexadecimal format,
+ for example "number = 0xC5F0".
+ </action>
<action dev="oheger" type="fix" issue="31745">
Fixed HierarchicalConfiguration.getKeys(String), it returned an empty
iterator if the prefix string contained indices.
1.2 +47 -6
jakarta-commons/configuration/src/java/org/apache/commons/configuration/PropertyConverter.java
Index: PropertyConverter.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/configuration/src/java/org/apache/commons/configuration/PropertyConverter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- PropertyConverter.java 18 Oct 2004 09:54:37 -0000 1.1
+++ PropertyConverter.java 18 Oct 2004 10:44:31 -0000 1.2
@@ -84,7 +84,15 @@
{
try
{
- return new Byte((String) value);
+ String string = (String) value;
+ if (string.startsWith("0x"))
+ {
+ return new Byte((byte) Integer.parseInt(string.substring(2),
16));
+ }
+ else
+ {
+ return new Byte(string);
+ }
}
catch (NumberFormatException e)
{
@@ -113,7 +121,16 @@
{
try
{
- return new Short((String) value);
+ String string = (String) value;
+ if (string.startsWith("0x"))
+ {
+ return new Short((short) Integer.parseInt(string.substring(2),
16));
+ }
+ else
+ {
+ return new Short(string);
+ }
+
}
catch (NumberFormatException e)
{
@@ -142,7 +159,15 @@
{
try
{
- return new Integer((String) value);
+ String string = (String) value;
+ if (string.startsWith("0x"))
+ {
+ return new Integer((int) Long.parseLong(string.substring(2),
16));
+ }
+ else
+ {
+ return new Integer(string);
+ }
}
catch (NumberFormatException e)
{
@@ -171,7 +196,15 @@
{
try
{
- return new Long((String) value);
+ String string = (String) value;
+ if (string.startsWith("0x"))
+ {
+ return new Long(new BigInteger(string.substring(2),
16).longValue());
+ }
+ else
+ {
+ return new Long(string);
+ }
}
catch (NumberFormatException e)
{
@@ -258,7 +291,15 @@
{
try
{
- return new BigInteger((String) value);
+ String string = (String) value;
+ if (string.startsWith("0x"))
+ {
+ return new BigInteger(string.substring(2), 16);
+ }
+ else
+ {
+ return new BigInteger(string);
+ }
}
catch (NumberFormatException e)
{
1.16 +570 -504
jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestBaseConfiguration.java
Index: TestBaseConfiguration.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestBaseConfiguration.java,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- TestBaseConfiguration.java 19 Sep 2004 22:01:50 -0000 1.15
+++ TestBaseConfiguration.java 18 Oct 2004 10:44:31 -0000 1.16
@@ -1,5 +1,3 @@
-package org.apache.commons.configuration;
-
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
@@ -16,6 +14,8 @@
* limitations under the License.
*/
+package org.apache.commons.configuration;
+
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Iterator;
@@ -35,510 +35,560 @@
*/
public class TestBaseConfiguration extends TestCase
{
- protected BaseConfiguration config = null;
+ protected BaseConfiguration config = null;
+
+ protected static Class missingElementException = NoSuchElementException.class;
+ protected static Class incompatibleElementException = ConversionException.class;
+
+ protected void setUp() throws Exception
+ {
+ config = new BaseConfiguration();
+ config.setThrowExceptionOnMissing(true);
+ }
+
+ public void testThrowExceptionOnMissing()
+ {
+ assertTrue("Throw Exception Property is not set!",
config.isThrowExceptionOnMissing());
+ }
+
+ public void testGetProperty()
+ {
+ /* should be empty and return null */
+ assertEquals("This returns null", config.getProperty("foo"), null);
+
+ /* add a real value, and get it two different ways */
+ config.setProperty("number", "1");
+ assertEquals("This returns '1'", config.getProperty("number"), "1");
+ assertEquals("This returns '1'", config.getString("number"), "1");
+ }
+
+ public void testGetByte()
+ {
+ config.setProperty("number", "1");
+ byte oneB = 1;
+ byte twoB = 2;
+ assertEquals("This returns 1(byte)", oneB, config.getByte("number"));
+ assertEquals("This returns 1(byte)", oneB, config.getByte("number", twoB));
+ assertEquals("This returns 2(default byte)", twoB,
config.getByte("numberNotInConfig", twoB));
+ assertEquals("This returns 1(Byte)", new Byte(oneB),
config.getByte("number", new Byte("2")));
+
+ // missing key without default value
+ Throwable t = null;
+ try
+ {
+ config.getByte("numberNotInConfig");
+ }
+ catch (Throwable T)
+ {
+ t = T;
+ }
+ assertNotNull("No exception thrown for missing keys", t);
+ ObjectAssert.assertInstanceOf("Exception thrown for missing keys",
missingElementException, t);
+
+ // existing key with an incompatible value
+ config.setProperty("test.empty", "");
+ t = null;
+ try
+ {
+ config.getByte("test.empty");
+ }
+ catch (Throwable T)
+ {
+ t = T;
+ }
+ assertNotNull("No exception thrown for incompatible values", t);
+ ObjectAssert.assertInstanceOf("Exception thrown for incompatible values",
incompatibleElementException, t);
+ }
- protected static Class missingElementException = NoSuchElementException.class;
- protected static Class incompatibleElementException =
ConversionException.class;
+ public void testGetShort()
+ {
+ config.setProperty("numberS", "1");
+ short oneS = 1;
+ short twoS = 2;
+ assertEquals("This returns 1(short)", oneS, config.getShort("numberS"));
+ assertEquals("This returns 1(short)", oneS, config.getShort("numberS",
twoS));
+ assertEquals("This returns 2(default short)", twoS,
config.getShort("numberNotInConfig", twoS));
+ assertEquals("This returns 1(Short)", new Short(oneS),
config.getShort("numberS", new Short("2")));
+
+ // missing key without default value
+ Throwable t = null;
+ try
+ {
+ config.getShort("numberNotInConfig");
+ }
+ catch (Throwable T)
+ {
+ t = T;
+ }
+ assertNotNull("No exception thrown for missing keys", t);
+ ObjectAssert.assertInstanceOf("Exception thrown for missing keys",
missingElementException, t);
- protected void setUp()
- throws Exception
+ // existing key with an incompatible value
+ config.setProperty("test.empty", "");
+ t = null;
+ try
{
- config = new BaseConfiguration();
- config.setThrowExceptionOnMissing(true);
- }
-
- public void testThrowExceptionOnMissing()
- {
- assertTrue("Throw Exception Property is not set!",
config.isThrowExceptionOnMissing());
- }
-
- public void testGetProperty()
- {
- /* should be empty and return null */
- assertEquals("This returns null", config.getProperty("foo"), null);
-
- /* add a real value, and get it two different ways */
- config.setProperty("number", "1");
- assertEquals("This returns '1'", config.getProperty("number"), "1");
- assertEquals("This returns '1'", config.getString("number"), "1");
- }
-
- public void testGetByte()
- {
- config.setProperty("number", "1");
- byte oneB = 1;
- byte twoB = 2;
- assertEquals("This returns 1(byte)", oneB, config.getByte("number"));
- assertEquals("This returns 1(byte)", oneB, config.getByte("number",
twoB));
- assertEquals("This returns 2(default byte)", twoB,
config.getByte("numberNotInConfig", twoB));
- assertEquals("This returns 1(Byte)", new Byte(oneB),
config.getByte("number", new Byte("2")));
-
- // missing key without default value
- Throwable t = null;
- try {
- config.getByte("numberNotInConfig");
- } catch (Throwable T) {
- t = T;
- }
- assertNotNull("No exception thrown for missing keys", t);
- ObjectAssert.assertInstanceOf("Exception thrown for missing keys",
missingElementException, t);
-
- // existing key with an incompatible value
- config.setProperty("test.empty", "");
- t = null;
- try {
- config.getByte("test.empty");
- } catch (Throwable T) {
- t = T;
- }
- assertNotNull("No exception thrown for incompatible values", t);
- ObjectAssert.assertInstanceOf("Exception thrown for incompatible
values", incompatibleElementException, t);
- }
-
- public void testGetShort()
- {
- config.setProperty("numberS", "1");
- short oneS = 1;
- short twoS = 2;
- assertEquals("This returns 1(short)", oneS,
config.getShort("numberS"));
- assertEquals("This returns 1(short)", oneS, config.getShort("numberS",
twoS));
- assertEquals("This returns 2(default short)", twoS,
config.getShort("numberNotInConfig", twoS));
- assertEquals("This returns 1(Short)", new Short(oneS),
config.getShort("numberS", new Short("2")));
-
- // missing key without default value
- Throwable t = null;
- try {
- config.getShort("numberNotInConfig");
- } catch (Throwable T) {
- t = T;
- }
- assertNotNull("No exception thrown for missing keys", t);
- ObjectAssert.assertInstanceOf("Exception thrown for missing keys",
missingElementException, t);
-
- // existing key with an incompatible value
- config.setProperty("test.empty", "");
- t = null;
- try {
- config.getShort("test.empty");
- } catch (Throwable T) {
- t = T;
- }
- assertNotNull("No exception thrown for incompatible values", t);
- ObjectAssert.assertInstanceOf("Exception thrown for incompatible
values", incompatibleElementException, t);
- }
-
- public void testGetLong()
- {
- config.setProperty("numberL", "1");
- long oneL = 1;
- long twoL = 2;
- assertEquals("This returns 1(long)", oneL, config.getLong("numberL"));
- assertEquals("This returns 1(long)", oneL, config.getLong("numberL",
twoL));
- assertEquals("This returns 2(default long)", twoL,
config.getLong("numberNotInConfig", twoL));
- assertEquals("This returns 1(Long)", new Long(oneL),
config.getLong("numberL", new Long("2")));
-
- // missing key without default value
- Throwable t = null;
- try {
- config.getLong("numberNotInConfig");
- } catch (Throwable T) {
- t = T;
- }
- assertNotNull("No exception thrown for missing keys", t);
- ObjectAssert.assertInstanceOf("Exception thrown for missing keys",
missingElementException, t);
-
- // existing key with an incompatible value
- config.setProperty("test.empty", "");
- t = null;
- try {
- config.getLong("test.empty");
- } catch (Throwable T) {
- t = T;
- }
- assertNotNull("No exception thrown for incompatible values", t);
- ObjectAssert.assertInstanceOf("Exception thrown for incompatible
values", incompatibleElementException, t);
- }
-
- public void testGetFloat()
- {
- config.setProperty("numberF", "1.0");
- float oneF = 1;
- float twoF = 2;
- assertEquals("This returns 1(float)", oneF,
config.getFloat("numberF"), 0);
- assertEquals("This returns 1(float)", oneF, config.getFloat("numberF",
twoF), 0);
- assertEquals("This returns 2(default float)", twoF,
config.getFloat("numberNotInConfig", twoF), 0);
- assertEquals("This returns 1(Float)", new Float(oneF),
config.getFloat("numberF", new Float("2")));
-
- // missing key without default value
- Throwable t = null;
- try {
- config.getFloat("numberNotInConfig");
- } catch (Throwable T) {
- t = T;
- }
- assertNotNull("No exception thrown for missing keys", t);
- ObjectAssert.assertInstanceOf("Exception thrown for missing keys",
missingElementException, t);
-
- // existing key with an incompatible value
- config.setProperty("test.empty", "");
- t = null;
- try {
- config.getFloat("test.empty");
- } catch (Throwable T) {
- t = T;
- }
- assertNotNull("No exception thrown for incompatible values", t);
- ObjectAssert.assertInstanceOf("Exception thrown for incompatible
values", incompatibleElementException, t);
- }
-
- public void testGetDouble()
- {
- config.setProperty("numberD", "1.0");
- double oneD = 1;
- double twoD = 2;
- assertEquals("This returns 1(double)", oneD,
config.getDouble("numberD"), 0);
- assertEquals("This returns 1(double)", oneD,
config.getDouble("numberD", twoD), 0);
- assertEquals("This returns 2(default double)", twoD,
config.getDouble("numberNotInConfig", twoD), 0);
- assertEquals("This returns 1(Double)", new Double(oneD),
config.getDouble("numberD", new Double("2")));
-
- // missing key without default value
- Throwable t = null;
- try {
- config.getDouble("numberNotInConfig");
- } catch (Throwable T) {
- t = T;
- }
- assertNotNull("No exception thrown for missing keys", t);
- ObjectAssert.assertInstanceOf("Exception thrown for missing keys",
missingElementException, t);
-
- // existing key with an incompatible value
- config.setProperty("test.empty", "");
- t = null;
- try {
- config.getDouble("test.empty");
- } catch (Throwable T) {
- t = T;
- }
- assertNotNull("No exception thrown for incompatible values", t);
- ObjectAssert.assertInstanceOf("Exception thrown for incompatible
values", incompatibleElementException, t);
- }
-
- public void testGetBigDecimal()
- {
- config.setProperty("numberBigD", "123.456");
- BigDecimal number = new BigDecimal("123.456");
- BigDecimal defaultValue = new BigDecimal("654.321");
-
- assertEquals("Existing key", number,
config.getBigDecimal("numberBigD"));
- assertEquals("Existing key with default value", number,
config.getBigDecimal("numberBigD", defaultValue));
- assertEquals("Missing key with default value", defaultValue,
config.getBigDecimal("numberNotInConfig", defaultValue));
-
- // missing key without default value
- Throwable t = null;
- try {
- config.getBigDecimal("numberNotInConfig");
- } catch (Throwable T) {
- t = T;
- }
- assertNotNull("No exception thrown for missing keys", t);
- ObjectAssert.assertInstanceOf("Exception thrown for missing keys",
missingElementException, t);
-
- // existing key with an incompatible value
- config.setProperty("test.empty", "");
- t = null;
- try {
- config.getBigDecimal("test.empty");
- } catch (Throwable T) {
- t = T;
- }
- assertNotNull("No exception thrown for incompatible values", t);
- ObjectAssert.assertInstanceOf("Exception thrown for incompatible
values", incompatibleElementException, t);
- }
-
- public void testGetBigInteger()
- {
- config.setProperty("numberBigI", "1234567890");
- BigInteger number = new BigInteger("1234567890");
- BigInteger defaultValue = new BigInteger("654321");
-
- assertEquals("Existing key", number,
config.getBigInteger("numberBigI"));
- assertEquals("Existing key with default value", number,
config.getBigInteger("numberBigI", defaultValue));
- assertEquals("Missing key with default value", defaultValue,
config.getBigInteger("numberNotInConfig", defaultValue));
-
- // missing key without default value
- Throwable t = null;
- try {
- config.getBigInteger("numberNotInConfig");
- } catch (Throwable T) {
- t = T;
- }
- assertNotNull("No exception thrown for missing keys", t);
- ObjectAssert.assertInstanceOf("Exception thrown for missing keys",
missingElementException, t);
-
- // existing key with an incompatible value
- config.setProperty("test.empty", "");
- t = null;
- try {
- config.getBigInteger("test.empty");
- } catch (Throwable T) {
- t = T;
- }
- assertNotNull("No exception thrown for incompatible values", t);
- ObjectAssert.assertInstanceOf("Exception thrown for incompatible
values", incompatibleElementException, t);
- }
-
- public void testGetString()
- {
- config.setProperty("testString", "The quick brown fox");
- String string = new String("The quick brown fox");
- String defaultValue = new String("jumps over the lazy dog");
-
- assertEquals("Existing key", string, config.getString("testString"));
- assertEquals("Existing key with default value", string,
config.getString("testString", defaultValue));
- assertEquals("Missing key with default value", defaultValue,
config.getString("stringNotInConfig", defaultValue));
-
- // missing key without default value
- Throwable t = null;
- try {
- config.getString("stringNotInConfig");
- } catch (Throwable T) {
- t = T;
- }
- assertNotNull("No exception thrown for missing keys", t);
- ObjectAssert.assertInstanceOf("Exception thrown for missing keys",
missingElementException, t);
- }
-
- public void testGetBoolean()
- {
- config.setProperty("boolA", Boolean.TRUE);
- boolean boolT = true, boolF = false;
- assertEquals("This returns true", boolT, config.getBoolean("boolA"));
- assertEquals("This returns true, not the default", boolT,
config.getBoolean("boolA", boolF));
- assertEquals("This returns false(default)", boolF,
config.getBoolean("boolNotInConfig", boolF));
- assertEquals("This returns true(Boolean)", new Boolean(boolT),
config.getBoolean("boolA", new Boolean(boolF)));
-
- // missing key without default value
- Throwable t = null;
- try {
- config.getBoolean("numberNotInConfig");
- } catch (Throwable T) {
- t = T;
- }
- assertNotNull("No exception thrown for missing keys", t);
- ObjectAssert.assertInstanceOf("Exception thrown for missing keys",
missingElementException, t);
-
- // existing key with an incompatible value
- config.setProperty("test.empty", "");
- t = null;
- try {
- config.getBoolean("test.empty");
- } catch (Throwable T) {
- t = T;
- }
- assertNotNull("No exception thrown for incompatible values", t);
- ObjectAssert.assertInstanceOf("Exception thrown for incompatible
values", incompatibleElementException, t);
- }
-
- public void testGetList()
- {
- config.addProperty("number", "1");
- config.addProperty("number", "2");
- List list = config.getList("number");
- assertNotNull("The list is null", list);
- assertEquals("List size", 2, list.size());
- assertTrue("The number 1 is missing from the list",
list.contains("1"));
- assertTrue("The number 2 is missing from the list",
list.contains("2"));
-
- /*
- * now test dan's new fix where we get the first scalar
- * when we access a list valued property
- */
- try
- {
- config.getString("number");
- }
- catch (NoSuchElementException nsse)
- {
- fail("Should return a string");
- }
- }
-
- public void testGetVector()
- {
- config.addProperty("number", "1");
- config.addProperty("number", "2");
- Vector vector = config.getVector("number");
- assertNotNull("The vector is null", vector);
- assertEquals("Vector size", 2, vector.size());
- assertTrue("The number 1 is missing from the vector",
vector.contains("1"));
- assertTrue("The number 2 is missing from the vector",
vector.contains("2"));
-
- /*
- * now test dan's new fix where we get the first scalar
- * when we access a vector valued property
- */
- try
- {
- config.getString("number");
- }
- catch (NoSuchElementException nsse)
- {
- fail("Should return a string");
- }
- }
-
- public void testCommaSeparatedString()
- {
- String prop = "hey, that's a test";
- config.setProperty("prop.string", prop);
- try
- {
- config.getList("prop.string");
- }
- catch (NoSuchElementException nsse)
- {
- fail("Should return a list");
- }
-
- String prop2 = "hey\\, that's a test";
- config.clearProperty("prop.string");
- config.setProperty("prop.string", prop2);
- try
- {
- config.getString("prop.string");
- }
- catch (NoSuchElementException nsse)
- {
- fail("Should return a list");
- }
-
- }
-
- public void testPropertyAccess()
- {
- config.clearProperty("prop.properties");
- config.setProperty("prop.properties", "");
- assertEquals(
- "This returns an empty Properties object",
- config.getProperties("prop.properties"),
- new Properties());
- config.clearProperty("prop.properties");
- config.setProperty("prop.properties", "foo=bar, baz=moo,
seal=clubber");
-
- Properties p = new Properties();
- p.setProperty("foo", "bar");
- p.setProperty("baz", "moo");
- p.setProperty("seal", "clubber");
- assertEquals(
- "This returns a filled in Properties object",
- config.getProperties("prop.properties"),
- p);
- }
-
- public void testSubset()
- {
- /*
- * test subset : assure we don't reprocess the data elements
- * when generating the subset
- */
-
- String prop = "hey, that's a test";
- String prop2 = "hey\\, that's a test";
- config.setProperty("prop.string", prop2);
- config.setProperty("property.string", "hello");
-
- Configuration subEprop = config.subset("prop");
-
- assertEquals(
- "Returns the full string",
- prop,
- subEprop.getString("string"));
- try
- {
- subEprop.getString("string");
- }
- catch (NoSuchElementException nsse)
- {
- fail("Should return a string");
- }
- try
- {
- subEprop.getList("string");
- }
- catch (NoSuchElementException nsse)
- {
- fail("Should return a list");
- }
-
- Iterator it = subEprop.getKeys();
- it.next();
- assertFalse(it.hasNext());
-
- subEprop = config.subset("prop.");
- it = subEprop.getKeys();
- assertFalse(it.hasNext());
- }
-
- public void testInterpolation() throws Exception
- {
- config.setProperty("applicationRoot", "/home/applicationRoot");
- config.setProperty("db", "${applicationRoot}/db/hypersonic");
- String unInterpolatedValue = "${applicationRoot2}/db/hypersonic";
- config.setProperty("dbFailedInterpolate", unInterpolatedValue);
- String dbProp = "/home/applicationRoot/db/hypersonic";
-
- //construct a new config, using config as the defaults config for it.
- BaseConfiguration superProp = config;
-
- assertEquals(
- "Checking interpolated variable",dbProp,
- superProp.getString("db"));
- assertEquals(
- "lookup fails, leave variable as is",
- superProp.getString("dbFailedInterpolate"),
- unInterpolatedValue);
-
- superProp.setProperty("arrayInt", "${applicationRoot}/1");
- String[] arrayInt = superProp.getStringArray("arrayInt");
- assertEquals(
- "check first entry was interpolated",
- "/home/applicationRoot/1",
- arrayInt[0]);
- }
-
- public void testMultipleInterpolation() throws Exception
- {
- config.setProperty("test.base-level", "/base-level");
- config.setProperty("test.first-level",
"${test.base-level}/first-level");
- config.setProperty(
- "test.second-level",
- "${test.first-level}/second-level");
- config.setProperty(
- "test.third-level",
- "${test.second-level}/third-level");
-
- String expectedValue =
- "/base-level/first-level/second-level/third-level";
-
- assertEquals(config.getString("test.third-level"), expectedValue);
- }
-
- public void testInterpolationLoop() throws Exception
- {
- config.setProperty("test.a", "${test.b}");
- config.setProperty("test.b", "${test.a}");
-
- try
- {
- config.getString("test.a");
- }
- catch (IllegalStateException e)
- {
- return;
- }
+ config.getShort("test.empty");
+ }
+ catch (Throwable T)
+ {
+ t = T;
+ }
+ assertNotNull("No exception thrown for incompatible values", t);
+ ObjectAssert.assertInstanceOf("Exception thrown for incompatible values",
incompatibleElementException, t);
+ }
- fail("IllegalStateException should have been thrown for looped
property references");
- }
+ public void testGetLong()
+ {
+ config.setProperty("numberL", "1");
+ long oneL = 1;
+ long twoL = 2;
+ assertEquals("This returns 1(long)", oneL, config.getLong("numberL"));
+ assertEquals("This returns 1(long)", oneL, config.getLong("numberL", twoL));
+ assertEquals("This returns 2(default long)", twoL,
config.getLong("numberNotInConfig", twoL));
+ assertEquals("This returns 1(Long)", new Long(oneL),
config.getLong("numberL", new Long("2")));
+
+ // missing key without default value
+ Throwable t = null;
+ try
+ {
+ config.getLong("numberNotInConfig");
+ }
+ catch (Throwable T)
+ {
+ t = T;
+ }
+ assertNotNull("No exception thrown for missing keys", t);
+ ObjectAssert.assertInstanceOf("Exception thrown for missing keys",
missingElementException, t);
+
+ // existing key with an incompatible value
+ config.setProperty("test.empty", "");
+ t = null;
+ try
+ {
+ config.getLong("test.empty");
+ }
+ catch (Throwable T)
+ {
+ t = T;
+ }
+ assertNotNull("No exception thrown for incompatible values", t);
+ ObjectAssert.assertInstanceOf("Exception thrown for incompatible values",
incompatibleElementException, t);
+ }
+
+ public void testGetFloat()
+ {
+ config.setProperty("numberF", "1.0");
+ float oneF = 1;
+ float twoF = 2;
+ assertEquals("This returns 1(float)", oneF, config.getFloat("numberF"), 0);
+ assertEquals("This returns 1(float)", oneF, config.getFloat("numberF",
twoF), 0);
+ assertEquals("This returns 2(default float)", twoF,
config.getFloat("numberNotInConfig", twoF), 0);
+ assertEquals("This returns 1(Float)", new Float(oneF),
config.getFloat("numberF", new Float("2")));
+
+ // missing key without default value
+ Throwable t = null;
+ try
+ {
+ config.getFloat("numberNotInConfig");
+ }
+ catch (Throwable T)
+ {
+ t = T;
+ }
+ assertNotNull("No exception thrown for missing keys", t);
+ ObjectAssert.assertInstanceOf("Exception thrown for missing keys",
missingElementException, t);
+
+ // existing key with an incompatible value
+ config.setProperty("test.empty", "");
+ t = null;
+ try
+ {
+ config.getFloat("test.empty");
+ }
+ catch (Throwable T)
+ {
+ t = T;
+ }
+ assertNotNull("No exception thrown for incompatible values", t);
+ ObjectAssert.assertInstanceOf("Exception thrown for incompatible values",
incompatibleElementException, t);
+ }
+
+ public void testGetDouble()
+ {
+ config.setProperty("numberD", "1.0");
+ double oneD = 1;
+ double twoD = 2;
+ assertEquals("This returns 1(double)", oneD, config.getDouble("numberD"),
0);
+ assertEquals("This returns 1(double)", oneD, config.getDouble("numberD",
twoD), 0);
+ assertEquals("This returns 2(default double)", twoD,
config.getDouble("numberNotInConfig", twoD), 0);
+ assertEquals("This returns 1(Double)", new Double(oneD),
config.getDouble("numberD", new Double("2")));
+
+ // missing key without default value
+ Throwable t = null;
+ try
+ {
+ config.getDouble("numberNotInConfig");
+ }
+ catch (Throwable T)
+ {
+ t = T;
+ }
+ assertNotNull("No exception thrown for missing keys", t);
+ ObjectAssert.assertInstanceOf("Exception thrown for missing keys",
missingElementException, t);
+
+ // existing key with an incompatible value
+ config.setProperty("test.empty", "");
+ t = null;
+ try
+ {
+ config.getDouble("test.empty");
+ }
+ catch (Throwable T)
+ {
+ t = T;
+ }
+ assertNotNull("No exception thrown for incompatible values", t);
+ ObjectAssert.assertInstanceOf("Exception thrown for incompatible values",
incompatibleElementException, t);
+ }
+
+ public void testGetBigDecimal()
+ {
+ config.setProperty("numberBigD", "123.456");
+ BigDecimal number = new BigDecimal("123.456");
+ BigDecimal defaultValue = new BigDecimal("654.321");
+
+ assertEquals("Existing key", number, config.getBigDecimal("numberBigD"));
+ assertEquals("Existing key with default value", number,
config.getBigDecimal("numberBigD", defaultValue));
+ assertEquals("Missing key with default value", defaultValue,
config.getBigDecimal("numberNotInConfig", defaultValue));
+
+ // missing key without default value
+ Throwable t = null;
+ try
+ {
+ config.getBigDecimal("numberNotInConfig");
+ }
+ catch (Throwable T)
+ {
+ t = T;
+ }
+ assertNotNull("No exception thrown for missing keys", t);
+ ObjectAssert.assertInstanceOf("Exception thrown for missing keys",
missingElementException, t);
+
+ // existing key with an incompatible value
+ config.setProperty("test.empty", "");
+ t = null;
+ try
+ {
+ config.getBigDecimal("test.empty");
+ }
+ catch (Throwable T)
+ {
+ t = T;
+ }
+ assertNotNull("No exception thrown for incompatible values", t);
+ ObjectAssert.assertInstanceOf("Exception thrown for incompatible values",
incompatibleElementException, t);
+ }
+
+ public void testGetBigInteger()
+ {
+ config.setProperty("numberBigI", "1234567890");
+ BigInteger number = new BigInteger("1234567890");
+ BigInteger defaultValue = new BigInteger("654321");
+
+ assertEquals("Existing key", number, config.getBigInteger("numberBigI"));
+ assertEquals("Existing key with default value", number,
config.getBigInteger("numberBigI", defaultValue));
+ assertEquals("Missing key with default value", defaultValue,
config.getBigInteger("numberNotInConfig", defaultValue));
+
+ // missing key without default value
+ Throwable t = null;
+ try
+ {
+ config.getBigInteger("numberNotInConfig");
+ }
+ catch (Throwable T)
+ {
+ t = T;
+ }
+ assertNotNull("No exception thrown for missing keys", t);
+ ObjectAssert.assertInstanceOf("Exception thrown for missing keys",
missingElementException, t);
+
+ // existing key with an incompatible value
+ config.setProperty("test.empty", "");
+ t = null;
+ try
+ {
+ config.getBigInteger("test.empty");
+ }
+ catch (Throwable T)
+ {
+ t = T;
+ }
+ assertNotNull("No exception thrown for incompatible values", t);
+ ObjectAssert.assertInstanceOf("Exception thrown for incompatible values",
incompatibleElementException, t);
+ }
+
+ public void testGetString()
+ {
+ config.setProperty("testString", "The quick brown fox");
+ String string = new String("The quick brown fox");
+ String defaultValue = new String("jumps over the lazy dog");
+
+ assertEquals("Existing key", string, config.getString("testString"));
+ assertEquals("Existing key with default value", string,
config.getString("testString", defaultValue));
+ assertEquals("Missing key with default value", defaultValue,
config.getString("stringNotInConfig", defaultValue));
+
+ // missing key without default value
+ Throwable t = null;
+ try
+ {
+ config.getString("stringNotInConfig");
+ }
+ catch (Throwable T)
+ {
+ t = T;
+ }
+ assertNotNull("No exception thrown for missing keys", t);
+ ObjectAssert.assertInstanceOf("Exception thrown for missing keys",
missingElementException, t);
+ }
+
+ public void testGetBoolean()
+ {
+ config.setProperty("boolA", Boolean.TRUE);
+ boolean boolT = true, boolF = false;
+ assertEquals("This returns true", boolT, config.getBoolean("boolA"));
+ assertEquals("This returns true, not the default", boolT,
config.getBoolean("boolA", boolF));
+ assertEquals("This returns false(default)", boolF,
config.getBoolean("boolNotInConfig", boolF));
+ assertEquals("This returns true(Boolean)", new Boolean(boolT),
config.getBoolean("boolA", new Boolean(boolF)));
+
+ // missing key without default value
+ Throwable t = null;
+ try
+ {
+ config.getBoolean("numberNotInConfig");
+ }
+ catch (Throwable T)
+ {
+ t = T;
+ }
+ assertNotNull("No exception thrown for missing keys", t);
+ ObjectAssert.assertInstanceOf("Exception thrown for missing keys",
missingElementException, t);
+
+ // existing key with an incompatible value
+ config.setProperty("test.empty", "");
+ t = null;
+ try
+ {
+ config.getBoolean("test.empty");
+ }
+ catch (Throwable T)
+ {
+ t = T;
+ }
+ assertNotNull("No exception thrown for incompatible values", t);
+ ObjectAssert.assertInstanceOf("Exception thrown for incompatible values",
incompatibleElementException, t);
+ }
+
+ public void testGetList()
+ {
+ config.addProperty("number", "1");
+ config.addProperty("number", "2");
+ List list = config.getList("number");
+ assertNotNull("The list is null", list);
+ assertEquals("List size", 2, list.size());
+ assertTrue("The number 1 is missing from the list", list.contains("1"));
+ assertTrue("The number 2 is missing from the list", list.contains("2"));
+
+ /*
+ * now test dan's new fix where we get the first scalar
+ * when we access a list valued property
+ */
+ try
+ {
+ config.getString("number");
+ }
+ catch (NoSuchElementException nsse)
+ {
+ fail("Should return a string");
+ }
+ }
+
+ public void testGetVector()
+ {
+ config.addProperty("number", "1");
+ config.addProperty("number", "2");
+ Vector vector = config.getVector("number");
+ assertNotNull("The vector is null", vector);
+ assertEquals("Vector size", 2, vector.size());
+ assertTrue("The number 1 is missing from the vector", vector.contains("1"));
+ assertTrue("The number 2 is missing from the vector", vector.contains("2"));
+
+ /*
+ * now test dan's new fix where we get the first scalar
+ * when we access a vector valued property
+ */
+ try
+ {
+ config.getString("number");
+ }
+ catch (NoSuchElementException nsse)
+ {
+ fail("Should return a string");
+ }
+ }
+
+ public void testCommaSeparatedString()
+ {
+ String prop = "hey, that's a test";
+ config.setProperty("prop.string", prop);
+ try
+ {
+ config.getList("prop.string");
+ }
+ catch (NoSuchElementException nsse)
+ {
+ fail("Should return a list");
+ }
+
+ String prop2 = "hey\\, that's a test";
+ config.clearProperty("prop.string");
+ config.setProperty("prop.string", prop2);
+ try
+ {
+ config.getString("prop.string");
+ }
+ catch (NoSuchElementException nsse)
+ {
+ fail("Should return a list");
+ }
+
+ }
+
+ public void testPropertyAccess()
+ {
+ config.clearProperty("prop.properties");
+ config.setProperty("prop.properties", "");
+ assertEquals(
+ "This returns an empty Properties object",
+ config.getProperties("prop.properties"),
+ new Properties());
+ config.clearProperty("prop.properties");
+ config.setProperty("prop.properties", "foo=bar, baz=moo, seal=clubber");
+
+ Properties p = new Properties();
+ p.setProperty("foo", "bar");
+ p.setProperty("baz", "moo");
+ p.setProperty("seal", "clubber");
+ assertEquals(
+ "This returns a filled in Properties object",
+ config.getProperties("prop.properties"),
+ p);
+ }
+
+ public void testSubset()
+ {
+ /*
+ * test subset : assure we don't reprocess the data elements
+ * when generating the subset
+ */
+
+ String prop = "hey, that's a test";
+ String prop2 = "hey\\, that's a test";
+ config.setProperty("prop.string", prop2);
+ config.setProperty("property.string", "hello");
+
+ Configuration subEprop = config.subset("prop");
+
+ assertEquals(
+ "Returns the full string",
+ prop,
+ subEprop.getString("string"));
+ try
+ {
+ subEprop.getString("string");
+ }
+ catch (NoSuchElementException nsse)
+ {
+ fail("Should return a string");
+ }
+ try
+ {
+ subEprop.getList("string");
+ }
+ catch (NoSuchElementException nsse)
+ {
+ fail("Should return a list");
+ }
+
+ Iterator it = subEprop.getKeys();
+ it.next();
+ assertFalse(it.hasNext());
+
+ subEprop = config.subset("prop.");
+ it = subEprop.getKeys();
+ assertFalse(it.hasNext());
+ }
+
+ public void testInterpolation() throws Exception
+ {
+ config.setProperty("applicationRoot", "/home/applicationRoot");
+ config.setProperty("db", "${applicationRoot}/db/hypersonic");
+ String unInterpolatedValue = "${applicationRoot2}/db/hypersonic";
+ config.setProperty("dbFailedInterpolate", unInterpolatedValue);
+ String dbProp = "/home/applicationRoot/db/hypersonic";
+
+ //construct a new config, using config as the defaults config for it.
+ BaseConfiguration superProp = config;
+
+ assertEquals(
+ "Checking interpolated variable", dbProp,
+ superProp.getString("db"));
+ assertEquals(
+ "lookup fails, leave variable as is",
+ superProp.getString("dbFailedInterpolate"),
+ unInterpolatedValue);
+
+ superProp.setProperty("arrayInt", "${applicationRoot}/1");
+ String[] arrayInt = superProp.getStringArray("arrayInt");
+ assertEquals(
+ "check first entry was interpolated",
+ "/home/applicationRoot/1",
+ arrayInt[0]);
+ }
+
+ public void testMultipleInterpolation() throws Exception
+ {
+ config.setProperty("test.base-level", "/base-level");
+ config.setProperty("test.first-level", "${test.base-level}/first-level");
+ config.setProperty(
+ "test.second-level",
+ "${test.first-level}/second-level");
+ config.setProperty(
+ "test.third-level",
+ "${test.second-level}/third-level");
+
+ String expectedValue =
+ "/base-level/first-level/second-level/third-level";
+
+ assertEquals(config.getString("test.third-level"), expectedValue);
+ }
+
+ public void testInterpolationLoop() throws Exception
+ {
+ config.setProperty("test.a", "${test.b}");
+ config.setProperty("test.b", "${test.a}");
+
+ try
+ {
+ config.getString("test.a");
+ }
+ catch (IllegalStateException e)
+ {
+ return;
+ }
+
+ fail("IllegalStateException should have been thrown for looped property
references");
+ }
public void testSplit()
{
@@ -554,5 +604,21 @@
assertEquals("1st token for '" + s2 + "'", "abc,xyz", tokens.get(0));
}
-}
+ public void testGetHexadecimalValue()
+ {
+ config.setProperty("number", "0xFF");
+ assertEquals("byte value", (byte) 0xFF, config.getByte("number"));
+
+ config.setProperty("number", "0xFFFF");
+ assertEquals("short value", (short) 0xFFFF, config.getShort("number"));
+ config.setProperty("number", "0xFFFFFFFF");
+ assertEquals("int value", 0xFFFFFFFF, config.getInt("number"));
+
+ config.setProperty("number", "0xFFFFFFFFFFFFFFFF");
+ assertEquals("long value", 0xFFFFFFFFFFFFFFFFL, config.getLong("number"));
+
+ assertEquals("long value", 0xFFFFFFFFFFFFFFFFL,
config.getBigInteger("number").longValue());
+ }
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]