openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject [openwebbeans] branch master updated: MEECROWAVE-203 enable to pass a custom merger to ProperyLoader#getProperties
Date Mon, 15 Jul 2019 09:24:11 GMT
This is an automated email from the ASF dual-hosted git repository.

rmannibucau pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/openwebbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new 4cf201a  MEECROWAVE-203 enable to pass a custom merger to ProperyLoader#getProperties
4cf201a is described below

commit 4cf201a861381d01deb1fe6b350fc4e072ecd515
Author: Romain Manni-Bucau <rmannibucau@gmail.com>
AuthorDate: Mon Jul 15 11:24:06 2019 +0200

    MEECROWAVE-203 enable to pass a custom merger to ProperyLoader#getProperties
---
 .../org/apache/webbeans/config/PropertyLoader.java | 16 +++--
 .../webbeans/test/config/PropertyLoaderTest.java   | 74 ++++++++++++++++++++++
 2 files changed, 84 insertions(+), 6 deletions(-)

diff --git a/webbeans-impl/src/main/java/org/apache/webbeans/config/PropertyLoader.java b/webbeans-impl/src/main/java/org/apache/webbeans/config/PropertyLoader.java
index aa23b8f..bfcc22d 100644
--- a/webbeans-impl/src/main/java/org/apache/webbeans/config/PropertyLoader.java
+++ b/webbeans-impl/src/main/java/org/apache/webbeans/config/PropertyLoader.java
@@ -28,6 +28,7 @@ import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.List;
 import java.util.Properties;
+import java.util.function.Function;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -74,7 +75,8 @@ public final class PropertyLoader
      * @param propertyFileName the name of the properties file
      * @return the final property values
      */
-    public static synchronized Properties getProperties(String propertyFileName)
+    public static synchronized Properties getProperties(String propertyFileName,
+                                                        Function<List<Properties>,
Properties> merger)
     {
         try
         {
@@ -83,10 +85,7 @@ public final class PropertyLoader
             {
                 return null;
             }
-            
-            List<Properties> sortedProperties = sortProperties(allProperties);
-            Properties properties = mergeProperties(sortedProperties);
-            return properties;
+            return merger.apply(sortProperties(allProperties));
         }
         catch (IOException e)
         {
@@ -95,6 +94,11 @@ public final class PropertyLoader
         }
     }
 
+    public static synchronized Properties getProperties(String propertyFileName)
+    {
+        return getProperties(propertyFileName, PropertyLoader::mergeProperties);
+    }
+
     public static List<Properties> loadAllProperties(String propertyFileName)
             throws IOException
     {
@@ -204,7 +208,7 @@ public final class PropertyLoader
      * @param sortedProperties
      * @return the merged Properties
      */
-    private static Properties mergeProperties(List<Properties> sortedProperties)
+    public static Properties mergeProperties(List<Properties> sortedProperties)
     {
         Properties mergedProperties = new Properties();
         for (Properties p : sortedProperties)
diff --git a/webbeans-impl/src/test/java/org/apache/webbeans/test/config/PropertyLoaderTest.java
b/webbeans-impl/src/test/java/org/apache/webbeans/test/config/PropertyLoaderTest.java
index a344a81..863467d 100644
--- a/webbeans-impl/src/test/java/org/apache/webbeans/test/config/PropertyLoaderTest.java
+++ b/webbeans-impl/src/test/java/org/apache/webbeans/test/config/PropertyLoaderTest.java
@@ -18,11 +18,23 @@
  */
 package org.apache.webbeans.test.config;
 
+import static java.util.Arrays.asList;
+import static java.util.Collections.enumeration;
+import static java.util.Comparator.comparing;
+
 import org.apache.webbeans.config.PropertyLoader;
 import org.junit.Assert;
 import org.junit.Test;
 
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
+import java.util.Enumeration;
 import java.util.List;
 import java.util.Properties;
 
@@ -31,6 +43,41 @@ public class PropertyLoaderTest
     private static final String PROPERTY_FILE = "org/apache/webbeans/test/config/propertyloadertest.properties";
     private static final String PROPERTY_FILE2 = "org/apache/webbeans/test/config/propertyloadertest2.properties";
     private static final String PROPERTY_FILE3 = "org/apache/webbeans/test/config/propertyloadertest3.properties";
+    private static final String PROPERTY_FILE4 = "org/apache/webbeans/test/config/propertyloadertest3.properties";
+
+    @Test
+    public void testPropertyLoaderCustomMerger() {
+        final Thread thread = Thread.currentThread();
+        final ClassLoader loader = thread.getContextClassLoader();
+        thread.setContextClassLoader(new ClassLoader(loader)
+        {
+            @Override
+            public Enumeration<URL> getResources(String name) throws IOException
+            {
+                return enumeration(asList(
+                        new URL("memory", null, -1, PROPERTY_FILE4,
+                                new MemoryHandler("order = 2\ntestConfig=second\nconfiguration.ordinal=2")),
+                        new URL("memory", null, -1, PROPERTY_FILE4,
+                                new MemoryHandler("order = 1\ntestConfig=first\nconfiguration.ordinal=1"))
+                ));
+            }
+        });
+        try
+        {
+            final Properties p = PropertyLoader.getProperties(PROPERTY_FILE4, props ->
+                    props.stream().sorted(comparing(it -> Integer.parseInt(it.getProperty("order"))))
+                            .findFirst().orElseThrow(IllegalStateException::new));
+            Assert.assertNotNull(p);
+
+            String testValue = p.getProperty("testConfig");
+            Assert.assertNotNull(testValue);
+            Assert.assertEquals("first", testValue);
+        }
+        finally
+        {
+            thread.setContextClassLoader(loader);
+        }
+    }
 
     @Test
     public void testPropertyLoader() throws Exception
@@ -74,4 +121,31 @@ public class PropertyLoaderTest
         Assert.assertEquals("20", prop.get("unique_3"));
         
     }
+
+    private static class MemoryHandler extends URLStreamHandler
+    {
+        private final String content;
+
+        private MemoryHandler(final String content)
+        {
+            this.content = content;
+        }
+
+        @Override
+        protected URLConnection openConnection(final URL u) {
+            return new URLConnection(u)
+            {
+                @Override
+                public void connect()
+                {
+                    // no-op
+                }
+
+                @Override
+                public InputStream getInputStream() {
+                    return new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
+                }
+            };
+        }
+    }
 }


Mime
View raw message