JAL-629 extensive tests for ArgParser features
[jalview.git] / test / jalview / bin / CommandsTest.java
1 package jalview.bin;
2
3 import java.util.Date;
4
5 import org.testng.Assert;
6 import org.testng.annotations.AfterClass;
7 import org.testng.annotations.AfterMethod;
8 import org.testng.annotations.BeforeClass;
9 import org.testng.annotations.DataProvider;
10 import org.testng.annotations.Test;
11
12 import jalview.gui.AlignFrame;
13 import jalview.gui.Desktop;
14 import jalview.gui.JvOptionPane;
15
16 @Test(singleThreaded = true)
17 public class CommandsTest
18 {
19   @BeforeClass(alwaysRun = true)
20   public static void setUpBeforeClass() throws Exception
21   {
22     Cache.loadProperties("test/jalview/gui/quitProps.jvprops");
23     Date oneHourFromNow = new Date(
24             System.currentTimeMillis() + 3600 * 1000);
25     Cache.setDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED", oneHourFromNow);
26   }
27
28   @AfterClass(alwaysRun = true)
29   public static void resetProps()
30   {
31     Cache.loadProperties("test/jalview/testProps.jvprops");
32   }
33
34   @BeforeClass(alwaysRun = true)
35   public void setUpJvOptionPane()
36   {
37     JvOptionPane.setInteractiveMode(false);
38     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
39   }
40
41   @AfterMethod(alwaysRun = true)
42   public void tearDown()
43   {
44     if (Desktop.instance != null)
45       Desktop.instance.closeAll_actionPerformed(null);
46   }
47
48   @Test(groups = "Functional", dataProvider = "cmdLines")
49   public void commandsOpenTest(String cmdLine, String sequence)
50   {
51     String[] args = cmdLine.split("\\s+");
52     Jalview.main(args);
53     Commands cmds = Jalview.getInstance().getCommands();
54     Assert.assertNotNull(cmds);
55     Assert.assertTrue(cmds.commandArgsProvided(),
56             "Commands were not provided in the args");
57     Assert.assertTrue(cmds.argsWereParsed(),
58             "Overall command parse and operation is false");
59
60     if (sequence != null)
61     {
62       Assert.assertTrue(lookForSequenceName(sequence),
63               "Sequence '" + sequence
64                       + "' was not found in opened alignment files: "
65                       + cmdLine);
66     }
67   }
68
69   public static boolean lookForSequenceName(String sequenceName)
70   {
71     AlignFrame[] afs = Desktop.getAlignFrames();
72     for (AlignFrame af : afs)
73     {
74       for (String name : af.getViewport().getAlignment().getSequenceNames())
75       {
76         if (sequenceName.equals(name))
77         {
78           return true;
79         }
80       }
81     }
82     return false;
83   }
84
85   @DataProvider(name = "cmdLines")
86   public Object[][] cmdLines()
87   {
88     return new Object[][] {
89         { "--open=examples/uniref50.fa", "FER1_SPIOL" },
90         { "--argfile=test/jalview/bin/argparser/testfiles/argfile0.txt",
91             null } };
92   }
93
94   @DataProvider(name = "openingAlignments")
95   public Object[][] openingAlignments()
96   {
97     return new Object[][] {
98         { "--open=examples/uniref50.fa", 1 },
99         { "--argfile=test/jalview/bin/argparser/testfiles/argfile0.txt", } };
100   }
101
102 }