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.Files;
31 import java.nio.file.Path;
32 import java.nio.file.Paths;
33 import java.util.ArrayList;
34 import java.util.List;
36 import org.testng.Assert;
37 import org.testng.FileAssert;
38 import org.testng.annotations.BeforeClass;
39 import org.testng.annotations.DataProvider;
40 import org.testng.annotations.Test;
42 import io.github.classgraph.ClassGraph;
43 import io.github.classgraph.ModuleRef;
44 import io.github.classgraph.ScanResult;
45 import jalview.gui.JvOptionPane;
47 public class CommandLineOperationsNG
50 @BeforeClass(alwaysRun = true)
51 public void setUpJvOptionPane()
53 JvOptionPane.setInteractiveMode(false);
54 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
57 // Note longer timeout needed on full test run than on individual tests
58 private static final int TEST_TIMEOUT = 13000;
60 private static final int SETUP_TIMEOUT = 9500;
62 private static final int MINFILESIZE_SMALL = 2096;
64 private static final int MINFILESIZE_BIG = 4096;
66 private List<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 private static String java_exe = null;
132 public synchronized static String getClassPath()
136 scanner = new ClassGraph();
137 ScanResult scan = scanner.scan();
138 classpath = scan.getClasspath();
140 for (ModuleRef mr : scan.getModules())
142 modules.concat(mr.getName());
144 java_exe = System.getProperty("java.home") + File.separator + "bin"
145 + File.separator + "java";
149 while (classpath == null)
154 } catch (InterruptedException x)
162 private Worker getJalviewDesktopRunner(boolean withAwt, String cmd,
165 return getJalviewDesktopRunner(withAwt, cmd, timeout, true);
168 private Worker getJalviewDesktopRunner(boolean withAwt, String cmd,
169 int timeout, boolean testoutput)
172 boolean win = System.getProperty("os.name").indexOf("Win") >= 0;
176 Path currentRelativePath = Paths.get("");
177 pwd = currentRelativePath.toAbsolutePath().toString();
178 } catch (Exception q)
182 if (pwd == null || pwd.length() == 0)
184 String[] classpaths = new String[] { pwd + "/bin/main",
185 pwd + "/j11lib/*", pwd + "/resources", pwd + "/help" };
186 String classpath = String.join(win ? ";" : ":", classpaths);
189 // Note: JAL-3065 - don't include quotes for lib/* because the arguments are
190 // not expanded by the shell
191 String classpath = getClassPath();
192 String _cmd = java_exe + " "
193 + (withAwt ? "-Djava.awt.headless=true" : "") + " -classpath "
195 + ((modules != null && modules.length() > 2)
196 ? "--add-modules=\"" + modules + "\""
198 + " jalview.bin.Jalview ";
199 Process ls2_proc = null;
200 Worker worker = null;
203 cmd = cmd + (testoutput ? " --testoutput " : "");
204 System.out.println("Running '" + _cmd + cmd + "'");
205 ls2_proc = Runtime.getRuntime().exec(_cmd + cmd);
206 } catch (Throwable e1)
208 e1.printStackTrace();
210 if (ls2_proc != null)
212 BufferedReader outputReader = new BufferedReader(
213 new InputStreamReader(ls2_proc.getInputStream()));
214 BufferedReader errorReader = new BufferedReader(
215 new InputStreamReader(ls2_proc.getErrorStream()));
216 worker = new Worker(ls2_proc);
220 worker.join(timeout);
221 } catch (InterruptedException e)
223 System.err.println("Thread interrupted");
225 worker.setOutputReader(outputReader);
226 worker.setErrorReader(errorReader);
231 @Test(groups = { "Functional", "testTask1" })
232 public void reportCurrentWorkingDirectory()
236 Path currentRelativePath = Paths.get("");
237 String s = currentRelativePath.toAbsolutePath().toString();
238 System.out.println("Test CWD is " + s);
239 } catch (Exception q)
245 @BeforeClass(alwaysRun = true)
246 public void initialize()
248 new CommandLineOperationsNG();
251 @BeforeClass(alwaysRun = true)
252 public void setUpForHeadlessCommandLineInputOperations()
255 String cmds = "--headless " + "--open examples/uniref50.fa "
257 + "--props test/jalview/bin/testProps.jvprops "
259 + "--jabaws http://www.compbio.dundee.ac.uk/jabaws "
260 + "--features examples/testdata/plantfdx.features "
261 + "--annotations examples/testdata/plantfdx.annotations "
262 + "--tree examples/testdata/uniref50_test_tree "
264 Worker worker = getJalviewDesktopRunner(true, cmds, SETUP_TIMEOUT);
266 while ((ln = worker.getOutputReader().readLine()) != null)
268 System.out.println(ln);
269 successfulCMDs.add(ln);
271 while ((ln = worker.getErrorReader().readLine()) != null)
273 System.err.println(ln);
277 @BeforeClass(alwaysRun = true)
278 public void setUpForCommandLineInputOperations() throws IOException
280 String cmds = "--open examples/uniref50.fa --noquestionnaire --nousagestats";
281 final Worker worker = getJalviewDesktopRunner(false, cmds,
284 // number of lines expected on STDERR when Jalview starts up normally
285 // may need to adjust this if Jalview is excessively noisy ?
286 final int STDERR_SETUPLINES = 50;
288 // thread monitors stderr - bails after SETUP_TIMEOUT or when
289 // STDERR_SETUPLINES have been read
290 Thread runner = new Thread(new Runnable()
299 while ((ln = worker.getOutputReader().readLine()) != null)
301 System.out.println(ln);
302 successfulCMDs.add(ln);
303 if (++count > STDERR_SETUPLINES)
308 } catch (Exception e)
311 "Unexpected Exception reading stderr from the Jalview process");
316 long t = System.currentTimeMillis() + SETUP_TIMEOUT;
318 while (!runner.isInterrupted() && System.currentTimeMillis() < t)
323 } catch (InterruptedException e)
328 if (worker != null && worker.exit == null)
331 Thread.currentThread().interrupt();
332 worker.process.destroy();
338 { "Functional", "testTask1" },
339 dataProvider = "allInputOperationsData")
340 public void testAllInputOperations(String expectedString,
343 Assert.assertTrue(successfulCMDs.contains(expectedString),
344 failureMsg + "; was expecting '" + expectedString + "'");
349 { "Functional", "testTask1" },
350 dataProvider = "headlessModeOutputOperationsData")
351 public void testHeadlessModeOutputOperations(String harg, String type,
352 String fileName, boolean withAWT, int expectedMinFileSize,
355 String cmd = harg + type + " " + fileName;
356 // System.out.println(">>>>>>>>>>>>>>>> Command : " + cmd);
357 File file = new File(fileName);
359 Worker worker = getJalviewDesktopRunner(withAWT, cmd, timeout);
360 assertNotNull(worker, "worker is null");
361 String msg = "Didn't create an output" + type + " file '" + fileName
362 + "'. [" + cmd + "]";
363 assertTrue(file.exists(), msg);
364 FileAssert.assertFile(file, msg);
365 FileAssert.assertMinLength(file, expectedMinFileSize);
366 if (worker != null && worker.exit == null)
369 Thread.currentThread().interrupt();
370 worker.process.destroy();
371 Assert.fail("Jalview did not exit after " + type
372 + " generation (try running test again to verify - timeout at "
373 + timeout + "ms). [" + harg + "]");
380 { "Functional", "testTask1" },
381 dataProvider = "headlessModeOutputToStdout")
382 public void testHeadlessModeOutputToStdout(String args,
383 String comparisonFile, int timeout)
386 File file = new File(comparisonFile);
387 Worker worker = getJalviewDesktopRunner(true, cmd, timeout, false);
389 StringBuilder sb = new StringBuilder();
392 while ((b = worker.getOutputReader().read()) != -1)
394 sb.append(Character.toChars(b));
396 } catch (IOException e)
398 Assert.fail("IOException whilst trying to read from jalview process");
401 String comparisonContent = null;
404 comparisonContent = new String(Files.readAllBytes(file.toPath()));
405 } catch (IOException e)
407 Assert.fail("IOException whilst trying to read comparison file");
410 Assert.assertEquals(sb.toString(), comparisonContent,
411 "STDOUT from jalview command did not match the comparison file");
414 @DataProvider(name = "allInputOperationsData")
415 public Object[][] getHeadlessModeInputParams()
417 return new Object[][] {
418 // headless mode input operations
419 { "[TESTOUTPUT] arg --colour='zappo' was set",
420 "Failed setting arg --colour" },
421 { "[TESTOUTPUT] arg --props='test/jalview/bin/testProps.jvprops' was set",
422 "Failed setting arg --props" },
423 { "[TESTOUTPUT] arg --sortbytree was set",
424 "Failed setting arg --sortbytree" },
425 { "[TESTOUTPUT] arg --jabaws='http://www.compbio.dundee.ac.uk/jabaws' was set",
426 "Failed setting arg --jabaws" },
427 { "[TESTOUTPUT] arg --open='examples/uniref50.fa' was set",
428 "Failed setting arg --open" },
429 { "[TESTOUTPUT] arg --features='examples/testdata/plantfdx.features' was set",
430 "Failed setting arg --features" },
431 { "[TESTOUTPUT] arg --annotations='examples/testdata/plantfdx.annotations' was set",
432 "Failed setting arg --annotations" },
433 { "[TESTOUTPUT] arg --tree='examples/testdata/uniref50_test_tree' was set",
434 "Failed setting arg --tree" },
435 // non headless mode input operations
436 { "[TESTOUTPUT] arg --nousagestats was set",
437 "Failed setting arg --nousagestats" },
438 { "[TESTOUTPUT] arg --noquestionnaire was set",
439 "Failed setting arg --noquestionnaire" }
444 @DataProvider(name = "headlessModeOutputOperationsData")
445 public static Object[][] getHeadlessModeOutputParams()
447 // JBPNote: I'm not clear why need to specify full path for output file
448 // when running tests on build server, but we will keep this patch for now
450 // https://issues.jalview.org/browse/JAL-1889?focusedCommentId=21609&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-21609
451 String workingDir = "test/jalview/bin/";
452 return new Object[][] {
454 { "--headless --open examples/uniref50.fa", " --image",
455 workingDir + "test_uniref50_out.eps", true, MINFILESIZE_BIG,
457 { "--headless --open examples/uniref50.fa", " --image",
458 workingDir + "test_uniref50_out.eps", false, MINFILESIZE_BIG,
460 { "--headless --open examples/uniref50.fa", " --image",
461 workingDir + "test_uniref50_out.eps", true, MINFILESIZE_BIG,
463 { "--headless --open examples/uniref50.fa", " --image",
464 workingDir + "test_uniref50_out.eps", false, MINFILESIZE_BIG,
466 { "--headless --open examples/uniref50.fa", " --image",
467 workingDir + "test_uniref50_out.eps", true, MINFILESIZE_BIG,
469 { "--headless --open examples/uniref50.fa", " --image",
470 workingDir + "test_uniref50_out.svg", false, MINFILESIZE_BIG,
472 { "--headless --open examples/uniref50.fa", " --image",
473 workingDir + "test_uniref50_out.png", true, MINFILESIZE_BIG,
475 { "--headless --open examples/uniref50.fa", " --image",
476 workingDir + "test_uniref50_out.html", true, MINFILESIZE_BIG,
478 { "--headless --open examples/uniref50.fa", " --output",
479 workingDir + "test_uniref50_out.mfa", true, MINFILESIZE_SMALL,
481 { "--headless --open examples/uniref50.fa", " --output",
482 workingDir + "test_uniref50_out.aln", true, MINFILESIZE_SMALL,
484 { "--headless --open examples/uniref50.fa", " --output",
485 workingDir + "test_uniref50_out.msf", true, MINFILESIZE_SMALL,
487 { "--headless --open examples/uniref50.fa", " --output",
488 workingDir + "test_uniref50_out.aln", true, MINFILESIZE_SMALL,
490 { "--headless --open examples/uniref50.fa", " --output",
491 workingDir + "test_uniref50_out.pir", true, MINFILESIZE_SMALL,
493 { "--headless --open examples/uniref50.fa", " --output",
494 workingDir + "test_uniref50_out.pfam", true, MINFILESIZE_SMALL,
496 { "--headless --open examples/uniref50.fa", " --output",
497 workingDir + "test_uniref50_out.blc", true, MINFILESIZE_SMALL,
499 { "--headless --open examples/uniref50.fa", " --output",
500 workingDir + "test_uniref50_out.jvp", true, MINFILESIZE_SMALL,
506 @DataProvider(name = "headlessModeOutputToStdout")
507 public static Object[][] getHeadlessModeOutputToStdout()
509 // JBPNote: I'm not clear why need to specify full path for output file
510 // when running tests on build server, but we will keep this patch for now
512 // https://issues.jalview.org/browse/JAL-1889?focusedCommentId=21609&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-21609
513 String workingDir = "test/jalview/bin";
514 return new Object[][] {
516 { "--open=examples/uniref50.fa --output=-",
517 workingDir + "/uniref50-output.fa", TEST_TIMEOUT },
518 { "--open examples/uniref50.fa --output -",
519 workingDir + "/uniref50-output.fa", TEST_TIMEOUT },
520 { "--open examples/uniref50.fa --output=[format=blc]-",
521 workingDir + "/uniref50-output.blc", TEST_TIMEOUT },
522 { "--open examples/uniref50.fa --output - --format blc",
523 workingDir + "/uniref50-output.blc", TEST_TIMEOUT },
524 { "./examples/uniref50.fa --output=-",
525 workingDir + "/uniref50-output.fa", TEST_TIMEOUT },
526 { "./examples/uniref50.fa --output - --format blc",
527 workingDir + "/uniref50-output.blc", TEST_TIMEOUT },
528 // remember you can't use shell wildcards for filenames in a test
529 { "./test/jalview/bin/argparser/testfiles/test1.fa ./test/jalview/bin/argparser/testfiles/test2.fa ./test/jalview/bin/argparser/testfiles/test3.fa --all --output -",
530 workingDir + "/test1-3.fa", TEST_TIMEOUT },
531 // but you can use java wildcards when using an equals sign
532 { "--open=./test/jalview/bin/argparser/testfiles/test*.fa --all --output -",
533 workingDir + "/test1-3.fa", TEST_TIMEOUT },