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