JAL-1805 test envirionment separation
[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
28 import org.testng.Assert;
29 import org.testng.FileAssert;
30 import org.testng.annotations.AfterClass;
31 import org.testng.annotations.BeforeClass;
32 import org.testng.annotations.BeforeTest;
33 import org.testng.annotations.DataProvider;
34 import org.testng.annotations.Test;
35
36 public class CommandLineOperations
37 {
38
39   @BeforeClass
40   public static void setUpBeforeClass() throws Exception
41   {
42   }
43
44   @AfterClass
45   public static void tearDownAfterClass() throws Exception
46   {
47   }
48
49   /***
50    * from
51    * http://stackoverflow.com/questions/808276/how-to-add-a-timeout-value-when
52    * -using-javas-runtime-exec
53    * 
54    * @author jimp
55    * 
56    */
57   private static class Worker extends Thread
58   {
59     private final Process process;
60
61     private Integer exit;
62
63     private Worker(Process process)
64     {
65       this.process = process;
66     }
67
68     public void run()
69     {
70       try
71       {
72         exit = process.waitFor();
73       } catch (InterruptedException ignore)
74       {
75         return;
76       }
77     }
78   }
79
80   private Worker jalviewDesktopRunner(boolean withAwt, String cmd,
81           int timeout) throws InterruptedException, IOException
82   {
83     String _cmd = "java "
84             + (withAwt ? "-Djava.awt.headless=true" : "")
85             + " -Djava.ext.dirs=./lib -classpath ./classes jalview.bin.Jalview ";
86     System.out.println("############ Testing Jalview CMD: " + _cmd + cmd);
87     Process ls2_proc = Runtime.getRuntime().exec(_cmd + cmd);
88     BufferedReader outputReader = new BufferedReader(new InputStreamReader(
89             ls2_proc.getInputStream()));
90
91     BufferedReader errorReader = new BufferedReader(new InputStreamReader(
92             ls2_proc.getErrorStream()));
93     Worker worker = new Worker(ls2_proc);
94     worker.start();
95     worker.join(timeout);
96     // System.out.println("Output:  ");
97     // String ln = null;
98     // while ((ln = outputReader.readLine()) != null)
99     // {
100     // System.out.println(ln);
101     // }
102     //
103     // System.out.println("Error:  ");
104     // while ((ln = errorReader.readLine()) != null)
105     // {
106     // System.out.println(ln);
107     // }
108     return worker;
109   }
110
111   @BeforeTest
112   public void initialize()
113   {
114     new CommandLineOperations();
115   }
116
117   @Test(groups =
118   { "Functional" }, dataProvider = "headlessModeOutputParams")
119   public void testHeadlessModeOutputs(String harg, String type,
120           String fileName, boolean withAWT, int expectedMinFileSize)
121   {
122     String cmd = harg + type + " " + fileName;
123     // System.out.println(">>>>>>>>>>>>>>>> Command : " + cmd);
124     File file = new File(fileName);
125     Worker worker = null;
126     try
127     {
128       worker = jalviewDesktopRunner(withAWT, cmd, 9000);
129     } catch (InterruptedException e)
130     {
131       e.printStackTrace();
132     } catch (IOException e)
133     {
134       e.printStackTrace();
135     }
136
137     FileAssert.assertFile(file, "Didn't create an output" + type
138             + " file.[" + harg + "]");
139     FileAssert.assertMinLength(new File(fileName), expectedMinFileSize);
140     if (worker != null && worker.exit == null)
141     {
142       worker.interrupt();
143       Thread.currentThread().interrupt();
144       worker.process.destroy();
145       Assert.fail("Jalview did not exit after  "
146               + type
147               + " generation (try running test again to verify - timeout at 9000ms). ["
148               + harg + "]");
149     }
150     new File(fileName).delete();
151   }
152
153   @DataProvider(name = "headlessModeOutputParams")
154   public static Object[][] headlessModeOutput()
155   {
156     return new Object[][]
157     {
158         { "nodisplay -open examples/uniref50.fa", " -eps",
159             "test_uniref50_out.eps", true, 4096 },
160         { "nodisplay -open examples/uniref50.fa", " -eps",
161             "test_uniref50_out.eps", false, 4096 },
162         { "headless -open examples/uniref50.fa", " -eps",
163             "test_uniref50_out.eps", true, 4096 },
164         { "headless -open examples/uniref50.fa", " -eps",
165             "test_uniref50_out.eps", false, 4096 },
166         { "nogui -open examples/uniref50.fa", " -eps",
167             "test_uniref50_out.eps", true, 4096 },
168         { "nogui -open examples/uniref50.fa", " -eps",
169             "test_uniref50_out.eps", false, 4096 },
170         { "headless -open examples/uniref50.fa", " -svg",
171             "test_uniref50_out.svg", true, 4096 },
172         { "headless -open examples/uniref50.fa", " -svg",
173             "test_uniref50_out.svg", false, 4096 },
174         { "headless -open examples/uniref50.fa", " -png",
175             "test_uniref50_out.png", true, 4096 },
176         { "headless -open examples/uniref50.fa", " -png",
177             "test_uniref50_out.png", false, 4096 },
178         { "headless -open examples/uniref50.fa", " -html",
179             "test_uniref50_out.html", true, 4096 },
180         { "headless -open examples/uniref50.fa", " -html",
181             "test_uniref50_out.html", false, 4096 },
182         { "headless -open examples/uniref50.fa", " -fasta",
183             "test_uniref50_out.mfa", true, 2096 },
184         { "headless -open examples/uniref50.fa", " -fasta",
185             "test_uniref50_out.mfa", false, 2096 },
186         { "headless -open examples/uniref50.fa", " -clustal",
187             "test_uniref50_out.aln", true, 2096 },
188         { "headless -open examples/uniref50.fa", " -clustal",
189             "test_uniref50_out.aln", false, 2096 },
190         { "headless -open examples/uniref50.fa", " -msf",
191             "test_uniref50_out.msf", true, 2096 },
192         { "headless -open examples/uniref50.fa", " -msf",
193             "test_uniref50_out.msf", false, 2096 },
194         { "headless -open examples/uniref50.fa", " -pileup",
195             "test_uniref50_out.aln", true, 2096 },
196         { "headless -open examples/uniref50.fa", " -pileup",
197             "test_uniref50_out.aln", false, 2096 },
198         { "headless -open examples/uniref50.fa", " -pir",
199             "test_uniref50_out.pir", true, 2096 },
200         { "headless -open examples/uniref50.fa", " -pir",
201             "test_uniref50_out.pir", false, 2096 },
202         { "headless -open examples/uniref50.fa", " -pfam",
203             "test_uniref50_out.pfam", true, 2096 },
204         { "headless -open examples/uniref50.fa", " -pfam",
205             "test_uniref50_out.pfam", false, 2096 },
206         { "headless -open examples/uniref50.fa", " -blc",
207             "test_uniref50_out.blc", true, 2096 },
208         { "headless -open examples/uniref50.fa", " -blc",
209             "test_uniref50_out.blc", false, 2096 },
210         { "headless -open examples/uniref50.fa", " -jalview",
211             "test_uniref50_out.jvp", true, 2096 },
212         { "headless -open examples/uniref50.fa", " -jalview",
213             "test_uniref50_out.jvp", false, 2096 },
214     };
215   }
216
217
218 }