JAL-4090 JAL-1551 source license
[jalview.git] / test / jalview / bin / argparser / ArgParserTest.java
index 17f19b7..442812e 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.argparser;
 
 import java.io.File;
@@ -90,14 +110,14 @@ public class ArgParserTest
     Assert.assertTrue(b.contains(a));
     if (a == Arg.PROPS)
     {
-      Properties bP = Cache.bootstrapProperties(b.get(Arg.PROPS));
+      Properties bP = Cache.bootstrapProperties(b.getValue(Arg.PROPS));
       Assert.assertNotNull(bP);
       Assert.assertTrue(other.equals(bP.get(Cache.BOOTSTRAP_TEST)));
       Assert.assertFalse(bP.contains("NOT" + Cache.BOOTSTRAP_TEST));
     }
     else if (a == Arg.ARGFILE)
     {
-      List<String> filenames = b.getList(a);
+      List<String> filenames = b.getValueList(a);
       boolean found = false;
       for (String s : filenames)
       {
@@ -130,7 +150,7 @@ public class ArgParserTest
     Assert.assertFalse(b.contains(Arg.APPEND));
     if (a == Arg.PROPS)
     {
-      Properties bP = Cache.bootstrapProperties(b.get(Arg.PROPS));
+      Properties bP = Cache.bootstrapProperties(b.getValue(Arg.PROPS));
       Assert.assertTrue("true".equals(bP.get(Cache.BOOTSTRAP_TEST)));
     }
   }
@@ -184,9 +204,12 @@ public class ArgParserTest
   @DataProvider(name = "argSubValsAndLinkedIds")
   public Object[][] argSubValsAndLinkedIds()
   {
-    return new Object[][] { {
-        "--debug --append=[hi]test/jalview/bin/argparser/testfiles/test1.fa",
-        "JALVIEW:0", Arg.APPEND, "hi", "true", true },
+    return new Object[][] {
+        //
+        /*
+         */
+        { "--debug --append=[hi]test/jalview/bin/argparser/testfiles/test1.fa",
+            "JALVIEW:0", Arg.APPEND, "hi", "true", true },
         { "--append[linkedId1]=[new,hello=world,1]test/jalview/bin/argparser/testfiles/test1.fa --headless",
             "linkedId1", Arg.APPEND, "new", "true", true },
         { "--append[linkedId2]=[new,hello=world,1]test/jalview/bin/argparser/testfiles/test1.fa --headless",
@@ -198,7 +221,11 @@ public class ArgParserTest
         { "--append[linkedId5]=[new,hello=worlddomination,1]test/jalview/bin/argparser/testfiles/test1.fa --append[linkedId2]=[new;hello=world;1]test/jalview/bin/argparser/testfiles/test1.fa --headless",
             "linkedId5", Arg.APPEND, "hello", "world", false },
         { "--append[linkedId6]=[new,hello=world,0]test/jalview/bin/argparser/testfiles/test1.fa --append[linkedId7]=[new;hello=world;1]test/jalview/bin/argparser/testfiles/test1.fa --headless",
-            "linkedId7", Arg.APPEND, "GETINDEX", "0", false }, };
+            "linkedId7", Arg.APPEND, "GETINDEX", "0", false },
+        /*
+         */
+        //
+    };
   }
 
   @DataProvider(name = "argAutoIndexAndSubstitutions")
@@ -272,9 +299,15 @@ public class ArgParserTest
   }
 
   @Test(groups = "Functional", dataProvider = "allLinkedIdsData")
-  public void allLinkedIdsTest(String commandLineArgs, Arg a,
-          String[] values)
+  public void allLinkedIdsTest(String pwd, String commandLineArgs, Arg a,
+          String[] values, String[] nonvalues)
   {
+    String userDir = System.getProperty("user.dir");
+    if (pwd != null)
+    {
+      File pwdFile = new File(pwd);
+      System.setProperty("user.dir", pwdFile.getAbsolutePath());
+    }
     String[] args = commandLineArgs.split("\\s+");
     ArgParser argparser = new ArgParser(args);
 
@@ -299,12 +332,13 @@ public class ArgParserTest
         ArgValues avs = avm.getArgValues(a);
         ArgValue av = avs.getArgValue();
         String v = av.getValue();
-        value = new File(value).getAbsolutePath();
+        value = new File(value).getPath();
         Assert.assertEquals(v, value, "Arg value for " + a.argString()
                 + " not applied correctly to linkedId '" + linkedId + "'");
       }
     }
 
+    System.setProperty("user.dir", userDir);
   }
 
   @DataProvider(name = "allLinkedIdsData")
@@ -314,21 +348,108 @@ public class ArgParserTest
         //
         /*
         */
-        { "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --all --image={dirname}/{basename}.png --close",
+        { null,
+            "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --all --image={dirname}/{basename}.png --close",
             Arg.CLOSE, new String[]
-            { null, null,
-                null } },
-        { "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --all --output={dirname}/{basename}.stk --close",
+            { null, null, null },
+            null },
+        { null,
+            "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --all --output={dirname}/{basename}.stk --close",
             Arg.OUTPUT, new String[]
             { "test/jalview/bin/argparser/testfiles/test1.stk",
                 "test/jalview/bin/argparser/testfiles/test2.stk",
-                "test/jalview/bin/argparser/testfiles/test3.stk", } },
-        { "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --all --image={dirname}/{basename}.png --close",
+                "test/jalview/bin/argparser/testfiles/test3.stk", },
+            null },
+        { null,
+            "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --all --image={dirname}/{basename}.png --close",
             Arg.IMAGE, new String[]
             { "test/jalview/bin/argparser/testfiles/test1.png",
                 "test/jalview/bin/argparser/testfiles/test2.png",
-                "test/jalview/bin/argparser/testfiles/test3.png", } },
+                "test/jalview/bin/argparser/testfiles/test3.png", },
+            null },
+        /*
+         * Find a way to change pwd reliably -- need to match "*.fa" against some files!
+         { "test/jalview/bin/argparser/testfiles",
+         
+            "--open=*.fa --image={dirname}/{basename}.png --close",
+            Arg.IMAGE, new String[]
+            { "./test1.png", "./test2.png", "./test3.png", }, null },
+            */
+        //
+    };
+  }
+
+  @Test(groups = "Functional", dataProvider = "bootstrapArgsData")
+  public void bootstrapArgsValuesAndHeadlessModeTest(String commandLineArgs,
+          Arg a, String valS, boolean valB, boolean headlessValue)
+  {
+    String[] args = commandLineArgs.split("\\s+");
+    BootstrapArgs bsa = BootstrapArgs.getBootstrapArgs(args);
+    if (a != null)
+    {
+      if (valS != null)
+      {
+        Assert.assertEquals(bsa.getValue(a), valS,
+                "BootstrapArg " + a.argString()
+                        + " value does not match expected '" + valS + "'");
+      }
+      else
+      {
+        Assert.assertEquals(bsa.getBoolean(a), valB,
+                "Boolean/Unary value of BootstrapArg " + a.argString()
+                        + "' is not the expected '" + valB + "'");
+      }
+    }
+
+    boolean isHeadless = bsa.isHeadless();
+    Assert.assertEquals(isHeadless, headlessValue,
+            "Assumed headless setting '" + isHeadless + "' is wrong.");
+  }
+
+  @DataProvider(name = "bootstrapArgsData")
+  public Object[][] bootstrapArgsData()
+  {
+    return new Object[][] {
+        /*
+         * cmdline args
+         * Arg (null if only testing headless)
+         * String value if there is one (null otherwise)
+         * boolean value if String value is null
+         * expected value of isHeadless()
+         */
+        /*
+        */
+        { "--open thisway.fa --output thatway.fa --jabaws https://forwardsandbackwards.com/",
+            Arg.JABAWS, "https://forwardsandbackwards.com/", false, true },
+        { "--help-all --open thisway.fa --output thatway.fa --jabaws https://forwardsandbackwards.com/",
+            Arg.HELP, null, true, true },
+        { "--help-all --nonews --open thisway.fa --output thatway.fa --jabaws https://forwardsandbackwards.com/",
+            Arg.NEWS, null, false, true },
+        { "--help --nonews --open thisway.fa --output thatway.fa --jabaws https://forwardsandbackwards.com/",
+            Arg.NEWS, null, false, true },
+        { "--help-opening --nonews --open thisway.fa --output thatway.fa --jabaws https://forwardsandbackwards.com/",
+            Arg.NEWS, null, false, true },
+        { "--nonews --open thisway.fa --output thatway.fa --jabaws https://forwardsandbackwards.com/",
+            Arg.NEWS, null, false, true },
+        { "--open thisway.fa --image thatway.png", null, null, false,
+            true },
+        { "--open thisway.fa --output thatway.png", null, null, false,
+            true },
+        { "--open thisway.fa --image thatway.png --noheadless", null, null,
+            false, false },
+        { "--open thisway.fa --output thatway.png --noheadless", null, null,
+            false, false },
+        { "--open thisway.fa --image thatway.png --gui", null, null, false,
+            false },
+        { "--open thisway.fa --output thatway.png --gui", null, null, false,
+            false },
+        // --gui takes precedence
+        { "--open thisway.fa --image thatway.png --gui --headless", null,
+            null, false, false },
+        { "--open thisway.fa --output thatway.png --gui --headless", null,
+            null, false, false },
         //
     };
   }
-}
+
+}
\ No newline at end of file