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 pwd, String commandLineArgs, Arg a,
283 String[] values, String[] nonvalues)
285 String userDir = System.getProperty("user.dir");
288 File pwdFile = new File(pwd);
289 System.setProperty("user.dir", pwdFile.getAbsolutePath());
291 String[] args = commandLineArgs.split("\\s+");
292 ArgParser argparser = new ArgParser(args);
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++)
300 String value = values[i];
301 String linkedId = linkedIds.get(i);
302 ArgValuesMap avm = argparser.getLinkedArgs(linkedId);
305 Assert.assertTrue(avm.containsArg(a),
306 "Arg value for " + a.argString()
307 + " not applied correctly to linkedId '" + linkedId
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 + "'");
321 System.setProperty("user.dir", userDir);
324 @DataProvider(name = "allLinkedIdsData")
325 public Object[][] allLinkedIdsData()
327 return new Object[][] {
332 "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --all --image={dirname}/{basename}.png --close",
333 Arg.CLOSE, new String[]
334 { null, null, 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", },
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", },
351 * Find a way to change pwd reliably -- need to match "*.fa" against some files!
352 { "test/jalview/bin/argparser/testfiles",
354 "--open=*.fa --image={dirname}/{basename}.png --close",
355 Arg.IMAGE, new String[]
356 { "./test1.png", "./test2.png", "./test3.png", }, null },
362 @Test(groups = "Functional", dataProvider = "bootstrapArgsData")
363 public void bootstrapArgsValuesAndHeadlessModeTest(String commandLineArgs,
364 Arg a, String valS, boolean valB, boolean headlessValue)
366 String[] args = commandLineArgs.split("\\s+");
367 BootstrapArgs bsa = BootstrapArgs.getBootstrapArgs(args);
372 Assert.assertEquals(bsa.getValue(a), valS,
373 "BootstrapArg " + a.argString()
374 + " value does not match expected '" + valS + "'");
378 Assert.assertEquals(bsa.getBoolean(a), valB,
379 "Boolean/Unary value of BootstrapArg " + a.argString()
380 + "' is not the expected '" + valB + "'");
384 boolean isHeadless = bsa.isHeadless();
385 Assert.assertEquals(isHeadless, headlessValue,
386 "Assumed headless setting '" + isHeadless + "' is wrong.");
389 @DataProvider(name = "bootstrapArgsData")
390 public Object[][] bootstrapArgsData()
392 return new Object[][] {
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()
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,
416 { "--open thisway.fa --output thatway.png", null, null, false,
418 { "--open thisway.fa --image thatway.png --noheadless", null, null,
420 { "--open thisway.fa --output thatway.png --noheadless", null, null,
422 { "--open thisway.fa --image thatway.png --gui", null, null, false,
424 { "--open thisway.fa --output thatway.png --gui", null, null, 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 },