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