1 package jalview.bin.argparser;
4 import java.io.IOException;
6 import java.util.Properties;
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;
14 import jalview.bin.Cache;
15 import jalview.gui.Desktop;
17 @Test(singleThreaded = true)
18 public class ArgParserTest
20 @AfterClass(alwaysRun = true)
21 public static void resetProps()
23 Cache.loadProperties("test/jalview/testProps.jvprops");
26 @AfterMethod(alwaysRun = true)
27 public void tearDown()
29 if (Desktop.instance != null)
30 Desktop.instance.closeAll_actionPerformed(null);
33 @Test(groups = "Functional", dataProvider = "argLines")
34 public void parseArgsTest(String commandLineArgs, Arg a, String other)
36 String[] args = commandLineArgs.split("\\s+");
37 ArgParser argparser = new ArgParser(args);
40 @Test(groups = "Functional", dataProvider = "argSubValsAndLinkedIds")
41 public void parseSubValsAndLinkedIdsTest(String commandLineArgs,
42 String linkedId, Arg a, String subvalKey, String value,
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"))
53 testString = String.valueOf(sv.getIndex());
57 testString = sv.get(subvalKey);
61 Assert.assertEquals(testString, value);
65 Assert.assertNotEquals(testString, value);
70 groups = "Functional",
71 dataProvider = "argAutoIndexAndSubstitutions")
72 public void parseAutoIndexAndSubstitutionsTest(String commandLineArgs,
73 String linkedId, Arg a, String filename)
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);
84 @Test(groups = "Functional", dataProvider = "argLines")
85 public void bootstrapArgsTest(String commandLineArgs, Arg a, String other)
87 String[] args = commandLineArgs.split("\\s+");
88 BootstrapArgs b = BootstrapArgs.getBootstrapArgs(args);
90 Assert.assertTrue(b.contains(a));
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));
98 else if (a == Arg.ARGFILE)
100 List<String> filenames = b.getValueList(a);
101 boolean found = false;
102 for (String s : filenames)
104 File f = new File(s);
105 File fo = new File(other);
108 if (fo.getCanonicalPath().equals(f.getCanonicalPath()))
113 } catch (IOException e)
117 Assert.assertTrue(found,
118 "File '" + other + "' not found in shell expanded glob '"
119 + commandLineArgs + "'");
123 @Test(groups = "Functional", dataProvider = "argFiles")
124 public void argFilesTest(String commandLineArgs, Arg a, String other)
126 String[] args = commandLineArgs.split("\\s+");
127 BootstrapArgs b = BootstrapArgs.getBootstrapArgs(args);
129 Assert.assertTrue(b.contains(a));
130 Assert.assertFalse(b.contains(Arg.APPEND));
133 Properties bP = Cache.bootstrapProperties(b.getValue(Arg.PROPS));
134 Assert.assertTrue("true".equals(bP.get(Cache.BOOTSTRAP_TEST)));
138 @DataProvider(name = "argLinesNotworking")
139 public Object[][] argLinesTest()
141 return new Object[][] {
142 // can't use this one yet as it doesn't get shell glob expanded by the
144 { "--argfile test/jalview/bin/argparser/testfiles/argfile*.txt",
146 "test/jalview/bin/argparser/testfiles/argfile0.txt" }, };
149 @DataProvider(name = "argLines")
150 public Object[][] argLines()
152 return new Object[][] { {
153 "--append=test/jalview/bin/argparser/testfiles/test1.fa --props=test/jalview/bin/argparser/testfiles/testProps.jvprops",
155 { "--debug --append=test/jalview/bin/argparser/testfiles/test1.fa",
157 { "--append=test/jalview/bin/argparser/testfiles/test1.fa --headless",
158 Arg.HEADLESS, null },
160 { "--argfile test/jalview/bin/argparser/testfiles/argfile0.txt",
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",
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",
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",
172 "test/jalview/bin/argparser/testfiles/argfile2.txt" },
173 { "--argfile=test/jalview/bin/argparser/testfiles/argfile*.txt",
175 "test/jalview/bin/argparser/testfiles/argfile0.txt" },
176 { "--argfile=test/jalview/bin/argparser/testfiles/argfile*.txt",
178 "test/jalview/bin/argparser/testfiles/argfile1.txt" },
179 { "--argfile=test/jalview/bin/argparser/testfiles/argfile*.txt",
181 "test/jalview/bin/argparser/testfiles/argfile2.txt" } };
184 @DataProvider(name = "argSubValsAndLinkedIds")
185 public Object[][] argSubValsAndLinkedIds()
187 return new Object[][] {
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 },
211 @DataProvider(name = "argAutoIndexAndSubstitutions")
212 public Object[][] argAutoIndexAndSubstitutions()
214 return new Object[][] {
218 { "--append=filename0 --append=filename1", "JALVIEW:0", Arg.APPEND,
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,
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" },
273 @DataProvider(name = "argFiles")
274 public Object[][] argFiles()
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" } };
281 @Test(groups = "Functional", dataProvider = "allLinkedIdsData")
282 public void allLinkedIdsTest(String commandLineArgs, Arg a,
283 String[] values, String[] nonvalues)
285 String[] args = commandLineArgs.split("\\s+");
286 ArgParser argparser = new ArgParser(args);
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++)
294 String value = values[i];
295 String linkedId = linkedIds.get(i);
296 ArgValuesMap avm = argparser.getLinkedArgs(linkedId);
299 Assert.assertTrue(avm.containsArg(a),
300 "Arg value for " + a.argString()
301 + " not applied correctly to linkedId '" + linkedId
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 + "'");
317 @DataProvider(name = "allLinkedIdsData")
318 public Object[][] allLinkedIdsData()
320 return new Object[][] {
324 { "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --all --image={dirname}/{basename}.png --close",
325 Arg.CLOSE, new String[]
326 { null, null, 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", },
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", },
344 @Test(groups = "Functional", dataProvider = "bootstrapArgsData")
345 public void bootstrapArgsValuesAndHeadlessModeTest(String commandLineArgs,
346 Arg a, String valS, boolean valB, boolean headlessValue)
348 String[] args = commandLineArgs.split("\\s+");
349 BootstrapArgs bsa = BootstrapArgs.getBootstrapArgs(args);
354 Assert.assertEquals(bsa.getValue(a), valS,
355 "BootstrapArg " + a.argString()
356 + " value does not match expected '" + valS + "'");
360 Assert.assertEquals(bsa.getBoolean(a), valB,
361 "Boolean/Unary value of BootstrapArg " + a.argString()
362 + "' is not the expected '" + valB + "'");
366 boolean isHeadless = bsa.isHeadless();
367 Assert.assertEquals(isHeadless, headlessValue,
368 "Assumed headless setting '" + isHeadless + "' is wrong.");
371 @DataProvider(name = "bootstrapArgsData")
372 public Object[][] bootstrapArgsData()
374 return new Object[][] {
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()
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,
398 { "--open thisway.fa --output thatway.png", null, null, false,
400 { "--open thisway.fa --image thatway.png --noheadless", null, null,
402 { "--open thisway.fa --output thatway.png --noheadless", null, null,
404 { "--open thisway.fa --image thatway.png --gui", null, null, false,
406 { "--open thisway.fa --output thatway.png --gui", null, null, 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 },