geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1833156 - in /geronimo/components/config/trunk/impl: pom.xml src/main/java/org/apache/geronimo/config/configsource/SystemEnvConfigSource.java src/test/java/org/apache/geronimo/config/test/internal/SystemEnvConfigSourceTest.java
Date Fri, 08 Jun 2018 09:50:46 GMT
Author: struberg
Date: Fri Jun  8 09:50:46 2018
New Revision: 1833156

URL: http://svn.apache.org/viewvc?rev=1833156&view=rev
Log:
fix SystemEnvConfigSource + add test for it

Added:
    geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/test/internal/SystemEnvConfigSourceTest.java
Modified:
    geronimo/components/config/trunk/impl/pom.xml
    geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/configsource/SystemEnvConfigSource.java

Modified: geronimo/components/config/trunk/impl/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/components/config/trunk/impl/pom.xml?rev=1833156&r1=1833155&r2=1833156&view=diff
==============================================================================
--- geronimo/components/config/trunk/impl/pom.xml (original)
+++ geronimo/components/config/trunk/impl/pom.xml Fri Jun  8 09:50:46 2018
@@ -103,6 +103,13 @@
                         <MY_BOOLEAN_PROPERTY>true</MY_BOOLEAN_PROPERTY>
                         <my_string_property>haha</my_string_property>
                         <MY_STRING_PROPERTY>woohoo</MY_STRING_PROPERTY>
+
+                        <!-- and for our internal SystemEnvConfigSourceTest -->
+                        <A_b_c>1</A_b_c>
+                        <A_B_C>2</A_B_C>
+                        <A_B_D>3</A_B_D>
+                        <A_B_e>4</A_B_e>
+
                     </environmentVariables>
                     <systemPropertyVariables>
                         <!-- TCKs assume it but this is not really a good option for prod
-->

Modified: geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/configsource/SystemEnvConfigSource.java
URL: http://svn.apache.org/viewvc/geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/configsource/SystemEnvConfigSource.java?rev=1833156&r1=1833155&r2=1833156&view=diff
==============================================================================
--- geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/configsource/SystemEnvConfigSource.java
(original)
+++ geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/configsource/SystemEnvConfigSource.java
Fri Jun  8 09:50:46 2018
@@ -19,6 +19,7 @@
 package org.apache.geronimo.config.configsource;
 
 
+import java.util.HashMap;
 import java.util.Map;
 
 import javax.enterprise.inject.Typed;
@@ -38,10 +39,20 @@ import org.eclipse.microprofile.config.s
 @Vetoed
 public class SystemEnvConfigSource extends BaseConfigSource {
     private Map<String, String> configValues;
+    private Map<String, String> uppercasePosixValues;
 
     public SystemEnvConfigSource() {
+        uppercasePosixValues = new HashMap<>();
         configValues = System.getenv();
         initOrdinal(300);
+
+        for (Map.Entry<String, String> e : configValues.entrySet()) {
+            String originalKey = e.getKey();
+            String posixKey = replaceNonPosixEnvChars(originalKey).toUpperCase();
+            if (!originalKey.equals(posixKey)) {
+                uppercasePosixValues.put(posixKey, e.getValue());
+            }
+        }
     }
 
     @Override
@@ -58,9 +69,21 @@ public class SystemEnvConfigSource exten
     public String getValue(String key) {
         String val = configValues.get(key);
         if (val == null) {
-            val = configValues.get(key.replaceAll("[^Aa-zZ0-9]", "_"));
+            key = replaceNonPosixEnvChars(key);
+            val = configValues.get(key);
+        }
+        if (val == null) {
+            key = key.toUpperCase();
+            val = configValues.get(key);
+        }
+        if (val == null) {
+            val = uppercasePosixValues.get(key);
         }
 
         return val;
     }
+
+    private String replaceNonPosixEnvChars(String key) {
+        return key.replaceAll("[^A-Za-z0-9]", "_");
+    }
 }

Added: geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/test/internal/SystemEnvConfigSourceTest.java
URL: http://svn.apache.org/viewvc/geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/test/internal/SystemEnvConfigSourceTest.java?rev=1833156&view=auto
==============================================================================
--- geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/test/internal/SystemEnvConfigSourceTest.java
(added)
+++ geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/test/internal/SystemEnvConfigSourceTest.java
Fri Jun  8 09:50:46 2018
@@ -0,0 +1,49 @@
+/*
+ *  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.geronimo.config.test.internal;
+
+import org.eclipse.microprofile.config.Config;
+import org.eclipse.microprofile.config.ConfigProvider;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+/**
+ * This needs to have some ENV settings set up.
+ * This is usually done via maven.
+ * For running the test yourself you have to set the following environment properties:
+ *
+ * A_b_c=1
+ * A_B_C=2
+ * A_B_D=3
+ * A_B_e=4
+ */
+public class SystemEnvConfigSourceTest {
+    @Test
+    public void testEnvReplacement() {
+        Config config = ConfigProvider.getConfig();
+
+        Assert.assertEquals(config.getValue("A.b#c", Integer.class), Integer.valueOf(1));
+
+        Assert.assertEquals(config.getValue("a.b.c", Integer.class), Integer.valueOf(2));
+
+        Assert.assertEquals(config.getValue("a.b.d", Integer.class), Integer.valueOf(3));
+
+        Assert.assertEquals(config.getValue("a.b.e", Integer.class), Integer.valueOf(4));
+    }
+}



Mime
View raw message