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