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