JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / test / jalview / bin / CommandLineOperations.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
3  * Copyright (C) 2015 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
52     private BufferedReader outputReader;
53
54     private BufferedReader errorReader;
55
56     private Integer exit;
57
58     private Worker(Process process)
59     {
60       this.process = process;
61     }
62
63     public void run()
64     {
65       try
66       {
67         exit = process.waitFor();
68       } catch (InterruptedException ignore)
69       {
70         return;
71       }
72     }
73
74     public BufferedReader getOutputReader()
75     {
76       return outputReader;
77     }
78
79     public void setOutputReader(BufferedReader outputReader)
80     {
81       this.outputReader = outputReader;
82     }
83
84     public BufferedReader getErrorReader()
85     {
86       return errorReader;
87     }
88
89     public void setErrorReader(BufferedReader errorReader)
90     {
91       this.errorReader = errorReader;
92     }
93   }
94
95   private Worker jalviewDesktopRunner(boolean withAwt, String cmd,
96           int timeout)
97   {
98     String _cmd = "java "
99             + (withAwt ? "-Djava.awt.headless=true" : "")
100             + " -Djava.ext.dirs=./lib -classpath ./classes jalview.bin.Jalview ";
101     System.out.println("CMD [" + cmd + "]");
102     Process ls2_proc = null;
103     Worker worker = null;
104     try
105     {
106       ls2_proc = Runtime.getRuntime().exec(_cmd + cmd);
107     } catch (IOException e1)
108     {
109       e1.printStackTrace();
110     }
111     if (ls2_proc != null)
112     {
113       BufferedReader outputReader = new BufferedReader(
114               new InputStreamReader(ls2_proc.getInputStream()));
115       BufferedReader errorReader = new BufferedReader(
116               new InputStreamReader(ls2_proc.getErrorStream()));
117       worker = new Worker(ls2_proc);
118       worker.start();
119       try
120       {
121         worker.join(timeout);
122       } catch (InterruptedException e)
123       {
124         // e.printStackTrace();
125       }
126       worker.setOutputReader(outputReader);
127       worker.setErrorReader(errorReader);
128     }
129     return worker;
130   }
131
132   @BeforeTest(alwaysRun = true)
133   public void initialize()
134   {
135     new CommandLineOperations();
136   }
137
138   @BeforeTest(alwaysRun = true)
139   public void setUpForHeadlessCommandLineInputOperations()
140           throws IOException
141   {
142     String cmds = "nodisplay -open examples/uniref50.fa -sortbytree -props FILE -colour zappo "
143             + "-jabaws http://www.compbio.dundee.ac.uk/jabaws -nosortbytree -dasserver nickname=www.test.com "
144             + "-features examples/testdata/plantfdx.features -annotations examples/testdata/plantfdx.annotations -tree examples/testdata/uniref50_test_tree";
145     Worker worker = jalviewDesktopRunner(true, cmds, 9000);
146     String ln = null;
147     while ((ln = worker.getOutputReader().readLine()) != null)
148     {
149       System.out.println(ln);
150       successfulCMDs.add(ln);
151     }
152   }
153
154   @BeforeTest(alwaysRun = true)
155   public void setUpForCommandLineInputOperations() throws IOException
156   {
157     String cmds = "-open examples/uniref50.fa -noquestionnaire -nousagestats";
158     Worker worker = jalviewDesktopRunner(false, cmds, 9000);
159     String ln = null;
160     int count = 0;
161     while ((ln = worker.getErrorReader().readLine()) != null)
162     {
163       System.out.println(ln);
164       successfulCMDs.add(ln);
165       if (++count > 5)
166       {
167         break;
168       }
169     }
170     if (worker != null && worker.exit == null)
171     {
172       worker.interrupt();
173       Thread.currentThread().interrupt();
174       worker.process.destroy();
175     }
176   }
177
178   @Test(groups = { "Functional" }, dataProvider = "allInputOpearationsData")
179   public void testAllInputOperations(String expectedString,
180           String failureMsg)
181   {
182     Assert.assertTrue(successfulCMDs.contains(expectedString), failureMsg);
183   }
184
185   @Test(
186     groups = { "Functional" },
187     dataProvider = "headlessModeOutputOperationsData")
188   public void testHeadlessModeOutputOperations(String harg, String type,
189           String fileName, boolean withAWT, int expectedMinFileSize,
190           int timeout)
191   {
192     String cmd = harg + type + " " + fileName;
193     // System.out.println(">>>>>>>>>>>>>>>> Command : " + cmd);
194     File file = new File(fileName);
195     Worker worker = jalviewDesktopRunner(withAWT, cmd, timeout);
196
197     FileAssert.assertFile(file, "Didn't create an output" + type
198             + " file.[" + harg + "]");
199     FileAssert.assertMinLength(new File(fileName), expectedMinFileSize);
200     if (worker != null && worker.exit == null)
201     {
202       worker.interrupt();
203       Thread.currentThread().interrupt();
204       worker.process.destroy();
205       Assert.fail("Jalview did not exit after "
206               + type
207               + " generation (try running test again to verify - timeout at 9000ms). ["
208               + harg + "]");
209     }
210     new File(fileName).delete();
211   }
212
213   @DataProvider(name = "allInputOpearationsData")
214   public Object[][] getHeadlessModeInputParams()
215   {
216     return new Object[][] {
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   @DataProvider(name = "headlessModeOutputOperationsData")
251   public static Object[][] getHeadlessModeOutputParams()
252   {
253     return new Object[][] {
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 }