JAL-4414 fix logic for local file paths that cannot be found and test to verify excep...
authorJim Procter <jprocter@dundee.ac.uk>
Wed, 5 Jun 2024 14:17:50 +0000 (15:17 +0100)
committerJim Procter <jprocter@dundee.ac.uk>
Wed, 5 Jun 2024 14:17:50 +0000 (15:17 +0100)
src/jalview/gui/Desktop.java
src/jalview/io/FileParse.java
src/jalview/io/IdentifyFile.java
test/jalview/io/IdentifyFileTest.java

index 9072b75..6c3cc5a 100644 (file)
@@ -1398,25 +1398,38 @@ public class Desktop extends jalview.jbgui.GDesktop
       else
       {
         FileFormatI format = null;
+        boolean file_not_found=false;
+        String fmtmessage="";
         try
         {
           format = new IdentifyFile().identify(url, DataSourceType.URL);
         } catch (FileNotFoundException e)
         {
           jalview.bin.Console.error("URL '" + url + "' not found", e);
+          file_not_found=true;
         } catch (FileFormatException e)
         {
           jalview.bin.Console.error(
                   "File at URL '" + url + "' format not recognised", e);
+          fmtmessage = e.getLocalizedMessage();
         }
 
         if (format == null)
         {
-          String msg = MessageManager.formatMessage("label.couldnt_locate",
-                  url);
-          JvOptionPane.showInternalMessageDialog(Desktop.desktop, msg,
-                  MessageManager.getString("label.url_not_found"),
-                  JvOptionPane.WARNING_MESSAGE);
+          if (file_not_found) {
+            String msg = MessageManager.formatMessage("label.couldnt_locate",
+                    url);
+            JvOptionPane.showInternalMessageDialog(Desktop.desktop, msg,
+                    MessageManager.getString("label.url_not_found"),
+                    JvOptionPane.WARNING_MESSAGE);
+          } else {
+            String msg = MessageManager.formatMessage("label.error_retrieving_data",
+                    url);
+            JvOptionPane.showInternalMessageDialog(Desktop.desktop, msg +"<br/>"+fmtmessage,
+                    MessageManager.getString("label.error_retrieving_data"),
+                    JvOptionPane.WARNING_MESSAGE);
+          
+          }
           return;
         }
 
index dd85907..a68040b 100755 (executable)
@@ -124,6 +124,11 @@ public class FileParse
 
   protected boolean error = true;
 
+  /**
+   * set if location coordinates resolved to no data..
+   */
+  protected boolean filenotfound = false;
+
   protected String warningMessage = null;
 
   /**
@@ -178,25 +183,28 @@ public class FileParse
   private boolean checkFileSource(String fileStr) throws IOException
   {
     error = false;
+    filenotfound=false;
     this.inFile = new File(fileStr);
-    // check to see if it's a Jar file in disguise.
+
     if (!inFile.exists())
     {
       errormessage = "FILE NOT FOUND";
-      error = true;
-    }
-    if (!inFile.canRead())
-    {
-      errormessage = "FILE CANNOT BE OPENED FOR READING";
-      error = true;
+      filenotfound = true;
+      return(error = true);
     }
     if (inFile.isDirectory())
     {
       // this is really a 'complex' filetype - but we don't handle directory
       // reads yet.
       errormessage = "FILE IS A DIRECTORY";
-      error = true;
+      return(error = true);
+    }
+    if (!inFile.canRead())
+    {
+      errormessage = "FILE CANNOT BE OPENED FOR READING";
+      return(error = true);
     }
+    // final check - whether we have a gzipped source
     if (!error)
     {
       try
@@ -325,6 +333,7 @@ public class FileParse
       int rc = conn.getResponseCode();
       if (rc != HttpURLConnection.HTTP_OK)
       {
+       filenotfound=true;
         throw new FileNotFoundException("Response status from " + urlStr
                 + " was " + conn.getResponseCode());
       }
@@ -446,7 +455,7 @@ public class FileParse
     bytes = Platform.getFileBytes(file);
     dataSourceType = sourceType;
     error = false;
-    boolean filenotfound = false;
+    filenotfound = false;
 
     if (sourceType == DataSourceType.FILE)
     {
@@ -461,11 +470,16 @@ public class FileParse
       }
       else if (checkFileSource(fileStr))
       {
+        boolean fileStrNotFound=filenotfound;
         String suffixLess = extractSuffix(fileStr);
         if (suffixLess != null)
         {
           if (checkFileSource(suffixLess))
           {
+            if (filenotfound)
+            {
+              throw(new FileNotFoundException("Couldn't find '"+suffixLess+"'"+ (fileStrNotFound ? " and also couldn't find "+fileStr : "")));
+            }
             throw new IOException(MessageManager.formatMessage(
                     "exception.problem_opening_file_also_tried",
                     new String[]
@@ -474,6 +488,10 @@ public class FileParse
         }
         else
         {
+          if (filenotfound)
+          {
+            throw(new FileNotFoundException("Couldn't find '"+fileStr+"'"));
+          }
           throw new IOException(MessageManager.formatMessage(
                   "exception.problem_opening_file", new String[]
                   { inFile.getName(), errormessage }));
@@ -627,7 +645,7 @@ public class FileParse
    */
   public boolean isExporting()
   {
-    return !error && dataIn == null;
+    return isValid() && dataIn == null;
   }
 
   /**
@@ -636,7 +654,7 @@ public class FileParse
    */
   public boolean isValid()
   {
-    return !error;
+    return !error && !filenotfound;
   }
 
   /**
@@ -646,6 +664,7 @@ public class FileParse
   {
     errormessage = "EXCEPTION ON CLOSE";
     error = true;
+    filenotfound = false;
     dataIn.close();
     dataIn = null;
     errormessage = "SOURCE IS CLOSED";
index 8251c22..2a17bd2 100755 (executable)
@@ -81,7 +81,7 @@ public class IdentifyFile
    * @param file
    * @param sourceType
    * @return
-   * @throws FileFormatException
+   * @throws FileFormatException, FileNotFoundException
    */
   public FileFormatI identify(String file, DataSourceType sourceType)
           throws FileFormatException, FileNotFoundException
@@ -101,6 +101,7 @@ public class IdentifyFile
       fnf = e;
       emessage = "Could not find '" + file + "'";
       Console.error("Could not find '" + file + "'", e);
+      throw(fnf);
     } catch (IOException e)
     {
       Console.error("Error whilst trying to read " + file, e);
@@ -113,10 +114,6 @@ public class IdentifyFile
     {
       throw new FileFormatException(parser.errormessage);
     }
-    if (fnf != null)
-    {
-      throw (fnf);
-    }
     throw new FileFormatException(emessage);
   }
 
index 1d0785e..04efa11 100644 (file)
@@ -193,5 +193,20 @@ public class IdentifyFileTest
         //
     };
   }
-
+  @Test(groups="Functional")
+  public void testFileNotFound()
+  {
+    try {
+      new IdentifyFile().identify("/path/that/does/not/exist", DataSourceType.FILE);
+    }
+    catch (FileNotFoundException ff)
+    {
+      return;
+    }
+    catch (Exception other) {
+      Assert.fail("Wrong exception raised for file not found",other);
+    }
+    Assert.fail("Expected to raise a FileNotFoundException");
+  }
 }
+