c6c82c9d313c29ca41136b38353acf8c49fdac2b
[jalview.git] / test / jalview / bin / CommandLineOperations.java
1 package jalview.bin;
2
3 import static org.junit.Assert.*;
4
5 import java.io.BufferedReader;
6 import java.io.File;
7 import java.io.InputStreamReader;
8
9 import org.junit.AfterClass;
10 import org.junit.BeforeClass;
11 import org.junit.Test;
12
13 public class CommandLineOperations
14 {
15
16   @BeforeClass
17   public static void setUpBeforeClass() throws Exception
18   {
19   }
20
21   @AfterClass
22   public static void tearDownAfterClass() throws Exception
23   {
24   }
25 /***
26  * from http://stackoverflow.com/questions/808276/how-to-add-a-timeout-value-when-using-javas-runtime-exec
27  * @author jimp
28  *
29  */
30 private static class Worker extends Thread {
31   private final Process process;
32   private Integer exit;
33   private Worker(Process process) {
34     this.process = process;
35   }
36   public void run() {
37     try { 
38       exit = process.waitFor();
39     } catch (InterruptedException ignore) {
40       return;
41     }
42   }
43 }
44   @Test
45   public void testHeadlessModeEPS() throws Exception
46   {
47     String[] headlessArgs=new String[] { "nodisplay","headless","nogui"};
48     for (String _harg:headlessArgs)
49     {
50       boolean _switch=false,withAwt=false;
51       do {
52         if (_switch)
53         {
54           withAwt=true;
55         }
56         _switch=true;
57     String jalview_input = "examples/uniref50.fa";
58     String jalview_output = "test_uniref50_out.eps";
59     String cmd = "java "+(withAwt ? "-Djava.awt.headless=true":"")+" -Djava.ext.dirs=./lib -classpath ./classes jalview.bin.Jalview -"+_harg+" -open "+ jalview_input + " -eps " + jalview_output;
60     String harg = _harg+(withAwt ? "-Djava.awt.headless=true":" NO AWT.HEADLESS");
61     System.out.println("Testing with Headless argument: '"+harg+"'\n###############Jalview CMD: " + cmd);
62     Process ls2_proc = Runtime.getRuntime().exec(cmd);
63     BufferedReader outputReader = new BufferedReader(new InputStreamReader(ls2_proc.getInputStream()));
64         
65     BufferedReader errorReader = new BufferedReader(new InputStreamReader(ls2_proc.getErrorStream()));
66     Worker worker = new Worker(ls2_proc);
67     worker.start();
68     worker.join(9000);
69     System.out.println("Output:  ");
70     String ln=null;
71     while ((ln=outputReader.readLine())!=null) {
72       System.out.println(ln);
73     }
74     
75     System.out.println("Error:  " );
76     while ((ln=errorReader.readLine())!=null) {
77       System.out.println(ln);
78     }
79     assertTrue("Didn't create an output EPS file.["+harg+"]", new File("test_uniref50_out.eps").exists());
80     assertTrue("Didn't create an EPS file with any content["+harg+"]", new File("test_uniref50_out.eps").length()>4096);
81     if (worker.exit == null){
82       worker.interrupt();
83       Thread.currentThread().interrupt();
84       ls2_proc.destroy();
85       fail("Jalview did not exit after EPS generation (try running test again to verify - timeout at 9000ms). ["+harg+"]");
86     }
87     } while (!withAwt);
88     } 
89   }
90
91 }