JAL-1805 modified test setup's so the are ran for groups which requires them
[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       BufferedReader errorReader = new BufferedReader(
113               new InputStreamReader(ls2_proc.getErrorStream()));
114       worker = new Worker(ls2_proc);
115       worker.start();
116       try
117       {
118         worker.join(timeout);
119       } catch (InterruptedException e)
120       {
121         // e.printStackTrace();
122       }
123       worker.setOutputReader(outputReader);
124       worker.setErrorReader(errorReader);
125     }
126     return worker;
127   }
128
129   @BeforeTest(alwaysRun = true)
130   public void initialize()
131   {
132     new CommandLineOperations();
133   }
134
135   
136   @BeforeTest(alwaysRun = true)
137   public void setUpForHeadlessCommandLineInputOperations()
138           throws IOException
139   {
140     String cmds = "nodisplay -open examples/uniref50.fa -sortbytree -props FILE -colour zappo "
141             + "-jabaws http://www.compbio.dundee.ac.uk/jabaws -nosortbytree -dasserver nickname=www.test.com "
142             + "-features examples/testdata/plantfdx.features -annotations examples/testdata/plantfdx.annotations -tree examples/testdata/uniref50_test_tree";
143     Worker worker = jalviewDesktopRunner(true, cmds, 9000);
144     String ln = null;
145     while ((ln = worker.getOutputReader().readLine()) != null)
146     {
147       System.out.println(ln);
148       successfulCMDs.add(ln);
149     }
150   }
151
152   @BeforeTest(alwaysRun = true)
153   public void setUpForCommandLineInputOperations() throws IOException
154   {
155     String cmds = "-open examples/uniref50.fa -noquestionnaire -nousagestats";
156     Worker worker = jalviewDesktopRunner(false, cmds, 9000);
157     String ln = null;
158     int count = 0;
159     while ((ln = worker.getErrorReader().readLine()) != null)
160     {
161       System.out.println(ln);
162       successfulCMDs.add(ln);
163       if (++count > 5)
164       {
165         break;
166       }
167     }
168     if (worker != null && worker.exit == null)
169     {
170       worker.interrupt();
171       Thread.currentThread().interrupt();
172       worker.process.destroy();
173     }
174   }
175
176   @Test(groups =
177   { "Functional" }, dataProvider = "allInputOpearationsData")
178   public void testAllInputOperations(String expectedString,
179           String failureMsg)
180   {
181     Assert.assertTrue(successfulCMDs.contains(expectedString), failureMsg);
182   }
183
184   @Test(groups =
185   { "Functional" }, dataProvider = "headlessModeOutputOperationsData")
186   public void testHeadlessModeOutputOperations(String harg, String type,
187           String fileName, boolean withAWT, int expectedMinFileSize,
188           int timeout)
189   {
190     String cmd = harg + type + " " + fileName;
191     // System.out.println(">>>>>>>>>>>>>>>> Command : " + cmd);
192     File file = new File(fileName);
193     Worker worker = jalviewDesktopRunner(withAWT, cmd, timeout);
194
195     FileAssert.assertFile(file, "Didn't create an output" + type
196             + " file.[" + harg + "]");
197     FileAssert.assertMinLength(new File(fileName), expectedMinFileSize);
198     if (worker != null && worker.exit == null)
199     {
200       worker.interrupt();
201       Thread.currentThread().interrupt();
202       worker.process.destroy();
203       Assert.fail("Jalview did not exit after "
204               + type
205               + " generation (try running test again to verify - timeout at 9000ms). ["
206               + harg + "]");
207     }
208     new File(fileName).delete();
209   }
210
211
212   @DataProvider(name = "allInputOpearationsData")
213   public Object[][] getHeadlessModeInputParams()
214   {
215     return new Object[][]
216     {
217         // headless mode input operations
218         { "CMD [-color zappo] executed successfully!",
219             "Failed command : -color zappo" },
220         { "CMD [-props FILE] executed successfully!",
221             "Failed command : -props File" },
222         { "CMD [-sortbytree] executed successfully!",
223             "Failed command : -sortbytree" },
224         {
225             "CMD [-jabaws http://www.compbio.dundee.ac.uk/jabaws] executed successfully!",
226             "Failed command : -jabaws http://www.compbio.dundee.ac.uk/jabaws" },
227         { "CMD [-open examples/uniref50.fa] executed successfully!",
228             "Failed command : -open examples/uniref50.fa" },
229         { "CMD [-nosortbytree] executed successfully!",
230             "Failed command : -nosortbytree" },
231         { "CMD [-dasserver nickname=www.test.com] executed successfully!",
232             "Failed command : -dasserver nickname=www.test.com" },
233         {
234             "CMD [-features examples/testdata/plantfdx.features]  executed successfully!",
235             "Failed command : -features examples/testdata/plantfdx.features" },
236         {
237             "CMD [-annotations examples/testdata/plantfdx.annotations] executed successfully!",
238             "Failed command : -annotations examples/testdata/plantfdx.annotations" },
239         {
240             "CMD [-tree examples/testdata/uniref50_test_tree] executed successfully!",
241             "Failed command : -tree examples/testdata/uniref50_test_tree" },
242         // non headless mode input operations
243         { "CMD [-nousagestats] executed successfully!",
244             "Failed command : -nousagestats" },
245         { "CMD [-noquestionnaire] executed successfully!",
246             "Failed command : -noquestionnaire nickname=www.test.com" }
247     };
248
249   }
250    
251   @DataProvider(name = "headlessModeOutputOperationsData")
252   public static Object[][] getHeadlessModeOutputParams()
253   {
254     return new Object[][]
255     {
256         { "nodisplay -open examples/uniref50.fa", " -eps",
257             "test_uniref50_out.eps", true, 4096, 4000 },
258         { "nodisplay -open examples/uniref50.fa", " -eps",
259             "test_uniref50_out.eps", false, 4096, 4000 },
260         { "nogui -open examples/uniref50.fa", " -eps",
261             "test_uniref50_out.eps", true, 4096, 4000 },
262         { "nogui -open examples/uniref50.fa", " -eps",
263             "test_uniref50_out.eps", false, 4096, 4000 },
264         { "headless -open examples/uniref50.fa", " -eps",
265             "test_uniref50_out.eps", true, 4096, 4000 },
266         { "headless -open examples/uniref50.fa", " -svg",
267             "test_uniref50_out.svg", false, 4096, 3000 },
268         { "headless -open examples/uniref50.fa", " -png",
269             "test_uniref50_out.png", true, 4096, 3000 },
270         { "headless -open examples/uniref50.fa", " -html",
271             "test_uniref50_out.html", true, 4096, 3000 },
272         { "headless -open examples/uniref50.fa", " -fasta",
273             "test_uniref50_out.mfa", true, 2096, 3000 },
274         { "headless -open examples/uniref50.fa", " -clustal",
275             "test_uniref50_out.aln", true, 2096, 3000 },
276         { "headless -open examples/uniref50.fa", " -msf",
277             "test_uniref50_out.msf", true, 2096, 3000 },
278         { "headless -open examples/uniref50.fa", " -pileup",
279             "test_uniref50_out.aln", true, 2096, 3000 },
280         { "headless -open examples/uniref50.fa", " -pir",
281             "test_uniref50_out.pir", true, 2096, 3000 },
282         { "headless -open examples/uniref50.fa", " -pfam",
283             "test_uniref50_out.pfam", true, 2096, 3000 },
284         { "headless -open examples/uniref50.fa", " -blc",
285             "test_uniref50_out.blc", true, 2096, 3000 },
286         { "headless -open examples/uniref50.fa", " -jalview",
287             "test_uniref50_out.jvp", true, 2096, 3000 },
288     };
289   }
290 }