Reinstate imports from "JAL-1889 JAL-3130 patch broken tests" commit 05efc2ac3c6427a4...
[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 at '"
269             + file.getAbsolutePath() + "'.[" + harg + "]";
270     assertTrue(file.exists(), msg);
271     FileAssert.assertFile(file, msg);
272     FileAssert.assertMinLength(file, expectedMinFileSize);
273     if (worker != null && worker.exit == null)
274     {
275       worker.interrupt();
276       Thread.currentThread().interrupt();
277       worker.process.destroy();
278       Assert.fail("Jalview did not exit after " + type
279               + " generation (try running test again to verify - timeout at "
280               + timeout + "ms). [" + harg + "]");
281     }
282     file.delete();
283   }
284
285   @DataProvider(name = "allInputOperationsData")
286   public Object[][] getHeadlessModeInputParams()
287   {
288     return new Object[][] {
289         // headless mode input operations
290         { "CMD [-color zappo] executed successfully!",
291             "Failed command : -color zappo" },
292         { "CMD [-props test/jalview/io/testProps.jvprops] executed successfully!",
293             "Failed command : -props File" },
294         { "CMD [-sortbytree] executed successfully!",
295             "Failed command : -sortbytree" },
296         { "CMD [-jabaws http://www.compbio.dundee.ac.uk/jabaws] executed successfully!",
297             "Failed command : -jabaws http://www.compbio.dundee.ac.uk/jabaws" },
298         { "CMD [-open examples/uniref50.fa] executed successfully!",
299             "Failed command : -open examples/uniref50.fa" },
300         { "CMD [-nosortbytree] executed successfully!",
301             "Failed command : -nosortbytree" },
302         { "CMD [-features examples/testdata/plantfdx.features]  executed successfully!",
303             "Failed command : -features examples/testdata/plantfdx.features" },
304         { "CMD [-annotations examples/testdata/plantfdx.annotations] executed successfully!",
305             "Failed command : -annotations examples/testdata/plantfdx.annotations" },
306         { "CMD [-tree examples/testdata/uniref50_test_tree] executed successfully!",
307             "Failed command : -tree examples/testdata/uniref50_test_tree" },
308         // non headless mode input operations
309         { "CMD [-nousagestats] executed successfully!",
310             "Failed command : -nousagestats" },
311         { "CMD [-noquestionnaire] executed successfully!",
312             "Failed command : -noquestionnaire" } };
313   }
314
315   @DataProvider(name = "headlessModeOutputOperationsData")
316   public static Object[][] getHeadlessModeOutputParams()
317   {
318     // JBPNote: I'm not clear why need to specify full path for output file
319     // when running tests on build server, but we will keep this patch for now
320     // since it works.
321     // https://issues.jalview.org/browse/JAL-1889?focusedCommentId=21609&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-21609
322     String workingDir = "test/jalview/bin/";
323     return new Object[][] { { "nodisplay -open examples/uniref50.fa",
324         " -eps", workingDir + "test_uniref50_out.eps", true,
325         MINFILESIZE_BIG, TEST_TIMEOUT },
326         { "nodisplay -open examples/uniref50.fa", " -eps",
327             workingDir + "test_uniref50_out.eps", false,
328             MINFILESIZE_BIG, TEST_TIMEOUT },
329         { "nogui -open examples/uniref50.fa", " -eps",
330             workingDir + "test_uniref50_out.eps", true, MINFILESIZE_BIG,
331             TEST_TIMEOUT },
332         { "nogui -open examples/uniref50.fa", " -eps",
333             workingDir + "test_uniref50_out.eps", false,
334             MINFILESIZE_BIG, TEST_TIMEOUT },
335         { "headless -open examples/uniref50.fa", " -eps",
336             workingDir + "test_uniref50_out.eps", true, MINFILESIZE_BIG,
337             TEST_TIMEOUT },
338         { "headless -open examples/uniref50.fa", " -svg",
339             workingDir + "test_uniref50_out.svg", false,
340             MINFILESIZE_BIG, TEST_TIMEOUT },
341         { "headless -open examples/uniref50.fa", " -png",
342             workingDir + "test_uniref50_out.png", true, MINFILESIZE_BIG,
343             TEST_TIMEOUT },
344         { "headless -open examples/uniref50.fa", " -html",
345             workingDir + "test_uniref50_out.html", true,
346             MINFILESIZE_BIG, TEST_TIMEOUT },
347         { "headless -open examples/uniref50.fa", " -fasta",
348             workingDir + "test_uniref50_out.mfa", true, MINFILESIZE_SMALL,
349             TEST_TIMEOUT },
350         { "headless -open examples/uniref50.fa", " -clustal",
351             workingDir + "test_uniref50_out.aln", true, MINFILESIZE_SMALL,
352             TEST_TIMEOUT },
353         { "headless -open examples/uniref50.fa", " -msf",
354             workingDir + "test_uniref50_out.msf", true, MINFILESIZE_SMALL,
355             TEST_TIMEOUT },
356         { "headless -open examples/uniref50.fa", " -pileup",
357             workingDir + "test_uniref50_out.aln", true, MINFILESIZE_SMALL,
358             TEST_TIMEOUT },
359         { "headless -open examples/uniref50.fa", " -pir",
360             workingDir + "test_uniref50_out.pir", true, MINFILESIZE_SMALL,
361             TEST_TIMEOUT },
362         { "headless -open examples/uniref50.fa", " -pfam",
363             workingDir + "test_uniref50_out.pfam", true, MINFILESIZE_SMALL,
364             TEST_TIMEOUT },
365         { "headless -open examples/uniref50.fa", " -blc",
366             workingDir + "test_uniref50_out.blc", true, MINFILESIZE_SMALL,
367             TEST_TIMEOUT },
368         { "headless -open examples/uniref50.fa", " -jalview",
369             workingDir + "test_uniref50_out.jvp", true, MINFILESIZE_SMALL,
370             TEST_TIMEOUT }, };
371   }
372 }