4487178acec4dea58d85154901c24ee2401ecb13
[jalview.git] / test / jalview / bin / ArgParserTest.java
1 package jalview.bin;
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.argparser.Arg;
14 import jalview.bin.argparser.ArgParser;
15 import jalview.bin.argparser.BootstrapArgs;
16
17 @Test(singleThreaded = true)
18 public class ArgParserTest
19 {
20   @AfterClass(alwaysRun = true)
21   public static void resetProps()
22   {
23     Cache.applicationProperties.clear();
24     Cache.loadProperties("test/jalview/testProps.jvprops");
25   }
26
27   @Test(groups = "Functional", dataProvider = "argLines")
28   public void parseArgsAndSubValsTest(String commandLineArgs, Arg a,
29           String other)
30   {
31     String[] args = commandLineArgs.split("\\s*");
32     ArgParser argparser = new ArgParser(args);
33   }
34
35   @Test(groups = "Functional", dataProvider = "argLines")
36   public void bootstrapArgsTest(String commandLineArgs, Arg a, String other)
37   {
38     String[] args = commandLineArgs.split("\\s+");
39     BootstrapArgs b = BootstrapArgs.getBootstrapArgs(args);
40
41     Assert.assertTrue(b.contains(a));
42     if (a == Arg.PROPS)
43     {
44       Properties bP = Cache.bootstrapProperties(b.get(Arg.PROPS));
45       Assert.assertTrue(other.equals(bP.get(Cache.BOOTSTRAP_TEST)));
46       Assert.assertFalse(bP.contains("NOT" + Cache.BOOTSTRAP_TEST));
47     }
48     else if (a == Arg.ARGFILE)
49     {
50       List<String> filenames = b.getList(a);
51       boolean found = false;
52       for (String s : filenames)
53       {
54         File f = new File(s);
55         File fo = new File(other);
56         try
57         {
58           if (fo.getCanonicalPath().equals(f.getCanonicalPath()))
59           {
60             found = true;
61             break;
62           }
63         } catch (IOException e)
64         {
65         }
66       }
67       Assert.assertTrue(found,
68               "File '" + other + "' not found in shell expanded glob '"
69                       + commandLineArgs + "'");
70     }
71
72   }
73
74   @Test(groups = "Functional", dataProvider = "argFiles")
75   public void argFilesTest(String commandLineArgs, Arg a, String other)
76   {
77     String[] args = commandLineArgs.split("\\s+");
78     BootstrapArgs b = BootstrapArgs.getBootstrapArgs(args);
79
80     Assert.assertTrue(b.contains(a));
81     Assert.assertFalse(b.contains(Arg.OPEN));
82     if (a == Arg.PROPS)
83     {
84       Properties bP = Cache.bootstrapProperties(b.get(Arg.PROPS));
85       Assert.assertTrue("true".equals(bP.get(Cache.BOOTSTRAP_TEST)));
86     }
87   }
88
89   @DataProvider(name = "argLinesNotworking")
90   public Object[][] argLinesTest()
91   {
92     return new Object[][] {
93         // can't use this one yet as it doesn't get shell glob expanded by the
94         // test
95         { "--argfile test/jalview/bin/argparser/argfile*.txt", Arg.ARGFILE,
96             "test/jalview/bin/argparser/argfile0.txt" }, };
97   }
98
99   @DataProvider(name = "argLines")
100   public Object[][] argLines()
101   {
102     return new Object[][] {
103         { "--debug --open=test/jalview/bin/argparser/test1.fa", Arg.DEBUG,
104             null },
105         { "--open=test/jalview/bin/argparser/test1.fa --headless",
106             Arg.HEADLESS, null },
107         { "--open=test/jalview/bin/argparser/test1.fa --props=test/jalview/bin/argparser/testProps.jvprops",
108             Arg.PROPS, "true" },
109         { "--argfile test/jalview/bin/argparser/argfile0.txt", Arg.ARGFILE,
110             "test/jalview/bin/argparser/argfile0.txt" },
111         // these next three are what a shell glob expansion would look like
112         { "--argfile test/jalview/bin/argparser/argfile0.txt test/jalview/bin/argparser/argfile1.txt test/jalview/bin/argparser/argfile2.txt",
113             Arg.ARGFILE, "test/jalview/bin/argparser/argfile0.txt" },
114         { "--argfile test/jalview/bin/argparser/argfile0.txt test/jalview/bin/argparser/argfile1.txt test/jalview/bin/argparser/argfile2.txt",
115             Arg.ARGFILE, "test/jalview/bin/argparser/argfile1.txt" },
116         { "--argfile test/jalview/bin/argparser/argfile0.txt test/jalview/bin/argparser/argfile1.txt test/jalview/bin/argparser/argfile2.txt",
117             Arg.ARGFILE, "test/jalview/bin/argparser/argfile2.txt" },
118         { "--argfile=test/jalview/bin/argparser/argfile*.txt", Arg.ARGFILE,
119             "test/jalview/bin/argparser/argfile0.txt" },
120         { "--argfile=test/jalview/bin/argparser/argfile*.txt", Arg.ARGFILE,
121             "test/jalview/bin/argparser/argfile1.txt" },
122         { "--argfile=test/jalview/bin/argparser/argfile*.txt", Arg.ARGFILE,
123             "test/jalview/bin/argparser/argfile2.txt" } };
124
125   }
126
127   @DataProvider(name = "argFiles")
128   public Object[][] argFiles()
129   {
130     return new Object[][] { {
131         "--argfile=test/jalview/bin/argparser/argfile0.txt --open=shouldntbeabootstrap",
132         Arg.ARGFILE, "test/jalview/bin/argfiles/test1.fa" } };
133   }
134 }