Author: ggregory Date: Fri Sep 28 15:43:43 2018 New Revision: 1842261 URL: http://svn.apache.org/viewvc?rev=1842261&view=rev Log: [CONFIGURATION-724] Add support for Commons Text 1.4 localhost string lookup as a default lookup.
Added: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/interpol/StringLookupAdapter.java Modified: commons/proper/configuration/trunk/src/changes/changes.xml commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/interpol/DefaultLookups.java commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/InterpolationTestHelper.java commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestAbstractHierarchicalConfiguration.java commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestBaseConfiguration.java Modified: commons/proper/configuration/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/changes/changes.xml?rev=1842261&r1=1842260&r2=1842261&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/changes/changes.xml (original) +++ commons/proper/configuration/trunk/src/changes/changes.xml Fri Sep 28 15:43:43 2018 @@ -59,6 +59,9 @@ <action dev="ggregory" type="update" issue="CONFIGURATION-723"> Update optional Spring dependencies from 4.3.18.RELEASE to 4.3.19.RELEASE. </action> + <action dev="ggregory" type="update" issue="CONFIGURATION-724"> + Add support for Commons Text 1.4 localhost string lookup as a default lookup. + </action> </release> <release version="2.3" date="2018-08-04" Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/interpol/DefaultLookups.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/interpol/DefaultLookups.java?rev=1842261&r1=1842260&r2=1842261&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/interpol/DefaultLookups.java (original) +++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/interpol/DefaultLookups.java Fri Sep 28 15:43:43 2018 @@ -16,6 +16,8 @@ */ package org.apache.commons.configuration2.interpol; +import org.apache.commons.text.lookup.StringLookupFactory; + /** * <p> * An enumeration class defining constants for the {@code Lookup} objects @@ -37,6 +39,9 @@ package org.apache.commons.configuration */ public enum DefaultLookups { + /** @since 2.4 */ + LOCAL_HOST("localhost", new StringLookupAdapter(StringLookupFactory.INSTANCE.localHostStringLookup())), + /** The lookup for system properties. */ SYSTEM_PROPERTIES("sys", new SystemPropertiesLookup()), @@ -70,8 +75,7 @@ public enum DefaultLookups * * @return the prefix */ - public String getPrefix() - { + public String getPrefix() { return prefix; } @@ -80,8 +84,7 @@ public enum DefaultLookups * * @return the associated {@code Lookup} object */ - public Lookup getLookup() - { + public Lookup getLookup() { return lookup; } } Added: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/interpol/StringLookupAdapter.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/interpol/StringLookupAdapter.java?rev=1842261&view=auto ============================================================================== --- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/interpol/StringLookupAdapter.java (added) +++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/interpol/StringLookupAdapter.java Fri Sep 28 15:43:43 2018 @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.configuration2.interpol; + +import java.util.Objects; + +import org.apache.commons.text.lookup.StringLookup; + +/** + * Wraps an Apache Commons Text {@link StringLookup} as an Apache Commons Configuration {@link Lookup}. + * + * @since 2.4 + */ +class StringLookupAdapter implements Lookup { + + private final StringLookup stringLookup; + + StringLookupAdapter(StringLookup stringLookup) { + super(); + this.stringLookup = Objects.requireNonNull(stringLookup, "stringLookup"); + } + + @Override + public Object lookup(String key) { + return stringLookup.lookup(key); + } + +} Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/InterpolationTestHelper.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/InterpolationTestHelper.java?rev=1842261&r1=1842260&r2=1842261&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/InterpolationTestHelper.java (original) +++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/InterpolationTestHelper.java Fri Sep 28 15:43:43 2018 @@ -20,6 +20,8 @@ import static org.junit.Assert.assertEqu import static org.junit.Assert.fail; import java.awt.event.KeyEvent; +import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -27,6 +29,8 @@ import java.util.Map; import org.apache.commons.configuration2.interpol.ConfigurationInterpolator; import org.apache.commons.configuration2.interpol.Lookup; +import junitx.framework.Assert; + /** * A helper class that defines a bunch of tests related to variable * interpolation. It can be used for running these tests on different @@ -181,6 +185,37 @@ public class InterpolationTestHelper } /** + * Tests interpolation of localhost properties. + * + * @param config the configuration to test + */ + public static void testInterpolationLocalhost(Configuration config) + { + String[] localhostKeys = + { "name", "canonical-name", "address" }; + String[] localhostValues = null; + try { + localhostValues = new String[] { + InetAddress.getLocalHost().getHostName(), + InetAddress.getLocalHost().getCanonicalHostName(), + InetAddress.getLocalHost().getHostAddress() }; + } catch (UnknownHostException e) { + Assert.fail(e); + } + for (int i = 0; i < localhostKeys.length; i++) + { + config.addProperty("prop" + i, "${localhost:" + localhostKeys[i] + "}"); + } + + for (int i = 0; i < localhostKeys.length; i++) + { + assertEquals("Wrong value for system property " + + localhostKeys[i], localhostValues[i], + config.getString("prop" + i)); + } + } + + /** * Tests an invalid interpolation that results in an infinite loop. This * loop should be detected and an exception should be thrown. * Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestAbstractHierarchicalConfiguration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestAbstractHierarchicalConfiguration.java?rev=1842261&r1=1842260&r2=1842261&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestAbstractHierarchicalConfiguration.java (original) +++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestAbstractHierarchicalConfiguration.java Fri Sep 28 15:43:43 2018 @@ -756,6 +756,15 @@ public class TestAbstractHierarchicalCon } /** + * Tests interpolation with localhost values. + */ + @Test + public void testInterpolationLocalhost() + { + InterpolationTestHelper.testInterpolationLocalhost(config); + } + + /** * Tests manipulating the interpolator. */ @Test Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestBaseConfiguration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestBaseConfiguration.java?rev=1842261&r1=1842260&r2=1842261&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestBaseConfiguration.java (original) +++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestBaseConfiguration.java Fri Sep 28 15:43:43 2018 @@ -558,6 +558,15 @@ public class TestBaseConfiguration } /** + * Tests interpolation with localhost values. + */ + @Test + public void testInterpolationLocalhost() + { + InterpolationTestHelper.testInterpolationLocalhost(config); + } + + /** * Tests accessing and manipulating the interpolator object. */ @Test