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.nio.file.Path;
33 import java.nio.file.Paths;
34 import java.util.ArrayList;
36 import org.testng.Assert;
37 import org.testng.FileAssert;
38 import org.testng.annotations.BeforeClass;
39 import org.testng.annotations.BeforeTest;
40 import org.testng.annotations.DataProvider;
41 import org.testng.annotations.Test;
43 import io.github.classgraph.ClassGraph;
44 import io.github.classgraph.ModuleRef;
45 import io.github.classgraph.ScanResult;
47 public class CommandLineOperations
50 @BeforeClass(alwaysRun = true)
51 public void setUpJvOptionPane()
53 JvOptionPane.setInteractiveMode(false);
54 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
57 private static final int TEST_TIMEOUT = 12500; // Note longer timeout needed
59 // full test run than on
62 private static final int SETUP_TIMEOUT = 9000;
64 private static final int MINFILESIZE_SMALL = 2096;
66 private static final int MINFILESIZE_BIG = 4096;
68 private ArrayList<String> successfulCMDs = new ArrayList<>();
72 * http://stackoverflow.com/questions/808276/how-to-add-a-timeout-value-when
73 * -using-javas-runtime-exec
78 private static class Worker extends Thread
80 private final Process process;
82 private BufferedReader outputReader;
84 private BufferedReader errorReader;
88 private Worker(Process process)
90 this.process = process;
98 exit = process.waitFor();
99 } catch (InterruptedException ignore)
105 public BufferedReader getOutputReader()
110 public void setOutputReader(BufferedReader outputReader)
112 this.outputReader = outputReader;
115 public BufferedReader getErrorReader()
120 public void setErrorReader(BufferedReader errorReader)
122 this.errorReader = errorReader;
126 private static ClassGraph scanner = null;
128 private static String classpath = null;
130 private static String modules = null;
132 private static String java_exe = null;
134 public synchronized static String getClassPath()
138 scanner = new ClassGraph();
139 ScanResult scan = scanner.scan();
140 classpath = scan.getClasspath();
142 for (ModuleRef mr : scan.getModules())
144 modules.concat(mr.getName());
146 java_exe = System.getProperty("java.home") + File.separator + "bin"
147 + File.separator + "java";
150 while (classpath == null)
155 } catch (InterruptedException x)
163 private Worker getJalviewDesktopRunner(boolean withAwt, String cmd,
166 // Note: JAL-3065 - don't include quotes for lib/* because the arguments are
167 // not expanded by the shell
168 String classpath = getClassPath();
169 String _cmd = java_exe + " "
170 + (withAwt ? "-Djava.awt.headless=true" : "")
171 + " -classpath " + classpath
172 + (modules.length() > 2 ? "--add-modules=\"" + modules + "\""
174 + " jalview.bin.Jalview ";
175 Process ls2_proc = null;
176 Worker worker = null;
179 ls2_proc = Runtime.getRuntime().exec(_cmd + cmd);
180 } catch (Throwable e1)
182 e1.printStackTrace();
184 if (ls2_proc != null)
186 BufferedReader outputReader = new BufferedReader(
187 new InputStreamReader(ls2_proc.getInputStream()));
188 BufferedReader errorReader = new BufferedReader(
189 new InputStreamReader(ls2_proc.getErrorStream()));
190 worker = new Worker(ls2_proc);
194 worker.join(timeout);
195 } catch (InterruptedException e)
197 System.err.println("Thread interrupted");
199 worker.setOutputReader(outputReader);
200 worker.setErrorReader(errorReader);
205 @Test(groups = { "Functional" })
206 public void reportCurrentWorkingDirectory()
210 Path currentRelativePath = Paths.get("");
211 String s = currentRelativePath.toAbsolutePath().toString();
212 System.out.println("Test CWD is " + s);
213 } catch (Exception q)
219 @BeforeTest(alwaysRun = true)
220 public void initialize()
222 new CommandLineOperations();
225 @BeforeTest(alwaysRun = true)
226 public void setUpForHeadlessCommandLineInputOperations()
229 String cmds = "nodisplay -open examples/uniref50.fa -sortbytree -props test/jalview/bin/testProps.jvprops -colour zappo "
230 + "-jabaws http://www.compbio.dundee.ac.uk/jabaws -nosortbytree "
231 + "-features examples/testdata/plantfdx.features -annotations examples/testdata/plantfdx.annotations -tree examples/testdata/uniref50_test_tree";
232 Worker worker = getJalviewDesktopRunner(true, cmds, SETUP_TIMEOUT);
234 while ((ln = worker.getOutputReader().readLine()) != null)
236 System.out.println(ln);
237 successfulCMDs.add(ln);
239 while ((ln = worker.getErrorReader().readLine()) != null)
241 System.err.println(ln);
245 @BeforeTest(alwaysRun = true)
246 public void setUpForCommandLineInputOperations() throws IOException
248 String cmds = "-open examples/uniref50.fa -noquestionnaire -nousagestats";
249 final Worker worker = getJalviewDesktopRunner(false, cmds,
253 // number of lines expected on STDERR when Jalview starts up normally
254 // may need to adjust this if Jalview is excessively noisy ?
255 final int STDERR_SETUPLINES=30;
257 // thread monitors stderr - bails after SETUP_TIMEOUT or when
258 // STDERR_SETUPLINES have been read
259 Thread runner = new Thread(new Runnable()
267 while ((ln = worker.getErrorReader().readLine()) != null)
269 System.out.println(ln);
270 successfulCMDs.add(ln);
271 if (++count > STDERR_SETUPLINES)
276 } catch (Exception e)
279 "Unexpected Exception reading stderr from the Jalview process");
284 long t = System.currentTimeMillis() + SETUP_TIMEOUT;
286 while (!runner.isInterrupted() && System.currentTimeMillis() < t)
291 } catch (InterruptedException e)
296 if (worker != null && worker.exit == null)
299 Thread.currentThread().interrupt();
300 worker.process.destroy();
304 @Test(groups = { "Functional" }, dataProvider = "allInputOperationsData")
305 public void testAllInputOperations(String expectedString,
308 Assert.assertTrue(successfulCMDs.contains(expectedString), failureMsg);
314 dataProvider = "headlessModeOutputOperationsData")
315 public void testHeadlessModeOutputOperations(String harg, String type,
316 String fileName, boolean withAWT, int expectedMinFileSize,
319 String cmd = harg + type + " " + fileName;
320 // System.out.println(">>>>>>>>>>>>>>>> Command : " + cmd);
321 File file = new File(fileName);
323 Worker worker = getJalviewDesktopRunner(withAWT, cmd, timeout);
324 assertNotNull(worker, "worker is null");
325 String msg = "Didn't create an output" + type + " file.[" + harg + "]";
326 assertTrue(file.exists(), msg);
327 FileAssert.assertFile(file, msg);
328 FileAssert.assertMinLength(file, expectedMinFileSize);
329 if (worker != null && worker.exit == null)
332 Thread.currentThread().interrupt();
333 worker.process.destroy();
334 Assert.fail("Jalview did not exit after " + type
335 + " generation (try running test again to verify - timeout at "
336 + timeout + "ms). [" + harg + "]");
341 @DataProvider(name = "allInputOperationsData")
342 public Object[][] getHeadlessModeInputParams()
344 return new Object[][] {
345 // headless mode input operations
346 { "CMD [-color zappo] executed successfully!",
347 "Failed command : -color zappo" },
348 { "CMD [-props test/jalview/bin/testProps.jvprops] executed successfully!",
349 "Failed command : -props File" },
350 { "CMD [-sortbytree] executed successfully!",
351 "Failed command : -sortbytree" },
352 { "CMD [-jabaws http://www.compbio.dundee.ac.uk/jabaws] executed successfully!",
353 "Failed command : -jabaws http://www.compbio.dundee.ac.uk/jabaws" },
354 { "CMD [-open examples/uniref50.fa] executed successfully!",
355 "Failed command : -open examples/uniref50.fa" },
356 { "CMD [-nosortbytree] executed successfully!",
357 "Failed command : -nosortbytree" },
358 { "CMD [-features examples/testdata/plantfdx.features] executed successfully!",
359 "Failed command : -features examples/testdata/plantfdx.features" },
360 { "CMD [-annotations examples/testdata/plantfdx.annotations] executed successfully!",
361 "Failed command : -annotations examples/testdata/plantfdx.annotations" },
362 { "CMD [-tree examples/testdata/uniref50_test_tree] executed successfully!",
363 "Failed command : -tree examples/testdata/uniref50_test_tree" },
364 // non headless mode input operations
365 { "CMD [-nousagestats] executed successfully!",
366 "Failed command : -nousagestats" },
367 { "CMD [-noquestionnaire] executed successfully!",
368 "Failed command : -noquestionnaire" } };
371 @DataProvider(name = "headlessModeOutputOperationsData")
372 public static Object[][] getHeadlessModeOutputParams()
374 // JBPNote: I'm not clear why need to specify full path for output file
375 // when running tests on build server, but we will keep this patch for now
377 // https://issues.jalview.org/browse/JAL-1889?focusedCommentId=21609&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-21609
378 String workingDir = "test/jalview/bin/";
379 return new Object[][] { { "nodisplay -open examples/uniref50.fa",
380 " -eps", workingDir + "test_uniref50_out.eps", true,
381 MINFILESIZE_BIG, TEST_TIMEOUT },
382 { "nodisplay -open examples/uniref50.fa", " -eps",
383 workingDir + "test_uniref50_out.eps", false,
384 MINFILESIZE_BIG, TEST_TIMEOUT },
385 { "nogui -open examples/uniref50.fa", " -eps",
386 workingDir + "test_uniref50_out.eps", true, MINFILESIZE_BIG,
388 { "nogui -open examples/uniref50.fa", " -eps",
389 workingDir + "test_uniref50_out.eps", false,
390 MINFILESIZE_BIG, TEST_TIMEOUT },
391 { "headless -open examples/uniref50.fa", " -eps",
392 workingDir + "test_uniref50_out.eps", true, MINFILESIZE_BIG,
394 { "headless -open examples/uniref50.fa", " -svg",
395 workingDir + "test_uniref50_out.svg", false,
396 MINFILESIZE_BIG, TEST_TIMEOUT },
397 { "headless -open examples/uniref50.fa", " -png",
398 workingDir + "test_uniref50_out.png", true, MINFILESIZE_BIG,
400 { "headless -open examples/uniref50.fa", " -html",
401 workingDir + "test_uniref50_out.html", true,
402 MINFILESIZE_BIG, TEST_TIMEOUT },
403 { "headless -open examples/uniref50.fa", " -fasta",
404 workingDir + "test_uniref50_out.mfa", true, MINFILESIZE_SMALL,
406 { "headless -open examples/uniref50.fa", " -clustal",
407 workingDir + "test_uniref50_out.aln", true, MINFILESIZE_SMALL,
409 { "headless -open examples/uniref50.fa", " -msf",
410 workingDir + "test_uniref50_out.msf", true, MINFILESIZE_SMALL,
412 { "headless -open examples/uniref50.fa", " -pileup",
413 workingDir + "test_uniref50_out.aln", true, MINFILESIZE_SMALL,
415 { "headless -open examples/uniref50.fa", " -pir",
416 workingDir + "test_uniref50_out.pir", true, MINFILESIZE_SMALL,
418 { "headless -open examples/uniref50.fa", " -pfam",
419 workingDir + "test_uniref50_out.pfam", true, MINFILESIZE_SMALL,
421 { "headless -open examples/uniref50.fa", " -blc",
422 workingDir + "test_uniref50_out.blc", true, MINFILESIZE_SMALL,
424 { "headless -open examples/uniref50.fa", " -jalview",
425 workingDir + "test_uniref50_out.jvp", true, MINFILESIZE_SMALL,