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