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