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