JAL-1315 test for exit and successful generation of EPS using commandline
[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 jalview_input = "examples/uniref50.fa";
48     String jalview_output = "test_uniref50_out.eps";
49     String cmd = "java -Djava.awt.headless=true -Djava.ext.dirs=./lib -classpath ./classes jalview.bin.Jalview -nodisplay -open "+ jalview_input + " -eps " + jalview_output;
50
51     System.out.println("###############Jalview CMD: " + cmd);
52     Process ls2_proc = Runtime.getRuntime().exec(cmd);
53     BufferedReader outputReader = new BufferedReader(new InputStreamReader(ls2_proc.getInputStream()));
54         
55     BufferedReader errorReader = new BufferedReader(new InputStreamReader(ls2_proc.getErrorStream()));
56     Worker worker = new Worker(ls2_proc);
57     worker.start();
58     worker.join(9000);
59     System.out.println("Output:  ");
60     String ln=null;
61     while ((ln=outputReader.readLine())!=null) {
62       System.out.println(ln);
63     }
64     
65     System.out.println("Error:  " );
66     while ((ln=errorReader.readLine())!=null) {
67       System.out.println(ln);
68     }
69     assertTrue("Didn't create an output EPS file.", new File("test_uniref50_out.eps").exists());
70     assertTrue("Didn't create an EPS file with any content", new File("test_uniref50_out.eps").length()>4096);
71     if (worker.exit == null){
72       worker.interrupt();
73       Thread.currentThread().interrupt();
74       ls2_proc.destroy();
75       fail("Jalview did not exit after EPS generation (try running test again to verify - timeout at 9000ms).");
76 }
77   }
78
79 }