Merge branch 'develop' into features/r2_11_2_alphafold/JAL-629
[jalview.git] / test / jalview / bin / ArgParserTest.java
1 package jalview.bin;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.List;
6 import java.util.Properties;
7
8 import org.testng.Assert;
9 import org.testng.annotations.AfterClass;
10 import org.testng.annotations.DataProvider;
11 import org.testng.annotations.Test;
12
13 import jalview.bin.argparser.Arg;
14 import jalview.bin.argparser.ArgParser;
15 import jalview.bin.argparser.BootstrapArgs;
16
17 @Test(singleThreaded = true)
18 public class ArgParserTest
19 {
20   @AfterClass(alwaysRun = true)
21   public static void resetProps()
22   {
23     Cache.loadProperties("test/jalview/testProps.jvprops");
24   }
25
26   @Test(groups = "Functional", dataProvider = "argLines")
27   public void parseArgsAndSubValsTest(String commandLineArgs, Arg a,
28           String other)
29   {
30     String[] args = commandLineArgs.split("\\s*");
31     ArgParser argparser = new ArgParser(args);
32   }
33
34   @Test(groups = "Functional", dataProvider = "argLines")
35   public void bootstrapArgsTest(String commandLineArgs, Arg a, String other)
36   {
37     String[] args = commandLineArgs.split("\\s+");
38     BootstrapArgs b = BootstrapArgs.getBootstrapArgs(args);
39
40     Assert.assertTrue(b.contains(a));
41     if (a == Arg.PROPS)
42     {
43       Properties bP = Cache.bootstrapProperties(b.get(Arg.PROPS));
44       Assert.assertTrue(other.equals(bP.get(Cache.BOOTSTRAP_TEST)));
45       Assert.assertFalse(bP.contains("NOT" + Cache.BOOTSTRAP_TEST));
46     }
47     else if (a == Arg.ARGFILE)
48     {
49       List<String> filenames = b.getList(a);
50       boolean found = false;
51       for (String s : filenames)
52       {
53         File f = new File(s);
54         File fo = new File(other);
55         try
56         {
57           if (fo.getCanonicalPath().equals(f.getCanonicalPath()))
58           {
59             found = true;
60             break;
61           }
62         } catch (IOException e)
63         {
64         }
65       }
66       Assert.assertTrue(found,
67               "File '" + other + "' not found in shell expanded glob '"
68                       + commandLineArgs + "'");
69     }
70
71   }
72
73   @Test(groups = "Functional", dataProvider = "argFiles")
74   public void argFilesTest(String commandLineArgs, Arg a, String other)
75   {
76     String[] args = commandLineArgs.split("\\s+");
77     BootstrapArgs b = BootstrapArgs.getBootstrapArgs(args);
78
79     Assert.assertTrue(b.contains(a));
80     Assert.assertFalse(b.contains(Arg.OPEN));
81     if (a == Arg.PROPS)
82     {
83       Properties bP = Cache.bootstrapProperties(b.get(Arg.PROPS));
84       Assert.assertTrue("true".equals(bP.get(Cache.BOOTSTRAP_TEST)));
85     }
86   }
87
88   @DataProvider(name = "argLinesNotworking")
89   public Object[][] argLinesTest()
90   {
91     return new Object[][] {
92         // can't use this one yet as it doesn't get shell glob expanded by the
93         // test
94         { "--argfile test/jalview/bin/argparser/argfile*.txt", Arg.ARGFILE,
95             "test/jalview/bin/argparser/argfile0.txt" }, };
96   }
97
98   @DataProvider(name = "argLines")
99   public Object[][] argLines()
100   {
101     return new Object[][] {
102         { "--debug --open=test/jalview/bin/argparser/test1.fa", Arg.DEBUG,
103             null },
104         { "--open=test/jalview/bin/argparser/test1.fa --headless",
105             Arg.HEADLESS, null },
106         { "--open=test/jalview/bin/argparser/test1.fa --props=test/jalview/bin/argparser/testProps.jvprops",
107             Arg.PROPS, "true" },
108         { "--argfile test/jalview/bin/argparser/argfile0.txt", Arg.ARGFILE,
109             "test/jalview/bin/argparser/argfile0.txt" },
110         // these next three are what a shell glob expansion would look like
111         { "--argfile test/jalview/bin/argparser/argfile0.txt test/jalview/bin/argparser/argfile1.txt test/jalview/bin/argparser/argfile2.txt",
112             Arg.ARGFILE, "test/jalview/bin/argparser/argfile0.txt" },
113         { "--argfile test/jalview/bin/argparser/argfile0.txt test/jalview/bin/argparser/argfile1.txt test/jalview/bin/argparser/argfile2.txt",
114             Arg.ARGFILE, "test/jalview/bin/argparser/argfile1.txt" },
115         { "--argfile test/jalview/bin/argparser/argfile0.txt test/jalview/bin/argparser/argfile1.txt test/jalview/bin/argparser/argfile2.txt",
116             Arg.ARGFILE, "test/jalview/bin/argparser/argfile2.txt" },
117         { "--argfile=test/jalview/bin/argparser/argfile*.txt", Arg.ARGFILE,
118             "test/jalview/bin/argparser/argfile0.txt" },
119         { "--argfile=test/jalview/bin/argparser/argfile*.txt", Arg.ARGFILE,
120             "test/jalview/bin/argparser/argfile1.txt" },
121         { "--argfile=test/jalview/bin/argparser/argfile*.txt", Arg.ARGFILE,
122             "test/jalview/bin/argparser/argfile2.txt" } };
123
124   }
125
126   @DataProvider(name = "argFiles")
127   public Object[][] argFiles()
128   {
129     return new Object[][] { {
130         "--argfile=test/jalview/bin/argparser/argfile0.txt --open=shouldntbeabootstrap",
131         Arg.ARGFILE, "test/jalview/bin/argfiles/test1.fa" } };
132   }
133 }