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