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,
166 boolean win = System.getProperty("os.name").indexOf("Win") >= 0;
170 Path currentRelativePath = Paths.get("");
171 pwd = currentRelativePath.toAbsolutePath().toString();
172 } catch (Exception q)
176 if (pwd == null || pwd.length() == 0)
178 String[] classpaths = new String[] { pwd + "/bin/main",
179 pwd + "/j11lib/*", pwd + "/resources", pwd + "/help" };
180 String classpath = String.join(win ? ";" : ":", classpaths);
183 // Note: JAL-3065 - don't include quotes for lib/* because the arguments are
184 // not expanded by the shell
185 String classpath = getClassPath();
186 String _cmd = java_exe + " "
187 + (withAwt ? "-Djava.awt.headless=true" : "") + " -classpath "
189 + ((modules != null && modules.length() > 2)
190 ? "--add-modules=\"" + modules + "\""
192 + " jalview.bin.Jalview ";
193 Process ls2_proc = null;
194 Worker worker = null;
197 cmd = " --testoutput " + cmd;
198 System.out.println("Running '" + _cmd + cmd + "'");
199 ls2_proc = Runtime.getRuntime().exec(_cmd + cmd);
200 } catch (Throwable e1)
202 e1.printStackTrace();
204 if (ls2_proc != null)
206 BufferedReader outputReader = new BufferedReader(
207 new InputStreamReader(ls2_proc.getInputStream()));
208 BufferedReader errorReader = new BufferedReader(
209 new InputStreamReader(ls2_proc.getErrorStream()));
210 worker = new Worker(ls2_proc);
214 worker.join(timeout);
215 } catch (InterruptedException e)
217 System.err.println("Thread interrupted");
219 worker.setOutputReader(outputReader);
220 worker.setErrorReader(errorReader);
225 @Test(groups = { "Functional", "testTask1" })
226 public void reportCurrentWorkingDirectory()
230 Path currentRelativePath = Paths.get("");
231 String s = currentRelativePath.toAbsolutePath().toString();
232 System.out.println("Test CWD is " + s);
233 } catch (Exception q)
239 @BeforeClass(alwaysRun = true)
240 public void initialize()
242 new CommandLineOperationsNG();
245 @BeforeClass(alwaysRun = true)
246 public void setUpForHeadlessCommandLineInputOperations()
249 String cmds = "--headless " + "--open examples/uniref50.fa "
251 + "--props test/jalview/bin/testProps.jvprops "
253 + "--jabaws http://www.compbio.dundee.ac.uk/jabaws "
254 + "--features examples/testdata/plantfdx.features "
255 + "--annotations examples/testdata/plantfdx.annotations "
256 + "--tree examples/testdata/uniref50_test_tree "
258 Worker worker = getJalviewDesktopRunner(true, cmds, SETUP_TIMEOUT);
260 while ((ln = worker.getOutputReader().readLine()) != null)
262 System.out.println(ln);
263 successfulCMDs.add(ln);
265 while ((ln = worker.getErrorReader().readLine()) != null)
267 System.err.println(ln);
271 @BeforeClass(alwaysRun = true)
272 public void setUpForCommandLineInputOperations() throws IOException
274 String cmds = "--open examples/uniref50.fa --noquestionnaire --nousagestats";
275 final Worker worker = getJalviewDesktopRunner(false, cmds,
278 // number of lines expected on STDERR when Jalview starts up normally
279 // may need to adjust this if Jalview is excessively noisy ?
280 final int STDERR_SETUPLINES = 50;
282 // thread monitors stderr - bails after SETUP_TIMEOUT or when
283 // STDERR_SETUPLINES have been read
284 Thread runner = new Thread(new Runnable()
293 while ((ln = worker.getOutputReader().readLine()) != null)
295 System.out.println(ln);
296 successfulCMDs.add(ln);
297 if (++count > STDERR_SETUPLINES)
302 } catch (Exception e)
305 "Unexpected Exception reading stderr from the Jalview process");
310 long t = System.currentTimeMillis() + SETUP_TIMEOUT;
312 while (!runner.isInterrupted() && System.currentTimeMillis() < t)
317 } catch (InterruptedException e)
322 if (worker != null && worker.exit == null)
325 Thread.currentThread().interrupt();
326 worker.process.destroy();
332 { "Functional", "testTask1" },
333 dataProvider = "allInputOperationsData")
334 public void testAllInputOperations(String expectedString,
337 Assert.assertTrue(successfulCMDs.contains(expectedString),
338 failureMsg + "; was expecting '" + expectedString + "'");
343 { "Functional", "testTask1" },
344 dataProvider = "headlessModeOutputOperationsData")
345 public void testHeadlessModeOutputOperations(String harg, String type,
346 String fileName, boolean withAWT, int expectedMinFileSize,
349 String cmd = harg + type + " " + fileName;
350 // System.out.println(">>>>>>>>>>>>>>>> Command : " + cmd);
351 File file = new File(fileName);
353 Worker worker = getJalviewDesktopRunner(withAWT, cmd, timeout);
354 assertNotNull(worker, "worker is null");
355 String msg = "Didn't create an output" + type + " file '" + fileName
356 + "'. [" + cmd + "]";
357 assertTrue(file.exists(), msg);
358 FileAssert.assertFile(file, msg);
359 FileAssert.assertMinLength(file, expectedMinFileSize);
360 if (worker != null && worker.exit == null)
363 Thread.currentThread().interrupt();
364 worker.process.destroy();
365 Assert.fail("Jalview did not exit after " + type
366 + " generation (try running test again to verify - timeout at "
367 + timeout + "ms). [" + harg + "]");
374 { "Functional", "testTask1" },
375 dataProvider = "headlessModeOutputToStdout")
376 public void testHeadlessModeOutputToStdout(String args,
377 String comparisonFile, int timeout)
380 File file = new File(comparisonFile);
381 Worker worker = getJalviewDesktopRunner(true, cmd, timeout);
383 StringBuilder sb = new StringBuilder();
386 while ((b = worker.getOutputReader().read()) != -1)
388 sb.append(Character.toChars(b));
390 } catch (IOException e)
392 Assert.fail("IOException whilst trying to read from jalview process");
395 String comparisonContent = null;
398 comparisonContent = new String(Files.readAllBytes(file.toPath()));
399 } catch (IOException e)
401 Assert.fail("IOException whilst trying to read comparison file");
404 Assert.assertEquals(sb.toString(), comparisonContent,
405 "STDOUT from jalview command did not match the comparison file");
408 @DataProvider(name = "allInputOperationsData")
409 public Object[][] getHeadlessModeInputParams()
411 return new Object[][] {
412 // headless mode input operations
413 { "[TESTOUTPUT] arg --colour='zappo' was set",
414 "Failed setting arg --colour" },
415 { "[TESTOUTPUT] arg --props='test/jalview/bin/testProps.jvprops' was set",
416 "Failed setting arg --props" },
417 { "[TESTOUTPUT] arg --sortbytree was set",
418 "Failed setting arg --sortbytree" },
419 { "[TESTOUTPUT] arg --jabaws='http://www.compbio.dundee.ac.uk/jabaws' was set",
420 "Failed setting arg --jabaws" },
421 { "[TESTOUTPUT] arg --open='examples/uniref50.fa' was set",
422 "Failed setting arg --open" },
423 { "[TESTOUTPUT] arg --features='examples/testdata/plantfdx.features' was set",
424 "Failed setting arg --features" },
425 { "[TESTOUTPUT] arg --annotations='examples/testdata/plantfdx.annotations' was set",
426 "Failed setting arg --annotations" },
427 { "[TESTOUTPUT] arg --tree='examples/testdata/uniref50_test_tree' was set",
428 "Failed setting arg --tree" },
429 // non headless mode input operations
430 { "[TESTOUTPUT] arg --nousagestats was set",
431 "Failed setting arg --nousagestats" },
432 { "[TESTOUTPUT] arg --noquestionnaire was set",
433 "Failed setting arg --noquestionnaire" }
438 @DataProvider(name = "headlessModeOutputOperationsData")
439 public static Object[][] getHeadlessModeOutputParams()
441 // JBPNote: I'm not clear why need to specify full path for output file
442 // when running tests on build server, but we will keep this patch for now
444 // https://issues.jalview.org/browse/JAL-1889?focusedCommentId=21609&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-21609
445 String workingDir = "test/jalview/bin/";
446 return new Object[][] {
448 { "--headless --open examples/uniref50.fa", " --image",
449 workingDir + "test_uniref50_out.eps", true, MINFILESIZE_BIG,
451 { "--headless --open examples/uniref50.fa", " --image",
452 workingDir + "test_uniref50_out.eps", false, MINFILESIZE_BIG,
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.svg", false, MINFILESIZE_BIG,
466 { "--headless --open examples/uniref50.fa", " --image",
467 workingDir + "test_uniref50_out.png", true, MINFILESIZE_BIG,
469 { "--headless --open examples/uniref50.fa", " --image",
470 workingDir + "test_uniref50_out.html", true, MINFILESIZE_BIG,
472 { "--headless --open examples/uniref50.fa", " --output",
473 workingDir + "test_uniref50_out.mfa", true, MINFILESIZE_SMALL,
475 { "--headless --open examples/uniref50.fa", " --output",
476 workingDir + "test_uniref50_out.aln", true, MINFILESIZE_SMALL,
478 { "--headless --open examples/uniref50.fa", " --output",
479 workingDir + "test_uniref50_out.msf", 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.pir", true, MINFILESIZE_SMALL,
487 { "--headless --open examples/uniref50.fa", " --output",
488 workingDir + "test_uniref50_out.pfam", true, MINFILESIZE_SMALL,
490 { "--headless --open examples/uniref50.fa", " --output",
491 workingDir + "test_uniref50_out.blc", true, MINFILESIZE_SMALL,
493 { "--headless --open examples/uniref50.fa", " --output",
494 workingDir + "test_uniref50_out.jvp", true, MINFILESIZE_SMALL,
500 @DataProvider(name = "headlessModeOutputToStdout")
501 public static Object[][] getHeadlessModeOutputToStdout()
503 // JBPNote: I'm not clear why need to specify full path for output file
504 // when running tests on build server, but we will keep this patch for now
506 // https://issues.jalview.org/browse/JAL-1889?focusedCommentId=21609&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-21609
507 String workingDir = "test/jalview/bin/";
508 return new Object[][] {
510 { "--open=examples/uniref50.fa --output=-",
511 workingDir + "/uniref50-output.fa", TEST_TIMEOUT },
512 { "--open examples/uniref50.fa --output -",
513 workingDir + "/uniref50-output.fa", TEST_TIMEOUT },
514 { "--open examples/uniref50.fa --output=[format=blc]-",
515 workingDir + "/uniref50-output.blc", TEST_TIMEOUT },
516 { "--open examples/uniref50.fa --output - --format blc",
517 workingDir + "/uniref50-output.blc", TEST_TIMEOUT },