You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2019/08/05 15:26:30 UTC
[commons-configuration] 02/03: Sort members.
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 193140131452277d4f83ec51fb6812cd55da10ae
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Aug 5 11:23:07 2019 -0400
Sort members.
---
.../interpol/TestConfigurationInterpolator.java | 598 ++++++++++-----------
.../interpol/TestConstantLookup.java | 94 ++--
.../configuration2/interpol/TestExprLookup.java | 94 ++--
3 files changed, 393 insertions(+), 393 deletions(-)
diff --git a/src/test/java/org/apache/commons/configuration2/interpol/TestConfigurationInterpolator.java b/src/test/java/org/apache/commons/configuration2/interpol/TestConfigurationInterpolator.java
index 203fab1..937fde2 100644
--- a/src/test/java/org/apache/commons/configuration2/interpol/TestConfigurationInterpolator.java
+++ b/src/test/java/org/apache/commons/configuration2/interpol/TestConfigurationInterpolator.java
@@ -40,24 +40,15 @@ import org.junit.Test;
*/
public class TestConfigurationInterpolator
{
- /** Constant for a test variable prefix. */
- private static final String TEST_PREFIX = "prefix";
-
/** Constant for a test variable name. */
private static final String TEST_NAME = "varname";
+ /** Constant for a test variable prefix. */
+ private static final String TEST_PREFIX = "prefix";
+
/** Constant for the value of the test variable. */
private static final String TEST_VALUE = "TestVariableValue";
- /** Stores the object to be tested. */
- private ConfigurationInterpolator interpolator;
-
- @Before
- public void setUp() throws Exception
- {
- interpolator = new ConfigurationInterpolator();
- }
-
/**
* Creates a lookup object that can resolve the test variable (and nothing else).
*
@@ -96,51 +87,39 @@ public class TestConfigurationInterpolator
return lookup;
}
- /**
- * Tests creating an instance. Does it contain some predefined lookups?
- */
- @Test
- public void testInit()
- {
- assertTrue("A default lookup is set", interpolator.getDefaultLookups().isEmpty());
- assertTrue("Got predefined lookups", interpolator.getLookups().isEmpty());
- assertNull("Got a parent interpolator", interpolator.getParentInterpolator());
- }
+ /** Stores the object to be tested. */
+ private ConfigurationInterpolator interpolator;
- /**
- * Tests registering a lookup object at an instance.
- */
- @Test
- public void testRegisterLookup()
+ @Before
+ public void setUp() throws Exception
{
- final Lookup lookup = EasyMock.createMock(Lookup.class);
- EasyMock.replay(lookup);
- interpolator.registerLookup(TEST_PREFIX, lookup);
- assertSame("New lookup not registered", lookup, interpolator
- .getLookups().get(TEST_PREFIX));
- assertTrue("Not in prefix set",
- interpolator.prefixSet().contains(TEST_PREFIX));
- assertTrue("Default lookups were changed", interpolator
- .getDefaultLookups().isEmpty());
+ interpolator = new ConfigurationInterpolator();
}
/**
- * Tests registering a null lookup object. This should cause an exception.
+ * Tests whether multiple default lookups can be added.
*/
- @Test(expected = IllegalArgumentException.class)
- public void testRegisterLookupNull()
+ @Test
+ public void testAddDefaultLookups()
{
- interpolator.registerLookup(TEST_PREFIX, null);
+ final List<Lookup> lookups = new ArrayList<>();
+ lookups.add(setUpTestLookup());
+ lookups.add(setUpTestLookup("test", "value"));
+ interpolator.addDefaultLookups(lookups);
+ final List<Lookup> lookups2 = interpolator.getDefaultLookups();
+ assertEquals("Wrong number of default lookups", 2, lookups2.size());
+ assertTrue("Wrong content", lookups2.containsAll(lookups));
}
/**
- * Tests registering a lookup object for an undefined prefix. This should
- * cause an exception.
+ * Tests whether a null collection of default lookups is handled correctly.
*/
- @Test(expected = IllegalArgumentException.class)
- public void testRegisterLookupNullPrefix()
+ @Test
+ public void testAddDefaultLookupsNull()
{
- interpolator.registerLookup(null, EasyMock.createMock(Lookup.class));
+ interpolator.addDefaultLookups(null);
+ assertTrue("Got default lookups", interpolator.getDefaultLookups()
+ .isEmpty());
}
/**
@@ -170,222 +149,240 @@ public class TestConfigurationInterpolator
}
/**
- * Tests whether a variable can be resolved using the associated lookup
- * object. The lookup is identified by the variable's prefix.
+ * Tests whether the flag for substitution in variable names can be
+ * modified.
*/
@Test
- public void testResolveWithPrefix()
+ public void testEnableSubstitutionInVariables()
{
- interpolator.registerLookup(TEST_PREFIX, setUpTestLookup());
- assertEquals("Wrong variable value", TEST_VALUE, interpolator
- .resolve(TEST_PREFIX + ':' + TEST_NAME));
+ assertFalse("Variable substitution enabled",
+ interpolator.isEnableSubstitutionInVariables());
+ interpolator.addDefaultLookup(setUpTestLookup("java.version", "1.4"));
+ interpolator.addDefaultLookup(setUpTestLookup("jre-1.4",
+ "C:\\java\\1.4"));
+ final String var = "${jre-${java.version}}";
+ assertEquals("Wrong result (1)", var, interpolator.interpolate(var));
+ interpolator.setEnableSubstitutionInVariables(true);
+ assertTrue("Variable substitution not enabled",
+ interpolator.isEnableSubstitutionInVariables());
+ assertEquals("Wrong result (2)", "C:\\java\\1.4",
+ interpolator.interpolate(var));
}
/**
- * Tests the behavior of the lookup method for variables with an unknown
- * prefix. These variables should not be resolved.
+ * Tests fromSpecification() if the specification contains an instance.
*/
@Test
- public void testResolveWithUnknownPrefix()
+ public void testFromSpecificationInterpolator()
{
- interpolator.registerLookup(TEST_PREFIX, setUpTestLookup());
- assertNull("Variable could be resolved", interpolator
- .resolve("UnknownPrefix:" + TEST_NAME));
- assertNull("Variable with empty prefix could be resolved", interpolator
- .resolve(":" + TEST_NAME));
+ final ConfigurationInterpolator ci =
+ EasyMock.createMock(ConfigurationInterpolator.class);
+ EasyMock.replay(ci);
+ final InterpolatorSpecification spec =
+ new InterpolatorSpecification.Builder()
+ .withDefaultLookup(EasyMock.createMock(Lookup.class))
+ .withParentInterpolator(interpolator)
+ .withInterpolator(ci).create();
+ assertSame("Wrong result", ci,
+ ConfigurationInterpolator.fromSpecification(spec));
}
/**
- * Tests looking up a variable without a prefix. This should trigger the
- * default lookup object.
+ * Tests fromSpecification() if a new instance has to be created.
*/
@Test
- public void testResolveDefault()
+ public void testFromSpecificationNewInstance()
{
- final Lookup l1 = EasyMock.createMock(Lookup.class);
- final Lookup l2 = EasyMock.createMock(Lookup.class);
- final Lookup l3 = EasyMock.createMock(Lookup.class);
- EasyMock.expect(l1.lookup(TEST_NAME)).andReturn(null);
- EasyMock.expect(l2.lookup(TEST_NAME)).andReturn(TEST_VALUE);
- EasyMock.replay(l1, l2, l3);
- interpolator.addDefaultLookups(Arrays.asList(l1, l2, l3));
- assertEquals("Wrong variable value", TEST_VALUE, interpolator
- .resolve(TEST_NAME));
- EasyMock.verify(l1, l2, l3);
+ final Lookup defLookup = EasyMock.createMock(Lookup.class);
+ final Lookup preLookup = EasyMock.createMock(Lookup.class);
+ EasyMock.replay(defLookup, preLookup);
+ final InterpolatorSpecification spec =
+ new InterpolatorSpecification.Builder()
+ .withDefaultLookup(defLookup)
+ .withPrefixLookup("p", preLookup)
+ .withParentInterpolator(interpolator).create();
+ final ConfigurationInterpolator ci =
+ ConfigurationInterpolator.fromSpecification(spec);
+ assertEquals("Wrong number of default lookups", 1, ci
+ .getDefaultLookups().size());
+ assertTrue("Wrong default lookup",
+ ci.getDefaultLookups().contains(defLookup));
+ assertEquals("Wrong number of prefix lookups", 1, ci.getLookups()
+ .size());
+ assertSame("Wrong prefix lookup", preLookup, ci.getLookups().get("p"));
+ assertSame("Wrong parent", interpolator, ci.getParentInterpolator());
}
/**
- * Tests looking up a variable without a prefix when no default lookup is
- * specified. Result should be null in this case.
+ * Tries to obtain an instance from a null specification.
*/
- @Test
- public void testResolveNoDefault()
+ @Test(expected = IllegalArgumentException.class)
+ public void testFromSpecificationNull()
{
- assertNull("Variable could be resolved", interpolator.resolve(TEST_NAME));
+ ConfigurationInterpolator.fromSpecification(null);
}
/**
- * Tests the empty variable prefix. This is a special case, but legal.
+ * Tests whether modification of the list of default lookups does not affect
+ * the object.
*/
@Test
- public void testResolveEmptyPrefix()
+ public void testGetDefaultLookupsModify()
{
- interpolator.registerLookup("", setUpTestLookup());
- assertEquals("Wrong variable value", TEST_VALUE, interpolator
- .resolve(":" + TEST_NAME));
+ final List<Lookup> lookups = interpolator.getDefaultLookups();
+ lookups.add(setUpTestLookup());
+ assertTrue("List was modified", interpolator.getDefaultLookups()
+ .isEmpty());
}
/**
- * Tests an empty variable name.
+ * Tests whether default prefix lookups can be queried as a map.
*/
@Test
- public void testResolveEmptyVarName()
+ public void testGetDefaultPrefixLookups()
{
- interpolator.registerLookup(TEST_PREFIX, setUpTestLookup("", TEST_VALUE));
- assertEquals("Wrong variable value", TEST_VALUE, interpolator
- .resolve(TEST_PREFIX + ":"));
+ final Map<String, Lookup> lookups =
+ ConfigurationInterpolator.getDefaultPrefixLookups();
+ assertEquals("Wrong number of lookups", DefaultLookups.values().length,
+ lookups.size());
+ for (final DefaultLookups l : DefaultLookups.values())
+ {
+ assertSame("Wrong entry for " + l, l.getLookup(),
+ lookups.get(l.getPrefix()));
+ }
}
/**
- * Tests an empty variable name without a prefix.
+ * Tests that the map with default lookups cannot be modified.
*/
- @Test
- public void testResolveDefaultEmptyVarName()
+ @Test(expected = UnsupportedOperationException.class)
+ public void testGetDefaultPrefixLookupsModify()
{
- interpolator.addDefaultLookup(setUpTestLookup("", TEST_VALUE));
- assertEquals("Wrong variable value", TEST_VALUE, interpolator
- .resolve(""));
+ ConfigurationInterpolator.getDefaultPrefixLookups().put("test",
+ EasyMock.createMock(Lookup.class));
}
/**
- * Tests looking up a null variable. Result should be null, too.
+ * Tests that modification of the map with lookups does not affect the object.
*/
@Test
- public void testResolveNull()
+ public void testGetLookupsModify()
{
- assertNull("Could resolve null variable", interpolator.resolve(null));
+ final Map<String, Lookup> lookups = interpolator.getLookups();
+ lookups.put(TEST_PREFIX, setUpTestLookup());
+ assertTrue("Map was modified", interpolator.getLookups().isEmpty());
}
/**
- * Tests whether the default lookup is called for variables with a prefix
- * when the lookup that was registered for this prefix is not able to
- * resolve the variable.
+ * Tests creating an instance. Does it contain some predefined lookups?
*/
@Test
- public void testResolveDefaultAfterPrefixFails()
+ public void testInit()
{
- final String varName = TEST_PREFIX + ':' + TEST_NAME + "2";
- interpolator.registerLookup(TEST_PREFIX, setUpTestLookup());
- interpolator.addDefaultLookup(setUpTestLookup(varName, TEST_VALUE));
- assertEquals("Variable is not resolved by default lookup", TEST_VALUE,
- interpolator.resolve(varName));
+ assertTrue("A default lookup is set", interpolator.getDefaultLookups().isEmpty());
+ assertTrue("Got predefined lookups", interpolator.getLookups().isEmpty());
+ assertNull("Got a parent interpolator", interpolator.getParentInterpolator());
}
/**
- * Tests whether a map with lookup objects can be registered.
+ * Tests that an empty variable definition does not cause problems.
*/
@Test
- public void testRegisterLookups()
+ public void testInterpolateEmptyVariable()
{
- final Lookup l1 = setUpTestLookup();
- final Lookup l2 = setUpTestLookup("someVar", "someValue");
- final Map<String, Lookup> lookups = new HashMap<>();
- lookups.put(TEST_PREFIX, l1);
- final String prefix2 = TEST_PREFIX + "_other";
- lookups.put(prefix2, l2);
- interpolator.registerLookups(lookups);
- final Map<String, Lookup> lookups2 = interpolator.getLookups();
- assertEquals("Wrong number of lookups", 2, lookups2.size());
- assertEquals("Wrong l1", l1, lookups2.get(TEST_PREFIX));
- assertEquals("Wrong l2", l2, lookups2.get(prefix2));
+ final String value = "${}";
+ assertEquals("Wrong result", value, interpolator.interpolate(value));
}
/**
- * Tests whether a null map with lookup objects is handled correctly.
+ * Tests interpolation of a non string argument.
*/
@Test
- public void testRegisterLookupsNull()
+ public void testInterpolateObject()
{
- interpolator.registerLookups(null);
- assertTrue("Got lookups", interpolator.getLookups().isEmpty());
+ final Object value = 42;
+ assertSame("Value was changed", value, interpolator.interpolate(value));
}
/**
- * Tests that modification of the map with lookups does not affect the object.
+ * Tests a successful interpolation of a string value.
*/
@Test
- public void testGetLookupsModify()
+ public void testInterpolateString()
{
- final Map<String, Lookup> lookups = interpolator.getLookups();
- lookups.put(TEST_PREFIX, setUpTestLookup());
- assertTrue("Map was modified", interpolator.getLookups().isEmpty());
+ final String value = "${" + TEST_PREFIX + ':' + TEST_NAME + "}";
+ interpolator.registerLookup(TEST_PREFIX, setUpTestLookup());
+ assertEquals("Wrong result", TEST_VALUE,
+ interpolator.interpolate(value));
}
/**
- * Tests whether multiple default lookups can be added.
+ * Tests interpolation with a variable which cannot be resolved.
*/
@Test
- public void testAddDefaultLookups()
+ public void testInterpolateStringUnknownVariable()
{
- final List<Lookup> lookups = new ArrayList<>();
- lookups.add(setUpTestLookup());
- lookups.add(setUpTestLookup("test", "value"));
- interpolator.addDefaultLookups(lookups);
- final List<Lookup> lookups2 = interpolator.getDefaultLookups();
- assertEquals("Wrong number of default lookups", 2, lookups2.size());
- assertTrue("Wrong content", lookups2.containsAll(lookups));
+ final String value = "${unknownVariable}";
+ assertEquals("Wrong result", value, interpolator.interpolate(value));
}
/**
- * Tests whether a null collection of default lookups is handled correctly.
+ * Tests a property value consisting of multiple variables.
*/
@Test
- public void testAddDefaultLookupsNull()
+ public void testInterpolationMultipleVariables()
{
- interpolator.addDefaultLookups(null);
- assertTrue("Got default lookups", interpolator.getDefaultLookups()
- .isEmpty());
+ final String value = "The ${subject} jumps over ${object}.";
+ interpolator.addDefaultLookup(setUpTestLookup("subject", "quick brown fox"));
+ interpolator.addDefaultLookup(setUpTestLookup("object", "the lazy dog"));
+ assertEquals("Wrong result", "The quick brown fox jumps over the lazy dog.",
+ interpolator.interpolate(value));
}
/**
- * Tests whether modification of the list of default lookups does not affect
- * the object.
+ * Tests an interpolation that consists of a single variable only. The
+ * variable's value should be returned verbatim.
*/
@Test
- public void testGetDefaultLookupsModify()
+ public void testInterpolationSingleVariable()
{
- final List<Lookup> lookups = interpolator.getDefaultLookups();
- lookups.add(setUpTestLookup());
- assertTrue("List was modified", interpolator.getDefaultLookups()
- .isEmpty());
+ final Object value = 42;
+ interpolator.addDefaultLookup(setUpTestLookup(TEST_NAME, value));
+ assertEquals("Wrong result", value,
+ interpolator.interpolate("${" + TEST_NAME + "}"));
}
/**
- * Tests whether a default lookup object can be removed.
+ * Tests a variable declaration which lacks the trailing closing bracket.
*/
@Test
- public void testRemoveDefaultLookup()
+ public void testInterpolationVariableIncomplete()
{
- final List<Lookup> lookups = new ArrayList<>();
- lookups.add(setUpTestLookup());
- lookups.add(setUpTestLookup("test", "value"));
- interpolator.addDefaultLookups(lookups);
- assertTrue("Wrong result",
- interpolator.removeDefaultLookup(lookups.get(0)));
- assertFalse("Lookup still available", interpolator.getDefaultLookups()
- .contains(lookups.get(0)));
- assertEquals("Wrong number of default lookups", 1, interpolator
- .getDefaultLookups().size());
+ final String value = "${" + TEST_NAME;
+ interpolator.addDefaultLookup(setUpTestLookup(TEST_NAME, "someValue"));
+ assertEquals("Wrong result", value, interpolator.interpolate(value));
}
/**
- * Tests whether a non existing default lookup object can be removed.
+ * Tests nullSafeLookup() if a lookup object was provided.
*/
@Test
- public void testRemoveDefaultLookupNonExisting()
+ public void testNullSafeLookupExisting()
{
- assertFalse("Wrong result",
- interpolator.removeDefaultLookup(setUpTestLookup()));
+ final Lookup look = EasyMock.createMock(Lookup.class);
+ EasyMock.replay(look);
+ assertSame("Wrong result", look,
+ ConfigurationInterpolator.nullSafeLookup(look));
+ }
+
+ /**
+ * Tests whether nullSafeLookup() can handle null input.
+ */
+ @Test
+ public void testNullSafeLookupNull()
+ {
+ final Lookup lookup = ConfigurationInterpolator.nullSafeLookup(null);
+ assertNull("Got a lookup result", lookup.lookup("someVar"));
}
/**
@@ -401,236 +398,239 @@ public class TestConfigurationInterpolator
}
/**
- * Tests handling of a parent {@code ConfigurationInterpolator} if the
- * variable can already be resolved by the current instance.
+ * Tests registering a lookup object at an instance.
*/
@Test
- public void testResolveParentVariableFound()
+ public void testRegisterLookup()
{
- final ConfigurationInterpolator parent =
- EasyMock.createMock(ConfigurationInterpolator.class);
- EasyMock.replay(parent);
- interpolator.setParentInterpolator(parent);
- interpolator.registerLookup(TEST_PREFIX, setUpTestLookup());
- assertEquals("Wrong value", TEST_VALUE,
- interpolator.resolve(TEST_PREFIX + ':' + TEST_NAME));
+ final Lookup lookup = EasyMock.createMock(Lookup.class);
+ EasyMock.replay(lookup);
+ interpolator.registerLookup(TEST_PREFIX, lookup);
+ assertSame("New lookup not registered", lookup, interpolator
+ .getLookups().get(TEST_PREFIX));
+ assertTrue("Not in prefix set",
+ interpolator.prefixSet().contains(TEST_PREFIX));
+ assertTrue("Default lookups were changed", interpolator
+ .getDefaultLookups().isEmpty());
}
/**
- * Tests whether the parent {@code ConfigurationInterpolator} is invoked if
- * the test instance cannot resolve a variable.
+ * Tests registering a null lookup object. This should cause an exception.
*/
- @Test
- public void testResolveParentVariableNotFound()
+ @Test(expected = IllegalArgumentException.class)
+ public void testRegisterLookupNull()
{
- final ConfigurationInterpolator parent =
- EasyMock.createMock(ConfigurationInterpolator.class);
- EasyMock.expect(parent.resolve(TEST_NAME)).andReturn(TEST_VALUE);
- EasyMock.replay(parent);
- interpolator.setParentInterpolator(parent);
- assertEquals("Wrong value", TEST_VALUE, interpolator.resolve(TEST_NAME));
- EasyMock.verify(parent);
+ interpolator.registerLookup(TEST_PREFIX, null);
}
/**
- * Tests interpolation of a non string argument.
+ * Tests registering a lookup object for an undefined prefix. This should
+ * cause an exception.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testRegisterLookupNullPrefix()
+ {
+ interpolator.registerLookup(null, EasyMock.createMock(Lookup.class));
+ }
+
+ /**
+ * Tests whether a map with lookup objects can be registered.
*/
@Test
- public void testInterpolateObject()
+ public void testRegisterLookups()
{
- final Object value = 42;
- assertSame("Value was changed", value, interpolator.interpolate(value));
+ final Lookup l1 = setUpTestLookup();
+ final Lookup l2 = setUpTestLookup("someVar", "someValue");
+ final Map<String, Lookup> lookups = new HashMap<>();
+ lookups.put(TEST_PREFIX, l1);
+ final String prefix2 = TEST_PREFIX + "_other";
+ lookups.put(prefix2, l2);
+ interpolator.registerLookups(lookups);
+ final Map<String, Lookup> lookups2 = interpolator.getLookups();
+ assertEquals("Wrong number of lookups", 2, lookups2.size());
+ assertEquals("Wrong l1", l1, lookups2.get(TEST_PREFIX));
+ assertEquals("Wrong l2", l2, lookups2.get(prefix2));
}
/**
- * Tests a successful interpolation of a string value.
+ * Tests whether a null map with lookup objects is handled correctly.
*/
@Test
- public void testInterpolateString()
+ public void testRegisterLookupsNull()
{
- final String value = "${" + TEST_PREFIX + ':' + TEST_NAME + "}";
- interpolator.registerLookup(TEST_PREFIX, setUpTestLookup());
- assertEquals("Wrong result", TEST_VALUE,
- interpolator.interpolate(value));
+ interpolator.registerLookups(null);
+ assertTrue("Got lookups", interpolator.getLookups().isEmpty());
}
/**
- * Tests interpolation with a variable which cannot be resolved.
+ * Tests whether a default lookup object can be removed.
*/
@Test
- public void testInterpolateStringUnknownVariable()
+ public void testRemoveDefaultLookup()
{
- final String value = "${unknownVariable}";
- assertEquals("Wrong result", value, interpolator.interpolate(value));
+ final List<Lookup> lookups = new ArrayList<>();
+ lookups.add(setUpTestLookup());
+ lookups.add(setUpTestLookup("test", "value"));
+ interpolator.addDefaultLookups(lookups);
+ assertTrue("Wrong result",
+ interpolator.removeDefaultLookup(lookups.get(0)));
+ assertFalse("Lookup still available", interpolator.getDefaultLookups()
+ .contains(lookups.get(0)));
+ assertEquals("Wrong number of default lookups", 1, interpolator
+ .getDefaultLookups().size());
}
/**
- * Tests whether the flag for substitution in variable names can be
- * modified.
+ * Tests whether a non existing default lookup object can be removed.
*/
@Test
- public void testEnableSubstitutionInVariables()
+ public void testRemoveDefaultLookupNonExisting()
{
- assertFalse("Variable substitution enabled",
- interpolator.isEnableSubstitutionInVariables());
- interpolator.addDefaultLookup(setUpTestLookup("java.version", "1.4"));
- interpolator.addDefaultLookup(setUpTestLookup("jre-1.4",
- "C:\\java\\1.4"));
- final String var = "${jre-${java.version}}";
- assertEquals("Wrong result (1)", var, interpolator.interpolate(var));
- interpolator.setEnableSubstitutionInVariables(true);
- assertTrue("Variable substitution not enabled",
- interpolator.isEnableSubstitutionInVariables());
- assertEquals("Wrong result (2)", "C:\\java\\1.4",
- interpolator.interpolate(var));
+ assertFalse("Wrong result",
+ interpolator.removeDefaultLookup(setUpTestLookup()));
}
/**
- * Tests a property value consisting of multiple variables.
+ * Tests looking up a variable without a prefix. This should trigger the
+ * default lookup object.
*/
@Test
- public void testInterpolationMultipleVariables()
+ public void testResolveDefault()
{
- final String value = "The ${subject} jumps over ${object}.";
- interpolator.addDefaultLookup(setUpTestLookup("subject", "quick brown fox"));
- interpolator.addDefaultLookup(setUpTestLookup("object", "the lazy dog"));
- assertEquals("Wrong result", "The quick brown fox jumps over the lazy dog.",
- interpolator.interpolate(value));
+ final Lookup l1 = EasyMock.createMock(Lookup.class);
+ final Lookup l2 = EasyMock.createMock(Lookup.class);
+ final Lookup l3 = EasyMock.createMock(Lookup.class);
+ EasyMock.expect(l1.lookup(TEST_NAME)).andReturn(null);
+ EasyMock.expect(l2.lookup(TEST_NAME)).andReturn(TEST_VALUE);
+ EasyMock.replay(l1, l2, l3);
+ interpolator.addDefaultLookups(Arrays.asList(l1, l2, l3));
+ assertEquals("Wrong variable value", TEST_VALUE, interpolator
+ .resolve(TEST_NAME));
+ EasyMock.verify(l1, l2, l3);
}
/**
- * Tests an interpolation that consists of a single variable only. The
- * variable's value should be returned verbatim.
+ * Tests whether the default lookup is called for variables with a prefix
+ * when the lookup that was registered for this prefix is not able to
+ * resolve the variable.
*/
@Test
- public void testInterpolationSingleVariable()
+ public void testResolveDefaultAfterPrefixFails()
{
- final Object value = 42;
- interpolator.addDefaultLookup(setUpTestLookup(TEST_NAME, value));
- assertEquals("Wrong result", value,
- interpolator.interpolate("${" + TEST_NAME + "}"));
+ final String varName = TEST_PREFIX + ':' + TEST_NAME + "2";
+ interpolator.registerLookup(TEST_PREFIX, setUpTestLookup());
+ interpolator.addDefaultLookup(setUpTestLookup(varName, TEST_VALUE));
+ assertEquals("Variable is not resolved by default lookup", TEST_VALUE,
+ interpolator.resolve(varName));
}
/**
- * Tests a variable declaration which lacks the trailing closing bracket.
+ * Tests an empty variable name without a prefix.
*/
@Test
- public void testInterpolationVariableIncomplete()
+ public void testResolveDefaultEmptyVarName()
{
- final String value = "${" + TEST_NAME;
- interpolator.addDefaultLookup(setUpTestLookup(TEST_NAME, "someValue"));
- assertEquals("Wrong result", value, interpolator.interpolate(value));
+ interpolator.addDefaultLookup(setUpTestLookup("", TEST_VALUE));
+ assertEquals("Wrong variable value", TEST_VALUE, interpolator
+ .resolve(""));
}
/**
- * Tests that an empty variable definition does not cause problems.
+ * Tests the empty variable prefix. This is a special case, but legal.
*/
@Test
- public void testInterpolateEmptyVariable()
+ public void testResolveEmptyPrefix()
{
- final String value = "${}";
- assertEquals("Wrong result", value, interpolator.interpolate(value));
+ interpolator.registerLookup("", setUpTestLookup());
+ assertEquals("Wrong variable value", TEST_VALUE, interpolator
+ .resolve(":" + TEST_NAME));
}
/**
- * Tries to obtain an instance from a null specification.
+ * Tests an empty variable name.
*/
- @Test(expected = IllegalArgumentException.class)
- public void testFromSpecificationNull()
+ @Test
+ public void testResolveEmptyVarName()
{
- ConfigurationInterpolator.fromSpecification(null);
+ interpolator.registerLookup(TEST_PREFIX, setUpTestLookup("", TEST_VALUE));
+ assertEquals("Wrong variable value", TEST_VALUE, interpolator
+ .resolve(TEST_PREFIX + ":"));
}
/**
- * Tests fromSpecification() if the specification contains an instance.
+ * Tests looking up a variable without a prefix when no default lookup is
+ * specified. Result should be null in this case.
*/
@Test
- public void testFromSpecificationInterpolator()
+ public void testResolveNoDefault()
{
- final ConfigurationInterpolator ci =
- EasyMock.createMock(ConfigurationInterpolator.class);
- EasyMock.replay(ci);
- final InterpolatorSpecification spec =
- new InterpolatorSpecification.Builder()
- .withDefaultLookup(EasyMock.createMock(Lookup.class))
- .withParentInterpolator(interpolator)
- .withInterpolator(ci).create();
- assertSame("Wrong result", ci,
- ConfigurationInterpolator.fromSpecification(spec));
+ assertNull("Variable could be resolved", interpolator.resolve(TEST_NAME));
}
/**
- * Tests fromSpecification() if a new instance has to be created.
+ * Tests looking up a null variable. Result should be null, too.
*/
@Test
- public void testFromSpecificationNewInstance()
+ public void testResolveNull()
{
- final Lookup defLookup = EasyMock.createMock(Lookup.class);
- final Lookup preLookup = EasyMock.createMock(Lookup.class);
- EasyMock.replay(defLookup, preLookup);
- final InterpolatorSpecification spec =
- new InterpolatorSpecification.Builder()
- .withDefaultLookup(defLookup)
- .withPrefixLookup("p", preLookup)
- .withParentInterpolator(interpolator).create();
- final ConfigurationInterpolator ci =
- ConfigurationInterpolator.fromSpecification(spec);
- assertEquals("Wrong number of default lookups", 1, ci
- .getDefaultLookups().size());
- assertTrue("Wrong default lookup",
- ci.getDefaultLookups().contains(defLookup));
- assertEquals("Wrong number of prefix lookups", 1, ci.getLookups()
- .size());
- assertSame("Wrong prefix lookup", preLookup, ci.getLookups().get("p"));
- assertSame("Wrong parent", interpolator, ci.getParentInterpolator());
+ assertNull("Could resolve null variable", interpolator.resolve(null));
}
/**
- * Tests whether default prefix lookups can be queried as a map.
+ * Tests handling of a parent {@code ConfigurationInterpolator} if the
+ * variable can already be resolved by the current instance.
*/
@Test
- public void testGetDefaultPrefixLookups()
+ public void testResolveParentVariableFound()
{
- final Map<String, Lookup> lookups =
- ConfigurationInterpolator.getDefaultPrefixLookups();
- assertEquals("Wrong number of lookups", DefaultLookups.values().length,
- lookups.size());
- for (final DefaultLookups l : DefaultLookups.values())
- {
- assertSame("Wrong entry for " + l, l.getLookup(),
- lookups.get(l.getPrefix()));
- }
+ final ConfigurationInterpolator parent =
+ EasyMock.createMock(ConfigurationInterpolator.class);
+ EasyMock.replay(parent);
+ interpolator.setParentInterpolator(parent);
+ interpolator.registerLookup(TEST_PREFIX, setUpTestLookup());
+ assertEquals("Wrong value", TEST_VALUE,
+ interpolator.resolve(TEST_PREFIX + ':' + TEST_NAME));
}
/**
- * Tests that the map with default lookups cannot be modified.
+ * Tests whether the parent {@code ConfigurationInterpolator} is invoked if
+ * the test instance cannot resolve a variable.
*/
- @Test(expected = UnsupportedOperationException.class)
- public void testGetDefaultPrefixLookupsModify()
+ @Test
+ public void testResolveParentVariableNotFound()
{
- ConfigurationInterpolator.getDefaultPrefixLookups().put("test",
- EasyMock.createMock(Lookup.class));
+ final ConfigurationInterpolator parent =
+ EasyMock.createMock(ConfigurationInterpolator.class);
+ EasyMock.expect(parent.resolve(TEST_NAME)).andReturn(TEST_VALUE);
+ EasyMock.replay(parent);
+ interpolator.setParentInterpolator(parent);
+ assertEquals("Wrong value", TEST_VALUE, interpolator.resolve(TEST_NAME));
+ EasyMock.verify(parent);
}
/**
- * Tests nullSafeLookup() if a lookup object was provided.
+ * Tests whether a variable can be resolved using the associated lookup
+ * object. The lookup is identified by the variable's prefix.
*/
@Test
- public void testNullSafeLookupExisting()
+ public void testResolveWithPrefix()
{
- final Lookup look = EasyMock.createMock(Lookup.class);
- EasyMock.replay(look);
- assertSame("Wrong result", look,
- ConfigurationInterpolator.nullSafeLookup(look));
+ interpolator.registerLookup(TEST_PREFIX, setUpTestLookup());
+ assertEquals("Wrong variable value", TEST_VALUE, interpolator
+ .resolve(TEST_PREFIX + ':' + TEST_NAME));
}
/**
- * Tests whether nullSafeLookup() can handle null input.
+ * Tests the behavior of the lookup method for variables with an unknown
+ * prefix. These variables should not be resolved.
*/
@Test
- public void testNullSafeLookupNull()
+ public void testResolveWithUnknownPrefix()
{
- final Lookup lookup = ConfigurationInterpolator.nullSafeLookup(null);
- assertNull("Got a lookup result", lookup.lookup("someVar"));
+ interpolator.registerLookup(TEST_PREFIX, setUpTestLookup());
+ assertNull("Variable could be resolved", interpolator
+ .resolve("UnknownPrefix:" + TEST_NAME));
+ assertNull("Variable with empty prefix could be resolved", interpolator
+ .resolve(":" + TEST_NAME));
}
}
diff --git a/src/test/java/org/apache/commons/configuration2/interpol/TestConstantLookup.java b/src/test/java/org/apache/commons/configuration2/interpol/TestConstantLookup.java
index c2821da..501fac0 100644
--- a/src/test/java/org/apache/commons/configuration2/interpol/TestConstantLookup.java
+++ b/src/test/java/org/apache/commons/configuration2/interpol/TestConstantLookup.java
@@ -48,18 +48,6 @@ public class TestConstantLookup
}
/**
- * Generates the name of a variable for a lookup operation based on the
- * given field name of this class.
- *
- * @param field the field name
- * @return the variable for looking up this field
- */
- private String variable(final String field)
- {
- return getClass().getName() + '.' + field;
- }
-
- /**
* Clears the test environment. Here the static cache of the constant lookup
* class is wiped out.
*/
@@ -70,56 +58,58 @@ public class TestConstantLookup
}
/**
- * Tests resolving a valid constant.
+ * Tests accessing the cache by querying a variable twice.
*/
@Test
- public void testLookupConstant()
+ public void testLookupCache()
{
- assertEquals("Wrong value of constant", FIELD,
- lookup.lookup(variable("FIELD")));
+ testLookupConstant();
+ testLookupConstant();
}
/**
- * Tests resolving a non existing constant. Result should be null.
+ * Tests resolving a valid constant.
*/
@Test
- public void testLookupNonExisting()
+ public void testLookupConstant()
{
- assertNull("Non null return value for non existing constant",
- lookup.lookup(variable("NO_FIELD")));
+ assertEquals("Wrong value of constant", FIELD,
+ lookup.lookup(variable("FIELD")));
}
/**
- * Tests resolving a private constant. Because a private field cannot be
- * accessed this should again yield null.
+ * Tries to resolve a variable with an invalid syntax: The name does not
+ * contain a dot as a field separator.
*/
@Test
- public void testLookupPrivate()
+ public void testLookupInvalidSyntax()
{
- assertNull("Non null return value for non accessible field", lookup
- .lookup(variable("PRIVATE_FIELD")));
+ assertNull("Non null return value for invalid variable name", lookup
+ .lookup("InvalidVariableName"));
}
/**
- * Tests resolving a field from an unknown class.
+ * Tests resolving a non existing constant. Result should be null.
*/
@Test
- public void testLookupUnknownClass()
+ public void testLookupNonExisting()
{
- assertNull("Non null return value for unknown class", lookup
- .lookup("org.apache.commons.configuration.NonExistingConfig."
- + FIELD));
+ assertNull("Non null return value for non existing constant",
+ lookup.lookup(variable("NO_FIELD")));
}
/**
- * Tries to resolve a variable with an invalid syntax: The name does not
- * contain a dot as a field separator.
+ * Tests resolving a non string constant. Then looks the same variable up
+ * from the cache.
*/
@Test
- public void testLookupInvalidSyntax()
+ public void testLookupNonStringFromCache()
{
- assertNull("Non null return value for invalid variable name", lookup
- .lookup("InvalidVariableName"));
+ final String var = KeyEvent.class.getName() + ".VK_ESCAPE";
+ final Object expected = KeyEvent.VK_ESCAPE;
+ assertEquals("Wrong result of first lookup", expected, lookup
+ .lookup(var));
+ assertEquals("Wrong result of 2nd lookup", expected, lookup.lookup(var));
}
/**
@@ -133,26 +123,36 @@ public class TestConstantLookup
}
/**
- * Tests accessing the cache by querying a variable twice.
+ * Tests resolving a private constant. Because a private field cannot be
+ * accessed this should again yield null.
*/
@Test
- public void testLookupCache()
+ public void testLookupPrivate()
{
- testLookupConstant();
- testLookupConstant();
+ assertNull("Non null return value for non accessible field", lookup
+ .lookup(variable("PRIVATE_FIELD")));
}
/**
- * Tests resolving a non string constant. Then looks the same variable up
- * from the cache.
+ * Tests resolving a field from an unknown class.
*/
@Test
- public void testLookupNonStringFromCache()
+ public void testLookupUnknownClass()
{
- final String var = KeyEvent.class.getName() + ".VK_ESCAPE";
- final Object expected = KeyEvent.VK_ESCAPE;
- assertEquals("Wrong result of first lookup", expected, lookup
- .lookup(var));
- assertEquals("Wrong result of 2nd lookup", expected, lookup.lookup(var));
+ assertNull("Non null return value for unknown class", lookup
+ .lookup("org.apache.commons.configuration.NonExistingConfig."
+ + FIELD));
+ }
+
+ /**
+ * Generates the name of a variable for a lookup operation based on the
+ * given field name of this class.
+ *
+ * @param field the field name
+ * @return the variable for looking up this field
+ */
+ private String variable(final String field)
+ {
+ return getClass().getName() + '.' + field;
}
}
diff --git a/src/test/java/org/apache/commons/configuration2/interpol/TestExprLookup.java b/src/test/java/org/apache/commons/configuration2/interpol/TestExprLookup.java
index f3a36b2..0d24072 100644
--- a/src/test/java/org/apache/commons/configuration2/interpol/TestExprLookup.java
+++ b/src/test/java/org/apache/commons/configuration2/interpol/TestExprLookup.java
@@ -43,13 +43,33 @@ import org.junit.Test;
*/
public class TestExprLookup
{
- private static File TEST_FILE = ConfigurationAssert.getTestFile("test.xml");
+ public static class Utility
+ {
+ String message;
+
+ public Utility(final String msg)
+ {
+ this.message = msg;
+ }
+
+ public String getMessage()
+ {
+ return message;
+ }
+
+ public String str(final String str)
+ {
+ return str;
+ }
+ }
private static String PATTERN1 =
"String.replace(Util.message, 'Hello', 'Goodbye') + System.getProperty('user.name')";
private static String PATTERN2 =
"'$[element] ' + String.trimToEmpty('$[space.description]')";
+ private static File TEST_FILE = ConfigurationAssert.getTestFile("test.xml");
+
/**
* Loads the test configuration.
*
@@ -64,6 +84,32 @@ public class TestExprLookup
return config;
}
+ /**
+ * Tests whether variables can be queried.
+ */
+ @Test
+ public void testGetVariables()
+ {
+ final ExprLookup.Variables vars = new ExprLookup.Variables();
+ vars.add(new ExprLookup.Variable("String", org.apache.commons.lang3.StringUtils.class));
+ final ExprLookup lookup = new ExprLookup(vars);
+ assertEquals("Wrong variables", vars, lookup.getVariables());
+ }
+
+ /**
+ * Tests that getVariables() returns a copy of the original variables.
+ */
+ @Test
+ public void testGetVariablesDefensiveCopy()
+ {
+ final ExprLookup.Variables vars = new ExprLookup.Variables();
+ vars.add(new ExprLookup.Variable("String", org.apache.commons.lang3.StringUtils.class));
+ final ExprLookup lookup = new ExprLookup(vars);
+ final ExprLookup.Variables vars2 = lookup.getVariables();
+ vars2.add(new ExprLookup.Variable("System", "Class:java.lang.System"));
+ assertEquals("Modified variables", vars, lookup.getVariables());
+ }
+
@Test
public void testLookup() throws Exception
{
@@ -104,32 +150,6 @@ public class TestExprLookup
}
/**
- * Tests whether variables can be queried.
- */
- @Test
- public void testGetVariables()
- {
- final ExprLookup.Variables vars = new ExprLookup.Variables();
- vars.add(new ExprLookup.Variable("String", org.apache.commons.lang3.StringUtils.class));
- final ExprLookup lookup = new ExprLookup(vars);
- assertEquals("Wrong variables", vars, lookup.getVariables());
- }
-
- /**
- * Tests that getVariables() returns a copy of the original variables.
- */
- @Test
- public void testGetVariablesDefensiveCopy()
- {
- final ExprLookup.Variables vars = new ExprLookup.Variables();
- vars.add(new ExprLookup.Variable("String", org.apache.commons.lang3.StringUtils.class));
- final ExprLookup lookup = new ExprLookup(vars);
- final ExprLookup.Variables vars2 = lookup.getVariables();
- vars2.add(new ExprLookup.Variable("System", "Class:java.lang.System"));
- assertEquals("Modified variables", vars, lookup.getVariables());
- }
-
- /**
* Tests an expression that does not yield a string.
*/
@Test
@@ -159,24 +179,4 @@ public class TestExprLookup
assertNull("Wrong result",
lookup.lookup("System.getProperty('undefined.property')"));
}
-
- public static class Utility
- {
- String message;
-
- public Utility(final String msg)
- {
- this.message = msg;
- }
-
- public String getMessage()
- {
- return message;
- }
-
- public String str(final String str)
- {
- return str;
- }
- }
}