JAL-629 Fixed the argfiletests after subval separator change
[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.DataProvider;
11 import org.testng.annotations.Test;
12
13 import jalview.bin.Cache;
14
15 @Test(singleThreaded = true)
16 public class ArgParserTest
17 {
18   @AfterClass(alwaysRun = true)
19   public static void resetProps()
20   {
21     Cache.loadProperties("test/jalview/testProps.jvprops");
22   }
23
24   @Test(groups = "Functional", dataProvider = "argLines")
25   public void parseArgsTest(String commandLineArgs, Arg a, String other)
26   {
27     String[] args = commandLineArgs.split("\\s+");
28     ArgParser argparser = new ArgParser(args);
29   }
30
31   @Test(groups = "Functional", dataProvider = "argSubValsAndLinkedIds")
32   public void parseSubValsAndLinkedIdsTest(String commandLineArgs,
33           String linkedId, Arg a, String subvalKey, String value,
34           boolean trueOrFalse)
35   {
36     String[] args = commandLineArgs.split("\\s+");
37     ArgParser argparser = new ArgParser(args);
38     ArgValuesMap avm = argparser.linkedArgs(linkedId);
39     ArgValue av = avm.getArgValue(a);
40     SubVals sv = av.getSubVals();
41     String testString = null;
42     if (subvalKey.equals("GETINDEX"))
43     {
44       testString = String.valueOf(sv.getIndex());
45     }
46     else
47     {
48       testString = sv.get(subvalKey);
49     }
50     if (trueOrFalse)
51     {
52       Assert.assertEquals(testString, value);
53     }
54     else
55     {
56       Assert.assertNotEquals(testString, value);
57     }
58   }
59
60   @Test(
61     groups = "Functional",
62     dataProvider = "argAutoIndexAndSubstitutions")
63   public void parseAutoIndexAndSubstitutionsTest(String commandLineArgs,
64           String linkedId, Arg a, String filename)
65   {
66     // { "--append=filename0 --newframe --append=filename1", "JALVIEW:1",
67     // Arg.OPEN, "filename1" },
68     String[] args = commandLineArgs.split("\\s+");
69     ArgParser argparser = new ArgParser(args);
70     ArgValuesMap avm = argparser.linkedArgs(linkedId);
71     ArgValue av = avm.getArgValue(a);
72     Assert.assertEquals(av.getValue(), filename);
73   }
74
75   @Test(groups = "Functional", dataProvider = "argLines")
76   public void bootstrapArgsTest(String commandLineArgs, Arg a, String other)
77   {
78     String[] args = commandLineArgs.split("\\s+");
79     BootstrapArgs b = BootstrapArgs.getBootstrapArgs(args);
80
81     Assert.assertTrue(b.contains(a));
82     if (a == Arg.PROPS)
83     {
84       Properties bP = Cache.bootstrapProperties(b.get(Arg.PROPS));
85       Assert.assertNotNull(bP);
86       Assert.assertTrue(other.equals(bP.get(Cache.BOOTSTRAP_TEST)));
87       Assert.assertFalse(bP.contains("NOT" + Cache.BOOTSTRAP_TEST));
88     }
89     else if (a == Arg.ARGFILE)
90     {
91       List<String> filenames = b.getList(a);
92       boolean found = false;
93       for (String s : filenames)
94       {
95         File f = new File(s);
96         File fo = new File(other);
97         try
98         {
99           if (fo.getCanonicalPath().equals(f.getCanonicalPath()))
100           {
101             found = true;
102             break;
103           }
104         } catch (IOException e)
105         {
106         }
107       }
108       Assert.assertTrue(found,
109               "File '" + other + "' not found in shell expanded glob '"
110                       + commandLineArgs + "'");
111     }
112   }
113
114   @Test(groups = "Functional", dataProvider = "argFiles")
115   public void argFilesTest(String commandLineArgs, Arg a, String other)
116   {
117     String[] args = commandLineArgs.split("\\s+");
118     BootstrapArgs b = BootstrapArgs.getBootstrapArgs(args);
119
120     Assert.assertTrue(b.contains(a));
121     Assert.assertFalse(b.contains(Arg.APPEND));
122     if (a == Arg.PROPS)
123     {
124       Properties bP = Cache.bootstrapProperties(b.get(Arg.PROPS));
125       Assert.assertTrue("true".equals(bP.get(Cache.BOOTSTRAP_TEST)));
126     }
127   }
128
129   @DataProvider(name = "argLinesNotworking")
130   public Object[][] argLinesTest()
131   {
132     return new Object[][] {
133         // can't use this one yet as it doesn't get shell glob expanded by the
134         // test
135         { "--argfile test/jalview/bin/argparser/testfiles/argfile*.txt",
136             Arg.ARGFILE,
137             "test/jalview/bin/argparser/testfiles/argfile0.txt" }, };
138   }
139
140   @DataProvider(name = "argLines")
141   public Object[][] argLines()
142   {
143     return new Object[][] { {
144         "--append=test/jalview/bin/argparser/testfiles/test1.fa --props=test/jalview/bin/argparser/testfiles/testProps.jvprops",
145         Arg.PROPS, "true" },
146         { "--debug --append=test/jalview/bin/argparser/testfiles/test1.fa",
147             Arg.DEBUG, null },
148         { "--append=test/jalview/bin/argparser/testfiles/test1.fa --headless",
149             Arg.HEADLESS, null },
150
151         { "--argfile test/jalview/bin/argparser/testfiles/argfile0.txt",
152             Arg.ARGFILE,
153             "test/jalview/bin/argparser/testfiles/argfile0.txt" },
154         // these next three are what a shell glob expansion would look like
155         { "--argfile test/jalview/bin/argparser/testfiles/argfile0.txt test/jalview/bin/argparser/testfiles/argfile1.txt test/jalview/bin/argparser/testfiles/argfile2.txt",
156             Arg.ARGFILE,
157             "test/jalview/bin/argparser/testfiles/argfile0.txt" },
158         { "--argfile test/jalview/bin/argparser/testfiles/argfile0.txt test/jalview/bin/argparser/testfiles/argfile1.txt test/jalview/bin/argparser/testfiles/argfile2.txt",
159             Arg.ARGFILE,
160             "test/jalview/bin/argparser/testfiles/argfile1.txt" },
161         { "--argfile test/jalview/bin/argparser/testfiles/argfile0.txt test/jalview/bin/argparser/testfiles/argfile1.txt test/jalview/bin/argparser/testfiles/argfile2.txt",
162             Arg.ARGFILE,
163             "test/jalview/bin/argparser/testfiles/argfile2.txt" },
164         { "--argfile=test/jalview/bin/argparser/testfiles/argfile*.txt",
165             Arg.ARGFILE,
166             "test/jalview/bin/argparser/testfiles/argfile0.txt" },
167         { "--argfile=test/jalview/bin/argparser/testfiles/argfile*.txt",
168             Arg.ARGFILE,
169             "test/jalview/bin/argparser/testfiles/argfile1.txt" },
170         { "--argfile=test/jalview/bin/argparser/testfiles/argfile*.txt",
171             Arg.ARGFILE,
172             "test/jalview/bin/argparser/testfiles/argfile2.txt" } };
173   }
174
175   @DataProvider(name = "argSubValsAndLinkedIds")
176   public Object[][] argSubValsAndLinkedIds()
177   {
178     return new Object[][] { {
179         "--debug --append=[hi]test/jalview/bin/argparser/testfiles/test1.fa",
180         "JALVIEW:0", Arg.APPEND, "hi", "true", true },
181         { "--append[linkedId1]=[new,hello=world,1]test/jalview/bin/argparser/testfiles/test1.fa --headless",
182             "linkedId1", Arg.APPEND, "new", "true", true },
183         { "--append[linkedId2]=[new,hello=world,1]test/jalview/bin/argparser/testfiles/test1.fa --headless",
184             "linkedId2", Arg.APPEND, "hello", "world", true },
185         { "--append[linkedId3]=[new,hello=world,1]test/jalview/bin/argparser/testfiles/test1.fa --headless",
186             "linkedId3", Arg.APPEND, "GETINDEX", "1", true },
187         { "--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",
188             "linkedId5", Arg.APPEND, "new", "true", false },
189         { "--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",
190             "linkedId5", Arg.APPEND, "hello", "world", false },
191         { "--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",
192             "linkedId7", Arg.APPEND, "GETINDEX", "0", false }, };
193   }
194
195   @DataProvider(name = "argAutoIndexAndSubstitutions")
196   public Object[][] argAutoIndexAndSubstitutions()
197   {
198     return new Object[][] {
199         //
200         /*
201          */
202         { "--append=filename0 --append=filename1", "JALVIEW:0", Arg.APPEND,
203             "filename0" },
204         { "--append=filename0 --newframe --append=filename1", "JALVIEW:1",
205             Arg.APPEND, "filename1" },
206         { "--append=filename0 --newframe --newframe --append=filename2",
207             "JALVIEW:0", Arg.APPEND, "filename0" },
208         { "--append=filename0 --newframe --newframe --append=filename2",
209             "JALVIEW:2", Arg.APPEND, "filename2" },
210         { "--append[linkA-{n}]=filenameA0 --append[linkA-{++n}]=filenameA1",
211             "linkA-0", Arg.APPEND, "filenameA0" },
212         { "--append[linkB-{n}]=filenameB0 --append[linkB-{++n}]=filenameB1",
213             "linkB-1", Arg.APPEND, "filenameB1" },
214         { "--append[linkC-{n}]=filenameC0 --image[linkC-{n}]=outputC{n}.txt",
215             "linkC-0", Arg.IMAGE, "outputC{n}.txt" },
216         { "--append[linkD-{n}]=filenameD0 --substitutions --image[linkD-{n}]=outputD{n}.txt",
217             "linkD-0", Arg.IMAGE, "outputD0.txt" },
218         { "--append[linkE-{n}]=filenameE0 --substitutions --image[linkE-{n}]=output-E{n}.txt --nil[{++n}] --image[linkE-{n}]=outputE{n}.txt",
219             "linkE-0", Arg.IMAGE, "output-E0.txt" },
220         { "--append[linkF-{n}]=filenameF0 --substitutions --image[linkF-{n}]=output-F{n}.txt --nil[{++n}] --image[linkF-{n}]=outputF{n}.txt",
221             "linkF-1", Arg.IMAGE, "outputF1.txt" },
222         { "--append[linkG-{n}]=filenameG0 --substitutions --image[linkG-{n}]=output-G{n}.txt --nil[{++n}] --nosubstitutions --image[linkG-{n}]=outputG{n}.txt",
223             "linkG-1", Arg.IMAGE, "outputG{n}.txt" },
224         { "--append[linkH-{n}]=filenameH0 --substitutions --image[linkH-{n}]=output-H{n}.txt --nil[{++n}] --nosubstitutions --image[linkH-{n}]=outputH{n}.txt",
225             "linkH-0", Arg.IMAGE, "output-H0.txt" },
226         { "--open=filename0 --append=filename1", "JALVIEW:1", Arg.OPEN,
227             "filename0" },
228         { "--open=filename0 --newframe --append=filename1", "JALVIEW:2",
229             Arg.APPEND, "filename1" },
230         { "--open=filename0 --newframe --newframe --append=filename2",
231             "JALVIEW:1", Arg.OPEN, "filename0" },
232         { "--open=filename0 --newframe --newframe --append=filename2",
233             "JALVIEW:3", Arg.APPEND, "filename2" },
234         { "--open[linkA-{n}]=filenameA0 --append[linkA-{++n}]=filenameA1",
235             "linkA-0", Arg.OPEN, "filenameA0" },
236         { "--open[linkB-{n}]=filenameB0 --append[linkB-{++n}]=filenameB1",
237             "linkB-1", Arg.APPEND, "filenameB1" },
238         { "--open[linkC-{n}]=filenameC0 --image[linkC-{n}]=outputC{n}.txt",
239             "linkC-0", Arg.IMAGE, "outputC{n}.txt" },
240         { "--open[linkD-{n}]=filenameD0 --substitutions --image[linkD-{n}]=outputD{n}.txt",
241             "linkD-0", Arg.IMAGE, "outputD0.txt" },
242         { "--open[linkE-{n}]=filenameE0 --substitutions --image[linkE-{n}]=output-E{n}.txt --nil[{++n}] --image[linkE-{n}]=outputE{n}.txt",
243             "linkE-0", Arg.IMAGE, "output-E0.txt" },
244         { "--open[linkF-{n}]=filenameF0 --substitutions --image[linkF-{n}]=output-F{n}.txt --nil[{++n}] --image[linkF-{n}]=outputF{n}.txt",
245             "linkF-1", Arg.IMAGE, "outputF1.txt" },
246         { "--open[linkG-{n}]=filenameG0 --substitutions --image[linkG-{n}]=output-G{n}.txt --nil[{++n}] --nosubstitutions --image[linkG-{n}]=outputG{n}.txt",
247             "linkG-1", Arg.IMAGE, "outputG{n}.txt" },
248         { "--open[linkH-{n}]=filenameH0 --substitutions --image[linkH-{n}]=output-H{n}.txt --nil[{++n}] --nosubstitutions --image[linkH-{n}]=outputH{n}.txt",
249             "linkH-0", Arg.IMAGE, "output-H0.txt" },
250
251         //
252     };
253   }
254
255   @DataProvider(name = "argFiles")
256   public Object[][] argFiles()
257   {
258     return new Object[][] { {
259         "--argfile=test/jalview/bin/argparser/testfiles/argfile0.txt --open=shouldntbeabootstrap",
260         Arg.ARGFILE, "test/jalview/bin/argfiles/testfiles/test1.fa" } };
261   }
262 }