Merge branch 'spike/relaunch_memsetingds' into spikes/jim
[jalview.git] / test / jalview / bin / CommandLineOperations.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.bin;
22
23 import jalview.gui.JvOptionPane;
24 import jalview.ws.utils.Worker;
25
26 import java.io.BufferedReader;
27 import java.io.File;
28 import java.io.IOException;
29 import java.io.InputStreamReader;
30 import java.util.ArrayList;
31
32 import org.testng.Assert;
33 import org.testng.FileAssert;
34 import org.testng.annotations.BeforeClass;
35 import org.testng.annotations.BeforeTest;
36 import org.testng.annotations.DataProvider;
37 import org.testng.annotations.Test;
38
39 import io.github.classgraph.ClassGraph;
40 import io.github.classgraph.ScanResult;
41
42 public class CommandLineOperations
43 {
44
45   @BeforeClass(alwaysRun = true)
46   public void setUpJvOptionPane()
47   {
48     JvOptionPane.setInteractiveMode(false);
49     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
50   }
51
52   private static final int TEST_TIMEOUT = 9000; // Note longer timeout needed on
53                                                 // full test run than on
54                                                 // individual tests
55
56   private static final int SETUP_TIMEOUT = 9000;
57
58   private static final int MINFILESIZE_SMALL = 2096;
59
60   private static final int MINFILESIZE_BIG = 4096;
61
62   private ArrayList<String> successfulCMDs = new ArrayList<>();
63
64
65   private static ClassGraph scanner = null;
66
67   private static String classpath = null;
68
69   public synchronized static String getClassPath()
70   {
71     if (scanner == null)
72     {
73       scanner = new ClassGraph();
74       ScanResult scan = scanner.scan();
75       classpath = scan.getClasspath();
76     }
77     while (classpath == null)
78     {
79       try
80       {
81         Thread.sleep(10);
82       } catch (InterruptedException x)
83       {
84
85       }
86     }
87     return classpath;
88   }
89   private Worker jalviewDesktopRunner(boolean withAwt, String cmd,
90           int timeout)
91   {
92     // Note: JAL-3065 - don't include quotes for lib/* because the arguments are
93     // not expanded by the shell
94     String classpath = getClassPath();
95     String _cmd = "java "
96             + (withAwt ? "-Djava.awt.headless=true" : "")
97             + " -classpath " + classpath + " jalview.bin.Jalview ";
98     Process ls2_proc = null;
99     Worker worker = null;
100     try
101     {
102       ls2_proc = Runtime.getRuntime().exec(_cmd + cmd);
103     } catch (IOException e1)
104     {
105       e1.printStackTrace();
106     }
107     if (ls2_proc != null)
108     {
109       BufferedReader outputReader = new BufferedReader(
110               new InputStreamReader(ls2_proc.getInputStream()));
111       BufferedReader errorReader = new BufferedReader(
112               new InputStreamReader(ls2_proc.getErrorStream()));
113       worker = new Worker(ls2_proc);
114       worker.start();
115       try
116       {
117         worker.join(timeout);
118       } catch (InterruptedException e)
119       {
120         // e.printStackTrace();
121       }
122       worker.setOutputReader(outputReader);
123       worker.setErrorReader(errorReader);
124     }
125     return worker;
126   }
127
128   @BeforeTest(alwaysRun = true)
129   public void initialize()
130   {
131     new CommandLineOperations();
132   }
133
134   @BeforeTest(alwaysRun = true)
135   public void setUpForHeadlessCommandLineInputOperations()
136           throws IOException
137   {
138     String cmds = "nodisplay -open examples/uniref50.fa -sortbytree -props FILE -colour zappo "
139             + "-jabaws http://www.compbio.dundee.ac.uk/jabaws -nosortbytree -dasserver nickname=www.test.com "
140             + "-features examples/testdata/plantfdx.features -annotations examples/testdata/plantfdx.annotations -tree examples/testdata/uniref50_test_tree";
141     Worker worker = jalviewDesktopRunner(true, cmds, SETUP_TIMEOUT);
142     String ln = null;
143     while ((ln = worker.getOutputReader().readLine()) != null)
144     {
145       System.out.println(ln);
146       successfulCMDs.add(ln);
147     }
148   }
149
150   @BeforeTest(alwaysRun = true)
151   public void setUpForCommandLineInputOperations() throws IOException
152   {
153     String cmds = "-open examples/uniref50.fa -noquestionnaire -nousagestats";
154     Worker worker = jalviewDesktopRunner(false, cmds, SETUP_TIMEOUT);
155     String ln = null;
156     int count = 0;
157     while ((ln = worker.getErrorReader().readLine()) != null)
158     {
159       System.out.println(ln);
160       successfulCMDs.add(ln);
161       if (++count > 5)
162       {
163         break;
164       }
165     }
166     if (worker != null && worker.getExitValue() == null)
167     {
168       worker.destroyProcess();
169
170     }
171   }
172
173   @Test(groups = { "Functional" }, dataProvider = "allInputOpearationsData")
174   public void testAllInputOperations(String expectedString,
175           String failureMsg)
176   {
177     Assert.assertTrue(successfulCMDs.contains(expectedString), failureMsg);
178   }
179
180   @Test(
181     groups = { "Functional" },
182     dataProvider = "headlessModeOutputOperationsData")
183   public void testHeadlessModeOutputOperations(String harg, String type,
184           String fileName, boolean withAWT, int expectedMinFileSize,
185           int timeout)
186   {
187     String cmd = harg + type + " " + fileName;
188     // System.out.println(">>>>>>>>>>>>>>>> Command : " + cmd);
189     File file = new File(fileName);
190     Worker worker = jalviewDesktopRunner(withAWT, cmd, timeout);
191
192     FileAssert.assertFile(file, "Didn't create an output" + type
193             + " file.[" + harg + "]");
194     FileAssert.assertMinLength(new File(fileName), expectedMinFileSize);
195     if (worker != null && worker.getExitValue() == null)
196     {
197       worker.destroyProcess();
198       Assert.fail("Jalview did not exit after "
199               + type
200               + " generation (try running test again to verify - timeout at "
201               + SETUP_TIMEOUT + "ms). ["
202               + harg + "]");
203     }
204     new File(fileName).delete();
205   }
206
207   @DataProvider(name = "allInputOpearationsData")
208   public Object[][] getHeadlessModeInputParams()
209   {
210     return new Object[][] {
211         // headless mode input operations
212         { "CMD [-color zappo] executed successfully!",
213             "Failed command : -color zappo" },
214         { "CMD [-props FILE] executed successfully!",
215             "Failed command : -props File" },
216         { "CMD [-sortbytree] executed successfully!",
217             "Failed command : -sortbytree" },
218         {
219             "CMD [-jabaws http://www.compbio.dundee.ac.uk/jabaws] executed successfully!",
220             "Failed command : -jabaws http://www.compbio.dundee.ac.uk/jabaws" },
221         { "CMD [-open examples/uniref50.fa] executed successfully!",
222             "Failed command : -open examples/uniref50.fa" },
223         { "CMD [-nosortbytree] executed successfully!",
224             "Failed command : -nosortbytree" },
225         { "CMD [-dasserver nickname=www.test.com] executed successfully!",
226             "Failed command : -dasserver nickname=www.test.com" },
227         {
228             "CMD [-features examples/testdata/plantfdx.features]  executed successfully!",
229             "Failed command : -features examples/testdata/plantfdx.features" },
230         {
231             "CMD [-annotations examples/testdata/plantfdx.annotations] executed successfully!",
232             "Failed command : -annotations examples/testdata/plantfdx.annotations" },
233         {
234             "CMD [-tree examples/testdata/uniref50_test_tree] executed successfully!",
235             "Failed command : -tree examples/testdata/uniref50_test_tree" },
236         // non headless mode input operations
237         { "CMD [-nousagestats] executed successfully!",
238             "Failed command : -nousagestats" },
239         { "CMD [-noquestionnaire] executed successfully!",
240             "Failed command : -noquestionnaire nickname=www.test.com" } };
241
242   }
243
244   @DataProvider(name = "headlessModeOutputOperationsData")
245   public static Object[][] getHeadlessModeOutputParams()
246   {
247     return new Object[][] {
248         { "nodisplay -open examples/uniref50.fa", " -eps",
249             "test_uniref50_out.eps", true, MINFILESIZE_BIG, TEST_TIMEOUT },
250         { "nodisplay -open examples/uniref50.fa", " -eps",
251             "test_uniref50_out.eps", false, MINFILESIZE_BIG, TEST_TIMEOUT },
252         { "nogui -open examples/uniref50.fa", " -eps",
253             "test_uniref50_out.eps", true, MINFILESIZE_BIG, TEST_TIMEOUT },
254         { "nogui -open examples/uniref50.fa", " -eps",
255             "test_uniref50_out.eps", false, MINFILESIZE_BIG, TEST_TIMEOUT },
256         { "headless -open examples/uniref50.fa", " -eps",
257             "test_uniref50_out.eps", true, MINFILESIZE_BIG, TEST_TIMEOUT },
258         { "headless -open examples/uniref50.fa", " -svg",
259             "test_uniref50_out.svg", false, MINFILESIZE_BIG, TEST_TIMEOUT },
260         { "headless -open examples/uniref50.fa", " -png",
261             "test_uniref50_out.png", true, MINFILESIZE_BIG, TEST_TIMEOUT },
262         { "headless -open examples/uniref50.fa", " -html",
263             "test_uniref50_out.html", true, MINFILESIZE_BIG, TEST_TIMEOUT },
264         { "headless -open examples/uniref50.fa", " -fasta",
265             "test_uniref50_out.mfa", true, MINFILESIZE_SMALL, TEST_TIMEOUT },
266         { "headless -open examples/uniref50.fa", " -clustal",
267             "test_uniref50_out.aln", true, MINFILESIZE_SMALL, TEST_TIMEOUT },
268         { "headless -open examples/uniref50.fa", " -msf",
269             "test_uniref50_out.msf", true, MINFILESIZE_SMALL, TEST_TIMEOUT },
270         { "headless -open examples/uniref50.fa", " -pileup",
271             "test_uniref50_out.aln", true, MINFILESIZE_SMALL, TEST_TIMEOUT },
272         { "headless -open examples/uniref50.fa", " -pir",
273             "test_uniref50_out.pir", true, MINFILESIZE_SMALL, TEST_TIMEOUT },
274         { "headless -open examples/uniref50.fa", " -pfam",
275             "test_uniref50_out.pfam", true, MINFILESIZE_SMALL, TEST_TIMEOUT },
276         { "headless -open examples/uniref50.fa", " -blc",
277             "test_uniref50_out.blc", true, MINFILESIZE_SMALL, TEST_TIMEOUT },
278         { "headless -open examples/uniref50.fa", " -jalview",
279             "test_uniref50_out.jvp", true, MINFILESIZE_SMALL, TEST_TIMEOUT }, };
280   }
281 }