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;
33 import java.util.List;
35 import org.testng.Assert;
36 import org.testng.FileAssert;
37 import org.testng.annotations.BeforeClass;
38 import org.testng.annotations.DataProvider;
39 import org.testng.annotations.Test;
41 import io.github.classgraph.ClassGraph;
42 import jalview.gui.JvOptionPane;
44 public class CommandLineOperationsNG
47 @BeforeClass(alwaysRun = true)
48 public void setUpJvOptionPane()
50 JvOptionPane.setInteractiveMode(false);
51 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
54 // Note longer timeout needed on full test run than on individual tests
55 private static final int TEST_TIMEOUT = 13000;
57 private static final int SETUP_TIMEOUT = 9500;
59 private static final int MINFILESIZE_SMALL = 2096;
61 private static final int MINFILESIZE_BIG = 4096;
63 private List<String> successfulCMDs = new ArrayList<>();
67 * http://stackoverflow.com/questions/808276/how-to-add-a-timeout-value-when
68 * -using-javas-runtime-exec
73 private static class Worker extends Thread
75 private final Process process;
77 private BufferedReader outputReader;
79 private BufferedReader errorReader;
83 private Worker(Process process)
85 this.process = process;
93 exit = process.waitFor();
94 } catch (InterruptedException ignore)
100 public BufferedReader getOutputReader()
105 public void setOutputReader(BufferedReader outputReader)
107 this.outputReader = outputReader;
110 public BufferedReader getErrorReader()
115 public void setErrorReader(BufferedReader errorReader)
117 this.errorReader = errorReader;
121 private static ClassGraph scanner = null;
123 private static String classpath = null;
125 private static String modules = null;
127 private static String java_exe = null;
129 public synchronized static String getClassPath()
131 // if (scanner == null)
133 // scanner = new ClassGraph();
134 // ScanResult scan = scanner.scan();
135 // classpath = scan.getClasspath();
137 // for (ModuleRef mr : scan.getModules())
139 // modules.concat(mr.getName());
141 java_exe = System.getProperty("java.home") + File.separator + "bin"
142 + File.separator + "java";
146 // while (classpath == null)
151 // } catch (InterruptedException x)
159 private Worker getJalviewDesktopRunner(boolean withAwt, String cmd,
162 // Note: JAL-3065 - don't include quotes for lib/* because the arguments are
163 // not expanded by the shell
164 // String classpath = getClassPath();
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);
181 String _cmd = java_exe + " "
182 + (withAwt ? "-Djava.awt.headless=true" : "") + " -classpath "
184 + ((modules != null && modules.length() > 2)
185 ? "--add-modules=\"" + modules + "\""
187 + " jalview.bin.Jalview ";
188 Process ls2_proc = null;
189 Worker worker = null;
192 cmd = " --testoutput " + cmd;
193 System.out.println("Running '" + _cmd + cmd + "'");
194 ls2_proc = Runtime.getRuntime().exec(_cmd + cmd);
195 } catch (Throwable e1)
197 e1.printStackTrace();
199 if (ls2_proc != null)
201 BufferedReader outputReader = new BufferedReader(
202 new InputStreamReader(ls2_proc.getInputStream()));
203 BufferedReader errorReader = new BufferedReader(
204 new InputStreamReader(ls2_proc.getErrorStream()));
205 worker = new Worker(ls2_proc);
209 worker.join(timeout);
210 } catch (InterruptedException e)
212 System.err.println("Thread interrupted");
214 worker.setOutputReader(outputReader);
215 worker.setErrorReader(errorReader);
220 @Test(groups = { "Functional", "testTask1" })
221 public void reportCurrentWorkingDirectory()
225 Path currentRelativePath = Paths.get("");
226 String s = currentRelativePath.toAbsolutePath().toString();
227 System.out.println("Test CWD is " + s);
228 } catch (Exception q)
234 @BeforeClass(alwaysRun = true)
235 public void initialize()
237 new CommandLineOperationsNG();
240 @BeforeClass(alwaysRun = true)
241 public void setUpForHeadlessCommandLineInputOperations()
244 String cmds = "--headless " + "--open examples/uniref50.fa "
246 + "--props test/jalview/bin/testProps.jvprops "
248 + "--jabaws http://www.compbio.dundee.ac.uk/jabaws "
249 + "--features examples/testdata/plantfdx.features "
250 + "--annotations examples/testdata/plantfdx.annotations "
251 + "--tree examples/testdata/uniref50_test_tree "
253 Worker worker = getJalviewDesktopRunner(true, cmds, SETUP_TIMEOUT);
255 while ((ln = worker.getOutputReader().readLine()) != null)
257 System.out.println(ln);
258 successfulCMDs.add(ln);
260 while ((ln = worker.getErrorReader().readLine()) != null)
262 System.err.println(ln);
266 @BeforeClass(alwaysRun = true)
267 public void setUpForCommandLineInputOperations() throws IOException
269 String cmds = "--open examples/uniref50.fa --noquestionnaire --nousagestats";
270 final Worker worker = getJalviewDesktopRunner(false, cmds,
273 // number of lines expected on STDERR when Jalview starts up normally
274 // may need to adjust this if Jalview is excessively noisy ?
275 final int STDERR_SETUPLINES = 50;
277 // thread monitors stderr - bails after SETUP_TIMEOUT or when
278 // STDERR_SETUPLINES have been read
279 Thread runner = new Thread(new Runnable()
288 while ((ln = worker.getOutputReader().readLine()) != null)
290 System.out.println(ln);
291 successfulCMDs.add(ln);
292 if (++count > STDERR_SETUPLINES)
297 } catch (Exception e)
300 "Unexpected Exception reading stderr from the Jalview process");
305 long t = System.currentTimeMillis() + SETUP_TIMEOUT;
307 while (!runner.isInterrupted() && System.currentTimeMillis() < t)
312 } catch (InterruptedException e)
317 if (worker != null && worker.exit == null)
320 Thread.currentThread().interrupt();
321 worker.process.destroy();
327 { "Functional", "testTask1" },
328 dataProvider = "allInputOperationsData")
329 public void testAllInputOperations(String expectedString,
332 if ("[TESTOUTPUT] arg --nousagestats was set".equals(expectedString))
333 Assert.assertTrue(successfulCMDs.contains(expectedString),
339 { "Functional", "testTask1" },
340 dataProvider = "headlessModeOutputOperationsData")
341 public void testHeadlessModeOutputOperations(String harg, String type,
342 String fileName, boolean withAWT, int expectedMinFileSize,
345 String cmd = harg + type + " " + fileName;
346 // System.out.println(">>>>>>>>>>>>>>>> Command : " + cmd);
347 File file = new File(fileName);
349 Worker worker = getJalviewDesktopRunner(withAWT, cmd, timeout);
350 assertNotNull(worker, "worker is null");
351 String msg = "Didn't create an output" + type + " file.[" + cmd + "]";
352 assertTrue(file.exists(), msg);
353 FileAssert.assertFile(file, msg);
354 FileAssert.assertMinLength(file, expectedMinFileSize);
355 if (worker != null && worker.exit == null)
358 Thread.currentThread().interrupt();
359 worker.process.destroy();
360 Assert.fail("Jalview did not exit after " + type
361 + " generation (try running test again to verify - timeout at "
362 + timeout + "ms). [" + harg + "]");
367 @DataProvider(name = "allInputOperationsData")
368 public Object[][] getHeadlessModeInputParams()
370 return new Object[][] {
371 // headless mode input operations
372 { "[TESTOUTPUT] arg --colour='zappo' was set",
373 "Failed setting arg --colour" },
374 { "[TESTOUTPUT] arg --props='test/jalview/bin/testProps.jvprops' was set",
375 "Failed setting arg --props" },
376 { "[TESTOUTPUT] arg --sortbytree was set",
377 "Failed setting arg --sortbytree" },
378 { "[TESTOUTPUT] arg --jabaws='http://www.compbio.dundee.ac.uk/jabaws' was set",
379 "Failed setting arg --jabaws" },
380 { "[TESTOUTPUT] arg --open='examples/uniref50.fa' was set",
381 "Failed setting arg --open" },
382 { "[TESTOUTPUT] arg --features='examples/testdata/plantfdx.features' was set",
383 "Failed setting arg --features" },
384 { "[TESTOUTPUT] arg --annotations='examples/testdata/plantfdx.annotations' was set",
385 "Failed setting arg --annotations" },
386 { "[TESTOUTPUT] arg --tree='examples/testdata/uniref50_test_tree' was set",
387 "Failed setting arg --tree" },
388 // non headless mode input operations
389 { "[TESTOUTPUT] arg --nousagestats was set",
390 "Failed setting arg --nousagestats" },
391 { "[TESTOUTPUT] arg --noquestionnaire was set",
392 "Failed setting arg --noquestionnaire" }
397 @DataProvider(name = "headlessModeOutputOperationsData")
398 public static Object[][] getHeadlessModeOutputParams()
400 // JBPNote: I'm not clear why need to specify full path for output file
401 // when running tests on build server, but we will keep this patch for now
403 // https://issues.jalview.org/browse/JAL-1889?focusedCommentId=21609&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-21609
404 String workingDir = "test/jalview/bin/";
405 return new Object[][] {
407 { "--headless --open examples/uniref50.fa", " --image",
408 workingDir + "test_uniref50_out.eps", true, MINFILESIZE_BIG,
410 { "--headless --open examples/uniref50.fa", " --image",
411 workingDir + "test_uniref50_out.eps", false, MINFILESIZE_BIG,
413 { "--headless --open examples/uniref50.fa", " --image",
414 workingDir + "test_uniref50_out.eps", true, MINFILESIZE_BIG,
416 { "--headless --open examples/uniref50.fa", " --image",
417 workingDir + "test_uniref50_out.eps", false, MINFILESIZE_BIG,
419 { "--headless --open examples/uniref50.fa", " --image",
420 workingDir + "test_uniref50_out.eps", true, MINFILESIZE_BIG,
422 { "--headless --open examples/uniref50.fa", " --image",
423 workingDir + "test_uniref50_out.svg", false, MINFILESIZE_BIG,
425 { "--headless --open examples/uniref50.fa", " --image",
426 workingDir + "test_uniref50_out.png", true, MINFILESIZE_BIG,
428 { "--headless --open examples/uniref50.fa", " --image",
429 workingDir + "test_uniref50_out.html", true, MINFILESIZE_BIG,
431 { "--headless --open examples/uniref50.fa", " --output",
432 workingDir + "test_uniref50_out.mfa", true, MINFILESIZE_SMALL,
434 { "--headless --open examples/uniref50.fa", " --output",
435 workingDir + "test_uniref50_out.aln", true, MINFILESIZE_SMALL,
437 { "--headless --open examples/uniref50.fa", " --output",
438 workingDir + "test_uniref50_out.msf", true, MINFILESIZE_SMALL,
440 { "--headless --open examples/uniref50.fa", " --output",
441 workingDir + "test_uniref50_out.aln", true, MINFILESIZE_SMALL,
443 { "--headless --open examples/uniref50.fa", " --output",
444 workingDir + "test_uniref50_out.pir", true, MINFILESIZE_SMALL,
446 { "--headless --open examples/uniref50.fa", " --output",
447 workingDir + "test_uniref50_out.pfam", true, MINFILESIZE_SMALL,
449 { "--headless --open examples/uniref50.fa", " --output",
450 workingDir + "test_uniref50_out.blc", true, MINFILESIZE_SMALL,
452 { "--headless --open examples/uniref50.fa", " --output",
453 workingDir + "test_uniref50_out.jvp", true, MINFILESIZE_SMALL,