9beba17ced8a738c6cc0c69207961f93d6b05a4c
[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 commandLineArgs, Arg a,
283           String[] values, String[] nonvalues)
284   {
285     String[] args = commandLineArgs.split("\\s+");
286     ArgParser argparser = new ArgParser(args);
287
288     int num = values.length;
289     List<String> linkedIds = argparser.getLinkedIds();
290     Assert.assertEquals(linkedIds.size(), num,
291             "Wrong number of linkedIds: " + linkedIds.toString());
292     for (int i = 0; i < num; i++)
293     {
294       String value = values[i];
295       String linkedId = linkedIds.get(i);
296       ArgValuesMap avm = argparser.getLinkedArgs(linkedId);
297       if (value == null)
298       {
299         Assert.assertTrue(avm.containsArg(a),
300                 "Arg value for " + a.argString()
301                         + " not applied correctly to linkedId '" + linkedId
302                         + "'");
303       }
304       else
305       {
306         ArgValues avs = avm.getArgValues(a);
307         ArgValue av = avs.getArgValue();
308         String v = av.getValue();
309         value = new File(value).getAbsolutePath();
310         Assert.assertEquals(v, value, "Arg value for " + a.argString()
311                 + " not applied correctly to linkedId '" + linkedId + "'");
312       }
313     }
314
315   }
316
317   @DataProvider(name = "allLinkedIdsData")
318   public Object[][] allLinkedIdsData()
319   {
320     return new Object[][] {
321         //
322         /*
323         */
324         { "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --all --image={dirname}/{basename}.png --close",
325             Arg.CLOSE, new String[]
326             { null, null, null },
327             null },
328         { "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --all --output={dirname}/{basename}.stk --close",
329             Arg.OUTPUT, new String[]
330             { "test/jalview/bin/argparser/testfiles/test1.stk",
331                 "test/jalview/bin/argparser/testfiles/test2.stk",
332                 "test/jalview/bin/argparser/testfiles/test3.stk", },
333             null },
334         { "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --all --image={dirname}/{basename}.png --close",
335             Arg.IMAGE, new String[]
336             { "test/jalview/bin/argparser/testfiles/test1.png",
337                 "test/jalview/bin/argparser/testfiles/test2.png",
338                 "test/jalview/bin/argparser/testfiles/test3.png", },
339             null },
340         //
341     };
342   }
343
344   @Test(groups = "Functional", dataProvider = "bootstrapArgsData")
345   public void bootstrapArgsValuesAndHeadlessModeTest(String commandLineArgs,
346           Arg a, String valS, boolean valB, boolean headlessValue)
347   {
348     String[] args = commandLineArgs.split("\\s+");
349     BootstrapArgs bsa = BootstrapArgs.getBootstrapArgs(args);
350     if (a != null)
351     {
352       if (valS != null)
353       {
354         Assert.assertEquals(bsa.getValue(a), valS,
355                 "BootstrapArg " + a.argString()
356                         + " value does not match expected '" + valS + "'");
357       }
358       else
359       {
360         Assert.assertEquals(bsa.getBoolean(a), valB,
361                 "Boolean/Unary value of BootstrapArg " + a.argString()
362                         + "' is not the expected '" + valB + "'");
363       }
364     }
365
366     boolean isHeadless = bsa.isHeadless();
367     Assert.assertEquals(isHeadless, headlessValue,
368             "Assumed headless setting '" + isHeadless + "' is wrong.");
369   }
370
371   @DataProvider(name = "bootstrapArgsData")
372   public Object[][] bootstrapArgsData()
373   {
374     return new Object[][] {
375         /*
376          * cmdline args
377          * Arg (null if only testing headless)
378          * String value if there is one (null otherwise)
379          * boolean value if String value is null
380          * expected value of isHeadless()
381          */
382         /*
383         */
384         { "--open thisway.fa --output thatway.fa --jabaws https://forwardsandbackwards.com/",
385             Arg.JABAWS, "https://forwardsandbackwards.com/", false, true },
386         { "--help-all --open thisway.fa --output thatway.fa --jabaws https://forwardsandbackwards.com/",
387             Arg.HELP, null, true, true },
388         { "--help-all --nonews --open thisway.fa --output thatway.fa --jabaws https://forwardsandbackwards.com/",
389             Arg.NEWS, null, false, true },
390         { "--help --nonews --open thisway.fa --output thatway.fa --jabaws https://forwardsandbackwards.com/",
391             Arg.NEWS, null, false, true },
392         { "--help-opening --nonews --open thisway.fa --output thatway.fa --jabaws https://forwardsandbackwards.com/",
393             Arg.NEWS, null, false, true },
394         { "--nonews --open thisway.fa --output thatway.fa --jabaws https://forwardsandbackwards.com/",
395             Arg.NEWS, null, false, true },
396         { "--open thisway.fa --image thatway.png", null, null, false,
397             true },
398         { "--open thisway.fa --output thatway.png", null, null, false,
399             true },
400         { "--open thisway.fa --image thatway.png --noheadless", null, null,
401             false, false },
402         { "--open thisway.fa --output thatway.png --noheadless", null, null,
403             false, false },
404         { "--open thisway.fa --image thatway.png --gui", null, null, false,
405             false },
406         { "--open thisway.fa --output thatway.png --gui", null, null, false,
407             false },
408         // --gui takes precedence
409         { "--open thisway.fa --image thatway.png --gui --headless", null,
410             null, false, false },
411         { "--open thisway.fa --output thatway.png --gui --headless", null,
412             null, false, false },
413         //
414     };
415   }
416
417 }