2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import static org.testng.Assert.assertNotNull;
24 import static org.testng.Assert.assertTrue;
26 import jalview.gui.JvOptionPane;
28 import java.io.BufferedReader;
30 import java.io.IOException;
31 import java.io.InputStreamReader;
32 import java.util.ArrayList;
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;
41 import io.github.classgraph.ClassGraph;
42 import io.github.classgraph.ScanResult;
44 public class CommandLineOperations
47 @BeforeClass(alwaysRun = true)
48 public void setUpJvOptionPane()
50 JvOptionPane.setInteractiveMode(false);
51 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
54 private static final int TEST_TIMEOUT = 9000; // Note longer timeout needed on
55 // full test run than on
58 private static final int SETUP_TIMEOUT = 9000;
60 private static final int MINFILESIZE_SMALL = 2096;
62 private static final int MINFILESIZE_BIG = 4096;
64 private ArrayList<String> successfulCMDs = new ArrayList<>();
68 * http://stackoverflow.com/questions/808276/how-to-add-a-timeout-value-when
69 * -using-javas-runtime-exec
74 private static class Worker extends Thread
76 private final Process process;
78 private BufferedReader outputReader;
80 private BufferedReader errorReader;
84 private Worker(Process process)
86 this.process = process;
94 exit = process.waitFor();
95 } catch (InterruptedException ignore)
101 public BufferedReader getOutputReader()
106 public void setOutputReader(BufferedReader outputReader)
108 this.outputReader = outputReader;
111 public BufferedReader getErrorReader()
116 public void setErrorReader(BufferedReader errorReader)
118 this.errorReader = errorReader;
122 private static ClassGraph scanner = null;
124 private static String classpath = null;
126 public synchronized static String getClassPath()
130 scanner = new ClassGraph();
131 ScanResult scan = scanner.scan();
132 classpath = scan.getClasspath();
134 while (classpath == null)
139 } catch (InterruptedException x)
147 private Worker getJalviewDesktopRunner(boolean withAwt, String cmd,
150 // Note: JAL-3065 - don't include quotes for lib/* because the arguments are
151 // not expanded by the shell
152 String classpath = getClassPath();
153 String _cmd = "java "
154 + (withAwt ? "-Djava.awt.headless=true" : "")
155 + " -classpath " + classpath + " jalview.bin.Jalview ";
156 Process ls2_proc = null;
157 Worker worker = null;
160 ls2_proc = Runtime.getRuntime().exec(_cmd + cmd);
161 } catch (Throwable e1)
163 e1.printStackTrace();
165 if (ls2_proc != null)
167 BufferedReader outputReader = new BufferedReader(
168 new InputStreamReader(ls2_proc.getInputStream()));
169 BufferedReader errorReader = new BufferedReader(
170 new InputStreamReader(ls2_proc.getErrorStream()));
171 worker = new Worker(ls2_proc);
175 worker.join(timeout);
176 } catch (InterruptedException e)
178 System.err.println("Thread interrupted");
180 worker.setOutputReader(outputReader);
181 worker.setErrorReader(errorReader);
186 @BeforeTest(alwaysRun = true)
187 public void initialize()
189 new CommandLineOperations();
192 @BeforeTest(alwaysRun = true)
193 public void setUpForHeadlessCommandLineInputOperations()
196 String cmds = "nodisplay -open examples/uniref50.fa -sortbytree -props test/jalview/io/testProps.jvprops -colour zappo "
197 + "-jabaws http://www.compbio.dundee.ac.uk/jabaws -nosortbytree "
198 + "-features examples/testdata/plantfdx.features -annotations examples/testdata/plantfdx.annotations -tree examples/testdata/uniref50_test_tree";
199 System.out.println("CommandLine test " + ++ntest);
200 Worker worker = getJalviewDesktopRunner(true, cmds, SETUP_TIMEOUT);
202 while ((ln = worker.getOutputReader().readLine()) != null)
204 System.out.println(ln);
205 successfulCMDs.add(ln);
207 while ((ln = worker.getErrorReader().readLine()) != null)
209 System.err.println(ln);
216 @BeforeTest(alwaysRun = true)
217 public void setUpForCommandLineInputOperations() throws IOException
219 String cmds = "-open examples/uniref50.fa -noquestionnaire -nousagestats";
220 Worker worker = getJalviewDesktopRunner(false, cmds, SETUP_TIMEOUT);
223 while ((ln = worker.getErrorReader().readLine()) != null)
225 System.out.println(ln);
226 successfulCMDs.add(ln);
232 if (worker != null && worker.exit == null)
235 Thread.currentThread().interrupt();
236 worker.process.destroy();
240 @Test(groups = { "Functional" }, dataProvider = "allInputOperationsData")
241 public void testAllInputOperations(String expectedString,
244 Assert.assertTrue(successfulCMDs.contains(expectedString), failureMsg);
248 groups = { "Functional" },
249 dataProvider = "headlessModeOutputOperationsData")
250 public void testHeadlessModeOutputOperations(String harg, String type,
251 String fileName, boolean withAWT, int expectedMinFileSize,
254 String cmd = harg + type + " " + fileName;
255 System.out.println(">>>>>>>>>>>>>>>> Command : " + cmd);
256 File file = new File(fileName);
258 Worker worker = getJalviewDesktopRunner(withAWT, cmd, timeout);
259 assertNotNull(worker, "worker is null");
260 String msg = "Didn't create an output" + type + " file.[" + harg + "]";
261 assertTrue(file.exists(), msg);
262 FileAssert.assertFile(file, msg);
263 FileAssert.assertMinLength(file, expectedMinFileSize);
264 if (worker != null && worker.exit == null)
267 Thread.currentThread().interrupt();
268 worker.process.destroy();
269 Assert.fail("Jalview did not exit after "
271 + " generation (try running test again to verify - timeout at "
272 + SETUP_TIMEOUT + "ms). ["
278 @DataProvider(name = "allInputOperationsData")
279 public Object[][] getHeadlessModeInputParams()
281 return new Object[][] {
282 // headless mode input operations
283 { "CMD [-color zappo] executed successfully!",
284 "Failed command : -color zappo" },
285 { "CMD [-props test/jalview/io/testProps.jvprops] executed successfully!",
286 "Failed command : -props File" },
287 { "CMD [-sortbytree] executed successfully!",
288 "Failed command : -sortbytree" },
289 { "CMD [-jabaws http://www.compbio.dundee.ac.uk/jabaws] executed successfully!",
290 "Failed command : -jabaws http://www.compbio.dundee.ac.uk/jabaws" },
291 { "CMD [-open examples/uniref50.fa] executed successfully!",
292 "Failed command : -open examples/uniref50.fa" },
293 { "CMD [-nosortbytree] executed successfully!",
294 "Failed command : -nosortbytree" },
295 { "CMD [-features examples/testdata/plantfdx.features] executed successfully!",
296 "Failed command : -features examples/testdata/plantfdx.features" },
297 { "CMD [-annotations examples/testdata/plantfdx.annotations] executed successfully!",
298 "Failed command : -annotations examples/testdata/plantfdx.annotations" },
299 { "CMD [-tree examples/testdata/uniref50_test_tree] executed successfully!",
300 "Failed command : -tree examples/testdata/uniref50_test_tree" },
301 // non headless mode input operations
302 { "CMD [-nousagestats] executed successfully!",
303 "Failed command : -nousagestats" },
304 { "CMD [-noquestionnaire] executed successfully!",
305 "Failed command : -noquestionnaire" } };
308 @DataProvider(name = "headlessModeOutputOperationsData")
309 public static Object[][] getHeadlessModeOutputParams()
311 return new Object[][] {
312 { "nodisplay -open examples/uniref50.fa", " -eps",
313 "test/jalview/bin/test_uniref50_out.eps", true, MINFILESIZE_BIG,
315 { "nodisplay -open examples/uniref50.fa", " -eps",
316 "test/jalview/bin/test_uniref50_out.eps", false,
317 MINFILESIZE_BIG, TEST_TIMEOUT },
318 { "nogui -open examples/uniref50.fa", " -eps",
319 "test/jalview/bin/test_uniref50_out.eps", true, MINFILESIZE_BIG,
321 { "nogui -open examples/uniref50.fa", " -eps",
322 "test/jalview/bin/test_uniref50_out.eps", false,
323 MINFILESIZE_BIG, TEST_TIMEOUT },
324 { "headless -open examples/uniref50.fa", " -eps",
325 "test/jalview/bin/test_uniref50_out.eps", true, MINFILESIZE_BIG,
327 { "headless -open examples/uniref50.fa", " -svg",
328 "test/jalview/bin/test_uniref50_out.svg", false,
329 MINFILESIZE_BIG, TEST_TIMEOUT },
330 { "headless -open examples/uniref50.fa", " -png",
331 "test/jalview/bin/test_uniref50_out.png", true, MINFILESIZE_BIG,
333 { "headless -open examples/uniref50.fa", " -html",
334 "test/jalview/bin/test_uniref50_out.html", true,
335 MINFILESIZE_BIG, TEST_TIMEOUT },
336 { "headless -open examples/uniref50.fa", " -fasta",
337 "test/jalview/bin/test_uniref50_out.mfa", true,
338 MINFILESIZE_SMALL, TEST_TIMEOUT },
339 { "headless -open examples/uniref50.fa", " -clustal",
340 "test/jalview/bin/test_uniref50_out.aln", true,
341 MINFILESIZE_SMALL, TEST_TIMEOUT },
342 { "headless -open examples/uniref50.fa", " -msf",
343 "test/jalview/bin/test_uniref50_out.msf", true,
344 MINFILESIZE_SMALL, TEST_TIMEOUT },
345 { "headless -open examples/uniref50.fa", " -pileup",
346 "test/jalview/bin/test_uniref50_out.aln", true,
347 MINFILESIZE_SMALL, TEST_TIMEOUT },
348 { "headless -open examples/uniref50.fa", " -pir",
349 "test/jalview/bin/test_uniref50_out.pir", true,
350 MINFILESIZE_SMALL, TEST_TIMEOUT },
351 { "headless -open examples/uniref50.fa", " -pfam",
352 "test/jalview/bin/test_uniref50_out.pfam", true,
353 MINFILESIZE_SMALL, TEST_TIMEOUT },
354 { "headless -open examples/uniref50.fa", " -blc",
355 "test/jalview/bin/test_uniref50_out.blc", true,
356 MINFILESIZE_SMALL, TEST_TIMEOUT },
357 { "headless -open examples/uniref50.fa", " -jalview",
358 "test/jalview/bin/test_uniref50_out.jvp", true,
359 MINFILESIZE_SMALL, TEST_TIMEOUT }, };