JAL-1620 version bump and release notes
[jalview.git] / test / jalview / bin / CommandLineOperations.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
3  * Copyright (C) 2014 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 static org.junit.Assert.*;
24
25 import java.io.BufferedReader;
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.InputStreamReader;
29
30 import org.junit.AfterClass;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33
34 public class CommandLineOperations
35 {
36
37   @BeforeClass
38   public static void setUpBeforeClass() throws Exception
39   {
40   }
41
42   @AfterClass
43   public static void tearDownAfterClass() throws Exception
44   {
45   }
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 Integer exit;
60
61     private Worker(Process process)
62     {
63       this.process = process;
64     }
65
66     public void run()
67     {
68       try
69       {
70         exit = process.waitFor();
71       } catch (InterruptedException ignore)
72       {
73         return;
74       }
75     }
76   }
77
78   private Worker jalviewDesktopRunner(boolean withAwt, String cmd,
79           int timeout) throws InterruptedException, IOException
80   {
81     String _cmd = "java "
82             + (withAwt ? "-Djava.awt.headless=true" : "")
83             + " -Djava.ext.dirs=./lib -classpath ./classes jalview.bin.Jalview ";
84     System.out.println("###############Jalview CMD: " + _cmd + cmd);
85     Process ls2_proc = Runtime.getRuntime().exec(_cmd + cmd);
86     BufferedReader outputReader = new BufferedReader(new InputStreamReader(
87             ls2_proc.getInputStream()));
88
89     BufferedReader errorReader = new BufferedReader(new InputStreamReader(
90             ls2_proc.getErrorStream()));
91     Worker worker = new Worker(ls2_proc);
92     worker.start();
93     worker.join(timeout);
94     System.out.println("Output:  ");
95     String ln = null;
96     while ((ln = outputReader.readLine()) != null)
97     {
98       System.out.println(ln);
99     }
100
101     System.out.println("Error:  ");
102     while ((ln = errorReader.readLine()) != null)
103     {
104       System.out.println(ln);
105     }
106     return worker;
107   }
108
109   @Test
110   public void testHeadlessModeEPS() throws Exception
111   {
112     String[] headlessArgs = new String[]
113     { "nodisplay", "headless", "nogui" };
114     for (String _harg : headlessArgs)
115     {
116       boolean _switch = false, withAwt = false;
117       do
118       {
119         if (_switch)
120         {
121           withAwt = true;
122         }
123         _switch = true;
124         String jalview_input = "examples/uniref50.fa";
125         String jalview_output = "test_uniref50_out.eps";
126         String cmd = "" + _harg + " -open " + jalview_input + " -eps "
127                 + jalview_output;
128         String harg = _harg
129                 + (withAwt ? "-Djava.awt.headless=true"
130                         : " NO AWT.HEADLESS");
131         System.out.println("Testing with Headless argument: '" + harg
132                 + "'\n");
133         Worker worker = jalviewDesktopRunner(withAwt, cmd, 9000);
134         assertTrue("Didn't create an output EPS file.[" + harg + "]",
135                 new File("test_uniref50_out.eps").exists());
136         assertTrue("Didn't create an EPS file with any content[" + harg
137                 + "]", new File("test_uniref50_out.eps").length() > 4096);
138         if (worker.exit == null)
139         {
140           worker.interrupt();
141           Thread.currentThread().interrupt();
142           worker.process.destroy();
143           fail("Jalview did not exit after EPS generation (try running test again to verify - timeout at 9000ms). ["
144                   + harg + "]");
145         }
146       } while (!withAwt);
147     }
148   }
149   // @Test
150   // public void testJalview2XMLDataset() throws Exception
151   // {
152   // String jalview_input = "examples/uniref50.fa";
153   // String jalview_output = "test_uniref50_out.eps";
154   // String cmd = ""+" -open "+ jalview_input + " -eps " + jalview_output;
155   // //String harg = _harg+(withAwt ?
156   // "-Djava.awt.headless=true":" NO AWT.HEADLESS");
157   // System.out.println("Testing with Headless argument: '"+harg+"'\n");
158   // Worker worker = jalviewDesktopRunner(withAwt, cmd, 9000);
159   // assertTrue("Didn't create an output EPS file.["+harg+"]", new
160   // File("test_uniref50_out.eps").exists());
161   // assertTrue("Didn't create an EPS file with any content["+harg+"]", new
162   // File("test_uniref50_out.eps").length()>4096);
163   // if (worker.exit == null){
164   // worker.interrupt();
165   // Thread.currentThread().interrupt();
166   // worker.process.destroy();
167   // fail("Jalview did not exit after EPS generation (try running test again to verify - timeout at 9000ms). ["+harg+"]");
168   // }
169   // }
170 }