3ba096e6a54b9e155edd4732f903babd91564455
[jalview.git] / test / jalview / bin / argparser / ArgParserTest.java
1 package jalview.bin.argparser;
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.AfterMethod;
11 import org.testng.annotations.DataProvider;
12 import org.testng.annotations.Test;
13
14 import jalview.bin.Cache;
15 import jalview.gui.Desktop;
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   @AfterMethod(alwaysRun = true)
27   public void tearDown()
28   {
29     if (Desktop.instance != null)
30       Desktop.instance.closeAll_actionPerformed(null);
31   }
32
33   @Test(groups = "Functional", dataProvider = "argLines")
34   public void parseArgsTest(String commandLineArgs, Arg a, String other)
35   {
36     String[] args = commandLineArgs.split("\\s+");
37     ArgParser argparser = new ArgParser(args);
38   }
39
40   @Test(groups = "Functional", dataProvider = "argSubValsAndLinkedIds")
41   public void parseSubValsAndLinkedIdsTest(String commandLineArgs,
42           String linkedId, Arg a, String subvalKey, String value,
43           boolean trueOrFalse)
44   {
45     String[] args = commandLineArgs.split("\\s+");
46     ArgParser argparser = new ArgParser(args);
47     ArgValuesMap avm = argparser.getLinkedArgs(linkedId);
48     ArgValue av = avm.getArgValue(a);
49     SubVals sv = av.getSubVals();
50     String testString = null;
51     if (subvalKey.equals("GETINDEX"))
52     {
53       testString = String.valueOf(sv.getIndex());
54     }
55     else
56     {
57       testString = sv.get(subvalKey);
58     }
59     if (trueOrFalse)
60     {
61       Assert.assertEquals(testString, value);
62     }
63     else
64     {
65       Assert.assertNotEquals(testString, value);
66     }
67   }
68
69   @Test(
70     groups = "Functional",
71     dataProvider = "argAutoIndexAndSubstitutions")
72   public void parseAutoIndexAndSubstitutionsTest(String commandLineArgs,
73           String linkedId, Arg a, String filename)
74   {
75     // { "--append=filename0 --new --append=filename1", "JALVIEW:1",
76     // Arg.OPEN, "filename1" },
77     String[] args = commandLineArgs.split("\\s+");
78     ArgParser argparser = new ArgParser(args);
79     ArgValuesMap avm = argparser.getLinkedArgs(linkedId);
80     ArgValue av = avm.getArgValue(a);
81     Assert.assertEquals(av.getValue(), filename);
82   }
83
84   @Test(groups = "Functional", dataProvider = "argLines")
85   public void bootstrapArgsTest(String commandLineArgs, Arg a, String other)
86   {
87     String[] args = commandLineArgs.split("\\s+");
88     BootstrapArgs b = BootstrapArgs.getBootstrapArgs(args);
89
90     Assert.assertTrue(b.contains(a));
91     if (a == Arg.PROPS)
92     {
93       Properties bP = Cache.bootstrapProperties(b.getValue(Arg.PROPS));
94       Assert.assertNotNull(bP);
95       Assert.assertTrue(other.equals(bP.get(Cache.BOOTSTRAP_TEST)));
96       Assert.assertFalse(bP.contains("NOT" + Cache.BOOTSTRAP_TEST));
97     }
98     else if (a == Arg.ARGFILE)
99     {
100       List<String> filenames = b.getValueList(a);
101       boolean found = false;
102       for (String s : filenames)
103       {
104         File f = new File(s);
105         File fo = new File(other);
106         try
107         {
108           if (fo.getCanonicalPath().equals(f.getCanonicalPath()))
109           {
110             found = true;
111             break;
112           }
113         } catch (IOException e)
114         {
115         }
116       }
117       Assert.assertTrue(found,
118               "File '" + other + "' not found in shell expanded glob '"
119                       + commandLineArgs + "'");
120     }
121   }
122
123   @Test(groups = "Functional", dataProvider = "argFiles")
124   public void argFilesTest(String commandLineArgs, Arg a, String other)
125   {
126     String[] args = commandLineArgs.split("\\s+");
127     BootstrapArgs b = BootstrapArgs.getBootstrapArgs(args);
128
129     Assert.assertTrue(b.contains(a));
130     Assert.assertFalse(b.contains(Arg.APPEND));
131     if (a == Arg.PROPS)
132     {
133       Properties bP = Cache.bootstrapProperties(b.getValue(Arg.PROPS));
134       Assert.assertTrue("true".equals(bP.get(Cache.BOOTSTRAP_TEST)));
135     }
136   }
137
138   @DataProvider(name = "argLinesNotworking")
139   public Object[][] argLinesTest()
140   {
141     return new Object[][] {
142         // can't use this one yet as it doesn't get shell glob expanded by the
143         // test
144         { "--argfile test/jalview/bin/argparser/testfiles/argfile*.txt",
145             Arg.ARGFILE,
146             "test/jalview/bin/argparser/testfiles/argfile0.txt" }, };
147   }
148
149   @DataProvider(name = "argLines")
150   public Object[][] argLines()
151   {
152     return new Object[][] { {
153         "--append=test/jalview/bin/argparser/testfiles/test1.fa --props=test/jalview/bin/argparser/testfiles/testProps.jvprops",
154         Arg.PROPS, "true" },
155         { "--debug --append=test/jalview/bin/argparser/testfiles/test1.fa",
156             Arg.DEBUG, null },
157         { "--append=test/jalview/bin/argparser/testfiles/test1.fa --headless",
158             Arg.HEADLESS, null },
159
160         { "--argfile test/jalview/bin/argparser/testfiles/argfile0.txt",
161             Arg.ARGFILE,
162             "test/jalview/bin/argparser/testfiles/argfile0.txt" },
163         // these next three are what a shell glob expansion would look like
164         { "--argfile test/jalview/bin/argparser/testfiles/argfile0.txt test/jalview/bin/argparser/testfiles/argfile1.txt test/jalview/bin/argparser/testfiles/argfile2.txt",
165             Arg.ARGFILE,
166             "test/jalview/bin/argparser/testfiles/argfile0.txt" },
167         { "--argfile test/jalview/bin/argparser/testfiles/argfile0.txt test/jalview/bin/argparser/testfiles/argfile1.txt test/jalview/bin/argparser/testfiles/argfile2.txt",
168             Arg.ARGFILE,
169             "test/jalview/bin/argparser/testfiles/argfile1.txt" },
170         { "--argfile test/jalview/bin/argparser/testfiles/argfile0.txt test/jalview/bin/argparser/testfiles/argfile1.txt test/jalview/bin/argparser/testfiles/argfile2.txt",
171             Arg.ARGFILE,
172             "test/jalview/bin/argparser/testfiles/argfile2.txt" },
173         { "--argfile=test/jalview/bin/argparser/testfiles/argfile*.txt",
174             Arg.ARGFILE,
175             "test/jalview/bin/argparser/testfiles/argfile0.txt" },
176         { "--argfile=test/jalview/bin/argparser/testfiles/argfile*.txt",
177             Arg.ARGFILE,
178             "test/jalview/bin/argparser/testfiles/argfile1.txt" },
179         { "--argfile=test/jalview/bin/argparser/testfiles/argfile*.txt",
180             Arg.ARGFILE,
181             "test/jalview/bin/argparser/testfiles/argfile2.txt" } };
182   }
183
184   @DataProvider(name = "argSubValsAndLinkedIds")
185   public Object[][] argSubValsAndLinkedIds()
186   {
187     return new Object[][] {
188         //
189         /*
190          */
191         { "--debug --append=[hi]test/jalview/bin/argparser/testfiles/test1.fa",
192             "JALVIEW:0", Arg.APPEND, "hi", "true", true },
193         { "--append[linkedId1]=[new,hello=world,1]test/jalview/bin/argparser/testfiles/test1.fa --headless",
194             "linkedId1", Arg.APPEND, "new", "true", true },
195         { "--append[linkedId2]=[new,hello=world,1]test/jalview/bin/argparser/testfiles/test1.fa --headless",
196             "linkedId2", Arg.APPEND, "hello", "world", true },
197         { "--append[linkedId3]=[new,hello=world,1]test/jalview/bin/argparser/testfiles/test1.fa --headless",
198             "linkedId3", Arg.APPEND, "GETINDEX", "1", true },
199         { "--append[linkedId4]=[new,hello=world,1]test/jalview/bin/argparser/testfiles/test1.fa --append[linkedId5]=[notnew;hello=world;1]test/jalview/bin/argparser/testfiles/test1.fa --headless",
200             "linkedId5", Arg.APPEND, "new", "true", false },
201         { "--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",
202             "linkedId5", Arg.APPEND, "hello", "world", false },
203         { "--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",
204             "linkedId7", Arg.APPEND, "GETINDEX", "0", false },
205         /*
206          */
207         //
208     };
209   }
210
211   @DataProvider(name = "argAutoIndexAndSubstitutions")
212   public Object[][] argAutoIndexAndSubstitutions()
213   {
214     return new Object[][] {
215         //
216         /*
217          */
218         { "--append=filename0 --append=filename1", "JALVIEW:0", Arg.APPEND,
219             "filename0" },
220         { "--append=filename0 --new --append=filename1", "JALVIEW:1",
221             Arg.APPEND, "filename1" },
222         { "--append=filename0 --new --new --append=filename2", "JALVIEW:0",
223             Arg.APPEND, "filename0" },
224         { "--append=filename0 --new --new --append=filename2", "JALVIEW:2",
225             Arg.APPEND, "filename2" },
226         { "--append[linkA-{n}]=filenameA0 --append[linkA-{++n}]=filenameA1",
227             "linkA-0", Arg.APPEND, "filenameA0" },
228         { "--append[linkB-{n}]=filenameB0 --append[linkB-{++n}]=filenameB1",
229             "linkB-1", Arg.APPEND, "filenameB1" },
230         { "--append[linkC-{n}]=filenameC0 --image[linkC-{n}]=outputC{n}.txt",
231             "linkC-0", Arg.IMAGE, "outputC{n}.txt" },
232         { "--append[linkD-{n}]=filenameD0 --substitutions --image[linkD-{n}]=outputD{n}.txt",
233             "linkD-0", Arg.IMAGE, "outputD0.txt" },
234         { "--append[linkE-{n}]=filenameE0 --substitutions --image[linkE-{n}]=output-E{n}.txt --nil[{++n}] --image[linkE-{n}]=outputE{n}.txt",
235             "linkE-0", Arg.IMAGE, "output-E0.txt" },
236         { "--append[linkF-{n}]=filenameF0 --substitutions --image[linkF-{n}]=output-F{n}.txt --nil[{++n}] --image[linkF-{n}]=outputF{n}.txt",
237             "linkF-1", Arg.IMAGE, "outputF1.txt" },
238         { "--append[linkG-{n}]=filenameG0 --substitutions --image[linkG-{n}]=output-G{n}.txt --nil[{++n}] --nosubstitutions --image[linkG-{n}]=outputG{n}.txt",
239             "linkG-1", Arg.IMAGE, "outputG{n}.txt" },
240         { "--append[linkH-{n}]=filenameH0 --substitutions --image[linkH-{n}]=output-H{n}.txt --nil[{++n}] --nosubstitutions --image[linkH-{n}]=outputH{n}.txt",
241             "linkH-0", Arg.IMAGE, "output-H0.txt" },
242         { "--open=filename0 --append=filename1", "JALVIEW:0", Arg.OPEN,
243             "filename0" },
244         { "--open=filename0 --new --append=filename1", "JALVIEW:1",
245             Arg.APPEND, "filename1" },
246         { "--open=filename0 --new --new --append=filename2", "JALVIEW:0",
247             Arg.OPEN, "filename0" },
248         { "--open=filename0 --new --new --append=filename2", "JALVIEW:2",
249             Arg.APPEND, "filename2" },
250         { "--open[linkA-{n}]=filenameA0 --append[linkA-{++n}]=filenameA1",
251             "linkA-0", Arg.OPEN, "filenameA0" },
252         { "--open[linkB-{n}]=filenameB0 --append[linkB-{++n}]=filenameB1",
253             "linkB-1", Arg.APPEND, "filenameB1" },
254         { "--open[linkC-{n}]=filenameC0 --image[linkC-{n}]=outputC{n}.txt",
255             "linkC-0", Arg.IMAGE, "outputC{n}.txt" },
256         { "--open[linkD-{n}]=filenameD0 --substitutions --image[linkD-{n}]=outputD{n}.txt",
257             "linkD-0", Arg.IMAGE, "outputD0.txt" },
258         { "--open[linkE-{n}]=filenameE0 --substitutions --image[linkE-{n}]=output-E{n}.txt --nil[{++n}] --image[linkE-{n}]=outputE{n}.txt",
259             "linkE-0", Arg.IMAGE, "output-E0.txt" },
260         { "--open[linkF-{n}]=filenameF0 --substitutions --image[linkF-{n}]=output-F{n}.txt --nil[{++n}] --image[linkF-{n}]=outputF{n}.txt",
261             "linkF-1", Arg.IMAGE, "outputF1.txt" },
262         { "--open[linkG-{n}]=filenameG0 --substitutions --image[linkG-{n}]=output-G{n}.txt --nil[{++n}] --nosubstitutions --image[linkG-{n}]=outputG{n}.txt",
263             "linkG-1", Arg.IMAGE, "outputG{n}.txt" },
264         { "--open[linkH-{n}]=filenameH0 --substitutions --image[linkH-{n}]=output-H{n}.txt --nil[{++n}] --nosubstitutions --image[linkH-{n}]=outputH{n}.txt",
265             "linkH-0", Arg.IMAGE, "output-H0.txt" },
266         /*
267          */
268
269         //
270     };
271   }
272
273   @DataProvider(name = "argFiles")
274   public Object[][] argFiles()
275   {
276     return new Object[][] { {
277         "--argfile=test/jalview/bin/argparser/testfiles/argfile0.txt --open=shouldntbeabootstrap",
278         Arg.ARGFILE, "test/jalview/bin/argfiles/testfiles/test1.fa" } };
279   }
280
281   @Test(groups = "Functional", dataProvider = "allLinkedIdsData")
282   public void allLinkedIdsTest(String pwd, String commandLineArgs, Arg a,
283           String[] values, String[] nonvalues)
284   {
285     String userDir = System.getProperty("user.dir");
286     if (pwd != null)
287     {
288       File pwdFile = new File(pwd);
289       System.setProperty("user.dir", pwdFile.getAbsolutePath());
290     }
291     String[] args = commandLineArgs.split("\\s+");
292     ArgParser argparser = new ArgParser(args);
293
294     int num = values.length;
295     List<String> linkedIds = argparser.getLinkedIds();
296     Assert.assertEquals(linkedIds.size(), num,
297             "Wrong number of linkedIds: " + linkedIds.toString());
298     for (int i = 0; i < num; i++)
299     {
300       String value = values[i];
301       String linkedId = linkedIds.get(i);
302       ArgValuesMap avm = argparser.getLinkedArgs(linkedId);
303       if (value == null)
304       {
305         Assert.assertTrue(avm.containsArg(a),
306                 "Arg value for " + a.argString()
307                         + " not applied correctly to linkedId '" + linkedId
308                         + "'");
309       }
310       else
311       {
312         ArgValues avs = avm.getArgValues(a);
313         ArgValue av = avs.getArgValue();
314         String v = av.getValue();
315         value = new File(value).getPath();
316         Assert.assertEquals(v, value, "Arg value for " + a.argString()
317                 + " not applied correctly to linkedId '" + linkedId + "'");
318       }
319     }
320
321     System.setProperty("user.dir", userDir);
322   }
323
324   @DataProvider(name = "allLinkedIdsData")
325   public Object[][] allLinkedIdsData()
326   {
327     return new Object[][] {
328         //
329         /*
330         */
331         { null,
332             "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --all --image={dirname}/{basename}.png --close",
333             Arg.CLOSE, new String[]
334             { null, null, null },
335             null },
336         { null,
337             "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --all --output={dirname}/{basename}.stk --close",
338             Arg.OUTPUT, new String[]
339             { "test/jalview/bin/argparser/testfiles/test1.stk",
340                 "test/jalview/bin/argparser/testfiles/test2.stk",
341                 "test/jalview/bin/argparser/testfiles/test3.stk", },
342             null },
343         { null,
344             "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --all --image={dirname}/{basename}.png --close",
345             Arg.IMAGE, new String[]
346             { "test/jalview/bin/argparser/testfiles/test1.png",
347                 "test/jalview/bin/argparser/testfiles/test2.png",
348                 "test/jalview/bin/argparser/testfiles/test3.png", },
349             null },
350         /*
351          * Find a way to change pwd reliably -- need to match "*.fa" against some files!
352          { "test/jalview/bin/argparser/testfiles",
353          
354             "--open=*.fa --image={dirname}/{basename}.png --close",
355             Arg.IMAGE, new String[]
356             { "./test1.png", "./test2.png", "./test3.png", }, null },
357             */
358         //
359     };
360   }
361
362   @Test(groups = "Functional", dataProvider = "bootstrapArgsData")
363   public void bootstrapArgsValuesAndHeadlessModeTest(String commandLineArgs,
364           Arg a, String valS, boolean valB, boolean headlessValue)
365   {
366     String[] args = commandLineArgs.split("\\s+");
367     BootstrapArgs bsa = BootstrapArgs.getBootstrapArgs(args);
368     if (a != null)
369     {
370       if (valS != null)
371       {
372         Assert.assertEquals(bsa.getValue(a), valS,
373                 "BootstrapArg " + a.argString()
374                         + " value does not match expected '" + valS + "'");
375       }
376       else
377       {
378         Assert.assertEquals(bsa.getBoolean(a), valB,
379                 "Boolean/Unary value of BootstrapArg " + a.argString()
380                         + "' is not the expected '" + valB + "'");
381       }
382     }
383
384     boolean isHeadless = bsa.isHeadless();
385     Assert.assertEquals(isHeadless, headlessValue,
386             "Assumed headless setting '" + isHeadless + "' is wrong.");
387   }
388
389   @DataProvider(name = "bootstrapArgsData")
390   public Object[][] bootstrapArgsData()
391   {
392     return new Object[][] {
393         /*
394          * cmdline args
395          * Arg (null if only testing headless)
396          * String value if there is one (null otherwise)
397          * boolean value if String value is null
398          * expected value of isHeadless()
399          */
400         /*
401         */
402         { "--open thisway.fa --output thatway.fa --jabaws https://forwardsandbackwards.com/",
403             Arg.JABAWS, "https://forwardsandbackwards.com/", false, true },
404         { "--help-all --open thisway.fa --output thatway.fa --jabaws https://forwardsandbackwards.com/",
405             Arg.HELP, null, true, true },
406         { "--help-all --nonews --open thisway.fa --output thatway.fa --jabaws https://forwardsandbackwards.com/",
407             Arg.NEWS, null, false, true },
408         { "--help --nonews --open thisway.fa --output thatway.fa --jabaws https://forwardsandbackwards.com/",
409             Arg.NEWS, null, false, true },
410         { "--help-opening --nonews --open thisway.fa --output thatway.fa --jabaws https://forwardsandbackwards.com/",
411             Arg.NEWS, null, false, true },
412         { "--nonews --open thisway.fa --output thatway.fa --jabaws https://forwardsandbackwards.com/",
413             Arg.NEWS, null, false, true },
414         { "--open thisway.fa --image thatway.png", null, null, false,
415             true },
416         { "--open thisway.fa --output thatway.png", null, null, false,
417             true },
418         { "--open thisway.fa --image thatway.png --noheadless", null, null,
419             false, false },
420         { "--open thisway.fa --output thatway.png --noheadless", null, null,
421             false, false },
422         { "--open thisway.fa --image thatway.png --gui", null, null, false,
423             false },
424         { "--open thisway.fa --output thatway.png --gui", null, null, false,
425             false },
426         // --gui takes precedence
427         { "--open thisway.fa --image thatway.png --gui --headless", null,
428             null, false, false },
429         { "--open thisway.fa --output thatway.png --gui --headless", null,
430             null, false, false },
431         //
432     };
433   }
434
435 }