You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2019/05/07 17:29:16 UTC

[commons-vfs] branch master updated: [VFS-713] Add FileObjectUtils.readProperties(FileObject) method to read a .properties file.

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-vfs.git


The following commit(s) were added to refs/heads/master by this push:
     new 41fdf24  [VFS-713] Add FileObjectUtils.readProperties(FileObject) method to read a .properties file.
41fdf24 is described below

commit 41fdf24d27ef16ce02b05a4a8414f758f5b6e556
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue May 7 13:29:09 2019 -0400

    [VFS-713] Add FileObjectUtils.readProperties(FileObject) method to read
    a .properties file.
    
    Also make exists() tests not depend on a Windows path.
---
 .../apache/commons/vfs2/util/FileObjectUtils.java  | 42 +++++++++++++++++++++-
 .../commons/vfs2/util/FileObjectUtilsTest.java     | 30 ++++++++++++++--
 commons-vfs2/src/test/resources/test.properties    | 18 ++++++++++
 src/changes/changes.xml                            |  3 ++
 4 files changed, 90 insertions(+), 3 deletions(-)

diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/util/FileObjectUtils.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/util/FileObjectUtils.java
index 3f626ed..07b7180 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/util/FileObjectUtils.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/util/FileObjectUtils.java
@@ -16,6 +16,10 @@
  */
 package org.apache.commons.vfs2.util;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
 import org.apache.commons.vfs2.FileObject;
 import org.apache.commons.vfs2.FileSystemException;
 import org.apache.commons.vfs2.impl.DecoratedFileObject;
@@ -36,6 +40,7 @@ public final class FileObjectUtils {
      * @param fileObject
      * @return false if {@code fileObject} is null, otherwise, see {@link FileObject#exists()}.
      * @throws FileSystemException On error determining if this file exists.
+     * @since 2.4
      */
     public static boolean exists(final FileObject fileObject) throws FileSystemException {
         return fileObject != null && fileObject.exists();
@@ -67,7 +72,7 @@ public final class FileObjectUtils {
     /**
      * Checks if the given FileObject is instance of given class argument.
      *
-     * @param fileObject The FileObject.
+     * @param fileObject  The FileObject.
      * @param wantedClass The Class to check.
      * @return true if fileObject is an instance of the specified Class.
      * @throws FileSystemException if an error occurs.
@@ -89,4 +94,39 @@ public final class FileObjectUtils {
 
         return false;
     }
+
+    /**
+     * Reads the given file into a new {@link Properties}.
+     *
+     * @param fileObject the file to read
+     * @return a new {@link Properties}.
+     * @throws IOException
+     * @throws FileSystemException On error getting this file's content.
+     * @throws IOException On error getting this file's content.
+     * @since 2.4
+     */
+    public static Properties readProperties(final FileObject fileObject) throws FileSystemException, IOException {
+        return readProperties(fileObject, new Properties());
+    }
+
+    /**
+     * Reads the given file into a new given {@link Properties}.
+     *
+     * @param fileObject the file to read
+     * @param properties the destination
+     * @return a new {@link Properties}.
+     * @throws FileSystemException On error getting this file's content.
+     * @throws IOException On error getting this file's content.
+     * @since 2.4
+     */
+    public static Properties readProperties(final FileObject fileObject, final Properties properties)
+            throws FileSystemException, IOException {
+        if (fileObject == null) {
+            return properties;
+        }
+        try (InputStream inputStream = fileObject.getContent().getInputStream()) {
+            properties.load(inputStream);
+        }
+        return properties;
+    }
 }
diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/util/FileObjectUtilsTest.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/util/FileObjectUtilsTest.java
index f041037..7d11eb1 100644
--- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/util/FileObjectUtilsTest.java
+++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/util/FileObjectUtilsTest.java
@@ -17,6 +17,11 @@
 
 package org.apache.commons.vfs2.util;
 
+import java.io.File;
+import java.io.IOException;
+import java.util.Properties;
+
+import org.apache.commons.lang3.SystemUtils;
 import org.apache.commons.vfs2.FileSystemException;
 import org.apache.commons.vfs2.VFS;
 import org.junit.Assert;
@@ -31,17 +36,38 @@ public class FileObjectUtilsTest {
 
     @Test
     public void testExistsNotNull() throws FileSystemException {
-        Assert.assertFalse(FileObjectUtils.exists(VFS.getManager().getBaseFile()));
+        Assert.assertTrue(FileObjectUtils.exists(VFS.getManager().toFileObject(SystemUtils.getJavaIoTmpDir())));
     }
 
     @Test
     public void testNotExistsNotNull() throws FileSystemException {
         Assert.assertFalse(FileObjectUtils
-                .exists(VFS.getManager().resolveFile("file:///C:/This file can't possibly exist, right?")));
+                .exists(VFS.getManager().toFileObject(new File("This file can't possibly exist, right?"))));
     }
 
     @Test
     public void testNotExistsNull() throws FileSystemException {
         Assert.assertFalse(FileObjectUtils.exists(null));
     }
+
+    @Test
+    public void testReadProperties() throws FileSystemException, IOException {
+        assertProperties(FileObjectUtils
+                .readProperties(VFS.getManager().toFileObject(new File("src/test/resources/test.properties"))));
+    }
+
+    @Test
+    public void testReadPropertiesInto() throws FileSystemException, IOException {
+        Properties p = new Properties();
+        p.setProperty("extraKey", "extraValue");
+        assertProperties(FileObjectUtils
+                .readProperties(VFS.getManager().toFileObject(new File("src/test/resources/test.properties")), p));
+        Assert.assertEquals("extraValue", p.getProperty("extraKey"));
+    }
+
+    private void assertProperties(Properties p) {
+        Assert.assertNotNull(p);
+        Assert.assertEquals("1", p.getProperty("one"));
+        Assert.assertEquals("2", p.getProperty("two"));
+    }
 }
diff --git a/commons-vfs2/src/test/resources/test.properties b/commons-vfs2/src/test/resources/test.properties
new file mode 100644
index 0000000..acccd13
--- /dev/null
+++ b/commons-vfs2/src/test/resources/test.properties
@@ -0,0 +1,18 @@
+# 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.
+one = 1
+two = 2
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 02c13da..f1d80a5 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -113,6 +113,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action issue="VFS-712" dev="ggregory" type="add" due-to="Gary Gregory">
         Add null-safe org.apache.commons.vfs2.util.FileObjectUtils.exists(FileObject).
       </action>
+      <action issue="VFS-713" dev="ggregory" type="add" due-to="Gary Gregory">
+        Add FileObjectUtils.readProperties(FileObject) method to read a .properties file.
+      </action>
     </release>
     <release version="2.3" date="2019-02-01" description="New features and bug fix release.">
       <action issue="VFS-645" dev="ggregory" type="fix">