1   package net.sourceforge.quexec.proc;
2   
3   
4   import static org.junit.Assert.assertEquals;
5   import static org.junit.Assert.assertFalse;
6   import static org.junit.Assert.assertTrue;
7   
8   import java.io.IOException;
9   import java.util.concurrent.TimeoutException;
10  
11  import javax.annotation.Resource;
12  
13  import org.junit.Test;
14  import org.junit.runner.RunWith;
15  import org.springframework.test.context.ContextConfiguration;
16  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
17  
18  @RunWith(SpringJUnit4ClassRunner.class)
19  @ContextConfiguration(locations={
20  		"/net/sourceforge/quexec/proc/LocalProcessController-context.xml",
21          "/spring/beanPostProcess-context.xml"})
22  public class LocalProcessControllerTest {
23  	
24  	@Resource(name="quexec:localProcessController")
25  	LocalProcessController procCtrl;
26  	
27  	@Test(timeout=10000)
28  	public void runCommandViaLocalProcessController()
29  	throws IOException, InterruptedException, TimeoutException {
30  		ProcessExecutor pexec = procCtrl.getProcessExecutor();
31  
32  		pexec.execJava(CatMain.class.getName());
33  		
34  		final String[] msg = {"foo", "bar", "baz and something else"};
35  		for (String s : msg) {
36  			procCtrl.getProcessInput().sendPacket(s + "\n");
37  		}
38  		procCtrl.getProcessInput().finish();
39  		
40  		pexec.waitFor();
41  		
42  		for (String s : msg) {
43  			assertFalse(procCtrl.getProcessOutput().isFinished());
44  			assertEquals(s + "\n", procCtrl.getProcessOutput().readPacket());
45  		}
46  		assertTrue(procCtrl.getProcessOutput().isFinished());
47  	}
48  
49  	@Test(timeout=10000)
50  	public void runCommandLocallyAndCollectAllOutput()
51  	throws IOException, InterruptedException, TimeoutException {
52  		ProcessExecutor pexec = procCtrl.getProcessExecutor();
53  
54  		pexec.execJava(CatMain.class.getName());
55  		
56  		final String[] msg = {"foo", "bar", "baz and something else"};
57  		StringBuilder expected = new StringBuilder();
58  		for (String s : msg) {
59  			expected.append(s + "\n");
60  			procCtrl.getProcessInput().sendPacket(s + "\n");
61  		}
62  		procCtrl.getProcessInput().finish();
63  		
64  		pexec.waitFor();
65  
66  		assertEquals(expected.toString(), procCtrl.collectProcessOutput());
67  	}
68  }