1   /*
2    * Copyright (C) 2006  Tom Gibara
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
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  }