JAL-629 real life test for --allframes
[jalview.git] / test / jalview / bin / CommandsTest.java
1 package jalview.bin;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.nio.file.Files;
6 import java.util.Date;
7 import java.util.HashSet;
8 import java.util.Set;
9
10 import org.testng.Assert;
11 import org.testng.annotations.AfterClass;
12 import org.testng.annotations.AfterMethod;
13 import org.testng.annotations.BeforeClass;
14 import org.testng.annotations.DataProvider;
15 import org.testng.annotations.Test;
16
17 import jalview.bin.argparser.Arg;
18 import jalview.gui.AlignFrame;
19 import jalview.gui.Desktop;
20 import jalview.gui.JvOptionPane;
21 import jalview.util.ArrayUtils;
22
23 @Test
24 public class CommandsTest
25 {
26   private static final String testfiles = "test/jalview/bin/argparser/testfiles";
27
28   private static final String png1 = testfiles + "/dir1/test1.png";
29
30   private static final String png2 = testfiles + "/dir2/test1.png";
31
32   @BeforeClass(alwaysRun = true)
33   public static void setUpBeforeClass() throws Exception
34   {
35     Cache.loadProperties("test/jalview/gui/quitProps.jvprops");
36     Date oneHourFromNow = new Date(
37             System.currentTimeMillis() + 3600 * 1000);
38     Cache.setDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED", oneHourFromNow);
39   }
40
41   @AfterClass(alwaysRun = true)
42   public static void resetProps()
43   {
44     Cache.loadProperties("test/jalview/testProps.jvprops");
45   }
46
47   @BeforeClass(alwaysRun = true)
48   public void setUpJvOptionPane()
49   {
50     JvOptionPane.setInteractiveMode(false);
51     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
52   }
53
54   @AfterMethod(alwaysRun = true)
55   public void tearDown()
56   {
57     if (Desktop.instance != null)
58       Desktop.instance.closeAll_actionPerformed(null);
59   }
60
61   /* --setprops is currently disabled so this test won't work
62   @Test(groups = "Functional")
63   public void setpropsTest()
64   {
65     final String MOSTLY_HARMLESS = "MOSTLY_HARMLESS";
66     String cmdLine = "--setprop=" + MOSTLY_HARMLESS + "=Earth";
67     String[] args = cmdLine.split("\\s+");
68     Jalview.main(args);
69     Assert.assertEquals(Cache.getDefault(MOSTLY_HARMLESS, "Magrathea"),
70             "Earth");
71   }
72   */
73
74   @Test(groups = "Functional", dataProvider = "cmdLines")
75   public void commandsOpenTest(String cmdLine, boolean cmdArgs,
76           int numFrames, String[] sequences)
77   {
78     String[] args = cmdLine.split("\\s+");
79     Jalview.main(args);
80     Commands cmds = Jalview.getInstance().getCommands();
81     Assert.assertNotNull(cmds);
82     Assert.assertEquals(cmds.commandArgsProvided(), cmdArgs,
83             "Commands were not provided in the args");
84     Assert.assertEquals(cmds.argsWereParsed(), cmdArgs,
85             "Overall command parse and operation is false");
86
87     Assert.assertEquals(Desktop.getAlignFrames().length, numFrames,
88             "Wrong number of AlignFrames");
89
90     if (sequences != null)
91     {
92       Set<String> openedSequenceNames = new HashSet<>();
93       AlignFrame[] afs = Desktop.getAlignFrames();
94       for (AlignFrame af : afs)
95       {
96         openedSequenceNames
97                 .addAll(af.getViewport().getAlignment().getSequenceNames());
98       }
99       for (String sequence : sequences)
100       {
101         Assert.assertTrue(openedSequenceNames.contains(sequence),
102                 "Sequence '" + sequence
103                         + "' was not found in opened alignment files: "
104                         + cmdLine + ".\nOpened sequence names are:\n"
105                         + String.join("\n", openedSequenceNames));
106       }
107     }
108
109     Assert.assertFalse(
110             lookForSequenceName("THIS_SEQUENCE_ID_DOESN'T_EXIST"));
111   }
112
113   @Test(groups = "Functional", dataProvider = "argfileOutputFiles")
114   public void argFilesGlobAndSubstitutionsTest(String cmdLine,
115           String[] filenames) throws IOException
116   {
117     cleanupFiles(filenames);
118     String[] args = cmdLine.split("\\s+");
119     Jalview.main(args);
120     Commands cmds = Jalview.getInstance().getCommands();
121     Assert.assertNotNull(cmds);
122     File lastFile = null;
123     for (String filename : filenames)
124     {
125       File file = new File(filename);
126       Assert.assertTrue(file.exists(), "File '" + filename
127               + "' was not created by '" + cmdLine + "'");
128       Assert.assertTrue(file.isFile(), "File '" + filename
129               + "' is not a file from '" + cmdLine + "'");
130       Assert.assertTrue(Files.size(file.toPath()) > 0, "File '" + filename
131               + "' has no content from '" + cmdLine + "'");
132       // make sure the successive output files get bigger!
133       if (lastFile != null)
134         Assert.assertTrue(
135                 Files.size(file.toPath()) > Files.size(lastFile.toPath()));
136     }
137     cleanupFiles(filenames);
138     tearDown();
139   }
140
141   @DataProvider(name = "argfileOutputFiles")
142   public Object[][] argfileOutputFiles()
143   {
144     return new Object[][] {
145         { "--argfile=" + testfiles + "/**/*.txt", new String[]
146         { testfiles + "/dir1/test1.png", testfiles + "/dir2/test1.png",
147             testfiles + "/dir3/subdir/test0.png" } },
148         { "--argfile=" + testfiles + "/**/argfile.txt", new String[]
149         { testfiles + "/dir1/test1.png", testfiles + "/dir2/test1.png" } },
150         { "--argfile=" + testfiles + "/dir*/argfile.txt", new String[]
151         { testfiles + "/dir1/test1.png", testfiles + "/dir2/test1.png" } },
152         { "--initsubstitutions --append examples/uniref50.fa --image "
153                 + testfiles + "/{basename}.png",
154             new String[]
155             { testfiles + "/uniref50.png" } },
156         { "--append examples/uniref50.fa --image " + testfiles
157                 + "/{basename}.png",
158             new String[]
159             { testfiles + "/{basename}.png" } } };
160   }
161
162   @DataProvider(name = "cmdLines")
163   public Object[][] cmdLines()
164   {
165     String[] someUniref50Seqs = new String[] { "FER_CAPAA", "FER_CAPAN",
166         "FER1_MAIZE", "FER1_SPIOL", "O80429_MAIZE" };
167     String[] t1 = new String[] { "TEST1" };
168     String[] t2 = new String[] { "TEST2" };
169     String[] t3 = new String[] { "TEST3" };
170     return new Object[][] {
171         /*
172         */
173         { "--append=examples/uniref50.fa", true, 1, someUniref50Seqs },
174         { "--append examples/uniref50.fa", true, 1, someUniref50Seqs },
175         { "--append=examples/uniref50*.fa", true, 1, someUniref50Seqs },
176         // NOTE we cannot use shell expansion in tests, so list all files!
177         { "--append examples/uniref50.fa examples/uniref50_mz.fa", true, 1,
178             someUniref50Seqs },
179         { "--append=[new]examples/uniref50*.fa", true, 2,
180             someUniref50Seqs },
181         { "--open=examples/uniref50*.fa", true, 2, someUniref50Seqs },
182         { "examples/uniref50.fa", true, 1, someUniref50Seqs },
183         { "examples/uniref50.fa " + testfiles + "/test1.fa", true, 2,
184             ArrayUtils.concatArrays(someUniref50Seqs, t1) },
185         { "examples/uniref50.fa " + testfiles + "/test1.fa", true, 2, t1 },
186         { "--argfile=" + testfiles + "/argfile0.txt", true, 1,
187             ArrayUtils.concatArrays(t1, t3) },
188         { "--argfile=" + testfiles + "/argfile*.txt", true, 5,
189             ArrayUtils.concatArrays(t1, t2, t3) },
190         { "--argfile=" + testfiles + "/argfile.autocounter", true, 3,
191             ArrayUtils.concatArrays(t1, t2) } };
192
193   }
194
195   public static boolean lookForSequenceName(String sequenceName)
196   {
197     AlignFrame[] afs = Desktop.getAlignFrames();
198     for (AlignFrame af : afs)
199     {
200       for (String name : af.getViewport().getAlignment().getSequenceNames())
201       {
202         if (sequenceName.equals(name))
203         {
204           return true;
205         }
206       }
207     }
208     return false;
209   }
210
211   public static void cleanupFiles(String[] filenames)
212   {
213     for (String filename : filenames)
214     {
215       File file = new File(filename);
216       if (file.exists())
217       {
218         file.delete();
219       }
220     }
221   }
222
223   @Test(groups = "Functional", dataProvider = "allLinkedIdsData")
224   public void allLinkedIdsTest(String cmdLine, Arg a, String[] filenames)
225   {
226     String[] args = cmdLine.split("\\s+");
227     Jalview.main(args);
228     Commands cmds = Jalview.getInstance().getCommands();
229     Assert.assertNotNull(cmds);
230     for (String filename : filenames)
231     {
232       Assert.assertTrue(new File(filename).exists(),
233               "File '" + filename + "' was not created");
234     }
235     cleanupFiles(filenames);
236   }
237
238   @DataProvider(name = "allLinkedIdsData")
239   public Object[][] allLinkedIdsData()
240   {
241     return new Object[][] {
242         //
243         /*
244         { "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --allframes --output={dirname}/{basename}.stk --close",
245             Arg.OUTPUT, new String[]
246             { "test/jalview/bin/argparser/testfiles/test1.stk",
247                 "test/jalview/bin/argparser/testfiles/test2.stk",
248                 "test/jalview/bin/argparser/testfiles/test3.stk", } },
249         */
250         { "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --allframes --image={dirname}/{basename}.png --close",
251             Arg.IMAGE, new String[]
252             { "test/jalview/bin/argparser/testfiles/test1.png",
253                 "test/jalview/bin/argparser/testfiles/test2.png",
254                 "test/jalview/bin/argparser/testfiles/test3.png", } },
255         //
256     };
257   }
258
259 }