JAL-629 Change --open to --append and --opennew to --open. Make --open(new) part...
[jalview.git] / test / jalview / bin / CommandsTest.java
index c5f8bff..2d3724b 100644 (file)
@@ -1,6 +1,11 @@
 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;
@@ -12,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)
+@Test
 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
   {
@@ -45,27 +57,138 @@ public class CommandsTest
       Desktop.instance.closeAll_actionPerformed(null);
   }
 
+  /* --setprops is currently disabled so this test won't work
+  @Test(groups = "Functional")
+  public void setpropsTest()
+  {
+    final 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);
     Commands cmds = Jalview.getInstance().getCommands();
     Assert.assertNotNull(cmds);
-    Assert.assertTrue(cmds.commandArgsProvided(),
+    Assert.assertEquals(cmds.commandArgsProvided(), cmdArgs,
             "Commands were not provided in the args");
-    Assert.assertTrue(cmds.argsWereParsed(),
+    Assert.assertEquals(cmds.argsWereParsed(), cmdArgs,
             "Overall command parse and operation is false");
 
-    if (sequence != null)
+    Assert.assertEquals(Desktop.getAlignFrames().length, numFrames,
+            "Wrong number of AlignFrames");
+
+    if (sequences != null)
     {
-      Assert.assertTrue(lookForSequenceName(sequence),
-              "Sequence '" + sequence
-                      + "' was not found in opened alignment files: "
-                      + cmdLine);
+      Set<String> openedSequenceNames = new HashSet<>();
+      AlignFrame[] afs = Desktop.getAlignFrames();
+      for (AlignFrame af : afs)
+      {
+        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));
+      }
     }
 
-    System.out.println("##### GOT TO END");
+    Assert.assertFalse(
+            lookForSequenceName("THIS_SEQUENCE_ID_DOESN'T_EXIST"));
+  }
+
+  @Test(groups = "Functional", dataProvider = "argfileOutputFiles")
+  public void argFilesGlobAndSubstitutionsTest(String cmdLine,
+          String[] filenames) throws IOException
+  {
+    cleanupFiles(filenames);
+    String[] args = cmdLine.split("\\s+");
+    Jalview.main(args);
+    Commands cmds = Jalview.getInstance().getCommands();
+    Assert.assertNotNull(cmds);
+    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)
+        Assert.assertTrue(
+                Files.size(file.toPath()) > Files.size(lastFile.toPath()));
+    }
+    cleanupFiles(filenames);
+    tearDown();
+  }
+
+  @DataProvider(name = "argfileOutputFiles")
+  public Object[][] argfileOutputFiles()
+  {
+    return new Object[][] {
+        { "--argfile=" + testfiles + "/**/*.txt", new String[]
+        { testfiles + "/dir1/test1.png", testfiles + "/dir2/test1.png",
+            testfiles + "/dir3/subdir/test0.png" } },
+        { "--argfile=" + testfiles + "/**/argfile.txt", new String[]
+        { testfiles + "/dir1/test1.png", testfiles + "/dir2/test1.png" } },
+        { "--argfile=" + testfiles + "/dir*/argfile.txt", new String[]
+        { testfiles + "/dir1/test1.png", testfiles + "/dir2/test1.png" } },
+        { "--initsubstitutions --append examples/uniref50.fa --image "
+                + testfiles + "/{basename}.png",
+            new String[]
+            { testfiles + "/uniref50.png" } },
+        { "--append examples/uniref50.fa --image " + testfiles
+                + "/{basename}.png",
+            new String[]
+            { testfiles + "/{basename}.png" } } };
+  }
+
+  @DataProvider(name = "cmdLines")
+  public Object[][] cmdLines()
+  {
+    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[][] {
+        /*
+        */
+        { "--append=examples/uniref50.fa", true, 1, someUniref50Seqs },
+        { "--append examples/uniref50.fa", true, 1, someUniref50Seqs },
+        { "--append=examples/uniref50*.fa", true, 1, someUniref50Seqs },
+        // NOTE we cannot use shell expansion in tests, so list all files!
+        { "--append examples/uniref50.fa examples/uniref50_mz.fa", true, 1,
+            someUniref50Seqs },
+        { "--append=[new]examples/uniref50*.fa", true, 2,
+            someUniref50Seqs },
+        { "--open=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, 5,
+            ArrayUtils.concatArrays(t1, t2, t3) },
+        { "--argfile=" + testfiles + "/argfile.autocounter", true, 3,
+            ArrayUtils.concatArrays(t1, t2) } };
+
   }
 
   public static boolean lookForSequenceName(String sequenceName)
@@ -84,11 +207,16 @@ public class CommandsTest
     return false;
   }
 
-  @DataProvider(name = "cmdLines")
-  public Object[][] cmdLines()
+  public static void cleanupFiles(String[] filenames)
   {
-    return new Object[][] { { "--open=examples/uniref50.fa", "FER1_SPIOL" },
-        { "--argfile=test/jalview/bin/argparser/argfile0.txt", null } };
+    for (String filename : filenames)
+    {
+      File file = new File(filename);
+      if (file.exists())
+      {
+        file.delete();
+      }
+    }
   }
 
 }