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.assertEquals;
24 import static org.testng.Assert.assertNotNull;
25 import static org.testng.Assert.assertTrue;
27 import jalview.gui.JvOptionPane;
28 import jalview.io.DataSourceType;
29 import jalview.io.FileFormat;
30 import jalview.io.FileFormatException;
31 import jalview.io.FileFormatI;
32 import jalview.io.FileFormats;
33 import jalview.io.IdentifyFile;
35 import java.io.BufferedReader;
37 import java.io.IOException;
38 import java.io.InputStreamReader;
39 import java.nio.file.Path;
40 import java.nio.file.Paths;
41 import java.util.ArrayList;
43 import org.testng.Assert;
44 import org.testng.FileAssert;
45 import org.testng.annotations.BeforeClass;
46 import org.testng.annotations.BeforeTest;
47 import org.testng.annotations.DataProvider;
48 import org.testng.annotations.Test;
50 import io.github.classgraph.ClassGraph;
51 import io.github.classgraph.ModuleRef;
52 import io.github.classgraph.ScanResult;
54 public class CommandLineOperations
57 @BeforeClass(alwaysRun = true)
58 public void setUpJvOptionPane()
60 JvOptionPane.setInteractiveMode(false);
61 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
64 private static final int TEST_TIMEOUT = 9000; // Note longer timeout needed
66 // full test run than on
69 private static final int SETUP_TIMEOUT = 9000;
71 private static final int MINFILESIZE_SMALL = 2096;
73 private static final int MINFILESIZE_BIG = 4096;
75 private ArrayList<String> successfulCMDs = new ArrayList<>();
79 * http://stackoverflow.com/questions/808276/how-to-add-a-timeout-value-when
80 * -using-javas-runtime-exec
85 private static class Worker extends Thread
87 private final Process process;
89 private BufferedReader outputReader;
91 private BufferedReader errorReader;
95 private Worker(Process process)
97 this.process = process;
105 exit = process.waitFor();
106 } catch (InterruptedException ignore)
112 public BufferedReader getOutputReader()
117 public void setOutputReader(BufferedReader outputReader)
119 this.outputReader = outputReader;
122 public BufferedReader getErrorReader()
127 public void setErrorReader(BufferedReader errorReader)
129 this.errorReader = errorReader;
133 private static ClassGraph scanner = null;
135 private static String classpath = null;
137 private static String modules = null;
139 private static String java_exe = null;
141 public synchronized static String getClassPath()
145 scanner = new ClassGraph();
146 ScanResult scan = scanner.scan();
147 classpath = scan.getClasspath();
149 for (ModuleRef mr : scan.getModules())
151 modules.concat(mr.getName());
153 java_exe = System.getProperty("java.home") + File.separator + "bin"
154 + File.separator + "java";
157 while (classpath == null)
162 } catch (InterruptedException x)
170 private Worker getJalviewDesktopRunner(boolean withAwt, String cmd,
173 // Note: JAL-3065 - don't include quotes for lib/* because the arguments are
174 // not expanded by the shell
175 String classpath = getClassPath();
176 String _cmd = java_exe + " "
177 + (withAwt ? "-Djava.awt.headless=true" : "")
178 + " -classpath " + classpath
179 + (modules.length() > 2 ? "--add-modules=\"" + modules + "\""
181 + " jalview.bin.Jalview ";
182 Process ls2_proc = null;
183 Worker worker = null;
186 ls2_proc = Runtime.getRuntime().exec(_cmd + cmd);
187 } catch (Throwable e1)
189 e1.printStackTrace();
191 if (ls2_proc != null)
193 BufferedReader outputReader = new BufferedReader(
194 new InputStreamReader(ls2_proc.getInputStream()));
195 BufferedReader errorReader = new BufferedReader(
196 new InputStreamReader(ls2_proc.getErrorStream()));
197 worker = new Worker(ls2_proc);
201 worker.join(timeout);
202 } catch (InterruptedException e)
204 System.err.println("Thread interrupted");
206 worker.setOutputReader(outputReader);
207 worker.setErrorReader(errorReader);
212 @Test(groups = { "Functional" })
213 public void reportCurrentWorkingDirectory()
217 Path currentRelativePath = Paths.get("");
218 String s = currentRelativePath.toAbsolutePath().toString();
219 System.out.println("Test CWD is " + s);
220 } catch (Exception q)
226 @BeforeTest(alwaysRun = true)
227 public void initialize()
229 new CommandLineOperations();
232 @BeforeTest(alwaysRun = true)
233 public void setUpForHeadlessCommandLineInputOperations()
236 String cmds = "nodisplay -open examples/uniref50.fa -sortbytree -props test/jalview/io/testProps.jvprops -colour zappo "
237 + "-jabaws http://www.compbio.dundee.ac.uk/jabaws -nosortbytree "
238 + "-features examples/testdata/plantfdx.features -annotations examples/testdata/plantfdx.annotations -tree examples/testdata/uniref50_test_tree";
239 Worker worker = getJalviewDesktopRunner(true, cmds, SETUP_TIMEOUT);
241 while ((ln = worker.getOutputReader().readLine()) != null)
243 System.out.println(ln);
244 successfulCMDs.add(ln);
246 while ((ln = worker.getErrorReader().readLine()) != null)
248 System.err.println(ln);
252 @BeforeTest(alwaysRun = true)
253 public void setUpForCommandLineInputOperations() throws IOException
255 String cmds = "-open examples/uniref50.fa -noquestionnaire -nousagestats";
256 Worker worker = getJalviewDesktopRunner(false, cmds, SETUP_TIMEOUT);
259 while ((ln = worker.getErrorReader().readLine()) != null)
261 System.out.println(ln);
262 successfulCMDs.add(ln);
268 if (worker != null && worker.exit == null)
271 Thread.currentThread().interrupt();
272 worker.process.destroy();
276 @Test(groups = { "Functional" }, dataProvider = "allInputOperationsData")
277 public void testAllInputOperations(String expectedString,
280 Assert.assertTrue(successfulCMDs.contains(expectedString), failureMsg);
285 { "Functional", "testben" },
286 dataProvider = "headlessModeOutputOperationsData")
287 public void testHeadlessModeOutputOperations(String harg, String type,
288 String fileName, boolean withAWT, int expectedMinFileSize,
289 int timeout, String fileFormatType)
291 String cmd = harg + type + " " + fileName;
292 // System.out.println(">>>>>>>>>>>>>>>> Command : " + cmd);
293 File file = new File(fileName);
295 Worker worker = getJalviewDesktopRunner(withAWT, cmd, timeout);
296 assertNotNull(worker, "worker is null");
297 String msg = "Didn't create an output" + type + " file.[" + harg + "]";
298 assertTrue(file.exists(), msg);
299 FileAssert.assertFile(file, msg);
300 FileAssert.assertMinLength(file, expectedMinFileSize);
301 if (fileFormatType!=null && fileFormatType.length()>0)
303 FileFormatI format = FileFormats.getInstance()
304 .forName(fileFormatType);
309 FileFormatI exportedType = new IdentifyFile()
310 .identify(file.getAbsolutePath(), DataSourceType.FILE);
311 assertEquals(exportedType, format,
312 "Exported file type was wrong");
313 } catch (FileFormatException e)
315 Assert.fail("Couldn't identify file " + file
316 + " as an alignment format", e);
320 if (worker != null && worker.exit == null)
323 Thread.currentThread().interrupt();
324 worker.process.destroy();
325 Assert.fail("Jalview did not exit after " + type
326 + " generation (try running test again to verify - timeout at "
327 + timeout + "ms). [" + harg + "]");
332 @DataProvider(name = "allInputOperationsData")
333 public Object[][] getHeadlessModeInputParams()
335 return new Object[][] {
336 // headless mode input operations
337 { "CMD [-color zappo] executed successfully!",
338 "Failed command : -color zappo" },
339 { "CMD [-props test/jalview/io/testProps.jvprops] executed successfully!",
340 "Failed command : -props File" },
341 { "CMD [-sortbytree] executed successfully!",
342 "Failed command : -sortbytree" },
343 { "CMD [-jabaws http://www.compbio.dundee.ac.uk/jabaws] executed successfully!",
344 "Failed command : -jabaws http://www.compbio.dundee.ac.uk/jabaws" },
345 { "CMD [-open examples/uniref50.fa] executed successfully!",
346 "Failed command : -open examples/uniref50.fa" },
347 { "CMD [-nosortbytree] executed successfully!",
348 "Failed command : -nosortbytree" },
349 { "CMD [-features examples/testdata/plantfdx.features] executed successfully!",
350 "Failed command : -features examples/testdata/plantfdx.features" },
351 { "CMD [-annotations examples/testdata/plantfdx.annotations] executed successfully!",
352 "Failed command : -annotations examples/testdata/plantfdx.annotations" },
353 { "CMD [-tree examples/testdata/uniref50_test_tree] executed successfully!",
354 "Failed command : -tree examples/testdata/uniref50_test_tree" },
355 // non headless mode input operations
356 { "CMD [-nousagestats] executed successfully!",
357 "Failed command : -nousagestats" },
358 { "CMD [-noquestionnaire] executed successfully!",
359 "Failed command : -noquestionnaire" } };
362 @DataProvider(name = "headlessModeOutputOperationsData")
363 public static Object[][] getHeadlessModeOutputParams()
365 // JBPNote: I'm not clear why need to specify full path for output file
366 // when running tests on build server, but we will keep this patch for now
368 // https://issues.jalview.org/browse/JAL-1889?focusedCommentId=21609&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-21609
369 String workingDir = "test/jalview/bin/";
370 return new Object[][] { { "nodisplay -open examples/uniref50.fa",
371 " -eps", workingDir + "test_uniref50_out.eps", true,
372 MINFILESIZE_BIG, TEST_TIMEOUT, null },
373 { "nodisplay -open examples/uniref50.fa", " -eps",
374 workingDir + "test_uniref50_out.eps", false,
375 MINFILESIZE_BIG, TEST_TIMEOUT, null },
376 { "nogui -open examples/uniref50.fa", " -eps",
377 workingDir + "test_uniref50_out.eps", true, MINFILESIZE_BIG,
378 TEST_TIMEOUT, null },
379 { "nogui -open examples/uniref50.fa", " -eps",
380 workingDir + "test_uniref50_out.eps", false,
381 MINFILESIZE_BIG, TEST_TIMEOUT, null },
382 { "headless -open examples/uniref50.fa", " -eps",
383 workingDir + "test_uniref50_out.eps", true, MINFILESIZE_BIG,
384 TEST_TIMEOUT, null },
385 { "headless -open examples/uniref50.fa", " -svg",
386 workingDir + "test_uniref50_out.svg", false,
387 MINFILESIZE_BIG, TEST_TIMEOUT, null },
388 { "headless -open examples/uniref50.fa", " -png",
389 workingDir + "test_uniref50_out.png", true, MINFILESIZE_BIG,
390 TEST_TIMEOUT, null },
391 { "headless -open examples/uniref50.fa", " -html",
392 workingDir + "test_uniref50_out.html", true,
393 MINFILESIZE_BIG, TEST_TIMEOUT, null },
394 { "headless -open examples/uniref50.fa", " -fasta",
395 workingDir + "test_uniref50_out.mfa", true, MINFILESIZE_SMALL,
396 TEST_TIMEOUT, FileFormat.Fasta.toString() },
397 { "headless -open examples/uniref50.fa", " -clustal",
398 workingDir + "test_uniref50_out.aln", true, MINFILESIZE_SMALL,
399 TEST_TIMEOUT, FileFormat.Clustal.toString() },
400 { "headless -open examples/uniref50.fa", " -msf",
401 workingDir + "test_uniref50_out.msf", true, MINFILESIZE_SMALL,
402 TEST_TIMEOUT, FileFormat.MSF.toString() },
403 { "headless -open examples/uniref50.fa", " -pileup",
404 workingDir + "test_uniref50_out.aln", true, MINFILESIZE_SMALL,
405 TEST_TIMEOUT, FileFormat.Pileup.toString() },
406 { "headless -open examples/uniref50.fa", " -pir",
407 workingDir + "test_uniref50_out.pir", true, MINFILESIZE_SMALL,
408 TEST_TIMEOUT, FileFormat.PIR.toString() },
409 { "headless -open examples/uniref50.fa", " -pfam",
410 workingDir + "test_uniref50_out.pfam", true, MINFILESIZE_SMALL,
411 TEST_TIMEOUT, FileFormat.Pfam.toString() },
412 { "headless -open examples/uniref50.fa", " -blc",
413 workingDir + "test_uniref50_out.blc", true, MINFILESIZE_SMALL,
414 TEST_TIMEOUT, FileFormat.BLC.toString() },
415 { "headless -open examples/uniref50.fa", " -jalview",
416 workingDir + "test_uniref50_out.jvp", true, MINFILESIZE_SMALL,
417 TEST_TIMEOUT, FileFormat.Jalview.toString() }, };