This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-configuration.git
commit 283be499c8c0fee4a8ee85dd30105ff2178d8653 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Mon Apr 27 09:16:49 2020 -0400 Sort methods. --- .../configuration2/TestBaseConfiguration.java | 814 ++++++++++----------- 1 file changed, 407 insertions(+), 407 deletions(-) diff --git a/src/test/java/org/apache/commons/configuration2/TestBaseConfiguration.java b/src/test/java/org/apache/commons/configuration2/TestBaseConfiguration.java index f10829f..3c56005 100644 --- a/src/test/java/org/apache/commons/configuration2/TestBaseConfiguration.java +++ b/src/test/java/org/apache/commons/configuration2/TestBaseConfiguration.java @@ -58,10 +58,10 @@ public class TestBaseConfiguration /** Constant for the number key.*/ static final String KEY_NUMBER = "number"; - protected BaseConfiguration config = null; - protected static Class<?> missingElementException = NoSuchElementException.class; + protected static Class<?> incompatibleElementException = ConversionException.class; + protected BaseConfiguration config = null; @Before public void setUp() throws Exception @@ -72,261 +72,319 @@ public class TestBaseConfiguration } @Test - public void testThrowExceptionOnMissing() + public void testAddProperty() throws Exception { - assertTrue("Throw Exception Property is not set!", config.isThrowExceptionOnMissing()); - } + Collection<Object> props = new ArrayList<>(); + props.add("one"); + props.add("two,three,four"); + props.add(new String[] { "5.1", "5.2", "5.3,5.4", "5.5" }); + props.add("six"); + config.addProperty("complex.property", props); - @Test - public void testGetProperty() - { - /* should be empty and return null */ - assertEquals("This returns null", config.getProperty("foo"), null); + Object val = config.getProperty("complex.property"); + assertTrue(val instanceof Collection); + Collection<?> col = (Collection<?>) val; + assertEquals(10, col.size()); - /* 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"); + props = new ArrayList<>(); + props.add("quick"); + props.add("brown"); + props.add("fox,jumps"); + final Object[] data = new Object[] { + "The", props, "over,the", "lazy", "dog." + }; + config.setProperty("complex.property", data); + val = config.getProperty("complex.property"); + assertTrue(val instanceof Collection); + col = (Collection<?>) val; + final Iterator<?> it = col.iterator(); + final StringTokenizer tok = new StringTokenizer("The quick brown fox jumps over the lazy dog.", " "); + while(tok.hasMoreTokens()) + { + assertTrue(it.hasNext()); + assertEquals(tok.nextToken(), it.next()); + } + assertFalse(it.hasNext()); + + config.setProperty("complex.property", null); + assertFalse(config.containsKey("complex.property")); } + /** + * Tests cloning a BaseConfiguration. + */ @Test - public void testGetByte() + public void testClone() { - config.setProperty("number", "1"); - final byte oneB = 1; - final 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"))); - } + for (int i = 0; i < 10; i++) + { + config.addProperty("key" + i, new Integer(i)); + } + final BaseConfiguration config2 = (BaseConfiguration) config.clone(); - @Test(expected = NoSuchElementException.class) - public void testGetByteUnknown() - { - config.getByte("numberNotInConfig"); + for (final Iterator<String> it = config.getKeys(); it.hasNext();) + { + final String key = it.next(); + assertTrue("Key not found: " + key, config2.containsKey(key)); + assertEquals("Wrong value for key " + key, config.getProperty(key), + config2.getProperty(key)); + } } - @Test(expected = ConversionException.class) - public void testGetByteIncompatibleType() + /** + * Tests whether interpolation works as expected after cloning. + */ + @Test + public void testCloneInterpolation() { - config.setProperty("test.empty", ""); - config.getByte("test.empty"); + final String keyAnswer = "answer"; + config.addProperty(keyAnswer, "The answer is ${" + KEY_NUMBER + "}."); + config.addProperty(KEY_NUMBER, 42); + final BaseConfiguration clone = (BaseConfiguration) config.clone(); + clone.setProperty(KEY_NUMBER, 43); + assertEquals("Wrong interpolation in original", "The answer is 42.", + config.getString(keyAnswer)); + assertEquals("Wrong interpolation in clone", "The answer is 43.", + clone.getString(keyAnswer)); } + /** + * Tests the clone() method if a list property is involved. + */ @Test - public void testGetShort() + public void testCloneListProperty() { - config.setProperty("numberS", "1"); - final short oneS = 1; - final 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"))); + final String key = "list"; + config.addProperty(key, "value1"); + config.addProperty(key, "value2"); + final BaseConfiguration config2 = (BaseConfiguration) config.clone(); + config2.addProperty(key, "value3"); + assertEquals("Wrong number of original properties", 2, config.getList( + key).size()); } - @Test(expected = NoSuchElementException.class) - public void testGetShortUnknown() + /** + * Tests whether a cloned configuration is decoupled from its original. + */ + @Test + public void testCloneModify() { - config.getShort("numberNotInConfig"); + final EventListener<ConfigurationEvent> l = new EventListenerTestImpl(config); + config.addEventListener(ConfigurationEvent.ANY, l); + config.addProperty("original", Boolean.TRUE); + final BaseConfiguration config2 = (BaseConfiguration) config.clone(); + + config2.addProperty("clone", Boolean.TRUE); + assertFalse("New key appears in original", config.containsKey("clone")); + config2.setProperty("original", Boolean.FALSE); + assertTrue("Wrong value of original property", config + .getBoolean("original")); + + assertTrue("Event listener was copied", config2 + .getEventListeners(ConfigurationEvent.ANY).isEmpty()); } - @Test(expected = ConversionException.class) - public void testGetShortIncompatibleType() + @Test + public void testCommaSeparatedString() { - config.setProperty("test.empty", ""); - config.getShort("test.empty"); + final String prop = "hey, that's a test"; + config.setProperty("prop.string", prop); + final List<Object> list = config.getList("prop.string"); + assertEquals("Wrong number of list elements", 2, list.size()); + assertEquals("Wrong element 1", "hey", list.get(0)); } @Test - public void testGetLong() + public void testCommaSeparatedStringEscaped() { - config.setProperty("numberL", "1"); - final long oneL = 1; - final 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"))); + final String prop2 = "hey\\, that's a test"; + config.setProperty("prop.string", prop2); + assertEquals("Wrong value", "hey, that's a test", config.getString("prop.string")); } - @Test(expected = NoSuchElementException.class) - public void testGetLongUnknown() + @Test + public void testGetBigDecimal() { - config.getLong("numberNotInConfig"); + config.setProperty("numberBigD", "123.456"); + final BigDecimal number = new BigDecimal("123.456"); + final 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)); } @Test(expected = ConversionException.class) - public void testGetLongIncompatibleTypes() + public void testGetBigDecimalIncompatibleType() { config.setProperty("test.empty", ""); - config.getLong("test.empty"); + config.getBigDecimal("test.empty"); } - @Test - public void testGetFloat() + @Test(expected = NoSuchElementException.class) + public void testGetBigDecimalUnknown() { - config.setProperty("numberF", "1.0"); - final float oneF = 1; - final 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"))); + config.getBigDecimal("numberNotInConfig"); } - @Test(expected = NoSuchElementException.class) - public void testGetFloatUnknown() + @Test + public void testGetBigInteger() { - config.getFloat("numberNotInConfig"); + config.setProperty("numberBigI", "1234567890"); + final BigInteger number = new BigInteger("1234567890"); + final 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)); } @Test(expected = ConversionException.class) - public void testGetFloatIncompatibleType() + public void testGetBigIntegerIncompatibleType() { config.setProperty("test.empty", ""); - config.getFloat("test.empty"); + config.getBigInteger("test.empty"); + } + + @Test(expected = NoSuchElementException.class) + public void testGetBigIntegerUnknown() + { + config.getBigInteger("numberNotInConfig"); } @Test - public void testGetDouble() + public void testGetBinaryValue() { - config.setProperty("numberD", "1.0"); - final double oneD = 1; - final 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"))); + 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()); } - @Test(expected = NoSuchElementException.class) - public void testGetDoubleUnknown() + @Test + public void testGetBoolean() { - config.getDouble("numberNotInConfig"); + config.setProperty("boolA", Boolean.TRUE); + final 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))); } @Test(expected = ConversionException.class) - public void testGetDoubleIncompatibleType() + public void testGetBooleanIncompatibleType() { config.setProperty("test.empty", ""); - config.getDouble("test.empty"); + config.getBoolean("test.empty"); } - @Test - public void testGetBigDecimal() + @Test(expected = NoSuchElementException.class) + public void testGetBooleanUnknown() { - config.setProperty("numberBigD", "123.456"); - final BigDecimal number = new BigDecimal("123.456"); - final 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)); + config.getBoolean("numberNotInConfig"); } - @Test(expected = NoSuchElementException.class) - public void testGetBigDecimalUnknown() + @Test + public void testGetByte() { - config.getBigDecimal("numberNotInConfig"); + config.setProperty("number", "1"); + final byte oneB = 1; + final 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"))); } @Test(expected = ConversionException.class) - public void testGetBigDecimalIncompatibleType() + public void testGetByteIncompatibleType() { config.setProperty("test.empty", ""); - config.getBigDecimal("test.empty"); - } - - @Test - public void testGetBigInteger() - { - config.setProperty("numberBigI", "1234567890"); - final BigInteger number = new BigInteger("1234567890"); - final 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)); + config.getByte("test.empty"); } @Test(expected = NoSuchElementException.class) - public void testGetBigIntegerUnknown() + public void testGetByteUnknown() { - config.getBigInteger("numberNotInConfig"); + config.getByte("numberNotInConfig"); } - @Test(expected = ConversionException.class) - public void testGetBigIntegerIncompatibleType() + @Test + public void testGetDouble() { - config.setProperty("test.empty", ""); - config.getBigInteger("test.empty"); + config.setProperty("numberD", "1.0"); + final double oneD = 1; + final 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"))); } - @Test - public void testGetString() + @Test(expected = ConversionException.class) + public void testGetDoubleIncompatibleType() { - config.setProperty("testString", "The quick brown fox"); - final String string = "The quick brown fox"; - final String defaultValue = "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)); + config.setProperty("test.empty", ""); + config.getDouble("test.empty"); } @Test(expected = NoSuchElementException.class) - public void testGetStringUnknown() + public void testGetDoubleUnknown() { - config.getString("stringNotInConfig"); + config.getDouble("numberNotInConfig"); } @Test - public void testGetBoolean() - { - config.setProperty("boolA", Boolean.TRUE); - final 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))); - } - - @Test(expected = NoSuchElementException.class) - public void testGetBooleanUnknown() + public void testGetFloat() { - config.getBoolean("numberNotInConfig"); + config.setProperty("numberF", "1.0"); + final float oneF = 1; + final 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"))); } @Test(expected = ConversionException.class) - public void testGetBooleanIncompatibleType() + public void testGetFloatIncompatibleType() { config.setProperty("test.empty", ""); - config.getBoolean("test.empty"); + config.getFloat("test.empty"); } - @Test - public void testGetList() + @Test(expected = NoSuchElementException.class) + public void testGetFloatUnknown() { - config.addProperty("number", "1"); - config.addProperty("number", "2"); - final List<Object> 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")); + config.getFloat("numberNotInConfig"); } - /** - * Tests that the first scalar of a list is returned. - */ @Test - public void testGetStringForListValue() + public void testGetHexadecimalValue() { - config.addProperty("number", "1"); - config.addProperty("number", "2"); - assertEquals("Wrong result", "1", config.getString("number")); + 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()); } @Test @@ -374,158 +432,169 @@ public class TestBaseConfiguration assertEquals("BigDecimal interpolation", new BigDecimal("1"), config.getBigDecimal("value", null)); } + /** + * Tests accessing and manipulating the interpolator object. + */ @Test - public void testCommaSeparatedString() + public void testGetInterpolator() { - final String prop = "hey, that's a test"; - config.setProperty("prop.string", prop); - final List<Object> list = config.getList("prop.string"); - assertEquals("Wrong number of list elements", 2, list.size()); - assertEquals("Wrong element 1", "hey", list.get(0)); + InterpolationTestHelper.testGetInterpolator(config); } @Test - public void testCommaSeparatedStringEscaped() + public void testGetList() { - final String prop2 = "hey\\, that's a test"; - config.setProperty("prop.string", prop2); - assertEquals("Wrong value", "hey, that's a test", config.getString("prop.string")); + config.addProperty("number", "1"); + config.addProperty("number", "2"); + final List<Object> 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")); } @Test - public void testAddProperty() throws Exception + public void testGetLong() { - Collection<Object> props = new ArrayList<>(); - props.add("one"); - props.add("two,three,four"); - props.add(new String[] { "5.1", "5.2", "5.3,5.4", "5.5" }); - props.add("six"); - config.addProperty("complex.property", props); - - Object val = config.getProperty("complex.property"); - assertTrue(val instanceof Collection); - Collection<?> col = (Collection<?>) val; - assertEquals(10, col.size()); + config.setProperty("numberL", "1"); + final long oneL = 1; + final 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"))); + } - props = new ArrayList<>(); - props.add("quick"); - props.add("brown"); - props.add("fox,jumps"); - final Object[] data = new Object[] { - "The", props, "over,the", "lazy", "dog." - }; - config.setProperty("complex.property", data); - val = config.getProperty("complex.property"); - assertTrue(val instanceof Collection); - col = (Collection<?>) val; - final Iterator<?> it = col.iterator(); - final StringTokenizer tok = new StringTokenizer("The quick brown fox jumps over the lazy dog.", " "); - while(tok.hasMoreTokens()) - { - assertTrue(it.hasNext()); - assertEquals(tok.nextToken(), it.next()); - } - assertFalse(it.hasNext()); + @Test(expected = ConversionException.class) + public void testGetLongIncompatibleTypes() + { + config.setProperty("test.empty", ""); + config.getLong("test.empty"); + } - config.setProperty("complex.property", null); - assertFalse(config.containsKey("complex.property")); + @Test(expected = NoSuchElementException.class) + public void testGetLongUnknown() + { + config.getLong("numberNotInConfig"); } @Test - public void testPropertyAccess() + public void testGetProperty() { - 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"); + /* should be empty and return null */ + assertEquals("This returns null", config.getProperty("foo"), null); - final 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); + /* 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"); } @Test - public void testSubset() + public void testGetShort() { - /* - * test subset : assure we don't reprocess the data elements - * when generating the subset - */ - - final String prop = "hey, that's a test"; - final String prop2 = "hey\\, that's a test"; - config.setProperty("prop.string", prop2); - config.setProperty("property.string", "hello"); + config.setProperty("numberS", "1"); + final short oneS = 1; + final 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"))); + } - Configuration subEprop = config.subset("prop"); + @Test(expected = ConversionException.class) + public void testGetShortIncompatibleType() + { + config.setProperty("test.empty", ""); + config.getShort("test.empty"); + } - assertEquals( - "Returns the full string", - prop, - subEprop.getString("string")); - assertEquals("Wrong list size", 1, subEprop.getList("string").size()); + @Test(expected = NoSuchElementException.class) + public void testGetShortUnknown() + { + config.getShort("numberNotInConfig"); + } - Iterator<String> it = subEprop.getKeys(); - it.next(); - assertFalse(it.hasNext()); + @Test + public void testGetString() + { + config.setProperty("testString", "The quick brown fox"); + final String string = "The quick brown fox"; + final String defaultValue = "jumps over the lazy dog"; - subEprop = config.subset("prop."); - it = subEprop.getKeys(); - assertFalse(it.hasNext()); + 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)); } + /** + * Tests that the first scalar of a list is returned. + */ @Test - public void testInterpolation() + public void testGetStringForListValue() { - InterpolationTestHelper.testInterpolation(config); + config.addProperty("number", "1"); + config.addProperty("number", "2"); + assertEquals("Wrong result", "1", config.getString("number")); } - @Test - public void testMultipleInterpolation() + @Test(expected = NoSuchElementException.class) + public void testGetStringUnknown() { - InterpolationTestHelper.testMultipleInterpolation(config); + config.getString("stringNotInConfig"); } + /** + * Tests whether a {@code ConfigurationInterpolator} can be created and + * installed. + */ @Test - public void testInterpolationLoop() + public void testInstallInterpolator() { - InterpolationTestHelper.testInterpolationLoop(config); + final Lookup prefixLookup = EasyMock.createMock(Lookup.class); + final Lookup defLookup = EasyMock.createMock(Lookup.class); + EasyMock.replay(prefixLookup, defLookup); + final Map<String, Lookup> prefixLookups = new HashMap<>(); + prefixLookups.put("test", prefixLookup); + final List<Lookup> defLookups = new ArrayList<>(); + defLookups.add(defLookup); + config.installInterpolator(prefixLookups, defLookups); + final ConfigurationInterpolator interpolator = config.getInterpolator(); + assertEquals("Wrong prefix lookups", prefixLookups, + interpolator.getLookups()); + final List<Lookup> defLookups2 = interpolator.getDefaultLookups(); + assertEquals("Wrong number of default lookups", 2, defLookups2.size()); + assertSame("Wrong default lookup 1", defLookup, defLookups2.get(0)); + final String var = "testVariable"; + final Object value = 42; + config.addProperty(var, value); + assertEquals("Wrong lookup result", value, + defLookups2.get(1).lookup(var)); } /** - * Tests interpolation when a subset configuration is involved. + * Tests obtaining a configuration with all variables replaced by their + * actual values. */ @Test - public void testInterpolationSubset() + public void testInterpolatedConfiguration() { - InterpolationTestHelper.testInterpolationSubset(config); + InterpolationTestHelper.testInterpolatedConfiguration(config); } - /** - * Tests interpolation when the referred property is not found. - */ @Test - public void testInterpolationUnknownProperty() + public void testInterpolation() { - InterpolationTestHelper.testInterpolationUnknownProperty(config); + InterpolationTestHelper.testInterpolation(config); } /** - * Tests interpolation of system properties. + * Tests interpolation of constant values. */ @Test - public void testInterpolationSystemProperties() + public void testInterpolationConstants() { - InterpolationTestHelper.testInterpolationSystemProperties(config); + InterpolationTestHelper.testInterpolationConstants(config); } /** @@ -538,15 +607,6 @@ public class TestBaseConfiguration } /** - * Tests interpolation of constant values. - */ - @Test - public void testInterpolationConstants() - { - InterpolationTestHelper.testInterpolationConstants(config); - } - - /** * Tests whether a variable can be escaped, so that it won't be * interpolated. */ @@ -565,65 +625,43 @@ public class TestBaseConfiguration InterpolationTestHelper.testInterpolationLocalhost(config); } - /** - * Tests accessing and manipulating the interpolator object. - */ @Test - public void testGetInterpolator() + public void testInterpolationLoop() { - InterpolationTestHelper.testGetInterpolator(config); + InterpolationTestHelper.testInterpolationLoop(config); } /** - * Tests obtaining a configuration with all variables replaced by their - * actual values. + * Tests interpolation when a subset configuration is involved. */ @Test - public void testInterpolatedConfiguration() + public void testInterpolationSubset() { - InterpolationTestHelper.testInterpolatedConfiguration(config); + InterpolationTestHelper.testInterpolationSubset(config); } /** - * Tests whether a {@code ConfigurationInterpolator} can be set. + * Tests interpolation of system properties. */ @Test - public void testSetInterpolator() + public void testInterpolationSystemProperties() { - final ConfigurationInterpolator interpolator = - EasyMock.createMock(ConfigurationInterpolator.class); - EasyMock.replay(interpolator); - config.setInterpolator(interpolator); - assertSame("Interpolator not set", interpolator, - config.getInterpolator()); + InterpolationTestHelper.testInterpolationSystemProperties(config); } /** - * Tests whether a {@code ConfigurationInterpolator} can be created and - * installed. + * Tests interpolation when the referred property is not found. */ @Test - public void testInstallInterpolator() + public void testInterpolationUnknownProperty() { - final Lookup prefixLookup = EasyMock.createMock(Lookup.class); - final Lookup defLookup = EasyMock.createMock(Lookup.class); - EasyMock.replay(prefixLookup, defLookup); - final Map<String, Lookup> prefixLookups = new HashMap<>(); - prefixLookups.put("test", prefixLookup); - final List<Lookup> defLookups = new ArrayList<>(); - defLookups.add(defLookup); - config.installInterpolator(prefixLookups, defLookups); - final ConfigurationInterpolator interpolator = config.getInterpolator(); - assertEquals("Wrong prefix lookups", prefixLookups, - interpolator.getLookups()); - final List<Lookup> defLookups2 = interpolator.getDefaultLookups(); - assertEquals("Wrong number of default lookups", 2, defLookups2.size()); - assertSame("Wrong default lookup 1", defLookup, defLookups2.get(0)); - final String var = "testVariable"; - final Object value = 42; - config.addProperty(var, value); - assertEquals("Wrong lookup result", value, - defLookups2.get(1).lookup(var)); + InterpolationTestHelper.testInterpolationUnknownProperty(config); + } + + @Test + public void testMultipleInterpolation() + { + InterpolationTestHelper.testMultipleInterpolation(config); } /** @@ -638,42 +676,6 @@ public class TestBaseConfiguration assertEquals("Wrong result", "${value}", config.getString("test")); } - @Test - 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()); - } - - @Test - 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. */ @@ -698,78 +700,40 @@ public class TestBaseConfiguration .getBigDecimal(KEY_NUMBER)); } - /** - * Tests cloning a BaseConfiguration. - */ - @Test - public void testClone() - { - for (int i = 0; i < 10; i++) - { - config.addProperty("key" + i, new Integer(i)); - } - final BaseConfiguration config2 = (BaseConfiguration) config.clone(); - - for (final Iterator<String> it = config.getKeys(); it.hasNext();) - { - final String key = it.next(); - assertTrue("Key not found: " + key, config2.containsKey(key)); - assertEquals("Wrong value for key " + key, config.getProperty(key), - config2.getProperty(key)); - } - } - - /** - * Tests whether a cloned configuration is decoupled from its original. - */ @Test - public void testCloneModify() + public void testPropertyAccess() { - final EventListener<ConfigurationEvent> l = new EventListenerTestImpl(config); - config.addEventListener(ConfigurationEvent.ANY, l); - config.addProperty("original", Boolean.TRUE); - final BaseConfiguration config2 = (BaseConfiguration) config.clone(); - - config2.addProperty("clone", Boolean.TRUE); - assertFalse("New key appears in original", config.containsKey("clone")); - config2.setProperty("original", Boolean.FALSE); - assertTrue("Wrong value of original property", config - .getBoolean("original")); - - assertTrue("Event listener was copied", config2 - .getEventListeners(ConfigurationEvent.ANY).isEmpty()); - } + 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"); - /** - * Tests the clone() method if a list property is involved. - */ - @Test - public void testCloneListProperty() - { - final String key = "list"; - config.addProperty(key, "value1"); - config.addProperty(key, "value2"); - final BaseConfiguration config2 = (BaseConfiguration) config.clone(); - config2.addProperty(key, "value3"); - assertEquals("Wrong number of original properties", 2, config.getList( - key).size()); + final 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); } /** - * Tests whether interpolation works as expected after cloning. + * Tests whether a {@code ConfigurationInterpolator} can be set. */ @Test - public void testCloneInterpolation() + public void testSetInterpolator() { - final String keyAnswer = "answer"; - config.addProperty(keyAnswer, "The answer is ${" + KEY_NUMBER + "}."); - config.addProperty(KEY_NUMBER, 42); - final BaseConfiguration clone = (BaseConfiguration) config.clone(); - clone.setProperty(KEY_NUMBER, 43); - assertEquals("Wrong interpolation in original", "The answer is 42.", - config.getString(keyAnswer)); - assertEquals("Wrong interpolation in clone", "The answer is 43.", - clone.getString(keyAnswer)); + final ConfigurationInterpolator interpolator = + EasyMock.createMock(ConfigurationInterpolator.class); + EasyMock.replay(interpolator); + config.setInterpolator(interpolator); + assertSame("Interpolator not set", interpolator, + config.getInterpolator()); } /** @@ -785,4 +749,40 @@ public class TestBaseConfiguration } assertEquals("Wrong size", count, config.size()); } + + @Test + public void testSubset() + { + /* + * test subset : assure we don't reprocess the data elements + * when generating the subset + */ + + final String prop = "hey, that's a test"; + final 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")); + assertEquals("Wrong list size", 1, subEprop.getList("string").size()); + + Iterator<String> it = subEprop.getKeys(); + it.next(); + assertFalse(it.hasNext()); + + subEprop = config.subset("prop."); + it = subEprop.getKeys(); + assertFalse(it.hasNext()); + } + + @Test + public void testThrowExceptionOnMissing() + { + assertTrue("Throw Exception Property is not set!", config.isThrowExceptionOnMissing()); + } }