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