JAL-629 sort files from java glob for predictability. reduce maxdepth of walkfiletree
[jalview.git] / test / jalview / bin / CommandsTest.java
index e4fea7b..f7805fe 100644 (file)
@@ -2,102 +2,191 @@ package jalview.bin;
 
 import java.io.File;
 import java.io.IOException;
-import java.lang.management.ManagementFactory;
-
-import org.testng.annotations.BeforeTest;
+import java.nio.file.Files;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.testng.Assert;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
-import io.github.classgraph.ClassGraph;
+import jalview.gui.AlignFrame;
+import jalview.gui.Desktop;
+import jalview.gui.JvOptionPane;
+import jalview.util.ArrayUtils;
 
+@Test(singleThreaded = true)
 public class CommandsTest
 {
+  private static final String testfiles = "test/jalview/bin/argparser/testfiles";
+
+  private static final String png1 = testfiles + "/dir1/test1.png";
 
-  private static final int SETUP_TIMEOUT = 30000;
+  private static final String png2 = testfiles + "/dir2/test1.png";
 
-  private static class Worker extends Thread
+  @BeforeClass(alwaysRun = true)
+  public static void setUpBeforeClass() throws Exception
   {
-    private final Process process;
+    Cache.loadProperties("test/jalview/gui/quitProps.jvprops");
+    Date oneHourFromNow = new Date(
+            System.currentTimeMillis() + 3600 * 1000);
+    Cache.setDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED", oneHourFromNow);
+  }
 
-    private Integer exit;
+  @AfterClass(alwaysRun = true)
+  public static void resetProps()
+  {
+    Cache.loadProperties("test/jalview/testProps.jvprops");
+  }
 
-    private Worker(Process process)
-    {
-      this.process = process;
-    }
+  @BeforeClass(alwaysRun = true)
+  public void setUpJvOptionPane()
+  {
+    JvOptionPane.setInteractiveMode(false);
+    JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
+  }
 
-    @Override
-    public void run()
+  @AfterMethod(alwaysRun = true)
+  public void tearDown()
+  {
+    if (Desktop.instance != null)
+      Desktop.instance.closeAll_actionPerformed(null);
+  }
+
+  /* --setprop currently disabled
+  @Test(groups = "Functional")
+  public void setpropsTest()
+  {
+    String MOSTLY_HARMLESS = "MOSTLY_HARMLESS";
+    String cmdLine = "--setprop=" + MOSTLY_HARMLESS + "=Earth";
+    String[] args = cmdLine.split("\\s+");
+    Jalview.main(args);
+    Assert.assertEquals(Cache.getDefault(MOSTLY_HARMLESS, "Magrathea"),
+            "Earth");
+  }
+  */
+
+  @Test(groups = "Functional", dataProvider = "cmdLines")
+  public void commandsOpenTest(String cmdLine, boolean cmdArgs,
+          int numFrames, String[] sequences)
+  {
+    String[] args = cmdLine.split("\\s+");
+    Jalview.main(args);
+    Commands cmds = Jalview.getInstance().getCommands();
+    Assert.assertNotNull(cmds);
+    Assert.assertEquals(cmds.commandArgsProvided(), cmdArgs,
+            "Commands were not provided in the args");
+    Assert.assertEquals(cmds.argsWereParsed(), cmdArgs,
+            "Overall command parse and operation is false");
+
+    Assert.assertEquals(Desktop.getAlignFrames().length, numFrames);
+
+    if (sequences != null)
     {
-      try
+      Set<String> openedSequenceNames = new HashSet<>();
+      AlignFrame[] afs = Desktop.getAlignFrames();
+      for (AlignFrame af : afs)
       {
-        exit = process.waitFor();
-      } catch (InterruptedException ignore)
+        openedSequenceNames
+                .addAll(af.getViewport().getAlignment().getSequenceNames());
+      }
+      for (String sequence : sequences)
       {
-        return;
+        Assert.assertTrue(openedSequenceNames.contains(sequence),
+                "Sequence '" + sequence
+                        + "' was not found in opened alignment files: "
+                        + cmdLine + ".\nOpened sequence names are:\n"
+                        + String.join("\n", openedSequenceNames));
       }
     }
-  }
 
-  private static ClassGraph scanner = null;
-
-  private static String classpath = null;
-
-  private static String modules = null;
+    Assert.assertFalse(
+            lookForSequenceName("THIS_SEQUENCE_ID_DOESN'T_EXIST"));
+  }
 
-  private static String java_exe = null;
+  @Test(groups = "Functional")
+  public void argFilesGlobAndSubstitutionsTest() throws IOException
+  {
+    cleanupArgfilesImages();
+    String cmdLine = "--argfile=" + testfiles + "/dir*/argfile.txt";
+    String[] args = cmdLine.split("\\s+");
+    Jalview.main(args);
+    Commands cmds = Jalview.getInstance().getCommands();
+    File file1 = new File(png1);
+    File file2 = new File(png2);
+    Assert.assertTrue(file1.exists(),
+            "Did not make file " + png1 + " from argfile");
+    Assert.assertTrue(file2.exists(),
+            "Did not make file " + png2 + " from argfile");
+    long size1 = Files.size(file1.toPath());
+    long size2 = Files.size(file2.toPath());
+    Assert.assertTrue(file1.isFile() && size1 > 0);
+    Assert.assertTrue(file2.isFile() && size2 > 0);
+    Assert.assertTrue(size2 > size1); // png2 has three sequences, png1 has 2
+    cleanupArgfilesImages();
+  }
 
-  public synchronized static String getClassPath()
+  @DataProvider(name = "cmdLines")
+  public Object[][] cmdLines()
   {
-    java_exe = System.getProperty("java.home") + File.separator + "bin"
-            + File.separator + "java";
-    classpath = ManagementFactory.getRuntimeMXBean().getClassPath();
-    return classpath;
+    String[] someUniref50Seqs = new String[] { "FER_CAPAA", "FER_CAPAN",
+        "FER1_MAIZE", "FER1_SPIOL", "O80429_MAIZE" };
+    String[] t1 = new String[] { "TEST1" };
+    String[] t2 = new String[] { "TEST2" };
+    String[] t3 = new String[] { "TEST3" };
+    return new Object[][] {
+        { "--open=examples/uniref50.fa", true, 1, someUniref50Seqs },
+        { "--open examples/uniref50.fa", true, 1, someUniref50Seqs },
+        { "--open=examples/uniref50*.fa", true, 1, someUniref50Seqs },
+        // NOTE we cannot use shell expansion in tests, so list all files!
+        { "--open examples/uniref50.fa examples/uniref50_mz.fa", true, 1,
+            someUniref50Seqs },
+        { "--open=[new]examples/uniref50*.fa", true, 2, someUniref50Seqs },
+        { "--opennew=examples/uniref50*.fa", true, 2, someUniref50Seqs },
+        { "examples/uniref50.fa", true, 1, someUniref50Seqs },
+        { "examples/uniref50.fa " + testfiles + "/test1.fa", true, 2,
+            ArrayUtils.concatArrays(someUniref50Seqs, t1) },
+        { "examples/uniref50.fa " + testfiles + "/test1.fa", true, 2, t1 },
+        { "--argfile=" + testfiles + "/argfile0.txt", true, 1,
+            ArrayUtils.concatArrays(t1, t3) },
+        { "--argfile=" + testfiles + "/argfile*.txt", true, 4,
+            ArrayUtils.concatArrays(t1, t2, t3) },
+        { "--argfile=" + testfiles + "/argfile.autocounter", true, 3,
+            ArrayUtils.concatArrays(t1, t2) } };
   }
 
-  private Worker getJalviewDesktopRunner(boolean withAwt, String cmd,
-          int timeout)
+  public static boolean lookForSequenceName(String sequenceName)
   {
-    // Note: JAL-3065 - don't include quotes for lib/* because the arguments are
-    // not expanded by the shell
-    String classpath = getClassPath();
-    String _cmd = java_exe + " "
-            + (withAwt ? "-Djava.awt.headless=true" : "") + " -classpath "
-            + classpath + " jalview.bin.Jalview ";
-    Process ls2_proc = null;
-    Worker worker = null;
-    try
-    {
-      ls2_proc = Runtime.getRuntime().exec(_cmd + cmd);
-    } catch (Throwable e1)
-    {
-      e1.printStackTrace();
-    }
-    if (ls2_proc != null)
+    AlignFrame[] afs = Desktop.getAlignFrames();
+    for (AlignFrame af : afs)
     {
-      worker = new Worker(ls2_proc);
-      worker.start();
-      try
+      for (String name : af.getViewport().getAlignment().getSequenceNames())
       {
-        worker.join(timeout);
-      } catch (InterruptedException e)
-      {
-        System.err.println("Thread interrupted");
+        if (sequenceName.equals(name))
+        {
+          return true;
+        }
       }
     }
-    return worker;
+    return false;
   }
 
-  @BeforeTest(alwaysRun = true)
-  public void initialize()
-  {
-    new CommandsTest();
-  }
-
-  @Test(groups = "Functional")
-  public void setUpForHeadlessCommandsTest() throws IOException
+  public static void cleanupArgfilesImages()
   {
-    String cmds = "--open=./examples/uniref50.fa";
-    Worker worker = getJalviewDesktopRunner(true, cmds, SETUP_TIMEOUT);
+    File png1File = new File(png1);
+    File png2File = new File(png2);
+    if (png1File.exists())
+    {
+      png1File.delete();
+    }
+    if (png2File.exists())
+    {
+      png2File.delete();
+    }
   }
 
 }