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 java.io.BufferedReader;
28 import java.io.IOException;
29 import java.io.InputStreamReader;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
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;
44 import jalview.gui.JvOptionPane;
46 public class CommandLineOperations
49 @BeforeClass(alwaysRun = true)
50 public void setUpJvOptionPane()
52 JvOptionPane.setInteractiveMode(false);
53 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
56 // Note longer timeout needed on full test run than on individual tests
57 private static final int TEST_TIMEOUT = 13000;
59 private static final int SETUP_TIMEOUT = 9500;
61 private static final int MINFILESIZE_SMALL = 2096;
63 private static final int MINFILESIZE_BIG = 4096;
65 private ArrayList<String> successfulCMDs = new ArrayList<>();
69 * http://stackoverflow.com/questions/808276/how-to-add-a-timeout-value-when
70 * -using-javas-runtime-exec
75 private static class Worker extends Thread
77 private final Process process;
79 private BufferedReader outputReader;
81 private BufferedReader errorReader;
85 private Worker(Process process)
87 this.process = process;
95 exit = process.waitFor();
96 } catch (InterruptedException ignore)
102 public BufferedReader getOutputReader()
107 public void setOutputReader(BufferedReader outputReader)
109 this.outputReader = outputReader;
112 public BufferedReader getErrorReader()
117 public void setErrorReader(BufferedReader errorReader)
119 this.errorReader = errorReader;
123 private static ClassGraph scanner = null;
125 private static String classpath = null;
127 private static String modules = null;
129 private static String java_exe = null;
131 public synchronized static String getClassPath()
135 scanner = new ClassGraph();
136 ScanResult scan = scanner.scan();
137 classpath = scan.getClasspath();
139 for (ModuleRef mr : scan.getModules())
141 modules.concat(mr.getName());
143 java_exe = System.getProperty("java.home") + File.separator + "bin"
144 + File.separator + "java";
147 while (classpath == null)
152 } catch (InterruptedException x)
160 private Worker getJalviewDesktopRunner(boolean withAwt, String cmd,
163 // Note: JAL-3065 - don't include quotes for lib/* because the arguments are
164 // not expanded by the shell
165 String classpath = getClassPath();
166 String _cmd = java_exe + " "
167 + (withAwt ? "-Djava.awt.headless=true" : "") + " -classpath "
169 + (modules.length() > 2 ? "--add-modules=\"" + modules + "\""
171 + " jalview.bin.Jalview ";
172 Process ls2_proc = null;
173 Worker worker = null;
176 ls2_proc = Runtime.getRuntime().exec(_cmd + cmd);
177 } catch (Throwable e1)
179 e1.printStackTrace();
181 if (ls2_proc != null)
183 BufferedReader outputReader = new BufferedReader(
184 new InputStreamReader(ls2_proc.getInputStream()));
185 BufferedReader errorReader = new BufferedReader(
186 new InputStreamReader(ls2_proc.getErrorStream()));
187 worker = new Worker(ls2_proc);
191 worker.join(timeout);
192 } catch (InterruptedException e)
194 System.err.println("Thread interrupted");
196 worker.setOutputReader(outputReader);
197 worker.setErrorReader(errorReader);
202 @Test(groups = { "Functional" })
203 public void reportCurrentWorkingDirectory()
207 Path currentRelativePath = Paths.get("");
208 String s = currentRelativePath.toAbsolutePath().toString();
209 System.out.println("Test CWD is " + s);
210 } catch (Exception q)
216 @BeforeTest(alwaysRun = true)
217 public void initialize()
219 new CommandLineOperations();
222 @BeforeTest(alwaysRun = true)
223 public void setUpForHeadlessCommandLineInputOperations()
226 String cmds = "nodisplay -open examples/uniref50.fa -sortbytree --props=test/jalview/bin/testProps.jvprops -colour zappo "
227 + "-jabaws http://www.compbio.dundee.ac.uk/jabaws -nosortbytree "
228 + "-features examples/testdata/plantfdx.features -annotations examples/testdata/plantfdx.annotations -tree examples/testdata/uniref50_test_tree";
229 Worker worker = getJalviewDesktopRunner(true, cmds, SETUP_TIMEOUT);
231 while ((ln = worker.getOutputReader().readLine()) != null)
233 System.out.println(ln);
234 successfulCMDs.add(ln);
236 while ((ln = worker.getErrorReader().readLine()) != null)
238 System.err.println(ln);
242 @BeforeTest(alwaysRun = true)
243 public void setUpForCommandLineInputOperations() throws IOException
245 String cmds = "-open examples/uniref50.fa -noquestionnaire -nousagestats";
246 final Worker worker = getJalviewDesktopRunner(false, cmds,
249 // number of lines expected on STDERR when Jalview starts up normally
250 // may need to adjust this if Jalview is excessively noisy ?
251 final int STDERR_SETUPLINES = 50;
253 // thread monitors stderr - bails after SETUP_TIMEOUT or when
254 // STDERR_SETUPLINES have been read
255 Thread runner = new Thread(new Runnable()
264 while ((ln = worker.getErrorReader().readLine()) != null)
266 System.out.println(ln);
267 successfulCMDs.add(ln);
268 if (++count > STDERR_SETUPLINES)
273 } catch (Exception e)
276 "Unexpected Exception reading stderr from the Jalview process");
281 long t = System.currentTimeMillis() + SETUP_TIMEOUT;
283 while (!runner.isInterrupted() && System.currentTimeMillis() < t)
288 } catch (InterruptedException e)
293 if (worker != null && worker.exit == null)
296 Thread.currentThread().interrupt();
297 worker.process.destroy();
301 @Test(groups = { "Functional" }, dataProvider = "allInputOperationsData")
302 public void testAllInputOperations(String expectedString,
305 Assert.assertTrue(successfulCMDs.contains(expectedString), failureMsg);
311 dataProvider = "headlessModeOutputOperationsData")
312 public void testHeadlessModeOutputOperations(String harg, String type,
313 String fileName, boolean withAWT, int expectedMinFileSize,
316 String cmd = harg + type + " " + fileName;
317 // System.out.println(">>>>>>>>>>>>>>>> Command : " + cmd);
318 File file = new File(fileName);
320 Worker worker = getJalviewDesktopRunner(withAWT, cmd, timeout);
321 assertNotNull(worker, "worker is null");
322 String msg = "Didn't create an output" + type + " file.[" + harg + "]";
323 assertTrue(file.exists(), msg);
324 FileAssert.assertFile(file, msg);
325 FileAssert.assertMinLength(file, expectedMinFileSize);
326 if (worker != null && worker.exit == null)
329 Thread.currentThread().interrupt();
330 worker.process.destroy();
331 Assert.fail("Jalview did not exit after " + type
332 + " generation (try running test again to verify - timeout at "
333 + timeout + "ms). [" + harg + "]");
338 @DataProvider(name = "allInputOperationsData")
339 public Object[][] getHeadlessModeInputParams()
341 return new Object[][] {
342 // headless mode input operations
343 { "CMD [-colour zappo] executed successfully!",
344 "Failed command : -color zappo" },
345 { "CMD [-props test/jalview/bin/testProps.jvprops] executed successfully!",
346 "Failed command : --props=File" },
347 { "CMD [-sortbytree] executed successfully!",
348 "Failed command : -sortbytree" },
349 { "CMD [-jabaws http://www.compbio.dundee.ac.uk/jabaws] executed successfully!",
350 "Failed command : -jabaws http://www.compbio.dundee.ac.uk/jabaws" },
351 { "CMD [-open examples/uniref50.fa] executed successfully!",
352 "Failed command : -open examples/uniref50.fa" },
353 { "CMD [-nosortbytree] executed successfully!",
354 "Failed command : -nosortbytree" },
355 { "CMD [-features examples/testdata/plantfdx.features] executed successfully!",
356 "Failed command : -features examples/testdata/plantfdx.features" },
357 { "CMD [-annotations examples/testdata/plantfdx.annotations] executed successfully!",
358 "Failed command : -annotations examples/testdata/plantfdx.annotations" },
359 { "CMD [-tree examples/testdata/uniref50_test_tree] executed successfully!",
360 "Failed command : -tree examples/testdata/uniref50_test_tree" },
361 // non headless mode input operations
362 { "CMD [-nousagestats] executed successfully!",
363 "Failed command : -nousagestats" },
364 { "CMD [-noquestionnaire] executed successfully!",
365 "Failed command : -noquestionnaire" } };
368 @DataProvider(name = "headlessModeOutputOperationsData")
369 public static Object[][] getHeadlessModeOutputParams()
371 // JBPNote: I'm not clear why need to specify full path for output file
372 // when running tests on build server, but we will keep this patch for now
374 // https://issues.jalview.org/browse/JAL-1889?focusedCommentId=21609&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-21609
375 String workingDir = "test/jalview/bin/";
376 return new Object[][] { { "nodisplay -open examples/uniref50.fa",
377 " -eps", workingDir + "test_uniref50_out.eps", true,
378 MINFILESIZE_BIG, TEST_TIMEOUT },
379 { "nodisplay -open examples/uniref50.fa", " -eps",
380 workingDir + "test_uniref50_out.eps", false, MINFILESIZE_BIG,
382 { "nogui -open examples/uniref50.fa", " -eps",
383 workingDir + "test_uniref50_out.eps", true, MINFILESIZE_BIG,
385 { "nogui -open examples/uniref50.fa", " -eps",
386 workingDir + "test_uniref50_out.eps", false, MINFILESIZE_BIG,
388 { "headless -open examples/uniref50.fa", " -eps",
389 workingDir + "test_uniref50_out.eps", true, MINFILESIZE_BIG,
391 { "headless -open examples/uniref50.fa", " -svg",
392 workingDir + "test_uniref50_out.svg", false, MINFILESIZE_BIG,
394 { "headless -open examples/uniref50.fa", " -png",
395 workingDir + "test_uniref50_out.png", true, MINFILESIZE_BIG,
397 { "headless -open examples/uniref50.fa", " -html",
398 workingDir + "test_uniref50_out.html", true, MINFILESIZE_BIG,
400 { "headless -open examples/uniref50.fa", " -fasta",
401 workingDir + "test_uniref50_out.mfa", true, MINFILESIZE_SMALL,
403 { "headless -open examples/uniref50.fa", " -clustal",
404 workingDir + "test_uniref50_out.aln", true, MINFILESIZE_SMALL,
406 { "headless -open examples/uniref50.fa", " -msf",
407 workingDir + "test_uniref50_out.msf", true, MINFILESIZE_SMALL,
409 { "headless -open examples/uniref50.fa", " -pileup",
410 workingDir + "test_uniref50_out.aln", true, MINFILESIZE_SMALL,
412 { "headless -open examples/uniref50.fa", " -pir",
413 workingDir + "test_uniref50_out.pir", true, MINFILESIZE_SMALL,
415 { "headless -open examples/uniref50.fa", " -pfam",
416 workingDir + "test_uniref50_out.pfam", true, MINFILESIZE_SMALL,
418 { "headless -open examples/uniref50.fa", " -blc",
419 workingDir + "test_uniref50_out.blc", true, MINFILESIZE_SMALL,
421 { "headless -open examples/uniref50.fa", " -jalview",
422 workingDir + "test_uniref50_out.jvp", true, MINFILESIZE_SMALL,