diff --git a/src/main/java/org/apache/commons/lang3/text/StrLookup.java b/src/main/java/org/apache/commons/lang3/text/StrLookup.java index c4d42b3ee..446ee4cac 100644 --- a/src/main/java/org/apache/commons/lang3/text/StrLookup.java +++ b/src/main/java/org/apache/commons/lang3/text/StrLookup.java @@ -16,9 +16,7 @@ */ package org.apache.commons.lang3.text; -import java.util.Enumeration; import java.util.Map; -import java.util.Properties; /** * Lookup a String key to a String value. @@ -42,6 +40,11 @@ public abstract class StrLookup { */ private static final StrLookup NONE_LOOKUP = new MapStrLookup(null); + /** + * Lookup based on system properties. + */ + private static final StrLookup SYSTEM_PROPERTIES_LOOKUP = new SystemPropertiesStrLookup(); + //----------------------------------------------------------------------- /** * Returns a lookup which always returns null. @@ -52,29 +55,6 @@ public abstract class StrLookup { return NONE_LOOKUP; } - /** - * Creates a copy of the given properties instance. - * - * @param input the Properties instance to copy. - * @return a copy of {@code input}. - */ - private static Properties copyProperties(Properties input) { - if (input == null) { - return null; - } - - Properties output = new Properties(); - @SuppressWarnings("unchecked") // Property names are Strings. - Enumeration propertyNames = (Enumeration) input.propertyNames(); - - while (propertyNames.hasMoreElements()) { - String propertyName = propertyNames.nextElement(); - output.setProperty(propertyName, input.getProperty(propertyName)); - } - - return output; - } - /** * Returns a new lookup which uses a copy of the current * {@link System#getProperties() System properties}. @@ -87,19 +67,7 @@ public abstract class StrLookup { * @return a lookup using system properties, not null */ public static StrLookup systemPropertiesLookup() { - Properties systemProperties = null; - - try { - systemProperties = System.getProperties(); - } catch (final SecurityException ex) { - // Squelched. All lookup(String) will return null. - } - - Properties properties = copyProperties(systemProperties); - @SuppressWarnings("unchecked") // System property keys and values are always Strings - final Map propertiesMap = (Map) properties; - - return new MapStrLookup(propertiesMap); + return SYSTEM_PROPERTIES_LOOKUP; } /** @@ -188,4 +156,25 @@ public abstract class StrLookup { return obj.toString(); } } + + //----------------------------------------------------------------------- + /** + * Lookup implementation based on system properties. + */ + private static class SystemPropertiesStrLookup extends StrLookup { + /** + * {@inheritDoc} This implementation directly accesses system properties. + */ + @Override + public String lookup(String key) { + if (key.length() > 0) { + try { + return System.getProperty(key); + } catch (SecurityException scex) { + // Squelched. All lookup(String) will return null. + } + } + return null; + } + } } diff --git a/src/test/java/org/apache/commons/lang3/text/StrLookupTest.java b/src/test/java/org/apache/commons/lang3/text/StrLookupTest.java index 121dbff22..9190185bd 100644 --- a/src/test/java/org/apache/commons/lang3/text/StrLookupTest.java +++ b/src/test/java/org/apache/commons/lang3/text/StrLookupTest.java @@ -5,9 +5,9 @@ * 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. @@ -22,6 +22,7 @@ import static org.junit.Assert.fail; import java.util.HashMap; import java.util.Map; +import java.util.Properties; import org.junit.Test; @@ -51,20 +52,45 @@ public class StrLookupTest { } } + /** + * Tests that a lookup object for system properties can deal with a full + * replacement of the system properties object. This test is related to + * LANG-1055. + */ @Test - public void testSystemPropertiesLookupNotSingleton() { + public void testSystemPropertiesLookupReplacedProperties() { + Properties oldProperties = System.getProperties(); final String osName = "os.name"; - final String originalOsName = System.getProperty(osName); + final String newOsName = oldProperties.getProperty(osName) + "_changed"; - StrLookup properties1 = StrLookup.systemPropertiesLookup(); - assertEquals(originalOsName, properties1.lookup(osName)); + StrLookup sysLookup = StrLookup.systemPropertiesLookup(); + Properties newProps = new Properties(); + newProps.setProperty(osName, newOsName); + System.setProperties(newProps); + try { + assertEquals("Changed properties not detected", newOsName, sysLookup.lookup(osName)); + } finally { + System.setProperties(oldProperties); + } + } - final String differentOsName = "HAL-9000"; - System.setProperty(osName, differentOsName); - StrLookup properties2 = StrLookup.systemPropertiesLookup(); + /** + * Tests that a lookup object for system properties sees changes on system + * properties. This test is related to LANG-1141. + */ + @Test + public void testSystemPropertiesLookupUpdatedProperty() { + final String osName = "os.name"; + String oldOs = System.getProperty(osName); + final String newOsName = oldOs + "_changed"; - assertEquals(originalOsName, properties1.lookup(osName)); - assertEquals(differentOsName, properties2.lookup(osName)); + StrLookup sysLookup = StrLookup.systemPropertiesLookup(); + System.setProperty(osName, newOsName); + try { + assertEquals("Changed properties not detected", newOsName, sysLookup.lookup(osName)); + } finally { + System.setProperty(osName, oldOs); + } } @Test