JAL-629 start of tests
[jalview.git] / test / jalview / bin / CommandsTest.java
1 package jalview.bin;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.lang.management.ManagementFactory;
6
7 import org.testng.annotations.BeforeTest;
8 import org.testng.annotations.Test;
9
10 import io.github.classgraph.ClassGraph;
11
12 public class CommandsTest
13 {
14
15   private static final int SETUP_TIMEOUT = 30000;
16
17   private static class Worker extends Thread
18   {
19     private final Process process;
20
21     private Integer exit;
22
23     private Worker(Process process)
24     {
25       this.process = process;
26     }
27
28     @Override
29     public void run()
30     {
31       try
32       {
33         exit = process.waitFor();
34       } catch (InterruptedException ignore)
35       {
36         return;
37       }
38     }
39   }
40
41   private static ClassGraph scanner = null;
42
43   private static String classpath = null;
44
45   private static String modules = null;
46
47   private static String java_exe = null;
48
49   public synchronized static String getClassPath()
50   {
51     java_exe = System.getProperty("java.home") + File.separator + "bin"
52             + File.separator + "java";
53     classpath = ManagementFactory.getRuntimeMXBean().getClassPath();
54     return classpath;
55   }
56
57   private Worker getJalviewDesktopRunner(boolean withAwt, String cmd,
58           int timeout)
59   {
60     // Note: JAL-3065 - don't include quotes for lib/* because the arguments are
61     // not expanded by the shell
62     String classpath = getClassPath();
63     String _cmd = java_exe + " "
64             + (withAwt ? "-Djava.awt.headless=true" : "") + " -classpath "
65             + classpath + " jalview.bin.Jalview ";
66     Process ls2_proc = null;
67     Worker worker = null;
68     try
69     {
70       ls2_proc = Runtime.getRuntime().exec(_cmd + cmd);
71     } catch (Throwable e1)
72     {
73       e1.printStackTrace();
74     }
75     if (ls2_proc != null)
76     {
77       worker = new Worker(ls2_proc);
78       worker.start();
79       try
80       {
81         worker.join(timeout);
82       } catch (InterruptedException e)
83       {
84         System.err.println("Thread interrupted");
85       }
86     }
87     return worker;
88   }
89
90   @BeforeTest(alwaysRun = true)
91   public void initialize()
92   {
93     new CommandsTest();
94   }
95
96   @Test(groups = "Functional")
97   public void setUpForHeadlessCommandsTest() throws IOException
98   {
99     String cmds = "--open=./examples/uniref50.fa";
100     Worker worker = getJalviewDesktopRunner(true, cmds, SETUP_TIMEOUT);
101   }
102
103 }