a1c41c7deafd0d36b5c2020bd8a8d0abd6c959ef
[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.IOException;
26 import java.io.InputStreamReader;
27
28 import org.junit.AfterClass;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31
32 public class CommandLineOperations
33 {
34
35   @BeforeClass
36   public static void setUpBeforeClass() throws Exception
37   {
38   }
39
40   @AfterClass
41   public static void tearDownAfterClass() throws Exception
42   {
43   }
44 /***
45  * from http://stackoverflow.com/questions/808276/how-to-add-a-timeout-value-when-using-javas-runtime-exec
46  * @author jimp
47  *
48  */
49 private static class Worker extends Thread {
50   private final Process process;
51   private Integer exit;
52   private Worker(Process process) {
53     this.process = process;
54   }
55   public void run() {
56     try { 
57       exit = process.waitFor();
58     } catch (InterruptedException ignore) {
59       return;
60     }
61   }
62 }
63 private Worker jalviewDesktopRunner(boolean withAwt, String cmd, int timeout) throws InterruptedException, IOException
64   {
65     String _cmd = "java "+(withAwt ? "-Djava.awt.headless=true":"")+" -Djava.ext.dirs=./lib -classpath ./classes jalview.bin.Jalview ";
66     System.out.println("###############Jalview CMD: " + _cmd+cmd);
67     Process ls2_proc = Runtime.getRuntime().exec(_cmd+cmd);
68     BufferedReader outputReader = new BufferedReader(new InputStreamReader(
69             ls2_proc.getInputStream()));
70
71     BufferedReader errorReader = new BufferedReader(new InputStreamReader(
72             ls2_proc.getErrorStream()));
73     Worker worker = new Worker(ls2_proc);
74     worker.start();
75     worker.join(timeout);
76     System.out.println("Output:  ");
77     String ln = null;
78     while ((ln = outputReader.readLine()) != null)
79     {
80       System.out.println(ln);
81     }
82
83     System.out.println("Error:  ");
84     while ((ln = errorReader.readLine()) != null)
85     {
86       System.out.println(ln);
87     }
88     return worker;
89   }
90   @Test
91   public void testHeadlessModeEPS() throws Exception
92   {
93     String[] headlessArgs=new String[] { "nodisplay","headless","nogui"};
94     for (String _harg:headlessArgs)
95     {
96       boolean _switch=false,withAwt=false;
97       do {
98         if (_switch)
99         {
100           withAwt=true;
101         }
102         _switch=true;
103     String jalview_input = "examples/uniref50.fa";
104     String jalview_output = "test_uniref50_out.eps";
105     String cmd = ""+_harg+" -open "+ jalview_input + " -eps " + jalview_output;
106     String harg = _harg+(withAwt ? "-Djava.awt.headless=true":" NO AWT.HEADLESS");
107     System.out.println("Testing with Headless argument: '"+harg+"'\n");
108     Worker worker = jalviewDesktopRunner(withAwt, cmd, 9000);
109     assertTrue("Didn't create an output EPS file.["+harg+"]", new File("test_uniref50_out.eps").exists());
110     assertTrue("Didn't create an EPS file with any content["+harg+"]", new File("test_uniref50_out.eps").length()>4096);
111     if (worker.exit == null){
112       worker.interrupt();
113       Thread.currentThread().interrupt();
114       worker.process.destroy();
115       fail("Jalview did not exit after EPS generation (try running test again to verify - timeout at 9000ms). ["+harg+"]");
116     }
117     } while (!withAwt);
118     } 
119   }
120 //  @Test
121 //  public void testJalview2XMLDataset() throws Exception
122 //  {
123 //    String jalview_input = "examples/uniref50.fa";
124 //    String jalview_output = "test_uniref50_out.eps";
125 //    String cmd = ""+" -open "+ jalview_input + " -eps " + jalview_output;
126 //    //String harg = _harg+(withAwt ? "-Djava.awt.headless=true":" NO AWT.HEADLESS");
127 //    System.out.println("Testing with Headless argument: '"+harg+"'\n");
128 //    Worker worker = jalviewDesktopRunner(withAwt, cmd, 9000);
129 //    assertTrue("Didn't create an output EPS file.["+harg+"]", new File("test_uniref50_out.eps").exists());
130 //    assertTrue("Didn't create an EPS file with any content["+harg+"]", new File("test_uniref50_out.eps").length()>4096);
131 //    if (worker.exit == null){
132 //      worker.interrupt();
133 //      Thread.currentThread().interrupt();
134 //      worker.process.destroy();
135 //      fail("Jalview did not exit after EPS generation (try running test again to verify - timeout at 9000ms). ["+harg+"]");
136 //    }
137 //  }
138 }