1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.tomgibara.pronto.control;
19
20 import java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.io.OutputStreamWriter;
25
26 import com.tomgibara.pronto.control.TestUtil.Label;
27 import com.tomgibara.pronto.control.TestUtil.State;
28
29 import junit.framework.TestCase;
30
31 public class TestStdinController extends TestCase {
32
33 private static Controller<State, Label, Object> createController(String str) {
34 return TestUtil.createController(StdinControllerSettings.class, str);
35 }
36
37 public static void main(String... args) {
38
39 Controller<State, Label, Object> controller = createController("");
40 controller.start();
41 }
42
43 public void testCreate() {
44 assertNotNull( createController("") );
45 }
46
47 public void testStartStop() throws Exception {
48 Controller<State, Label, Object> controller = createController("");
49 controller.start();
50 controller.stop(0L);
51 }
52
53 public void testSingleChange() throws Exception {
54 doTest("", "init\n", State.initialized, true);
55 }
56
57 public void testMultipleChange() throws Exception {
58 doTest("", "init\nstart\n", State.started, true);
59 }
60
61 public void testNoChange() throws Exception {
62 doTest("", "", State.initialized, false);
63 }
64
65
66 private void doTest(String settings, String lines, State finalState, boolean reached) throws Exception {
67 final InputStream in = System.in;
68 ByteArrayOutputStream out = new ByteArrayOutputStream();
69 OutputStreamWriter writer = new OutputStreamWriter(out);
70 writer.write(lines);
71 writer.close();
72 InputStream in2 = new ByteArrayInputStream(out.toByteArray());
73 System.setIn(in2);
74 try {
75 Controller<State, Label, Object> controller = createController(settings);
76 controller.start();
77 assertEquals(reached, TestUtil.waitForState(finalState, controller, 1000) );
78 controller.stop(0L);
79 } finally {
80 System.setIn(in);
81 }
82 }
83 }