JAL-629 More extensive tests with real files being opened
[jalview.git] / test / jalview / bin / CommandsTest.java
1 package jalview.bin;
2
3 import java.util.Date;
4 import java.util.HashSet;
5 import java.util.Set;
6
7 import org.testng.Assert;
8 import org.testng.annotations.AfterClass;
9 import org.testng.annotations.AfterMethod;
10 import org.testng.annotations.BeforeClass;
11 import org.testng.annotations.DataProvider;
12 import org.testng.annotations.Test;
13
14 import jalview.gui.AlignFrame;
15 import jalview.gui.Desktop;
16 import jalview.gui.JvOptionPane;
17 import jalview.util.ArrayUtils;
18
19 @Test(singleThreaded = true)
20 public class CommandsTest
21 {
22   @BeforeClass(alwaysRun = true)
23   public static void setUpBeforeClass() throws Exception
24   {
25     Cache.loadProperties("test/jalview/gui/quitProps.jvprops");
26     Date oneHourFromNow = new Date(
27             System.currentTimeMillis() + 3600 * 1000);
28     Cache.setDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED", oneHourFromNow);
29   }
30
31   @AfterClass(alwaysRun = true)
32   public static void resetProps()
33   {
34     Cache.loadProperties("test/jalview/testProps.jvprops");
35   }
36
37   @BeforeClass(alwaysRun = true)
38   public void setUpJvOptionPane()
39   {
40     JvOptionPane.setInteractiveMode(false);
41     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
42   }
43
44   @AfterMethod(alwaysRun = true)
45   public void tearDown()
46   {
47     if (Desktop.instance != null)
48       Desktop.instance.closeAll_actionPerformed(null);
49   }
50
51   @Test(groups = "Functional", dataProvider = "cmdLines")
52   public void commandsOpenTest(String cmdLine, boolean cmdArgs,
53           int numFrames, String[] sequences)
54   {
55     String[] args = cmdLine.split("\\s+");
56     Jalview.main(args);
57     Commands cmds = Jalview.getInstance().getCommands();
58     Assert.assertNotNull(cmds);
59     Assert.assertEquals(cmds.commandArgsProvided(), cmdArgs,
60             "Commands were not provided in the args");
61     Assert.assertEquals(cmds.argsWereParsed(), cmdArgs,
62             "Overall command parse and operation is false");
63
64     Assert.assertEquals(Desktop.getAlignFrames().length, numFrames);
65
66     if (sequences != null)
67     {
68       Set<String> openedSequenceNames = new HashSet<>();
69       AlignFrame[] afs = Desktop.getAlignFrames();
70       for (AlignFrame af : afs)
71       {
72         openedSequenceNames
73                 .addAll(af.getViewport().getAlignment().getSequenceNames());
74       }
75       for (String sequence : sequences)
76       {
77         Assert.assertTrue(openedSequenceNames.contains(sequence),
78                 "Sequence '" + sequence
79                         + "' was not found in opened alignment files: "
80                         + cmdLine + ".\nOpened sequence names are:\n"
81                         + String.join("\n", openedSequenceNames));
82       }
83     }
84
85     Assert.assertFalse(
86             lookForSequenceName("THIS_SEQUENCE_ID_DOESN'T_EXIST"));
87   }
88
89   @DataProvider(name = "cmdLines")
90   public Object[][] cmdLines()
91   {
92     String[] someUniref50Seqs = new String[] { "FER_CAPAA", "FER_CAPAN",
93         "FER1_MAIZE", "FER1_SPIOL", "O80429_MAIZE" };
94     String[] t1 = new String[] { "TEST1" };
95     String[] t2 = new String[] { "TEST2" };
96     String[] t3 = new String[] { "TEST3" };
97     return new Object[][] {
98         { "--open=examples/uniref50.fa", true, 1, someUniref50Seqs },
99         { "--open examples/uniref50.fa", true, 1, someUniref50Seqs },
100         { "--open=examples/uniref50*.fa", true, 1, someUniref50Seqs },
101         // NOTE we cannot use shell expansion in tests, so list all files!
102         { "--open examples/uniref50.fa examples/uniref50_mz.fa", true, 1,
103             someUniref50Seqs },
104         { "--open=[new]examples/uniref50*.fa", true, 2, someUniref50Seqs },
105         { "--opennew=examples/uniref50*.fa", true, 2, someUniref50Seqs },
106         { "examples/uniref50.fa", true, 1, someUniref50Seqs },
107         { "examples/uniref50.fa test/jalview/bin/argparser/testfiles/test1.fa",
108             true, 2, ArrayUtils.concatArrays(someUniref50Seqs, t1) },
109         { "examples/uniref50.fa test/jalview/bin/argparser/testfiles/test1.fa",
110             true, 2, t1 },
111         { "--argfile=test/jalview/bin/argparser/testfiles/argfile0.txt",
112             true, 1, ArrayUtils.concatArrays(t1, t3) },
113         { "--argfile=test/jalview/bin/argparser/testfiles/argfile*.txt",
114             true, 4, ArrayUtils.concatArrays(t1, t2, t3) },
115         { "--argfile=test/jalview/bin/argparser/testfiles/argfile.autocounter",
116             true, 3, ArrayUtils.concatArrays(t1, t2) } };
117   }
118
119   public static boolean lookForSequenceName(String sequenceName)
120   {
121     AlignFrame[] afs = Desktop.getAlignFrames();
122     for (AlignFrame af : afs)
123     {
124       for (String name : af.getViewport().getAlignment().getSequenceNames())
125       {
126         if (sequenceName.equals(name))
127         {
128           return true;
129         }
130       }
131     }
132     return false;
133   }
134
135 }