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.util.ArrayList;
34 import org.testng.Assert;
35 import org.testng.FileAssert;
36 import org.testng.annotations.BeforeClass;
37 import org.testng.annotations.BeforeTest;
38 import org.testng.annotations.DataProvider;
39 import org.testng.annotations.Test;
41 import io.github.classgraph.ClassGraph;
42 import io.github.classgraph.ModuleRef;
43 import io.github.classgraph.ScanResult;
45 public class CommandLineOperations
48 @BeforeClass(alwaysRun = true)
49 public void setUpJvOptionPane()
51 JvOptionPane.setInteractiveMode(false);
52 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
55 private static final int TEST_TIMEOUT = 9000; // Note longer timeout needed
57 // full test run than on
60 private static final int SETUP_TIMEOUT = 9000;
62 private static final int MINFILESIZE_SMALL = 2096;
64 private static final int MINFILESIZE_BIG = 4096;
66 private ArrayList<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 public synchronized static String getClassPath()
134 scanner = new ClassGraph();
135 ScanResult scan = scanner.scan();
136 classpath = scan.getClasspath();
138 for (ModuleRef mr : scan.getModules())
140 modules.concat(mr.getName());
143 while (classpath == null)
148 } catch (InterruptedException x)
156 private Worker getJalviewDesktopRunner(boolean withAwt, String cmd,
159 // Note: JAL-3065 - don't include quotes for lib/* because the arguments are
160 // not expanded by the shell
161 String classpath = getClassPath();
162 String _cmd = "java " + (withAwt ? "-Djava.awt.headless=true" : "")
163 + " -classpath " + classpath
164 + (modules.length() > 2 ? "--add-modules=\"" + modules + "\""
166 + " jalview.bin.Jalview ";
167 Process ls2_proc = null;
168 Worker worker = null;
171 ls2_proc = Runtime.getRuntime().exec(_cmd + cmd);
172 } catch (Throwable e1)
174 e1.printStackTrace();
176 if (ls2_proc != null)
178 BufferedReader outputReader = new BufferedReader(
179 new InputStreamReader(ls2_proc.getInputStream()));
180 BufferedReader errorReader = new BufferedReader(
181 new InputStreamReader(ls2_proc.getErrorStream()));
182 worker = new Worker(ls2_proc);
186 worker.join(timeout);
187 } catch (InterruptedException e)
189 System.err.println("Thread interrupted");
191 worker.setOutputReader(outputReader);
192 worker.setErrorReader(errorReader);
197 @BeforeTest(alwaysRun = true)
198 public void initialize()
200 new CommandLineOperations();
203 @BeforeTest(alwaysRun = true)
204 public void setUpForHeadlessCommandLineInputOperations()
207 String cmds = "nodisplay -open examples/uniref50.fa -sortbytree -props test/jalview/io/testProps.jvprops -colour zappo "
208 + "-jabaws http://www.compbio.dundee.ac.uk/jabaws -nosortbytree "
209 + "-features examples/testdata/plantfdx.features -annotations examples/testdata/plantfdx.annotations -tree examples/testdata/uniref50_test_tree";
210 Worker worker = getJalviewDesktopRunner(true, cmds, SETUP_TIMEOUT);
212 while ((ln = worker.getOutputReader().readLine()) != null)
214 System.out.println(ln);
215 successfulCMDs.add(ln);
217 while ((ln = worker.getErrorReader().readLine()) != null)
219 System.err.println(ln);
223 @BeforeTest(alwaysRun = true)
224 public void setUpForCommandLineInputOperations() throws IOException
226 String cmds = "-open examples/uniref50.fa -noquestionnaire -nousagestats";
227 Worker worker = getJalviewDesktopRunner(false, cmds, SETUP_TIMEOUT);
230 while ((ln = worker.getErrorReader().readLine()) != null)
232 System.out.println(ln);
233 successfulCMDs.add(ln);
239 if (worker != null && worker.exit == null)
242 Thread.currentThread().interrupt();
243 worker.process.destroy();
247 @Test(groups = { "Functional" }, dataProvider = "allInputOperationsData")
248 public void testAllInputOperations(String expectedString,
251 Assert.assertTrue(successfulCMDs.contains(expectedString), failureMsg);
256 { "Functional", "testben" },
257 dataProvider = "headlessModeOutputOperationsData")
258 public void testHeadlessModeOutputOperations(String harg, String type,
259 String fileName, boolean withAWT, int expectedMinFileSize,
262 String cmd = harg + type + " " + fileName;
263 // System.out.println(">>>>>>>>>>>>>>>> Command : " + cmd);
264 File file = new File(fileName);
266 Worker worker = getJalviewDesktopRunner(withAWT, cmd, timeout);
267 assertNotNull(worker, "worker is null");
268 String msg = "Didn't create an output" + type + " file at '"
269 + file.getAbsolutePath() + "'.[" + harg + "]";
270 assertTrue(file.exists(), msg);
271 FileAssert.assertFile(file, msg);
272 FileAssert.assertMinLength(file, expectedMinFileSize);
273 if (worker != null && worker.exit == null)
276 Thread.currentThread().interrupt();
277 worker.process.destroy();
278 Assert.fail("Jalview did not exit after " + type
279 + " generation (try running test again to verify - timeout at "
280 + timeout + "ms). [" + harg + "]");
285 @DataProvider(name = "allInputOperationsData")
286 public Object[][] getHeadlessModeInputParams()
288 return new Object[][] {
289 // headless mode input operations
290 { "CMD [-color zappo] executed successfully!",
291 "Failed command : -color zappo" },
292 { "CMD [-props test/jalview/io/testProps.jvprops] executed successfully!",
293 "Failed command : -props File" },
294 { "CMD [-sortbytree] executed successfully!",
295 "Failed command : -sortbytree" },
296 { "CMD [-jabaws http://www.compbio.dundee.ac.uk/jabaws] executed successfully!",
297 "Failed command : -jabaws http://www.compbio.dundee.ac.uk/jabaws" },
298 { "CMD [-open examples/uniref50.fa] executed successfully!",
299 "Failed command : -open examples/uniref50.fa" },
300 { "CMD [-nosortbytree] executed successfully!",
301 "Failed command : -nosortbytree" },
302 { "CMD [-features examples/testdata/plantfdx.features] executed successfully!",
303 "Failed command : -features examples/testdata/plantfdx.features" },
304 { "CMD [-annotations examples/testdata/plantfdx.annotations] executed successfully!",
305 "Failed command : -annotations examples/testdata/plantfdx.annotations" },
306 { "CMD [-tree examples/testdata/uniref50_test_tree] executed successfully!",
307 "Failed command : -tree examples/testdata/uniref50_test_tree" },
308 // non headless mode input operations
309 { "CMD [-nousagestats] executed successfully!",
310 "Failed command : -nousagestats" },
311 { "CMD [-noquestionnaire] executed successfully!",
312 "Failed command : -noquestionnaire" } };
315 @DataProvider(name = "headlessModeOutputOperationsData")
316 public static Object[][] getHeadlessModeOutputParams()
318 // JBPNote: I'm not clear why need to specify full path for output file
319 // when running tests on build server, but we will keep this patch for now
321 // https://issues.jalview.org/browse/JAL-1889?focusedCommentId=21609&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-21609
322 String workingDir = "test/jalview/bin/";
323 return new Object[][] { { "nodisplay -open examples/uniref50.fa",
324 " -eps", workingDir + "test_uniref50_out.eps", true,
325 MINFILESIZE_BIG, TEST_TIMEOUT },
326 { "nodisplay -open examples/uniref50.fa", " -eps",
327 workingDir + "test_uniref50_out.eps", false,
328 MINFILESIZE_BIG, TEST_TIMEOUT },
329 { "nogui -open examples/uniref50.fa", " -eps",
330 workingDir + "test_uniref50_out.eps", true, MINFILESIZE_BIG,
332 { "nogui -open examples/uniref50.fa", " -eps",
333 workingDir + "test_uniref50_out.eps", false,
334 MINFILESIZE_BIG, TEST_TIMEOUT },
335 { "headless -open examples/uniref50.fa", " -eps",
336 workingDir + "test_uniref50_out.eps", true, MINFILESIZE_BIG,
338 { "headless -open examples/uniref50.fa", " -svg",
339 workingDir + "test_uniref50_out.svg", false,
340 MINFILESIZE_BIG, TEST_TIMEOUT },
341 { "headless -open examples/uniref50.fa", " -png",
342 workingDir + "test_uniref50_out.png", true, MINFILESIZE_BIG,
344 { "headless -open examples/uniref50.fa", " -html",
345 workingDir + "test_uniref50_out.html", true,
346 MINFILESIZE_BIG, TEST_TIMEOUT },
347 { "headless -open examples/uniref50.fa", " -fasta",
348 workingDir + "test_uniref50_out.mfa", true, MINFILESIZE_SMALL,
350 { "headless -open examples/uniref50.fa", " -clustal",
351 workingDir + "test_uniref50_out.aln", true, MINFILESIZE_SMALL,
353 { "headless -open examples/uniref50.fa", " -msf",
354 workingDir + "test_uniref50_out.msf", true, MINFILESIZE_SMALL,
356 { "headless -open examples/uniref50.fa", " -pileup",
357 workingDir + "test_uniref50_out.aln", true, MINFILESIZE_SMALL,
359 { "headless -open examples/uniref50.fa", " -pir",
360 workingDir + "test_uniref50_out.pir", true, MINFILESIZE_SMALL,
362 { "headless -open examples/uniref50.fa", " -pfam",
363 workingDir + "test_uniref50_out.pfam", true, MINFILESIZE_SMALL,
365 { "headless -open examples/uniref50.fa", " -blc",
366 workingDir + "test_uniref50_out.blc", true, MINFILESIZE_SMALL,
368 { "headless -open examples/uniref50.fa", " -jalview",
369 workingDir + "test_uniref50_out.jvp", true, MINFILESIZE_SMALL,