JAL-629 sort files from java glob for predictability. reduce maxdepth of walkfiletree
[jalview.git] / test / jalview / bin / CommandsTest.java
index bd67ded..f7805fe 100644 (file)
@@ -1,8 +1,14 @@
 package jalview.bin;
 
+import java.io.File;
+import java.io.IOException;
+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;
@@ -11,10 +17,17 @@ import org.testng.annotations.Test;
 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 String png2 = testfiles + "/dir2/test1.png";
+
   @BeforeClass(alwaysRun = true)
   public static void setUpBeforeClass() throws Exception
   {
@@ -24,6 +37,12 @@ public class CommandsTest
     Cache.setDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED", oneHourFromNow);
   }
 
+  @AfterClass(alwaysRun = true)
+  public static void resetProps()
+  {
+    Cache.loadProperties("test/jalview/testProps.jvprops");
+  }
+
   @BeforeClass(alwaysRun = true)
   public void setUpJvOptionPane()
   {
@@ -34,44 +53,140 @@ public class CommandsTest
   @AfterMethod(alwaysRun = true)
   public void tearDown()
   {
-    Desktop.instance.closeAll_actionPerformed(null);
+    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, String sequence)
+  public void commandsOpenTest(String cmdLine, boolean cmdArgs,
+          int numFrames, String[] sequences)
   {
     String[] args = cmdLine.split("\\s+");
     Jalview.main(args);
-    ArgParser argparser = new ArgParser(args);
-    boolean commandsSuccess = Commands.processArgs(argparser, false);
-    Assert.assertTrue(commandsSuccess, "Overall commandsSuccess is false");
+    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 (sequence != null)
+    if (sequences != null)
     {
+      Set<String> openedSequenceNames = new HashSet<>();
       AlignFrame[] afs = Desktop.getAlignFrames();
-      boolean found = false;
-      ALIGNFRAME: for (AlignFrame af : afs)
+      for (AlignFrame af : afs)
       {
-        for (String name : af.getViewport().getAlignment()
-                .getSequenceNames())
-        {
-          if (sequence.equals(name))
-          {
-            found = true;
-            break ALIGNFRAME;
-          }
-        }
+        openedSequenceNames
+                .addAll(af.getViewport().getAlignment().getSequenceNames());
+      }
+      for (String sequence : sequences)
+      {
+        Assert.assertTrue(openedSequenceNames.contains(sequence),
+                "Sequence '" + sequence
+                        + "' was not found in opened alignment files: "
+                        + cmdLine + ".\nOpened sequence names are:\n"
+                        + String.join("\n", openedSequenceNames));
       }
-      Assert.assertTrue(found, "Sequence '" + sequence
-              + "' was not found in opened alignment files: " + cmdLine);
     }
+
+    Assert.assertFalse(
+            lookForSequenceName("THIS_SEQUENCE_ID_DOESN'T_EXIST"));
+  }
+
+  @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();
   }
 
   @DataProvider(name = "cmdLines")
   public Object[][] cmdLines()
   {
-    return new Object[][] { { "--open=examples/uniref50.fa", "FER1_SPIOL" },
-        { "--argfile=test/jalview/bin/argparser/argfile0.txt", null } };
+    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) } };
+  }
+
+  public static boolean lookForSequenceName(String sequenceName)
+  {
+    AlignFrame[] afs = Desktop.getAlignFrames();
+    for (AlignFrame af : afs)
+    {
+      for (String name : af.getViewport().getAlignment().getSequenceNames())
+      {
+        if (sequenceName.equals(name))
+        {
+          return true;
+        }
+      }
+    }
+    return false;
+  }
+
+  public static void cleanupArgfilesImages()
+  {
+    File png1File = new File(png1);
+    File png2File = new File(png2);
+    if (png1File.exists())
+    {
+      png1File.delete();
+    }
+    if (png2File.exists())
+    {
+      png2File.delete();
+    }
   }
 
 }