05ada31c3d1e8e622a44ef8f2083788c7d558aaa
[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(dataProvider = "headlessModeOutputParams")
118   public void testHeadlessModeOutputs(String harg, String type,
119           String fileName, boolean withAWT, int expectedMinFileSize)
120   {
121     String cmd = harg + type + " " + fileName;
122     // System.out.println(">>>>>>>>>>>>>>>> Command : " + cmd);
123     File file = new File(fileName);
124     Worker worker = null;
125     try
126     {
127       worker = jalviewDesktopRunner(withAWT, cmd, 9000);
128     } catch (InterruptedException e)
129     {
130       e.printStackTrace();
131     } catch (IOException e)
132     {
133       e.printStackTrace();
134     }
135
136     FileAssert.assertFile(file, "Didn't create an output" + type
137             + " file.[" + harg + "]");
138     FileAssert.assertMinLength(new File(fileName), expectedMinFileSize);
139     if (worker != null && worker.exit == null)
140     {
141       worker.interrupt();
142       Thread.currentThread().interrupt();
143       worker.process.destroy();
144       Assert.fail("Jalview did not exit after  "
145               + type
146               + " generation (try running test again to verify - timeout at 9000ms). ["
147               + harg + "]");
148     }
149     new File(fileName).delete();
150   }
151
152   @DataProvider(name = "headlessModeOutputParams")
153   public static Object[][] headlessModeOutput()
154   {
155     return new Object[][]
156     {
157         { "nodisplay -open examples/uniref50.fa", " -eps",
158             "test_uniref50_out.eps", true, 4096 },
159         { "nodisplay -open examples/uniref50.fa", " -eps",
160             "test_uniref50_out.eps", false, 4096 },
161         { "headless -open examples/uniref50.fa", " -eps",
162             "test_uniref50_out.eps", true, 4096 },
163         { "headless -open examples/uniref50.fa", " -eps",
164             "test_uniref50_out.eps", false, 4096 },
165         { "nogui -open examples/uniref50.fa", " -eps",
166             "test_uniref50_out.eps", true, 4096 },
167         { "nogui -open examples/uniref50.fa", " -eps",
168             "test_uniref50_out.eps", false, 4096 },
169         { "headless -open examples/uniref50.fa", " -svg",
170             "test_uniref50_out.svg", true, 4096 },
171         { "headless -open examples/uniref50.fa", " -svg",
172             "test_uniref50_out.svg", false, 4096 },
173         { "headless -open examples/uniref50.fa", " -png",
174             "test_uniref50_out.png", true, 4096 },
175         { "headless -open examples/uniref50.fa", " -png",
176             "test_uniref50_out.png", false, 4096 },
177         { "headless -open examples/uniref50.fa", " -html",
178             "test_uniref50_out.html", true, 4096 },
179         { "headless -open examples/uniref50.fa", " -html",
180             "test_uniref50_out.html", false, 4096 },
181         { "headless -open examples/uniref50.fa", " -fasta",
182             "test_uniref50_out.mfa", true, 2096 },
183         { "headless -open examples/uniref50.fa", " -fasta",
184             "test_uniref50_out.mfa", false, 2096 },
185         { "headless -open examples/uniref50.fa", " -clustal",
186             "test_uniref50_out.aln", true, 2096 },
187         { "headless -open examples/uniref50.fa", " -clustal",
188             "test_uniref50_out.aln", false, 2096 },
189         { "headless -open examples/uniref50.fa", " -msf",
190             "test_uniref50_out.msf", true, 2096 },
191         { "headless -open examples/uniref50.fa", " -msf",
192             "test_uniref50_out.msf", false, 2096 },
193         { "headless -open examples/uniref50.fa", " -pileup",
194             "test_uniref50_out.aln", true, 2096 },
195         { "headless -open examples/uniref50.fa", " -pileup",
196             "test_uniref50_out.aln", false, 2096 },
197         { "headless -open examples/uniref50.fa", " -pir",
198             "test_uniref50_out.pir", true, 2096 },
199         { "headless -open examples/uniref50.fa", " -pir",
200             "test_uniref50_out.pir", false, 2096 },
201         { "headless -open examples/uniref50.fa", " -pfam",
202             "test_uniref50_out.pfam", true, 2096 },
203         { "headless -open examples/uniref50.fa", " -pfam",
204             "test_uniref50_out.pfam", false, 2096 },
205         { "headless -open examples/uniref50.fa", " -blc",
206             "test_uniref50_out.blc", true, 2096 },
207         { "headless -open examples/uniref50.fa", " -blc",
208             "test_uniref50_out.blc", false, 2096 },
209         { "headless -open examples/uniref50.fa", " -jalview",
210             "test_uniref50_out.jvp", true, 2096 },
211         { "headless -open examples/uniref50.fa", " -jalview",
212             "test_uniref50_out.jvp", false, 2096 },
213     };
214   }
215
216
217 }