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