commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject [1/3] [lang] [LANG-1141] StrLookup for system properties now sees updated values.
Date Wed, 24 Jun 2015 20:25:03 GMT
Repository: commons-lang
Updated Branches:
  refs/heads/master 04b55bc7c -> 80644cdab


[LANG-1141] StrLookup for system properties now sees updated values.

The lookup implementation now directly accesses system properties without
caching the Properties object in any way.


Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/54e63005
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/54e63005
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/54e63005

Branch: refs/heads/master
Commit: 54e63005446a2b0a4255ba16838122c5808ef886
Parents: 0343b4f
Author: oheger <oliver.heger@oliver-heger.de>
Authored: Wed Jun 24 22:14:13 2015 +0200
Committer: oheger <oliver.heger@oliver-heger.de>
Committed: Wed Jun 24 22:14:13 2015 +0200

----------------------------------------------------------------------
 .../apache/commons/lang3/text/StrLookup.java    | 65 ++++++++------------
 .../commons/lang3/text/StrLookupTest.java       | 48 +++++++++++----
 2 files changed, 64 insertions(+), 49 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/54e63005/src/main/java/org/apache/commons/lang3/text/StrLookup.java
----------------------------------------------------------------------
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 c4d42b3..446ee4c 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<V> {
      */
     private static final StrLookup<String> NONE_LOOKUP = new MapStrLookup<String>(null);
 
+    /**
+     * Lookup based on system properties.
+     */
+    private static final StrLookup<String> SYSTEM_PROPERTIES_LOOKUP = new SystemPropertiesStrLookup();
+
     //-----------------------------------------------------------------------
     /**
      * Returns a lookup which always returns null.
@@ -53,29 +56,6 @@ public abstract class StrLookup<V> {
     }
 
     /**
-     * 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<String> propertyNames = (Enumeration<String>) 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}.
      * <p>
@@ -87,19 +67,7 @@ public abstract class StrLookup<V> {
      * @return a lookup using system properties, not null
      */
     public static StrLookup<String> 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<String, String> propertiesMap = (Map) properties;
-
-        return new MapStrLookup<String>(propertiesMap);
+        return SYSTEM_PROPERTIES_LOOKUP;
     }
 
     /**
@@ -188,4 +156,25 @@ public abstract class StrLookup<V> {
             return obj.toString();
         }
     }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Lookup implementation based on system properties.
+     */
+    private static class SystemPropertiesStrLookup extends StrLookup<String> {
+        /**
+         * {@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;
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/54e63005/src/test/java/org/apache/commons/lang3/text/StrLookupTest.java
----------------------------------------------------------------------
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 121dbff..9190185 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<String> properties1 = StrLookup.systemPropertiesLookup();
-        assertEquals(originalOsName, properties1.lookup(osName));
+        StrLookup<String> 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<String> 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<String> sysLookup = StrLookup.systemPropertiesLookup();
+        System.setProperty(osName, newOsName);
+        try {
+            assertEquals("Changed properties not detected", newOsName, sysLookup.lookup(osName));
+        } finally {
+            System.setProperty(osName, oldOs);
+        }
     }
 
     @Test


Mime
View raw message