JAL-3065 don’t quote class path args for Runtime.exec java launch command (needs...
[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 jalview.gui.JvOptionPane;
24
25 import java.io.BufferedReader;
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.InputStreamReader;
29 import java.util.ArrayList;
30
31 import org.testng.Assert;
32 import org.testng.FileAssert;
33 import org.testng.annotations.BeforeClass;
34 import org.testng.annotations.BeforeTest;
35 import org.testng.annotations.DataProvider;
36 import org.testng.annotations.Test;
37
38 public class CommandLineOperations
39 {
40
41   @BeforeClass(alwaysRun = true)
42   public void setUpJvOptionPane()
43   {
44     JvOptionPane.setInteractiveMode(false);
45     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
46   }
47
48   private static final int TEST_TIMEOUT = 4500; // Note longer timeout needed on
49                                                 // full test run than on
50                                                 // individual tests
51
52   private static final int SETUP_TIMEOUT = 9000;
53
54   private static final int MINFILESIZE_SMALL = 2096;
55
56   private static final int MINFILESIZE_BIG = 4096;
57
58   private ArrayList<String> successfulCMDs = new ArrayList<>();
59
60   /***
61    * from
62    * http://stackoverflow.com/questions/808276/how-to-add-a-timeout-value-when
63    * -using-javas-runtime-exec
64    * 
65    * @author jimp
66    * 
67    */
68   private static class Worker extends Thread
69   {
70     private final Process process;
71
72     private BufferedReader outputReader;
73
74     private BufferedReader errorReader;
75
76     private Integer exit;
77
78     private Worker(Process process)
79     {
80       this.process = process;
81     }
82
83     @Override
84     public void run()
85     {
86       try
87       {
88         exit = process.waitFor();
89       } catch (InterruptedException ignore)
90       {
91         return;
92       }
93     }
94
95     public BufferedReader getOutputReader()
96     {
97       return outputReader;
98     }
99
100     public void setOutputReader(BufferedReader outputReader)
101     {
102       this.outputReader = outputReader;
103     }
104
105     public BufferedReader getErrorReader()
106     {
107       return errorReader;
108     }
109
110     public void setErrorReader(BufferedReader errorReader)
111     {
112       this.errorReader = errorReader;
113     }
114   }
115
116   private Worker jalviewDesktopRunner(boolean withAwt, String cmd,
117           int timeout)
118   {
119     String cp_sep = ":";
120     if (System.getProperty("os.name").indexOf("Windows") >= 0)
121     {
122       cp_sep = ";";
123     }
124     // Note: JAL-3065 - don't include quotes for lib/* because the arguments are
125     // not expanded by the shell
126     String _cmd = "java "
127             + (withAwt ? "-Djava.awt.headless=true" : "")
128             + " -classpath ./classes" + cp_sep
129             + "./lib/* jalview.bin.Jalview ";
130     System.out.println("CMD [" + cmd + "]");
131     Process ls2_proc = null;
132     Worker worker = null;
133     try
134     {
135       ls2_proc = Runtime.getRuntime().exec(_cmd + cmd);
136     } catch (IOException e1)
137     {
138       e1.printStackTrace();
139     }
140     if (ls2_proc != null)
141     {
142       BufferedReader outputReader = new BufferedReader(
143               new InputStreamReader(ls2_proc.getInputStream()));
144       BufferedReader errorReader = new BufferedReader(
145               new InputStreamReader(ls2_proc.getErrorStream()));
146       worker = new Worker(ls2_proc);
147       worker.start();
148       try
149       {
150         worker.join(timeout);
151       } catch (InterruptedException e)
152       {
153         // e.printStackTrace();
154       }
155       worker.setOutputReader(outputReader);
156       worker.setErrorReader(errorReader);
157     }
158     return worker;
159   }
160
161   @BeforeTest(alwaysRun = true)
162   public void initialize()
163   {
164     new CommandLineOperations();
165   }
166
167   @BeforeTest(alwaysRun = true)
168   public void setUpForHeadlessCommandLineInputOperations()
169           throws IOException
170   {
171     String cmds = "nodisplay -open examples/uniref50.fa -sortbytree -props FILE -colour zappo "
172             + "-jabaws http://www.compbio.dundee.ac.uk/jabaws -nosortbytree -dasserver nickname=www.test.com "
173             + "-features examples/testdata/plantfdx.features -annotations examples/testdata/plantfdx.annotations -tree examples/testdata/uniref50_test_tree";
174     Worker worker = jalviewDesktopRunner(true, cmds, SETUP_TIMEOUT);
175     String ln = null;
176     while ((ln = worker.getOutputReader().readLine()) != null)
177     {
178       System.out.println(ln);
179       successfulCMDs.add(ln);
180     }
181   }
182
183   @BeforeTest(alwaysRun = true)
184   public void setUpForCommandLineInputOperations() throws IOException
185   {
186     String cmds = "-open examples/uniref50.fa -noquestionnaire -nousagestats";
187     Worker worker = jalviewDesktopRunner(false, cmds, SETUP_TIMEOUT);
188     String ln = null;
189     int count = 0;
190     while ((ln = worker.getErrorReader().readLine()) != null)
191     {
192       System.out.println(ln);
193       successfulCMDs.add(ln);
194       if (++count > 5)
195       {
196         break;
197       }
198     }
199     if (worker != null && worker.exit == null)
200     {
201       worker.interrupt();
202       Thread.currentThread().interrupt();
203       worker.process.destroy();
204     }
205   }
206
207   @Test(groups = { "Functional" }, dataProvider = "allInputOpearationsData")
208   public void testAllInputOperations(String expectedString,
209           String failureMsg)
210   {
211     Assert.assertTrue(successfulCMDs.contains(expectedString), failureMsg);
212   }
213
214   @Test(
215     groups = { "Functional" },
216     dataProvider = "headlessModeOutputOperationsData")
217   public void testHeadlessModeOutputOperations(String harg, String type,
218           String fileName, boolean withAWT, int expectedMinFileSize,
219           int timeout)
220   {
221     String cmd = harg + type + " " + fileName;
222     // System.out.println(">>>>>>>>>>>>>>>> Command : " + cmd);
223     File file = new File(fileName);
224     Worker worker = jalviewDesktopRunner(withAWT, cmd, timeout);
225
226     FileAssert.assertFile(file, "Didn't create an output" + type
227             + " file.[" + harg + "]");
228     FileAssert.assertMinLength(new File(fileName), expectedMinFileSize);
229     if (worker != null && worker.exit == null)
230     {
231       worker.interrupt();
232       Thread.currentThread().interrupt();
233       worker.process.destroy();
234       Assert.fail("Jalview did not exit after "
235               + type
236               + " generation (try running test again to verify - timeout at "
237               + SETUP_TIMEOUT + "ms). ["
238               + harg + "]");
239     }
240     new File(fileName).delete();
241   }
242
243   @DataProvider(name = "allInputOpearationsData")
244   public Object[][] getHeadlessModeInputParams()
245   {
246     return new Object[][] {
247         // headless mode input operations
248         { "CMD [-color zappo] executed successfully!",
249             "Failed command : -color zappo" },
250         { "CMD [-props FILE] executed successfully!",
251             "Failed command : -props File" },
252         { "CMD [-sortbytree] executed successfully!",
253             "Failed command : -sortbytree" },
254         {
255             "CMD [-jabaws http://www.compbio.dundee.ac.uk/jabaws] executed successfully!",
256             "Failed command : -jabaws http://www.compbio.dundee.ac.uk/jabaws" },
257         { "CMD [-open examples/uniref50.fa] executed successfully!",
258             "Failed command : -open examples/uniref50.fa" },
259         { "CMD [-nosortbytree] executed successfully!",
260             "Failed command : -nosortbytree" },
261         { "CMD [-dasserver nickname=www.test.com] executed successfully!",
262             "Failed command : -dasserver nickname=www.test.com" },
263         {
264             "CMD [-features examples/testdata/plantfdx.features]  executed successfully!",
265             "Failed command : -features examples/testdata/plantfdx.features" },
266         {
267             "CMD [-annotations examples/testdata/plantfdx.annotations] executed successfully!",
268             "Failed command : -annotations examples/testdata/plantfdx.annotations" },
269         {
270             "CMD [-tree examples/testdata/uniref50_test_tree] executed successfully!",
271             "Failed command : -tree examples/testdata/uniref50_test_tree" },
272         // non headless mode input operations
273         { "CMD [-nousagestats] executed successfully!",
274             "Failed command : -nousagestats" },
275         { "CMD [-noquestionnaire] executed successfully!",
276             "Failed command : -noquestionnaire nickname=www.test.com" } };
277
278   }
279
280   @DataProvider(name = "headlessModeOutputOperationsData")
281   public static Object[][] getHeadlessModeOutputParams()
282   {
283     return new Object[][] {
284         { "nodisplay -open examples/uniref50.fa", " -eps",
285             "test_uniref50_out.eps", true, MINFILESIZE_BIG, TEST_TIMEOUT },
286         { "nodisplay -open examples/uniref50.fa", " -eps",
287             "test_uniref50_out.eps", false, MINFILESIZE_BIG, TEST_TIMEOUT },
288         { "nogui -open examples/uniref50.fa", " -eps",
289             "test_uniref50_out.eps", true, MINFILESIZE_BIG, TEST_TIMEOUT },
290         { "nogui -open examples/uniref50.fa", " -eps",
291             "test_uniref50_out.eps", false, MINFILESIZE_BIG, TEST_TIMEOUT },
292         { "headless -open examples/uniref50.fa", " -eps",
293             "test_uniref50_out.eps", true, MINFILESIZE_BIG, TEST_TIMEOUT },
294         { "headless -open examples/uniref50.fa", " -svg",
295             "test_uniref50_out.svg", false, MINFILESIZE_BIG, TEST_TIMEOUT },
296         { "headless -open examples/uniref50.fa", " -png",
297             "test_uniref50_out.png", true, MINFILESIZE_BIG, TEST_TIMEOUT },
298         { "headless -open examples/uniref50.fa", " -html",
299             "test_uniref50_out.html", true, MINFILESIZE_BIG, TEST_TIMEOUT },
300         { "headless -open examples/uniref50.fa", " -fasta",
301             "test_uniref50_out.mfa", true, MINFILESIZE_SMALL, TEST_TIMEOUT },
302         { "headless -open examples/uniref50.fa", " -clustal",
303             "test_uniref50_out.aln", true, MINFILESIZE_SMALL, TEST_TIMEOUT },
304         { "headless -open examples/uniref50.fa", " -msf",
305             "test_uniref50_out.msf", true, MINFILESIZE_SMALL, TEST_TIMEOUT },
306         { "headless -open examples/uniref50.fa", " -pileup",
307             "test_uniref50_out.aln", true, MINFILESIZE_SMALL, TEST_TIMEOUT },
308         { "headless -open examples/uniref50.fa", " -pir",
309             "test_uniref50_out.pir", true, MINFILESIZE_SMALL, TEST_TIMEOUT },
310         { "headless -open examples/uniref50.fa", " -pfam",
311             "test_uniref50_out.pfam", true, MINFILESIZE_SMALL, TEST_TIMEOUT },
312         { "headless -open examples/uniref50.fa", " -blc",
313             "test_uniref50_out.blc", true, MINFILESIZE_SMALL, TEST_TIMEOUT },
314         { "headless -open examples/uniref50.fa", " -jalview",
315             "test_uniref50_out.jvp", true, MINFILESIZE_SMALL, TEST_TIMEOUT }, };
316   }
317 }