JAL-1432 updated copyright notices
[jalview.git] / test / jalview / bin / CommandLineOperations.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.bin;
20
21 import static org.junit.Assert.*;
22
23 import java.io.BufferedReader;
24 import java.io.File;
25 import java.io.InputStreamReader;
26
27 import org.junit.AfterClass;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30
31 public class CommandLineOperations
32 {
33
34   @BeforeClass
35   public static void setUpBeforeClass() throws Exception
36   {
37   }
38
39   @AfterClass
40   public static void tearDownAfterClass() throws Exception
41   {
42   }
43 /***
44  * from http://stackoverflow.com/questions/808276/how-to-add-a-timeout-value-when-using-javas-runtime-exec
45  * @author jimp
46  *
47  */
48 private static class Worker extends Thread {
49   private final Process process;
50   private Integer exit;
51   private Worker(Process process) {
52     this.process = process;
53   }
54   public void run() {
55     try { 
56       exit = process.waitFor();
57     } catch (InterruptedException ignore) {
58       return;
59     }
60   }
61 }
62   @Test
63   public void testHeadlessModeEPS() throws Exception
64   {
65     String[] headlessArgs=new String[] { "nodisplay","headless","nogui"};
66     for (String _harg:headlessArgs)
67     {
68       boolean _switch=false,withAwt=false;
69       do {
70         if (_switch)
71         {
72           withAwt=true;
73         }
74         _switch=true;
75     String jalview_input = "examples/uniref50.fa";
76     String jalview_output = "test_uniref50_out.eps";
77     String cmd = "java "+(withAwt ? "-Djava.awt.headless=true":"")+" -Djava.ext.dirs=./lib -classpath ./classes jalview.bin.Jalview -"+_harg+" -open "+ jalview_input + " -eps " + jalview_output;
78     String harg = _harg+(withAwt ? "-Djava.awt.headless=true":" NO AWT.HEADLESS");
79     System.out.println("Testing with Headless argument: '"+harg+"'\n###############Jalview CMD: " + cmd);
80     Process ls2_proc = Runtime.getRuntime().exec(cmd);
81     BufferedReader outputReader = new BufferedReader(new InputStreamReader(ls2_proc.getInputStream()));
82         
83     BufferedReader errorReader = new BufferedReader(new InputStreamReader(ls2_proc.getErrorStream()));
84     Worker worker = new Worker(ls2_proc);
85     worker.start();
86     worker.join(9000);
87     System.out.println("Output:  ");
88     String ln=null;
89     while ((ln=outputReader.readLine())!=null) {
90       System.out.println(ln);
91     }
92     
93     System.out.println("Error:  " );
94     while ((ln=errorReader.readLine())!=null) {
95       System.out.println(ln);
96     }
97     assertTrue("Didn't create an output EPS file.["+harg+"]", new File("test_uniref50_out.eps").exists());
98     assertTrue("Didn't create an EPS file with any content["+harg+"]", new File("test_uniref50_out.eps").length()>4096);
99     if (worker.exit == null){
100       worker.interrupt();
101       Thread.currentThread().interrupt();
102       ls2_proc.destroy();
103       fail("Jalview did not exit after EPS generation (try running test again to verify - timeout at 9000ms). ["+harg+"]");
104     }
105     } while (!withAwt);
106     } 
107   }
108
109 }