JAL-3130 JAL-1889 report absolute paths for CommandLineTests
[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.nio.file.Path;
33 import java.nio.file.Paths;
34 import java.util.ArrayList;
35
36 import org.testng.Assert;
37 import org.testng.FileAssert;
38 import org.testng.annotations.BeforeClass;
39 import org.testng.annotations.BeforeTest;
40 import org.testng.annotations.DataProvider;
41 import org.testng.annotations.Test;
42
43 import io.github.classgraph.ClassGraph;
44 import io.github.classgraph.ModuleRef;
45 import io.github.classgraph.ScanResult;
46
47 public class CommandLineOperations
48 {
49
50   @BeforeClass(alwaysRun = true)
51   public void setUpJvOptionPane()
52   {
53     JvOptionPane.setInteractiveMode(false);
54     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
55   }
56
57   private static final int TEST_TIMEOUT = 9000; // Note longer timeout needed
58                                                 // on
59                                                 // full test run than on
60                                                 // individual tests
61
62   private static final int SETUP_TIMEOUT = 9000;
63
64   private static final int MINFILESIZE_SMALL = 2096;
65
66   private static final int MINFILESIZE_BIG = 4096;
67
68   private ArrayList<String> successfulCMDs = new ArrayList<>();
69
70   /***
71    * from
72    * http://stackoverflow.com/questions/808276/how-to-add-a-timeout-value-when
73    * -using-javas-runtime-exec
74    * 
75    * @author jimp
76    * 
77    */
78   private static class Worker extends Thread
79   {
80     private final Process process;
81
82     private BufferedReader outputReader;
83
84     private BufferedReader errorReader;
85
86     private Integer exit;
87
88     private Worker(Process process)
89     {
90       this.process = process;
91     }
92
93     @Override
94     public void run()
95     {
96       try
97       {
98         exit = process.waitFor();
99       } catch (InterruptedException ignore)
100       {
101         return;
102       }
103     }
104
105     public BufferedReader getOutputReader()
106     {
107       return outputReader;
108     }
109
110     public void setOutputReader(BufferedReader outputReader)
111     {
112       this.outputReader = outputReader;
113     }
114
115     public BufferedReader getErrorReader()
116     {
117       return errorReader;
118     }
119
120     public void setErrorReader(BufferedReader errorReader)
121     {
122       this.errorReader = errorReader;
123     }
124   }
125
126   private static ClassGraph scanner = null;
127
128   private static String classpath = null;
129
130   private static String modules = null;
131
132   public synchronized static String getClassPath()
133   {
134     if (scanner == null)
135     {
136       scanner = new ClassGraph();
137       ScanResult scan = scanner.scan();
138       classpath = scan.getClasspath();
139       modules = "";
140       for (ModuleRef mr : scan.getModules())
141       {
142         modules.concat(mr.getName());
143       }
144     }
145     while (classpath == null)
146     {
147       try
148       {
149         Thread.sleep(10);
150       } catch (InterruptedException x)
151       {
152
153       }
154     }
155     return classpath;
156   }
157
158   private Worker getJalviewDesktopRunner(boolean withAwt, String cmd,
159           int timeout)
160   {
161     // Note: JAL-3065 - don't include quotes for lib/* because the arguments are
162     // not expanded by the shell
163     String classpath = getClassPath();
164     String _cmd = "java " + (withAwt ? "-Djava.awt.headless=true" : "")
165             + " -classpath " + classpath
166             + (modules.length() > 2 ? "--add-modules=\"" + modules + "\""
167                     : "")
168             + " jalview.bin.Jalview ";
169     Process ls2_proc = null;
170     Worker worker = null;
171     try
172     {
173       ls2_proc = Runtime.getRuntime().exec(_cmd + cmd);
174     } catch (Throwable e1)
175     {
176       e1.printStackTrace();
177     }
178     if (ls2_proc != null)
179     {
180       BufferedReader outputReader = new BufferedReader(
181               new InputStreamReader(ls2_proc.getInputStream()));
182       BufferedReader errorReader = new BufferedReader(
183               new InputStreamReader(ls2_proc.getErrorStream()));
184       worker = new Worker(ls2_proc);
185       worker.start();
186       try
187       {
188         worker.join(timeout);
189       } catch (InterruptedException e)
190       {
191         System.err.println("Thread interrupted");
192       }
193       worker.setOutputReader(outputReader);
194       worker.setErrorReader(errorReader);
195     }
196     return worker;
197   }
198   @BeforeTest(alwaysRun=true)
199   public void reportCurrentWorkingDirectory()
200   {
201       try {
202           Path currentRelativePath = Paths.get("");
203           String s = currentRelativePath.toAbsolutePath().toString();
204           System.out.println("Test CWD is "+s);
205       } catch (Exception q) {
206           q.printStackTrace();
207       }
208   }
209   @BeforeTest(alwaysRun = true)
210   public void initialize()
211   {
212     new CommandLineOperations();
213   }
214
215   @BeforeTest(alwaysRun = true)
216   public void setUpForHeadlessCommandLineInputOperations()
217           throws IOException
218   {
219     String cmds = "nodisplay -open examples/uniref50.fa -sortbytree -props test/jalview/io/testProps.jvprops -colour zappo "
220             + "-jabaws http://www.compbio.dundee.ac.uk/jabaws -nosortbytree "
221             + "-features examples/testdata/plantfdx.features -annotations examples/testdata/plantfdx.annotations -tree examples/testdata/uniref50_test_tree";
222     Worker worker = getJalviewDesktopRunner(true, cmds, SETUP_TIMEOUT);
223     String ln = null;
224     while ((ln = worker.getOutputReader().readLine()) != null)
225     {
226       System.out.println(ln);
227       successfulCMDs.add(ln);
228     }
229     while ((ln = worker.getErrorReader().readLine()) != null)
230     {
231       System.err.println(ln);
232     }
233   }
234
235   @BeforeTest(alwaysRun = true)
236   public void setUpForCommandLineInputOperations() throws IOException
237   {
238     String cmds = "-open examples/uniref50.fa -noquestionnaire -nousagestats";
239     Worker worker = getJalviewDesktopRunner(false, cmds, SETUP_TIMEOUT);
240     String ln = null;
241     int count = 0;
242     while ((ln = worker.getErrorReader().readLine()) != null)
243     {
244       System.out.println(ln);
245       successfulCMDs.add(ln);
246       if (++count > 5)
247       {
248         break;
249       }
250     }
251     if (worker != null && worker.exit == null)
252     {
253       worker.interrupt();
254       Thread.currentThread().interrupt();
255       worker.process.destroy();
256     }
257   }
258
259   @Test(groups = { "Functional" }, dataProvider = "allInputOperationsData")
260   public void testAllInputOperations(String expectedString,
261           String failureMsg)
262   {
263     Assert.assertTrue(successfulCMDs.contains(expectedString), failureMsg);
264   }
265
266   @Test(
267     groups =
268     { "Functional", "testben" },
269     dataProvider = "headlessModeOutputOperationsData")
270   public void testHeadlessModeOutputOperations(String harg, String type,
271           String fileName, boolean withAWT, int expectedMinFileSize,
272           int timeout)
273   {
274     String cmd = harg + type + " " + fileName;
275     // System.out.println(">>>>>>>>>>>>>>>> Command : " + cmd);
276     File file = new File(fileName);
277     file.deleteOnExit();
278     Worker worker = getJalviewDesktopRunner(withAWT, cmd, timeout);
279     assertNotNull(worker, "worker is null");
280     String msg = "Didn't create an output" + type + " file.[" + harg + "]";
281     assertTrue(file.exists(), msg);
282     FileAssert.assertFile(file, msg);
283     FileAssert.assertMinLength(file, expectedMinFileSize);
284     if (worker != null && worker.exit == null)
285     {
286       worker.interrupt();
287       Thread.currentThread().interrupt();
288       worker.process.destroy();
289       Assert.fail("Jalview did not exit after " + type
290               + " generation (try running test again to verify - timeout at "
291               + timeout + "ms). [" + harg + "]");
292     }
293     file.delete();
294   }
295
296   @DataProvider(name = "allInputOperationsData")
297   public Object[][] getHeadlessModeInputParams()
298   {
299     return new Object[][] {
300         // headless mode input operations
301         { "CMD [-color zappo] executed successfully!",
302             "Failed command : -color zappo" },
303         { "CMD [-props test/jalview/io/testProps.jvprops] executed successfully!",
304             "Failed command : -props File" },
305         { "CMD [-sortbytree] executed successfully!",
306             "Failed command : -sortbytree" },
307         { "CMD [-jabaws http://www.compbio.dundee.ac.uk/jabaws] executed successfully!",
308             "Failed command : -jabaws http://www.compbio.dundee.ac.uk/jabaws" },
309         { "CMD [-open examples/uniref50.fa] executed successfully!",
310             "Failed command : -open examples/uniref50.fa" },
311         { "CMD [-nosortbytree] executed successfully!",
312             "Failed command : -nosortbytree" },
313         { "CMD [-features examples/testdata/plantfdx.features]  executed successfully!",
314             "Failed command : -features examples/testdata/plantfdx.features" },
315         { "CMD [-annotations examples/testdata/plantfdx.annotations] executed successfully!",
316             "Failed command : -annotations examples/testdata/plantfdx.annotations" },
317         { "CMD [-tree examples/testdata/uniref50_test_tree] executed successfully!",
318             "Failed command : -tree examples/testdata/uniref50_test_tree" },
319         // non headless mode input operations
320         { "CMD [-nousagestats] executed successfully!",
321             "Failed command : -nousagestats" },
322         { "CMD [-noquestionnaire] executed successfully!",
323             "Failed command : -noquestionnaire" } };
324   }
325
326   @DataProvider(name = "headlessModeOutputOperationsData")
327   public static Object[][] getHeadlessModeOutputParams()
328   {
329     // JBPNote: I'm not clear why need to specify full path for output file
330     // when running tests on build server, but we will keep this patch for now
331     // since it works.
332     // https://issues.jalview.org/browse/JAL-1889?focusedCommentId=21609&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-21609
333     String workingDir = "test/jalview/bin/";
334     return new Object[][] { { "nodisplay -open examples/uniref50.fa",
335         " -eps", workingDir + "test_uniref50_out.eps", true,
336         MINFILESIZE_BIG, TEST_TIMEOUT },
337         { "nodisplay -open examples/uniref50.fa", " -eps",
338             workingDir + "test_uniref50_out.eps", false,
339             MINFILESIZE_BIG, TEST_TIMEOUT },
340         { "nogui -open examples/uniref50.fa", " -eps",
341             workingDir + "test_uniref50_out.eps", true, MINFILESIZE_BIG,
342             TEST_TIMEOUT },
343         { "nogui -open examples/uniref50.fa", " -eps",
344             workingDir + "test_uniref50_out.eps", false,
345             MINFILESIZE_BIG, TEST_TIMEOUT },
346         { "headless -open examples/uniref50.fa", " -eps",
347             workingDir + "test_uniref50_out.eps", true, MINFILESIZE_BIG,
348             TEST_TIMEOUT },
349         { "headless -open examples/uniref50.fa", " -svg",
350             workingDir + "test_uniref50_out.svg", false,
351             MINFILESIZE_BIG, TEST_TIMEOUT },
352         { "headless -open examples/uniref50.fa", " -png",
353             workingDir + "test_uniref50_out.png", true, MINFILESIZE_BIG,
354             TEST_TIMEOUT },
355         { "headless -open examples/uniref50.fa", " -html",
356             workingDir + "test_uniref50_out.html", true,
357             MINFILESIZE_BIG, TEST_TIMEOUT },
358         { "headless -open examples/uniref50.fa", " -fasta",
359             workingDir + "test_uniref50_out.mfa", true, MINFILESIZE_SMALL,
360             TEST_TIMEOUT },
361         { "headless -open examples/uniref50.fa", " -clustal",
362             workingDir + "test_uniref50_out.aln", true, MINFILESIZE_SMALL,
363             TEST_TIMEOUT },
364         { "headless -open examples/uniref50.fa", " -msf",
365             workingDir + "test_uniref50_out.msf", true, MINFILESIZE_SMALL,
366             TEST_TIMEOUT },
367         { "headless -open examples/uniref50.fa", " -pileup",
368             workingDir + "test_uniref50_out.aln", true, MINFILESIZE_SMALL,
369             TEST_TIMEOUT },
370         { "headless -open examples/uniref50.fa", " -pir",
371             workingDir + "test_uniref50_out.pir", true, MINFILESIZE_SMALL,
372             TEST_TIMEOUT },
373         { "headless -open examples/uniref50.fa", " -pfam",
374             workingDir + "test_uniref50_out.pfam", true, MINFILESIZE_SMALL,
375             TEST_TIMEOUT },
376         { "headless -open examples/uniref50.fa", " -blc",
377             workingDir + "test_uniref50_out.blc", true, MINFILESIZE_SMALL,
378             TEST_TIMEOUT },
379         { "headless -open examples/uniref50.fa", " -jalview",
380             workingDir + "test_uniref50_out.jvp", true, MINFILESIZE_SMALL,
381             TEST_TIMEOUT }, };
382   }
383 }