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.ModuleRef;
43 import io.github.classgraph.ScanResult;
45 public class CommandLineOperations
48 @BeforeClass(alwaysRun = true)
49 public void setUpJvOptionPane()
51 JvOptionPane.setInteractiveMode(false);
52 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
55 private static final int TEST_TIMEOUT = 9000; // Note longer timeout needed
57 // full test run than on
60 private static final int SETUP_TIMEOUT = 9000;
62 private static final int MINFILESIZE_SMALL = 2096;
64 private static final int MINFILESIZE_BIG = 4096;
66 private ArrayList<String> successfulCMDs = new ArrayList<>();
70 * http://stackoverflow.com/questions/808276/how-to-add-a-timeout-value-when
71 * -using-javas-runtime-exec
76 private static class Worker extends Thread
78 private final Process process;
80 private BufferedReader outputReader;
82 private BufferedReader errorReader;
86 private Worker(Process process)
88 this.process = process;
96 exit = process.waitFor();
97 } catch (InterruptedException ignore)
103 public BufferedReader getOutputReader()
108 public void setOutputReader(BufferedReader outputReader)
110 this.outputReader = outputReader;
113 public BufferedReader getErrorReader()
118 public void setErrorReader(BufferedReader errorReader)
120 this.errorReader = errorReader;
124 private static ClassGraph scanner = null;
126 private static String classpath = null;
128 private static String modules = null;
130 public synchronized static String getClassPath()
134 scanner = new ClassGraph();
135 ScanResult scan = scanner.scan();
136 classpath = scan.getClasspath();
138 for (ModuleRef mr : scan.getModules())
140 modules.concat(mr.getName());
143 while (classpath == null)
148 } catch (InterruptedException x)
156 private Worker getJalviewDesktopRunner(boolean withAwt, String cmd,
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 + "\""
166 + " jalview.bin.Jalview ";
167 Process ls2_proc = null;
168 Worker worker = null;
171 ls2_proc = Runtime.getRuntime().exec(_cmd + cmd);
172 } catch (Throwable e1)
174 e1.printStackTrace();
176 if (ls2_proc != null)
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);
186 worker.join(timeout);
187 } catch (InterruptedException e)
189 System.err.println("Thread interrupted");
191 worker.setOutputReader(outputReader);
192 worker.setErrorReader(errorReader);
197 @BeforeTest(alwaysRun = true)
198 public void initialize()
200 new CommandLineOperations();
203 @BeforeTest(alwaysRun = true)
204 public void setUpForHeadlessCommandLineInputOperations()
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);
212 while ((ln = worker.getOutputReader().readLine()) != null)
214 System.out.println(ln);
215 successfulCMDs.add(ln);
217 while ((ln = worker.getErrorReader().readLine()) != null)
219 System.err.println(ln);
223 @BeforeTest(alwaysRun = true)
224 public void setUpForCommandLineInputOperations() throws IOException
226 String cmds = "-open examples/uniref50.fa -noquestionnaire -nousagestats";
227 Worker worker = getJalviewDesktopRunner(false, cmds, SETUP_TIMEOUT);
230 while ((ln = worker.getErrorReader().readLine()) != null)
232 System.out.println(ln);
233 successfulCMDs.add(ln);
239 if (worker != null && worker.exit == null)
242 Thread.currentThread().interrupt();
243 worker.process.destroy();
247 @Test(groups = { "Functional" }, dataProvider = "allInputOperationsData")
248 public void testAllInputOperations(String expectedString,
251 Assert.assertTrue(successfulCMDs.contains(expectedString), failureMsg);
256 { "Functional", "testben" },
257 dataProvider = "headlessModeOutputOperationsData")
258 public void testHeadlessModeOutputOperations(String harg, String type,
259 String fileName, boolean withAWT, int expectedMinFileSize,
262 String cmd = harg + type + " " + fileName;
263 // System.out.println(">>>>>>>>>>>>>>>> Command : " + cmd);
264 File file = new File(fileName);
266 Worker worker = getJalviewDesktopRunner(withAWT, cmd, timeout);
267 assertNotNull(worker, "worker is null");
268 String msg = "Didn't create an output" + type + " file.[" + harg + "]";
269 assertTrue(file.exists(), msg);
270 FileAssert.assertFile(file, msg);
271 FileAssert.assertMinLength(file, expectedMinFileSize);
272 if (worker != null && worker.exit == null)
275 Thread.currentThread().interrupt();
276 worker.process.destroy();
277 Assert.fail("Jalview did not exit after " + type
278 + " generation (try running test again to verify - timeout at "
279 + timeout + "ms). [" + harg + "]");
284 @DataProvider(name = "allInputOperationsData")
285 public Object[][] getHeadlessModeInputParams()
287 return new Object[][] {
288 // headless mode input operations
289 { "CMD [-color zappo] executed successfully!",
290 "Failed command : -color zappo" },
291 { "CMD [-props test/jalview/io/testProps.jvprops] executed successfully!",
292 "Failed command : -props File" },
293 { "CMD [-sortbytree] executed successfully!",
294 "Failed command : -sortbytree" },
295 { "CMD [-jabaws http://www.compbio.dundee.ac.uk/jabaws] executed successfully!",
296 "Failed command : -jabaws http://www.compbio.dundee.ac.uk/jabaws" },
297 { "CMD [-open examples/uniref50.fa] executed successfully!",
298 "Failed command : -open examples/uniref50.fa" },
299 { "CMD [-nosortbytree] executed successfully!",
300 "Failed command : -nosortbytree" },
301 { "CMD [-features examples/testdata/plantfdx.features] executed successfully!",
302 "Failed command : -features examples/testdata/plantfdx.features" },
303 { "CMD [-annotations examples/testdata/plantfdx.annotations] executed successfully!",
304 "Failed command : -annotations examples/testdata/plantfdx.annotations" },
305 { "CMD [-tree examples/testdata/uniref50_test_tree] executed successfully!",
306 "Failed command : -tree examples/testdata/uniref50_test_tree" },
307 // non headless mode input operations
308 { "CMD [-nousagestats] executed successfully!",
309 "Failed command : -nousagestats" },
310 { "CMD [-noquestionnaire] executed successfully!",
311 "Failed command : -noquestionnaire" } };
314 @DataProvider(name = "headlessModeOutputOperationsData")
315 public static Object[][] getHeadlessModeOutputParams()
317 return new Object[][] { { "nodisplay -open examples/uniref50.fa",
318 " -eps", "test/jalview/bin/test_uniref50_out.eps", true,
319 MINFILESIZE_BIG, TEST_TIMEOUT },
320 { "nodisplay -open examples/uniref50.fa", " -eps",
321 "test/jalview/bin/test_uniref50_out.eps", false,
322 MINFILESIZE_BIG, TEST_TIMEOUT },
323 { "nogui -open examples/uniref50.fa", " -eps",
324 "test/jalview/bin/test_uniref50_out.eps", true, MINFILESIZE_BIG,
326 { "nogui -open examples/uniref50.fa", " -eps",
327 "test/jalview/bin/test_uniref50_out.eps", false,
328 MINFILESIZE_BIG, TEST_TIMEOUT },
329 { "headless -open examples/uniref50.fa", " -eps",
330 "test/jalview/bin/test_uniref50_out.eps", true, MINFILESIZE_BIG,
332 { "headless -open examples/uniref50.fa", " -svg",
333 "test/jalview/bin/test_uniref50_out.svg", false,
334 MINFILESIZE_BIG, TEST_TIMEOUT },
335 { "headless -open examples/uniref50.fa", " -png",
336 "test/jalview/bin/test_uniref50_out.png", true, MINFILESIZE_BIG,
338 { "headless -open examples/uniref50.fa", " -html",
339 "test/jalview/bin/test_uniref50_out.html", true,
340 MINFILESIZE_BIG, TEST_TIMEOUT },
341 { "headless -open examples/uniref50.fa", " -fasta",
342 "test_uniref50_out.mfa", true, MINFILESIZE_SMALL,
344 { "headless -open examples/uniref50.fa", " -clustal",
345 "test_uniref50_out.aln", true, MINFILESIZE_SMALL,
347 { "headless -open examples/uniref50.fa", " -msf",
348 "test_uniref50_out.msf", true, MINFILESIZE_SMALL,
350 { "headless -open examples/uniref50.fa", " -pileup",
351 "test_uniref50_out.aln", true, MINFILESIZE_SMALL,
353 { "headless -open examples/uniref50.fa", " -pir",
354 "test_uniref50_out.pir", true, MINFILESIZE_SMALL,
356 { "headless -open examples/uniref50.fa", " -pfam",
357 "test_uniref50_out.pfam", true, MINFILESIZE_SMALL,
359 { "headless -open examples/uniref50.fa", " -blc",
360 "test_uniref50_out.blc", true, MINFILESIZE_SMALL,
362 { "headless -open examples/uniref50.fa", " -jalview",
363 "test_uniref50_out.jvp", true, MINFILESIZE_SMALL,