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 d794c6d  [VFS-736] Add org.apache.commons.vfs2.VFS.reset().
d794c6d is described below

commit d794c6dbd3a3563ce23cdfc98f172a2cea428cb6
Author: Gary Gregory <[email protected]>
AuthorDate: Sat Oct 5 18:35:52 2019 -0400

    [VFS-736] Add org.apache.commons.vfs2.VFS.reset().
    
    Also rename private method.
---
 .../src/main/java/org/apache/commons/vfs2/VFS.java | 66 +++++++++++++---------
 .../test/java/org/apache/commons/vfs2/VFSTest.java | 18 ++++++
 src/changes/changes.xml                            |  3 +
 3 files changed, 61 insertions(+), 26 deletions(-)

diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/VFS.java 
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/VFS.java
index 3312a3b..6cb5698 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/VFS.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/VFS.java
@@ -24,30 +24,11 @@ import java.lang.reflect.Method;
  */
 public final class VFS {
 
-    /** The URI style */
-    private static Boolean uriStyle;
-
     /** The FileSystemManager */
     private static FileSystemManager instance;
 
-    private VFS() {
-    }
-
-    /**
-     * Returns the default {@link FileSystemManager} instance.
-     * <p>
-     * Warning, if you close this instance you may affect all current and 
future users of this manager singleton.
-     * </p>
-     *
-     * @return The FileSystemManager.
-     * @throws FileSystemException if an error occurs creating the manager.
-     */
-    public static synchronized FileSystemManager getManager() throws 
FileSystemException {
-        if (instance == null) {
-            instance = 
createManager("org.apache.commons.vfs2.impl.StandardFileSystemManager");
-        }
-        return instance;
-    }
+    /** The URI style */
+    private static Boolean uriStyle;
 
     /**
      * Creates a file system manager instance.
@@ -56,7 +37,7 @@ public final class VFS {
      * @return The FileSystemManager.
      * @throws FileSystemException if an error occurs creating the manager.
      */
-    private static FileSystemManager createManager(final String 
managerClassName) throws FileSystemException {
+    private static FileSystemManager createFileSystemManager(final String 
managerClassName) throws FileSystemException {
         try {
             // Create instance
             final Class<?> mgrClass = Class.forName(managerClassName);
@@ -78,6 +59,22 @@ public final class VFS {
         }
     }
 
+    /**
+     * Returns the default {@link FileSystemManager} instance.
+     * <p>
+     * Warning, if you close this instance you may affect all current and 
future users of this manager singleton.
+     * </p>
+     *
+     * @return The FileSystemManager.
+     * @throws FileSystemException if an error occurs creating the manager.
+     */
+    public static synchronized FileSystemManager getManager() throws 
FileSystemException {
+        if (instance == null) {
+            instance = reset();
+        }
+        return instance;
+    }
+
     public static boolean isUriStyle() {
         if (uriStyle == null) {
             uriStyle = Boolean.FALSE;
@@ -85,11 +82,18 @@ public final class VFS {
         return uriStyle.booleanValue();
     }
 
-    public static void setUriStyle(final boolean uriStyle) {
-        if (VFS.uriStyle != null && VFS.uriStyle.booleanValue() != uriStyle) {
-            throw new IllegalStateException("VFS.uriStyle was already set 
differently.");
+    /**
+     * Resets the FileSystemManager to the default.
+     *
+     * @return the new FileSystemManager.
+     * @throws FileSystemException if an error occurs creating the manager.
+     * @since 2.5.0
+     */
+    public static FileSystemManager reset() throws FileSystemException {
+        if (instance != null) {
+            instance.close();
         }
-        VFS.uriStyle = Boolean.valueOf(uriStyle);
+        return instance = 
createFileSystemManager("org.apache.commons.vfs2.impl.StandardFileSystemManager");
     }
 
     /**
@@ -101,4 +105,14 @@ public final class VFS {
     public static synchronized void setManager(final FileSystemManager 
manager) {
         VFS.instance = manager;
     }
+
+    public static void setUriStyle(final boolean uriStyle) {
+        if (VFS.uriStyle != null && VFS.uriStyle.booleanValue() != uriStyle) {
+            throw new IllegalStateException("VFS.uriStyle was already set 
differently.");
+        }
+        VFS.uriStyle = Boolean.valueOf(uriStyle);
+    }
+
+    private VFS() {
+    }
 }
diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/VFSTest.java 
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/VFSTest.java
index 6b73ee7..b299d12 100644
--- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/VFSTest.java
+++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/VFSTest.java
@@ -17,12 +17,30 @@
 
 package org.apache.commons.vfs2;
 
+import java.nio.file.Paths;
+
 import org.apache.commons.vfs2.impl.StandardFileSystemManager;
 import org.junit.Assert;
 import org.junit.Test;
 
 public class VFSTest {
 
+    /**
+     * Tests {@link FileSystemManager#close()}.
+     *
+     * @throws FileSystemException
+     * @since 2.5.0
+     */
+    @Test
+    public void test_close() throws FileSystemException {
+        try (FileSystemManager fileSystemManager = new 
StandardFileSystemManager()) {
+            VFS.setManager(fileSystemManager);
+            VFS.setManager(null);
+        }
+        Assert.assertNotNull(VFS.getManager());
+        
Assert.assertFalse(VFS.getManager().resolveFile(Paths.get("DoesNotExist.not").toUri()).exists());
+    }
+
     @Test
     public void test_setManager() throws FileSystemException {
         final StandardFileSystemManager fileSystemManager = new 
StandardFileSystemManager();
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index e35bfa0..0bb5da7 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -83,6 +83,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action issue="VFS-735" dev="ggregory" type="add" due-to="Gary Gregory">
         Add org.apache.commons.vfs2.FileSystemManager.close() via 
AutoCloseable.
       </action>
+      <action issue="VFS-736" dev="ggregory" type="add" due-to="Gary Gregory">
+        Add org.apache.commons.vfs2.VFS.reset().
+      </action>
     </release>
     <release version="2.4.1" date="2019-08-10" description="Bug fix release.">
       <action issue="VFS-725" dev="ggregory" type="fix" due-to="Gary Gregory">

Reply via email to