You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jmeter.apache.org by fs...@apache.org on 2019/11/27 17:27:32 UTC

[jmeter] 02/02: Use a sane initial dir for the file chooser

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

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

commit bfabdb8fc84b992ab73c516f591ac5d08da1b0a7
Author: Felix Schumacher <fe...@internetallee.de>
AuthorDate: Sun Nov 24 11:33:32 2019 +0100

    Use a sane initial dir for the file chooser
    
    While having a look at 63945, I found a few places, where we look for the existence
    of the initial directory on which the file chooser should start.
    
    I extended and simplified those occurrences and think I found a bug in the logic that
    chooses the initial location in FileDialoger#promptToOpenFile from line 177.
    
    First we look, if existingFileName is pointing to something valid and use that as the
    initial location or the global initial location, if none location has been used before.
    
    After that a bit further down, we test if a location has been used before and may
    initialize such a location.
    
    Than -- and this is the potential bug -- we always use that location for the initial
    location of the file chooser.
    
    Another dodgy logic is that we test for the default initial location in two different ways.
    First -- when no existingFileName is given -- we test for an empty default location and refuse
    to use it, if it is empty. The second time, we accept the default location, even if it is
    empty AND use it as the default location for the next runs.
    
    Bugzilla Id: 63945
---
 .../org/apache/jmeter/gui/util/FileDialoger.java   | 39 ++++++++++++----------
 xdocs/changes.xml                                  |  2 ++
 2 files changed, 24 insertions(+), 17 deletions(-)

diff --git a/src/core/src/main/java/org/apache/jmeter/gui/util/FileDialoger.java b/src/core/src/main/java/org/apache/jmeter/gui/util/FileDialoger.java
index 23c795e..7fb50bd 100644
--- a/src/core/src/main/java/org/apache/jmeter/gui/util/FileDialoger.java
+++ b/src/core/src/main/java/org/apache/jmeter/gui/util/FileDialoger.java
@@ -20,6 +20,7 @@ package org.apache.jmeter.gui.util;
 
 import java.awt.Component;
 import java.io.File;
+import java.util.Arrays;
 
 import javax.swing.JFileChooser;
 import javax.swing.filechooser.FileFilter;
@@ -27,6 +28,8 @@ import javax.swing.filechooser.FileFilter;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.jmeter.gui.GuiPackage;
 import org.apache.jmeter.gui.JMeterFileFilter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Class implementing a file open dialogue
@@ -39,6 +42,8 @@ public final class FileDialoger {
 
     private static JFileChooser jfc = new JFileChooser();
 
+    private static final Logger LOG = LoggerFactory.getLogger(FileDialoger.class);
+
     /**
      * Prevent instantiation of utility class.
      */
@@ -180,19 +185,7 @@ public final class FileDialoger {
        } else {
            jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        }
-       if(!StringUtils.isEmpty(existingFileName)) {
-           File existingFileStart = new File(existingFileName);
-           if(existingFileStart.exists() && existingFileStart.canRead()) {
-               jfc.setCurrentDirectory(new File(existingFileName));
-           }
-       }
-       else if (lastJFCDirectory == null) {
-           String start = System.getProperty("user.dir", ""); //$NON-NLS-1$//$NON-NLS-2$
-
-           if (start.length() > 0) {
-               jfc.setCurrentDirectory(new File(start));
-           }
-       }
+       setCurrentDirOnJFC(existingFileName, lastJFCDirectory, System.getProperty("user.dir"));
        clearFileFilters();
        if(exts != null && exts.length > 0) {
            JMeterFileFilter currentFilter = new JMeterFileFilter(exts);
@@ -200,10 +193,6 @@ public final class FileDialoger {
            jfc.setAcceptAllFileFilterUsed(true);
            jfc.setFileFilter(currentFilter);
        }
-       if(lastJFCDirectory==null) {
-           lastJFCDirectory = System.getProperty("user.dir", ""); //$NON-NLS-1$//$NON-NLS-2$
-       }
-       jfc.setCurrentDirectory(new File(lastJFCDirectory));
        int retVal = jfc.showOpenDialog(parentComponent);
        lastJFCDirectory = jfc.getCurrentDirectory().getAbsolutePath();
 
@@ -213,6 +202,22 @@ public final class FileDialoger {
        return null;
    }
 
+    private static void setCurrentDirOnJFC(String... dirNames) {
+        for (String dirName : dirNames) {
+            if (StringUtils.isBlank(dirName)) {
+                continue;
+            }
+            File possibleDir = new File(dirName);
+            if (possibleDir.exists() && possibleDir.canRead()) {
+                jfc.setCurrentDirectory(possibleDir);
+                return;
+            }
+        }
+        LOG.info("No valid initial directory found for: {}",
+                    Arrays.asList(dirNames));
+        jfc.setCurrentDirectory(null);
+    }
+
     private static void clearFileFilters() {
         FileFilter[] filters = jfc.getChoosableFileFilters();
         for (FileFilter filter : filters) {
diff --git a/xdocs/changes.xml b/xdocs/changes.xml
index 5829da1..2c59925 100644
--- a/xdocs/changes.xml
+++ b/xdocs/changes.xml
@@ -162,6 +162,7 @@ to view the last release notes of version 5.2.1.
 
 <h3>General</h3>
 <ul>
+  <li><bug>63945</bug>NPE when opening a file after file system change</li>
 </ul>
 
  <!--  =================== Thanks =================== -->
@@ -173,6 +174,7 @@ to view the last release notes of version 5.2.1.
 </ul>
 <p>We also thank bug reporters who helped us improve JMeter.</p>
 <ul>
+  <li>Michael McDermott (mcdermott.michaelj at gmail.com)</li>
 </ul>
 <p>
 Apologies if we have omitted anyone else.