JAL-3130 specify module list when launching new Jalview for CLI tests
[jalview.git] / test / jalview / bin / CommandLineOperations.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.bin;
22
23 import static org.testng.Assert.assertNotNull;
24 import static org.testng.Assert.assertTrue;
25
26 import jalview.gui.JvOptionPane;
27
28 import java.io.BufferedReader;
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.InputStreamReader;
32 import java.util.ArrayList;
33
34 import org.testng.Assert;
35 import org.testng.FileAssert;
36 import org.testng.annotations.BeforeClass;
37 import org.testng.annotations.BeforeTest;
38 import org.testng.annotations.DataProvider;
39 import org.testng.annotations.Test;
40
41 import io.github.classgraph.ClassGraph;
42 import io.github.classgraph.ModuleRef;
43 import io.github.classgraph.ScanResult;
44
45 public class CommandLineOperations
46 {
47
48   @BeforeClass(alwaysRun = true)
49   public void setUpJvOptionPane()
50   {
51     JvOptionPane.setInteractiveMode(false);
52     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
53   }
54
55   private static final int TEST_TIMEOUT = 9000; // Note longer timeout needed
56                                                 // on
57                                                 // full test run than on
58                                                 // individual tests
59
60   private static final int SETUP_TIMEOUT = 9000;
61
62   private static final int MINFILESIZE_SMALL = 2096;
63
64   private static final int MINFILESIZE_BIG = 4096;
65
66   private ArrayList<String> successfulCMDs = new ArrayList<>();
67
68   /***
69    * from
70    * http://stackoverflow.com/questions/808276/how-to-add-a-timeout-value-when
71    * -using-javas-runtime-exec
72    * 
73    * @author jimp
74    * 
75    */
76   private static class Worker extends Thread
77   {
78     private final Process process;
79
80     private BufferedReader outputReader;
81
82     private BufferedReader errorReader;
83
84     private Integer exit;
85
86     private Worker(Process process)
87     {
88       this.process = process;
89     }
90
91     @Override
92     public void run()
93     {
94       try
95       {
96         exit = process.waitFor();
97       } catch (InterruptedException ignore)
98       {
99         return;
100       }
101     }
102
103     public BufferedReader getOutputReader()
104     {
105       return outputReader;
106     }
107
108     public void setOutputReader(BufferedReader outputReader)
109     {
110       this.outputReader = outputReader;
111     }
112
113     public BufferedReader getErrorReader()
114     {
115       return errorReader;
116     }
117
118     public void setErrorReader(BufferedReader errorReader)
119     {
120       this.errorReader = errorReader;
121     }
122   }
123
124   private static ClassGraph scanner = null;
125
126   private static String classpath = null;
127
128   private static String modules = null;
129
130   public synchronized static String getClassPath()
131   {
132     if (scanner == null)
133     {
134       scanner = new ClassGraph();
135       ScanResult scan = scanner.scan();
136       classpath = scan.getClasspath();
137       modules = "";
138       for (ModuleRef mr : scan.getModules())
139       {
140         modules.concat(mr.getName());
141       }
142     }
143     while (classpath == null)
144     {
145       try
146       {
147         Thread.sleep(10);
148       } catch (InterruptedException x)
149       {
150
151       }
152     }
153     return classpath;
154   }
155
156   private Worker getJalviewDesktopRunner(boolean withAwt, String cmd,
157           int timeout)
158   {
159     // Note: JAL-3065 - don't include quotes for lib/* because the arguments are
160     // not expanded by the shell
161     String classpath = getClassPath();
162     String _cmd = "java " + (withAwt ? "-Djava.awt.headless=true" : "")
163             + " -classpath " + classpath
164             + (modules.length() > 2 ? "--add-modules=\"" + modules + "\""
165                     : "")
166             + " jalview.bin.Jalview ";
167     Process ls2_proc = null;
168     Worker worker = null;
169     try
170     {
171       ls2_proc = Runtime.getRuntime().exec(_cmd + cmd);
172     } catch (Throwable e1)
173     {
174       e1.printStackTrace();
175     }
176     if (ls2_proc != null)
177     {
178       BufferedReader outputReader = new BufferedReader(
179               new InputStreamReader(ls2_proc.getInputStream()));
180       BufferedReader errorReader = new BufferedReader(
181               new InputStreamReader(ls2_proc.getErrorStream()));
182       worker = new Worker(ls2_proc);
183       worker.start();
184       try
185       {
186         worker.join(timeout);
187       } catch (InterruptedException e)
188       {
189         System.err.println("Thread interrupted");
190       }
191       worker.setOutputReader(outputReader);
192       worker.setErrorReader(errorReader);
193     }
194     return worker;
195   }
196
197   @BeforeTest(alwaysRun = true)
198   public void initialize()
199   {
200     new CommandLineOperations();
201   }
202
203   @BeforeTest(alwaysRun = true)
204   public void setUpForHeadlessCommandLineInputOperations()
205           throws IOException
206   {
207     String cmds = "nodisplay -open examples/uniref50.fa -sortbytree -props test/jalview/io/testProps.jvprops -colour zappo "
208             + "-jabaws http://www.compbio.dundee.ac.uk/jabaws -nosortbytree "
209             + "-features examples/testdata/plantfdx.features -annotations examples/testdata/plantfdx.annotations -tree examples/testdata/uniref50_test_tree";
210     Worker worker = getJalviewDesktopRunner(true, cmds, SETUP_TIMEOUT);
211     String ln = null;
212     while ((ln = worker.getOutputReader().readLine()) != null)
213     {
214       System.out.println(ln);
215       successfulCMDs.add(ln);
216     }
217     while ((ln = worker.getErrorReader().readLine()) != null)
218     {
219       System.err.println(ln);
220     }
221   }
222
223   @BeforeTest(alwaysRun = true)
224   public void setUpForCommandLineInputOperations() throws IOException
225   {
226     String cmds = "-open examples/uniref50.fa -noquestionnaire -nousagestats";
227     Worker worker = getJalviewDesktopRunner(false, cmds, SETUP_TIMEOUT);
228     String ln = null;
229     int count = 0;
230     while ((ln = worker.getErrorReader().readLine()) != null)
231     {
232       System.out.println(ln);
233       successfulCMDs.add(ln);
234       if (++count > 5)
235       {
236         break;
237       }
238     }
239     if (worker != null && worker.exit == null)
240     {
241       worker.interrupt();
242       Thread.currentThread().interrupt();
243       worker.process.destroy();
244     }
245   }
246
247   @Test(groups = { "Functional" }, dataProvider = "allInputOperationsData")
248   public void testAllInputOperations(String expectedString,
249           String failureMsg)
250   {
251     Assert.assertTrue(successfulCMDs.contains(expectedString), failureMsg);
252   }
253
254   @Test(
255     groups =
256     { "Functional", "testben" },
257     dataProvider = "headlessModeOutputOperationsData")
258   public void testHeadlessModeOutputOperations(String harg, String type,
259           String fileName, boolean withAWT, int expectedMinFileSize,
260           int timeout)
261   {
262     String cmd = harg + type + " " + fileName;
263     // System.out.println(">>>>>>>>>>>>>>>> Command : " + cmd);
264     File file = new File(fileName);
265     file.deleteOnExit();
266     Worker worker = getJalviewDesktopRunner(withAWT, cmd, timeout);
267     assertNotNull(worker, "worker is null");
268     String msg = "Didn't create an output" + type + " file.[" + harg + "]";
269     assertTrue(file.exists(), msg);
270     FileAssert.assertFile(file, msg);
271     FileAssert.assertMinLength(file, expectedMinFileSize);
272     if (worker != null && worker.exit == null)
273     {
274       worker.interrupt();
275       Thread.currentThread().interrupt();
276       worker.process.destroy();
277       Assert.fail("Jalview did not exit after " + type
278               + " generation (try running test again to verify - timeout at "
279               + timeout + "ms). [" + harg + "]");
280     }
281     file.delete();
282   }
283
284   @DataProvider(name = "allInputOperationsData")
285   public Object[][] getHeadlessModeInputParams()
286   {
287     return new Object[][] {
288         // headless mode input operations
289         { "CMD [-color zappo] executed successfully!",
290             "Failed command : -color zappo" },
291         { "CMD [-props test/jalview/io/testProps.jvprops] executed successfully!",
292             "Failed command : -props File" },
293         { "CMD [-sortbytree] executed successfully!",
294             "Failed command : -sortbytree" },
295         { "CMD [-jabaws http://www.compbio.dundee.ac.uk/jabaws] executed successfully!",
296             "Failed command : -jabaws http://www.compbio.dundee.ac.uk/jabaws" },
297         { "CMD [-open examples/uniref50.fa] executed successfully!",
298             "Failed command : -open examples/uniref50.fa" },
299         { "CMD [-nosortbytree] executed successfully!",
300             "Failed command : -nosortbytree" },
301         { "CMD [-features examples/testdata/plantfdx.features]  executed successfully!",
302             "Failed command : -features examples/testdata/plantfdx.features" },
303         { "CMD [-annotations examples/testdata/plantfdx.annotations] executed successfully!",
304             "Failed command : -annotations examples/testdata/plantfdx.annotations" },
305         { "CMD [-tree examples/testdata/uniref50_test_tree] executed successfully!",
306             "Failed command : -tree examples/testdata/uniref50_test_tree" },
307         // non headless mode input operations
308         { "CMD [-nousagestats] executed successfully!",
309             "Failed command : -nousagestats" },
310         { "CMD [-noquestionnaire] executed successfully!",
311             "Failed command : -noquestionnaire" } };
312   }
313
314   @DataProvider(name = "headlessModeOutputOperationsData")
315   public static Object[][] getHeadlessModeOutputParams()
316   {
317     return new Object[][] { { "nodisplay -open examples/uniref50.fa",
318         " -eps", "test/jalview/bin/test_uniref50_out.eps", true,
319         MINFILESIZE_BIG, TEST_TIMEOUT },
320         { "nodisplay -open examples/uniref50.fa", " -eps",
321             "test/jalview/bin/test_uniref50_out.eps", false,
322             MINFILESIZE_BIG, TEST_TIMEOUT },
323         { "nogui -open examples/uniref50.fa", " -eps",
324             "test/jalview/bin/test_uniref50_out.eps", true, MINFILESIZE_BIG,
325             TEST_TIMEOUT },
326         { "nogui -open examples/uniref50.fa", " -eps",
327             "test/jalview/bin/test_uniref50_out.eps", false,
328             MINFILESIZE_BIG, TEST_TIMEOUT },
329         { "headless -open examples/uniref50.fa", " -eps",
330             "test/jalview/bin/test_uniref50_out.eps", true, MINFILESIZE_BIG,
331             TEST_TIMEOUT },
332         { "headless -open examples/uniref50.fa", " -svg",
333             "test/jalview/bin/test_uniref50_out.svg", false,
334             MINFILESIZE_BIG, TEST_TIMEOUT },
335         { "headless -open examples/uniref50.fa", " -png",
336             "test/jalview/bin/test_uniref50_out.png", true, MINFILESIZE_BIG,
337             TEST_TIMEOUT },
338         { "headless -open examples/uniref50.fa", " -html",
339             "test/jalview/bin/test_uniref50_out.html", true,
340             MINFILESIZE_BIG, TEST_TIMEOUT },
341         { "headless -open examples/uniref50.fa", " -fasta",
342             "test_uniref50_out.mfa", true, MINFILESIZE_SMALL,
343             TEST_TIMEOUT },
344         { "headless -open examples/uniref50.fa", " -clustal",
345             "test_uniref50_out.aln", true, MINFILESIZE_SMALL,
346             TEST_TIMEOUT },
347         { "headless -open examples/uniref50.fa", " -msf",
348             "test_uniref50_out.msf", true, MINFILESIZE_SMALL,
349             TEST_TIMEOUT },
350         { "headless -open examples/uniref50.fa", " -pileup",
351             "test_uniref50_out.aln", true, MINFILESIZE_SMALL,
352             TEST_TIMEOUT },
353         { "headless -open examples/uniref50.fa", " -pir",
354             "test_uniref50_out.pir", true, MINFILESIZE_SMALL,
355             TEST_TIMEOUT },
356         { "headless -open examples/uniref50.fa", " -pfam",
357             "test_uniref50_out.pfam", true, MINFILESIZE_SMALL,
358             TEST_TIMEOUT },
359         { "headless -open examples/uniref50.fa", " -blc",
360             "test_uniref50_out.blc", true, MINFILESIZE_SMALL,
361             TEST_TIMEOUT },
362         { "headless -open examples/uniref50.fa", " -jalview",
363             "test_uniref50_out.jvp", true, MINFILESIZE_SMALL,
364             TEST_TIMEOUT }, };
365   }
366 }