JAL-4290 new CommandsTest.headlessOrGuiImageOutputTest - uses CommandLineOperations...
[jalview.git] / test / jalview / bin / CommandsTest.java
index de6185c..e42cc54 100644 (file)
@@ -1,3 +1,23 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.bin;
 
 import java.io.File;
@@ -49,7 +69,7 @@ public class CommandsTest
   }
 
   @AfterMethod(alwaysRun = true)
-  public void tearDown() 
+  public void tearDown()
   {
     try
     {
@@ -96,7 +116,8 @@ public class CommandsTest
   }
   */
 
-  @Test(groups = {"Functional","testTask3"}, dataProvider = "cmdLines")
+  @Test(groups = { "Functional", "testTask3" }, dataProvider = "cmdLines", singleThreaded = true)
+
   public void commandsOpenTest(String cmdLine, boolean cmdArgs,
           int numFrames, String[] sequences)
   {
@@ -148,7 +169,7 @@ public class CommandsTest
   @Test(
     groups =
     { "Functional", "testTask3" },
-    dataProvider = "structureImageOutputFiles")
+    dataProvider = "structureImageOutputFiles",    singleThreaded = true)
   public void structureImageOutputTest(String cmdLine, String[] filenames)
           throws IOException
   {
@@ -159,21 +180,44 @@ public class CommandsTest
       callJalviewMain(args);
       Commands cmds = Jalview.getInstance().getCommands();
       Assert.assertNotNull(cmds);
-      File lastFile = null;
-      for (String filename : filenames)
+      verifyIncreasingSize(cmdLine, filenames);
+    } catch (Exception x)
+    {
+      Assert.fail("Unexpected exception during structureImageOutputTest",
+              x);
+    } finally
+    {
+      cleanupFiles(filenames);
+      tearDown();
+    }
+  }
+
+  /**
+   * given two command lines, compare the output files produced - they should exist and be equal in size
+   */
+  @Test(
+    groups =
+    { "Functional", "testTask3" },
+    dataProvider = "compareHeadlessAndGUIOps",
+    singleThreaded = true)
+  public void headlessOrGuiImageOutputTest(String[] cmdLines,
+          String[] filenames) throws IOException
+  {
+    cleanupFiles(filenames);
+    try
+    {
+      for (String cmdLine : cmdLines)
       {
-        File file = new File(filename);
-        Assert.assertTrue(file.exists(), "File '" + filename
-                + "' was not created by '" + cmdLine + "'");
-        Assert.assertTrue(file.isFile(), "File '" + filename
-                + "' is not a file from '" + cmdLine + "'");
-        Assert.assertTrue(Files.size(file.toPath()) > 0, "File '" + filename
-                + "' has no content from '" + cmdLine + "'");
-        // make sure the successive output files get bigger!
-        if (lastFile != null)
-          Assert.assertTrue(Files.size(file.toPath()) > Files
-                  .size(lastFile.toPath()));
+        CommandLineOperations.Worker runner = CommandLineOperations.getJalviewDesktopRunner(false, cmdLine, 1000);
+        long timeOut = 10000;
+        while (runner.isAlive() && timeOut>0)
+        {
+          Thread.sleep(25);
+          timeOut-=25;
+        }
       }
+      verifyOrderedFileSet(cmdLines[0] + " vs " + cmdLines[1], filenames,
+              false);
     } catch (Exception x)
     {
       Assert.fail("Unexpected exception during structureImageOutputTest",
@@ -184,8 +228,81 @@ public class CommandsTest
       tearDown();
     }
   }
+  @DataProvider(name = "compareHeadlessAndGUIOps")
+  public Object[][] compareHeadlessAndGUIOps()
+  {
+    return new Object[][] {
+        new Object[]
+        { new String[] { "--open examples/uniref50.fa "
+                + "--structure [seqid=FER1_SPIOL,tempfac=plddt,showssannotations,structureviewer=jmol]"
+                + "examples/AlphaFold/AF-P00221-F1-model_v4.pdb "
+                + "--paematrix examples/AlphaFold/AF-P00221-F1-predicted_aligned_error_v4.json --image="
+                + testfiles + "test-al-pae-ss-gui.png --overwrite --gui --quit",
+            "--open examples/uniref50.fa "
+                    + "--structure [seqid=FER1_SPIOL,tempfac=plddt,showssannotations,structureviewer=jmol]"
+                    + "examples/AlphaFold/AF-P00221-F1-model_v4.pdb "
+                    + "--paematrix examples/AlphaFold/AF-P00221-F1-predicted_aligned_error_v4.json --image="
+                    + testfiles
+                    + "test-al-pae-ss-nogui.png --overwrite --nogui"
+
+        }, new String[] {
+            testfiles + "test-al-pae-ss-gui.png",
+            testfiles
+            + "test-al-pae-ss-nogui.png",
+        } } };
+  }
+  
+  private static void verifyIncreasingSize(String cmdLine, String[] filenames) throws Exception
+  {
+    verifyOrderedFileSet(cmdLine, filenames, true);
+  }
+  
+  private static void verifyOrderedFileSet(String cmdLine, String[] filenames, boolean increasingSize) throws Exception
+  {
+    File lastFile = null;
+    for (String filename : filenames)
+    {
+      File file = new File(filename);
+      Assert.assertTrue(file.exists(), "File '" + filename
+              + "' was not created by '" + cmdLine + "'");
+      Assert.assertTrue(file.isFile(), "File '" + filename
+              + "' is not a file from '" + cmdLine + "'");
+      Assert.assertTrue(Files.size(file.toPath()) > 0, "File '" + filename
+              + "' has no content from '" + cmdLine + "'");
+      // make sure the successive output files get bigger!
+      if (lastFile != null)
+      {
+        waitForLastWrite(file,25);
+        
+        if (increasingSize)
+        { Assert.assertTrue(Files.size(file.toPath()) > Files
+                .size(lastFile.toPath()),"Expected " + file.toPath()+ " to be larger than "+lastFile.toPath());
+        } else {
+          Assert.assertEquals(Files.size(file.toPath()), Files
+                .size(lastFile.toPath()), "New file "+file.toPath()+" (actual size) not same as last file's size "+lastFile.toString());
+        }
+      }
+      // remember it for next file
+      lastFile = file;
+    }
+
+  }
+  private static long waitForLastWrite(File file, int i) throws IOException
+  {
+    long lastSize,stableSize =Files.size(file.toPath());
+    // wait around until we are sure the file has been completely written.
+    do {
+      lastSize = stableSize;
+      try {
+        Thread.sleep(i);
+      } catch (Exception x) {}
+      stableSize=Files.size(file.toPath());
+    } while (stableSize!=lastSize);
+    return stableSize;
+  }
+
+  @Test(groups = "Functional", dataProvider = "argfileOutputFiles", singleThreaded = true)
 
-  @Test(groups = "Functional", dataProvider = "argfileOutputFiles")
   public void argFilesGlobAndSubstitutionsTest(String cmdLine,
           String[] filenames) throws IOException
   {
@@ -207,9 +324,13 @@ public class CommandsTest
         Assert.assertTrue(Files.size(file.toPath()) > 0, "File '" + filename
                 + "' has no content from '" + cmdLine + "'");
         // make sure the successive output files get bigger!
-        if (lastFile != null)
+        if (lastFile != null) {
           Assert.assertTrue(Files.size(file.toPath()) > Files
                   .size(lastFile.toPath()));
+          System.out.println("this file: "+file+" +"+Files.size(file.toPath()) + " greater than " +Files.size(lastFile.toPath()));
+        }
+        // remember it for next file
+        lastFile = file;
       }
     } catch (Exception x)
     {
@@ -258,6 +379,19 @@ public class CommandsTest
             { testfiles + "/structureimage1.png",
                 testfiles + "/structureimage2.png",
                 testfiles + "/structureimage3.png" } },
+        { "--gui --nonews --nosplash --open examples/1gaq.txt --append ./examples/3W5V.pdb "+"--structure examples/1gaq.txt --seqid \"1GAQ|A\" "+"--structureimage "+testfiles+"/1gaq.png --structure examples/3W5V.pdb "+"--seqid \"3W5V|A\" --structureimage "+testfiles+"/3w5v.png --overwrite",
+                       
+                new String[] {
+                               testfiles+"/1gaq.png",testfiles+"/3w5v.png"
+                }
+        },
+        { "--headless --noquit --open ./examples/1gaq.txt --append ./examples/3W5V.pdb "+"--structure examples/1gaq.txt --seqid \"1GAQ|A\" "+"--structureimage "+testfiles+"/1gaq.png --structure examples/3W5V.pdb "+"--seqid \"3W5V|A\" --structureimage "+testfiles+"/3w5v.png --overwrite",
+               
+            new String[] {
+                       testfiles+"/1gaq.png",testfiles+"/3w5v.png"
+            }
+    }
+
         /*
                 */
         //