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 = 9000; // 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 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());
145 while (classpath == null)
150 } catch (InterruptedException x)
158 private Worker getJalviewDesktopRunner(boolean withAwt, String cmd,
161 // Note: JAL-3065 - don't include quotes for lib/* because the arguments are
162 // not expanded by the shell
163 String classpath = getClassPath();
164 String _cmd = "java " + (withAwt ? "-Djava.awt.headless=true" : "")
165 + " -classpath " + classpath
166 + (modules.length() > 2 ? "--add-modules=\"" + modules + "\""
168 + " jalview.bin.Jalview ";
169 Process ls2_proc = null;
170 Worker worker = null;
173 ls2_proc = Runtime.getRuntime().exec(_cmd + cmd);
174 } catch (Throwable e1)
176 e1.printStackTrace();
178 if (ls2_proc != null)
180 BufferedReader outputReader = new BufferedReader(
181 new InputStreamReader(ls2_proc.getInputStream()));
182 BufferedReader errorReader = new BufferedReader(
183 new InputStreamReader(ls2_proc.getErrorStream()));
184 worker = new Worker(ls2_proc);
188 worker.join(timeout);
189 } catch (InterruptedException e)
191 System.err.println("Thread interrupted");
193 worker.setOutputReader(outputReader);
194 worker.setErrorReader(errorReader);
199 @Test(groups = { "Functional" })
200 public void reportCurrentWorkingDirectory()
204 Path currentRelativePath = Paths.get("");
205 String s = currentRelativePath.toAbsolutePath().toString();
206 System.out.println("Test CWD is " + s);
207 System.err.println("Test CWD is " + s);
208 } catch (Exception q)
214 @BeforeTest(alwaysRun = true)
215 public void initialize()
217 new CommandLineOperations();
220 @BeforeTest(alwaysRun = true)
221 public void setUpForHeadlessCommandLineInputOperations()
224 String cmds = "nodisplay -open examples/uniref50.fa -sortbytree -props test/jalview/io/testProps.jvprops -colour zappo "
225 + "-jabaws http://www.compbio.dundee.ac.uk/jabaws -nosortbytree "
226 + "-features examples/testdata/plantfdx.features -annotations examples/testdata/plantfdx.annotations -tree examples/testdata/uniref50_test_tree";
227 Worker worker = getJalviewDesktopRunner(true, cmds, SETUP_TIMEOUT);
229 while ((ln = worker.getOutputReader().readLine()) != null)
231 System.out.println(ln);
232 successfulCMDs.add(ln);
234 while ((ln = worker.getErrorReader().readLine()) != null)
236 System.err.println(ln);
240 @BeforeTest(alwaysRun = true)
241 public void setUpForCommandLineInputOperations() throws IOException
243 String cmds = "-open examples/uniref50.fa -noquestionnaire -nousagestats";
244 Worker worker = getJalviewDesktopRunner(false, cmds, SETUP_TIMEOUT);
247 while ((ln = worker.getErrorReader().readLine()) != null)
249 System.out.println(ln);
250 successfulCMDs.add(ln);
256 if (worker != null && worker.exit == null)
259 Thread.currentThread().interrupt();
260 worker.process.destroy();
264 @Test(groups = { "Functional" }, dataProvider = "allInputOperationsData")
265 public void testAllInputOperations(String expectedString,
268 Assert.assertTrue(successfulCMDs.contains(expectedString), failureMsg);
273 { "Functional", "testben" },
274 dataProvider = "headlessModeOutputOperationsData")
275 public void testHeadlessModeOutputOperations(String harg, String type,
276 String fileName, boolean withAWT, int expectedMinFileSize,
279 String cmd = harg + type + " " + fileName;
280 // System.out.println(">>>>>>>>>>>>>>>> Command : " + cmd);
281 File file = new File(fileName);
283 Worker worker = getJalviewDesktopRunner(withAWT, cmd, timeout);
284 assertNotNull(worker, "worker is null");
285 String msg = "Didn't create an output" + type + " file.[" + harg + "]";
286 assertTrue(file.exists(), msg);
287 FileAssert.assertFile(file, msg);
288 FileAssert.assertMinLength(file, expectedMinFileSize);
289 if (worker != null && worker.exit == null)
292 Thread.currentThread().interrupt();
293 worker.process.destroy();
294 Assert.fail("Jalview did not exit after " + type
295 + " generation (try running test again to verify - timeout at "
296 + timeout + "ms). [" + harg + "]");
301 @DataProvider(name = "allInputOperationsData")
302 public Object[][] getHeadlessModeInputParams()
304 return new Object[][] {
305 // headless mode input operations
306 { "CMD [-color zappo] executed successfully!",
307 "Failed command : -color zappo" },
308 { "CMD [-props test/jalview/io/testProps.jvprops] executed successfully!",
309 "Failed command : -props File" },
310 { "CMD [-sortbytree] executed successfully!",
311 "Failed command : -sortbytree" },
312 { "CMD [-jabaws http://www.compbio.dundee.ac.uk/jabaws] executed successfully!",
313 "Failed command : -jabaws http://www.compbio.dundee.ac.uk/jabaws" },
314 { "CMD [-open examples/uniref50.fa] executed successfully!",
315 "Failed command : -open examples/uniref50.fa" },
316 { "CMD [-nosortbytree] executed successfully!",
317 "Failed command : -nosortbytree" },
318 { "CMD [-features examples/testdata/plantfdx.features] executed successfully!",
319 "Failed command : -features examples/testdata/plantfdx.features" },
320 { "CMD [-annotations examples/testdata/plantfdx.annotations] executed successfully!",
321 "Failed command : -annotations examples/testdata/plantfdx.annotations" },
322 { "CMD [-tree examples/testdata/uniref50_test_tree] executed successfully!",
323 "Failed command : -tree examples/testdata/uniref50_test_tree" },
324 // non headless mode input operations
325 { "CMD [-nousagestats] executed successfully!",
326 "Failed command : -nousagestats" },
327 { "CMD [-noquestionnaire] executed successfully!",
328 "Failed command : -noquestionnaire" } };
331 @DataProvider(name = "headlessModeOutputOperationsData")
332 public static Object[][] getHeadlessModeOutputParams()
334 // JBPNote: I'm not clear why need to specify full path for output file
335 // when running tests on build server, but we will keep this patch for now
337 // https://issues.jalview.org/browse/JAL-1889?focusedCommentId=21609&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-21609
338 String workingDir = "test/jalview/bin/";
339 return new Object[][] { { "nodisplay -open examples/uniref50.fa",
340 " -eps", workingDir + "test_uniref50_out.eps", true,
341 MINFILESIZE_BIG, TEST_TIMEOUT },
342 { "nodisplay -open examples/uniref50.fa", " -eps",
343 workingDir + "test_uniref50_out.eps", false,
344 MINFILESIZE_BIG, TEST_TIMEOUT },
345 { "nogui -open examples/uniref50.fa", " -eps",
346 workingDir + "test_uniref50_out.eps", true, MINFILESIZE_BIG,
348 { "nogui -open examples/uniref50.fa", " -eps",
349 workingDir + "test_uniref50_out.eps", false,
350 MINFILESIZE_BIG, TEST_TIMEOUT },
351 { "headless -open examples/uniref50.fa", " -eps",
352 workingDir + "test_uniref50_out.eps", true, MINFILESIZE_BIG,
354 { "headless -open examples/uniref50.fa", " -svg",
355 workingDir + "test_uniref50_out.svg", false,
356 MINFILESIZE_BIG, TEST_TIMEOUT },
357 { "headless -open examples/uniref50.fa", " -png",
358 workingDir + "test_uniref50_out.png", true, MINFILESIZE_BIG,
360 { "headless -open examples/uniref50.fa", " -html",
361 workingDir + "test_uniref50_out.html", true,
362 MINFILESIZE_BIG, TEST_TIMEOUT },
363 { "headless -open examples/uniref50.fa", " -fasta",
364 workingDir + "test_uniref50_out.mfa", true, MINFILESIZE_SMALL,
366 { "headless -open examples/uniref50.fa", " -clustal",
367 workingDir + "test_uniref50_out.aln", true, MINFILESIZE_SMALL,
369 { "headless -open examples/uniref50.fa", " -msf",
370 workingDir + "test_uniref50_out.msf", true, MINFILESIZE_SMALL,
372 { "headless -open examples/uniref50.fa", " -pileup",
373 workingDir + "test_uniref50_out.aln", true, MINFILESIZE_SMALL,
375 { "headless -open examples/uniref50.fa", " -pir",
376 workingDir + "test_uniref50_out.pir", true, MINFILESIZE_SMALL,
378 { "headless -open examples/uniref50.fa", " -pfam",
379 workingDir + "test_uniref50_out.pfam", true, MINFILESIZE_SMALL,
381 { "headless -open examples/uniref50.fa", " -blc",
382 workingDir + "test_uniref50_out.blc", true, MINFILESIZE_SMALL,
384 { "headless -open examples/uniref50.fa", " -jalview",
385 workingDir + "test_uniref50_out.jvp", true, MINFILESIZE_SMALL,