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.File;
21  import java.io.FileWriter;
22  import java.util.Locale;
23  
24  import com.tomgibara.pronto.config.Config;
25  import com.tomgibara.pronto.config.ConfigFactory;
26  import com.tomgibara.pronto.config.DefaultConfigPolicy;
27  import com.tomgibara.pronto.config.source.StringConfigSource;
28  import com.tomgibara.pronto.control.TestUtil.Label;
29  import com.tomgibara.pronto.control.TestUtil.State;
30  import com.tomgibara.pronto.state.ProntoStateException;
31  import com.tomgibara.pronto.state.StateActivator;
32  import com.tomgibara.pronto.state.StateEngine;
33  import com.tomgibara.pronto.state.StateFactory;
34  import com.tomgibara.pronto.state.StateGraph;
35  import com.tomgibara.pronto.state.StateGraphEditor;
36  import com.tomgibara.pronto.state.StateTransition;
37  
38  import junit.framework.TestCase;
39  
40  public class TestFileController extends TestCase {
41  
42  	public TestFileController() {
43  	}
44  	
45  	
46  	public void testCreate() {
47  		assertNotNull( createController("") );
48  	}
49  
50  	public void testStartStop() {
51  		Controller<State, Label, Object> controller = createController("file: test.txt");
52  		controller.start();
53  		controller.stop(0);
54  		assertEquals(State.constructed, controller.getEngine().getState());
55  	}
56  	
57  	public void testSingleChange() throws Exception {
58  		doTest("test.txt", "file: %s; check-period: 10ms", "init", State.initialized, false, true, true);
59  	}
60  
61  	public void testMultipleChange() throws Exception {
62  		doTest("test.txt", "file: %s; check-period: 10ms", "init\nstart", State.started, false, true, true);
63  	}
64  
65  	public void testDeleteFile() throws Exception {
66  		doTest("test.txt", "file: %s; check-period: 10ms; file-deleted: true", "init", State.initialized, false, true, false);
67  	}
68  
69  	public void testPreexisting() throws Exception {
70  		doTest("test.txt", "file: %s; check-period: 10ms", "init", State.initialized, true, true, true);
71  	}
72  
73  	public void testPreexistingIgnored() throws Exception {
74  		doTest("test.txt", "file: %s; check-period: 10ms; preexisting-ignored: true", "init", State.initialized, true, false, true);
75  	}
76  
77  	public void testPreexistingIgnoredDelete() throws Exception {
78  		doTest("test.txt", "file: %s; check-period: 10ms; preexisting-ignored: true; file-deleted: true", "init", State.initialized, true, false, true);
79  	}
80  
81  	public void testNoopFile() throws Exception {
82  		doTest("test.txt", "file: %s; check-period: 10ms", "", State.initialized, false, false, true);
83  	}
84  
85  	private Controller createController(String str) {
86  		return TestUtil.createController(FileControllerSettings.class, str);
87  	}
88  	
89  	private void doTest(String filename, String settings, String lines, State finalState, boolean fileFirst, boolean reached, boolean fileExists) throws Exception {
90  		settings = String.format(settings, filename);
91  		Controller<State, Label, Object> controller = createController(settings);
92  		File file = new File(filename);
93  		try {
94  			if (fileFirst) {
95  				FileWriter writer = new FileWriter(file);
96  				writer.write(lines);
97  				writer.close();
98  			}
99  			controller.start();
100 			if (!fileFirst) {
101 				FileWriter writer = new FileWriter(file);
102 				writer.write(lines);
103 				writer.close();
104 			}
105 			assertEquals(reached, TestUtil.waitForState(finalState, controller, 1000) );
106 			controller.stop(0);
107 			//assertEquals(State.initialized, controller.getEngine().getState());
108 			assertEquals(fileExists, file.exists());
109 		} finally {
110 			file.delete();
111 		}
112 	}
113 	
114 }