2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import java.io.BufferedReader;
25 import java.io.IOException;
26 import java.io.InputStreamReader;
27 import java.util.ArrayList;
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;
35 public class CommandLineOperations
37 private static final int TEST_TIMEOUT = 4000;
39 private static final int SETUP_TIMEOUT = 9000;
41 private static final int MINFILESIZE_SMALL = 2096;
43 private static final int MINFILESIZE_BIG = 4096;
45 private ArrayList<String> successfulCMDs = new ArrayList<String>();
49 * http://stackoverflow.com/questions/808276/how-to-add-a-timeout-value-when
50 * -using-javas-runtime-exec
55 private static class Worker extends Thread
57 private final Process process;
59 private BufferedReader outputReader;
61 private BufferedReader errorReader;
65 private Worker(Process process)
67 this.process = process;
75 exit = process.waitFor();
76 } catch (InterruptedException ignore)
82 public BufferedReader getOutputReader()
87 public void setOutputReader(BufferedReader outputReader)
89 this.outputReader = outputReader;
92 public BufferedReader getErrorReader()
97 public void setErrorReader(BufferedReader errorReader)
99 this.errorReader = errorReader;
103 private Worker jalviewDesktopRunner(boolean withAwt, String cmd,
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;
114 ls2_proc = Runtime.getRuntime().exec(_cmd + cmd);
115 } catch (IOException e1)
117 e1.printStackTrace();
119 if (ls2_proc != null)
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);
129 worker.join(timeout);
130 } catch (InterruptedException e)
132 // e.printStackTrace();
134 worker.setOutputReader(outputReader);
135 worker.setErrorReader(errorReader);
140 @BeforeTest(alwaysRun = true)
141 public void initialize()
143 new CommandLineOperations();
146 @BeforeTest(alwaysRun = true)
147 public void setUpForHeadlessCommandLineInputOperations()
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);
155 while ((ln = worker.getOutputReader().readLine()) != null)
157 System.out.println(ln);
158 successfulCMDs.add(ln);
162 @BeforeTest(alwaysRun = true)
163 public void setUpForCommandLineInputOperations() throws IOException
165 String cmds = "-open examples/uniref50.fa -noquestionnaire -nousagestats";
166 Worker worker = jalviewDesktopRunner(false, cmds, SETUP_TIMEOUT);
169 while ((ln = worker.getErrorReader().readLine()) != null)
171 System.out.println(ln);
172 successfulCMDs.add(ln);
178 if (worker != null && worker.exit == null)
181 Thread.currentThread().interrupt();
182 worker.process.destroy();
186 @Test(groups = { "Functional" }, dataProvider = "allInputOpearationsData")
187 public void testAllInputOperations(String expectedString,
190 Assert.assertTrue(successfulCMDs.contains(expectedString), failureMsg);
194 groups = { "Functional" },
195 dataProvider = "headlessModeOutputOperationsData")
196 public void testHeadlessModeOutputOperations(String harg, String type,
197 String fileName, boolean withAWT, int expectedMinFileSize,
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);
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)
211 Thread.currentThread().interrupt();
212 worker.process.destroy();
213 Assert.fail("Jalview did not exit after "
215 + " generation (try running test again to verify - timeout at "
216 + SETUP_TIMEOUT + "ms). ["
219 new File(fileName).delete();
222 @DataProvider(name = "allInputOpearationsData")
223 public Object[][] getHeadlessModeInputParams()
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" },
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" },
243 "CMD [-features examples/testdata/plantfdx.features] executed successfully!",
244 "Failed command : -features examples/testdata/plantfdx.features" },
246 "CMD [-annotations examples/testdata/plantfdx.annotations] executed successfully!",
247 "Failed command : -annotations examples/testdata/plantfdx.annotations" },
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" } };
259 @DataProvider(name = "headlessModeOutputOperationsData")
260 public static Object[][] getHeadlessModeOutputParams()
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 }, };