Repository: deltaspike
Updated Branches:
  refs/heads/master e7fce70c2 -> e2b54d4db


DELTASPIKE-1253 handling file path too


Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/e2b54d4d
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/e2b54d4d
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/e2b54d4d

Branch: refs/heads/master
Commit: e2b54d4db684582e563aaaf5aa09c00cee42e6f7
Parents: e7fce70
Author: rmannibucau <rmannibu...@apache.org>
Authored: Tue May 9 11:14:40 2017 +0200
Committer: rmannibucau <rmannibu...@apache.org>
Committed: Tue May 9 11:15:02 2017 +0200

----------------------------------------------------------------------
 .../deltaspike/core/util/PropertyFileUtils.java |  13 ++-
 .../core/util/PropertyFileUtilsTest.java        | 107 +++++++++++++++++++
 2 files changed, 119 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/e2b54d4d/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/PropertyFileUtils.java
----------------------------------------------------------------------
diff --git 
a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/PropertyFileUtils.java
 
b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/PropertyFileUtils.java
index 514c2c9..ad9e10b 100644
--- 
a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/PropertyFileUtils.java
+++ 
b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/PropertyFileUtils.java
@@ -19,9 +19,11 @@
 package org.apache.deltaspike.core.util;
 
 import javax.enterprise.inject.Typed;
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
+import java.util.Collections;
 import java.util.Enumeration;
 import java.util.Locale;
 import java.util.Properties;
@@ -41,7 +43,7 @@ public abstract class PropertyFileUtils
 
     public static Enumeration<URL> resolvePropertyFiles(String 
propertyFileName) throws IOException
     {
-        if (propertyFileName != null && propertyFileName.contains("://"))
+        if (propertyFileName != null && (propertyFileName.contains("://") || 
propertyFileName.startsWith("file:")))
         {
             // the given string is actually already an URL
             Vector<URL> propertyFileUrls = new Vector<URL>();
@@ -49,6 +51,15 @@ public abstract class PropertyFileUtils
             return propertyFileUrls.elements();
         }
 
+        if (propertyFileName != null)
+        {
+            File file = new File(propertyFileName);
+            if (file.exists())
+            {
+                return 
Collections.enumeration(Collections.singleton(file.toURI().toURL()));
+            }
+        }
+
         ClassLoader cl = ClassUtils.getClassLoader(null);
 
         Enumeration<URL> propertyFileUrls = cl.getResources(propertyFileName);

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/e2b54d4d/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/PropertyFileUtilsTest.java
----------------------------------------------------------------------
diff --git 
a/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/PropertyFileUtilsTest.java
 
b/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/PropertyFileUtilsTest.java
new file mode 100644
index 0000000..cde3d5b
--- /dev/null
+++ 
b/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/PropertyFileUtilsTest.java
@@ -0,0 +1,107 @@
+/*
+ * 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.deltaspike.core.util;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Arrays;
+import java.util.Enumeration;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(Parameterized.class)
+public class PropertyFileUtilsTest
+{
+    @Parameterized.Parameters
+    public static Iterable<Object[]> cases() throws MalformedURLException
+    {
+        return Arrays.<Object[]>asList(
+                new Object[] // classpath
+                        {
+                            new Case("test.properties")
+                            {
+                                @Override
+                                public void run()
+                                {
+                                    assertTrue(result.hasMoreElements());
+                                    result.nextElement();
+                                    assertFalse(result.hasMoreElements());
+                                }
+                            }
+                        },
+                new Object[] // file path
+                        {
+                                new Case("src/test")
+                                {
+                                    @Override
+                                    public void run()
+                                    {
+                                        assertTrue(result.hasMoreElements());
+                                        result.nextElement();
+                                        assertFalse(result.hasMoreElements());
+                                    }
+                                }
+                        },
+                new Object[] // url
+                        {
+                                new Case(new 
File("src/test/resources/test.properties").toURI().toURL().toExternalForm())
+                                {
+                                    @Override
+                                    public void run()
+                                    {
+                                        assertTrue(result.hasMoreElements());
+                                        result.nextElement();
+                                        assertFalse(result.hasMoreElements());
+                                    }
+                                }
+                        });
+    }
+
+    private final Case test;
+
+    public PropertyFileUtilsTest(Case test)
+    {
+        this.test = test;
+    }
+
+    @Test
+    public void run() throws IOException
+    {
+        test.result = PropertyFileUtils.resolvePropertyFiles(test.file);
+        test.run();
+    }
+
+    private static abstract class Case implements Runnable
+    {
+        private String file;
+        protected Enumeration<URL> result;
+
+        private Case(final String file)
+        {
+            this.file = file;
+        }
+    }
+}

Reply via email to