refactored worker and made the launch work on OSX/16G machine
[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 jalview.gui.JvOptionPane;
24 import jalview.util.Worker;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.util.ArrayList;
29
30 import org.testng.Assert;
31 import org.testng.FileAssert;
32 import org.testng.annotations.BeforeClass;
33 import org.testng.annotations.BeforeTest;
34 import org.testng.annotations.DataProvider;
35 import org.testng.annotations.Test;
36
37 public class CommandLineOperations
38 {
39
40   @BeforeClass(alwaysRun = true)
41   public void setUpJvOptionPane()
42   {
43     JvOptionPane.setInteractiveMode(false);
44     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
45   }
46
47   private static final int TEST_TIMEOUT = 9000; // Note longer timeout needed on
48                                                 // full test run than on
49                                                 // individual tests
50
51   private static final int SETUP_TIMEOUT = 9000;
52
53   private static final int MINFILESIZE_SMALL = 2096;
54
55   private static final int MINFILESIZE_BIG = 4096;
56
57   private ArrayList<String> successfulCMDs = new ArrayList<>();
58
59
60   @BeforeTest(alwaysRun = true)
61   public void initialize()
62   {
63     new CommandLineOperations();
64   }
65
66   @BeforeTest(alwaysRun = true)
67   public void setUpForHeadlessCommandLineInputOperations()
68           throws IOException
69   {
70     String cmds = "nodisplay -open examples/uniref50.fa -sortbytree -props FILE -colour zappo "
71             + "-jabaws http://www.compbio.dundee.ac.uk/jabaws -nosortbytree -dasserver nickname=www.test.com "
72             + "-features examples/testdata/plantfdx.features -annotations examples/testdata/plantfdx.annotations -tree examples/testdata/uniref50_test_tree";
73     Worker worker = Worker.jalviewDesktopRunner(true, cmds, SETUP_TIMEOUT);
74     String ln = null;
75     while ((ln = worker.getOutputReader().readLine()) != null)
76     {
77       System.out.println(ln);
78       successfulCMDs.add(ln);
79     }
80   }
81
82   @BeforeTest(alwaysRun = true)
83   public void setUpForCommandLineInputOperations() throws IOException
84   {
85     String cmds = "-open examples/uniref50.fa -noquestionnaire -nousagestats";
86     Worker worker = Worker.jalviewDesktopRunner(false, cmds, SETUP_TIMEOUT);
87     String ln = null;
88     int count = 0;
89     while ((ln = worker.getErrorReader().readLine()) != null)
90     {
91       System.out.println(ln);
92       successfulCMDs.add(ln);
93       if (++count > 5)
94       {
95         break;
96       }
97     }
98     if (worker != null && worker.getExitValue() == null)
99     {
100       worker.destroyProcess();
101
102     }
103   }
104
105   @Test(groups = { "Functional" }, dataProvider = "allInputOpearationsData")
106   public void testAllInputOperations(String expectedString,
107           String failureMsg)
108   {
109     Assert.assertTrue(successfulCMDs.contains(expectedString), failureMsg);
110   }
111
112   @Test(
113     groups = { "Functional" },
114     dataProvider = "headlessModeOutputOperationsData")
115   public void testHeadlessModeOutputOperations(String harg, String type,
116           String fileName, boolean withAWT, int expectedMinFileSize,
117           int timeout)
118   {
119     String cmd = harg + type + " " + fileName;
120     // System.out.println(">>>>>>>>>>>>>>>> Command : " + cmd);
121     File file = new File(fileName);
122     Worker worker = Worker.jalviewDesktopRunner(withAWT, cmd, timeout);
123
124     FileAssert.assertFile(file, "Didn't create an output" + type
125             + " file.[" + harg + "]");
126     FileAssert.assertMinLength(new File(fileName), expectedMinFileSize);
127     if (worker != null && worker.getExitValue() == null)
128     {
129       worker.destroyProcess();
130       Assert.fail("Jalview did not exit after "
131               + type
132               + " generation (try running test again to verify - timeout at "
133               + SETUP_TIMEOUT + "ms). ["
134               + harg + "]");
135     }
136     new File(fileName).delete();
137   }
138
139   @DataProvider(name = "allInputOpearationsData")
140   public Object[][] getHeadlessModeInputParams()
141   {
142     return new Object[][] {
143         // headless mode input operations
144         { "CMD [-color zappo] executed successfully!",
145             "Failed command : -color zappo" },
146         { "CMD [-props FILE] executed successfully!",
147             "Failed command : -props File" },
148         { "CMD [-sortbytree] executed successfully!",
149             "Failed command : -sortbytree" },
150         {
151             "CMD [-jabaws http://www.compbio.dundee.ac.uk/jabaws] executed successfully!",
152             "Failed command : -jabaws http://www.compbio.dundee.ac.uk/jabaws" },
153         { "CMD [-open examples/uniref50.fa] executed successfully!",
154             "Failed command : -open examples/uniref50.fa" },
155         { "CMD [-nosortbytree] executed successfully!",
156             "Failed command : -nosortbytree" },
157         { "CMD [-dasserver nickname=www.test.com] executed successfully!",
158             "Failed command : -dasserver nickname=www.test.com" },
159         {
160             "CMD [-features examples/testdata/plantfdx.features]  executed successfully!",
161             "Failed command : -features examples/testdata/plantfdx.features" },
162         {
163             "CMD [-annotations examples/testdata/plantfdx.annotations] executed successfully!",
164             "Failed command : -annotations examples/testdata/plantfdx.annotations" },
165         {
166             "CMD [-tree examples/testdata/uniref50_test_tree] executed successfully!",
167             "Failed command : -tree examples/testdata/uniref50_test_tree" },
168         // non headless mode input operations
169         { "CMD [-nousagestats] executed successfully!",
170             "Failed command : -nousagestats" },
171         { "CMD [-noquestionnaire] executed successfully!",
172             "Failed command : -noquestionnaire nickname=www.test.com" } };
173
174   }
175
176   @DataProvider(name = "headlessModeOutputOperationsData")
177   public static Object[][] getHeadlessModeOutputParams()
178   {
179     return new Object[][] {
180         { "nodisplay -open examples/uniref50.fa", " -eps",
181             "test_uniref50_out.eps", true, MINFILESIZE_BIG, TEST_TIMEOUT },
182         { "nodisplay -open examples/uniref50.fa", " -eps",
183             "test_uniref50_out.eps", false, MINFILESIZE_BIG, TEST_TIMEOUT },
184         { "nogui -open examples/uniref50.fa", " -eps",
185             "test_uniref50_out.eps", true, MINFILESIZE_BIG, TEST_TIMEOUT },
186         { "nogui -open examples/uniref50.fa", " -eps",
187             "test_uniref50_out.eps", false, MINFILESIZE_BIG, TEST_TIMEOUT },
188         { "headless -open examples/uniref50.fa", " -eps",
189             "test_uniref50_out.eps", true, MINFILESIZE_BIG, TEST_TIMEOUT },
190         { "headless -open examples/uniref50.fa", " -svg",
191             "test_uniref50_out.svg", false, MINFILESIZE_BIG, TEST_TIMEOUT },
192         { "headless -open examples/uniref50.fa", " -png",
193             "test_uniref50_out.png", true, MINFILESIZE_BIG, TEST_TIMEOUT },
194         { "headless -open examples/uniref50.fa", " -html",
195             "test_uniref50_out.html", true, MINFILESIZE_BIG, TEST_TIMEOUT },
196         { "headless -open examples/uniref50.fa", " -fasta",
197             "test_uniref50_out.mfa", true, MINFILESIZE_SMALL, TEST_TIMEOUT },
198         { "headless -open examples/uniref50.fa", " -clustal",
199             "test_uniref50_out.aln", true, MINFILESIZE_SMALL, TEST_TIMEOUT },
200         { "headless -open examples/uniref50.fa", " -msf",
201             "test_uniref50_out.msf", true, MINFILESIZE_SMALL, TEST_TIMEOUT },
202         { "headless -open examples/uniref50.fa", " -pileup",
203             "test_uniref50_out.aln", true, MINFILESIZE_SMALL, TEST_TIMEOUT },
204         { "headless -open examples/uniref50.fa", " -pir",
205             "test_uniref50_out.pir", true, MINFILESIZE_SMALL, TEST_TIMEOUT },
206         { "headless -open examples/uniref50.fa", " -pfam",
207             "test_uniref50_out.pfam", true, MINFILESIZE_SMALL, TEST_TIMEOUT },
208         { "headless -open examples/uniref50.fa", " -blc",
209             "test_uniref50_out.blc", true, MINFILESIZE_SMALL, TEST_TIMEOUT },
210         { "headless -open examples/uniref50.fa", " -jalview",
211             "test_uniref50_out.jvp", true, MINFILESIZE_SMALL, TEST_TIMEOUT }, };
212   }
213 }