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.util.Locale;
21  
22  import com.tomgibara.pronto.config.Config;
23  import com.tomgibara.pronto.config.ConfigFactory;
24  import com.tomgibara.pronto.config.source.StringConfigSource;
25  import com.tomgibara.pronto.state.ProntoStateException;
26  import com.tomgibara.pronto.state.StateActivator;
27  import com.tomgibara.pronto.state.StateEngine;
28  import com.tomgibara.pronto.state.StateFactory;
29  import com.tomgibara.pronto.state.StateGraph;
30  import com.tomgibara.pronto.state.StateGraphEditor;
31  import com.tomgibara.pronto.state.StateTransition;
32  
33  public class TestUtil {
34  
35  	enum State { constructed, initialized, started, stopped, destroyed };
36  	
37  	enum Label { init, start, stop, destroy };
38  	
39  	static final StateGraph<State, Label> graph = createGraph();
40  
41  	static final Adapter adapter = new Adapter();
42  	
43  	static StateGraph<State, Label> createGraph() {
44  		StateGraphEditor<State, Label> editor = StateFactory.getInstance().<State, Label>emptyStateGraph().newEditor();
45  		editor.addTransition(State.constructed, Label.init, State.initialized);
46  		editor.addTransition(State.initialized, Label.start, State.started);
47  		editor.addTransition(State.started, Label.stop, State.initialized);
48  		editor.addTransition(State.initialized, Label.destroy, State.destroyed);
49  		return editor.getGraph();
50  	}
51  
52  
53  	static StateEngine<State, Label, Object> createEngine() {
54  		StateFactory factory = StateFactory.getInstance();
55  		StateEngine<State, Label, Object> engine = factory.newEngine(graph, new Activator());
56  		engine.setState(State.constructed);
57  		return engine;
58  	}
59  	
60  	static ControllerSettings createSettings(Class<? extends ControllerSettings> clss, String str) {
61  		Config config = ConfigFactory.getInstance().newConfig(new StringConfigSource(str), null);
62  		return config.adaptSettings(null, false, clss);
63  	}
64  
65  	static Controller<State, Label, Object> createController(Class<? extends ControllerSettings> clss, String str) {
66  		return ControlFactory.getInstance().<State, Label, Object>newController(createSettings(clss, str), createEngine(), adapter);
67  	}
68  
69  	static boolean waitForState(State state, Controller<State, Label, Object> controller, long timeout) {
70  		long start = System.currentTimeMillis();
71  		while (System.currentTimeMillis() - start < timeout) {
72  			if (controller.getEngine().getState() == state) return true;
73  			try {
74  				Thread.sleep(10);
75  			} catch (InterruptedException e) {
76  				return false;
77  			}
78  		}
79  		return false;
80  	}
81  
82  
83  	static class Activator implements StateActivator<State, Label, Object> {
84  
85  		public void changeState(State state) throws ProntoStateException, RuntimeException {
86  			/* we do nothing */
87  			
88  		}
89  
90  		public void transitionState(StateTransition<State, Label> transition, Object param) throws ProntoStateException, RuntimeException {
91  			/* we do nothing */
92  		}
93  		
94  	}
95  	
96  	static class Adapter implements EngineControlAdapter<State, Label, Object> {
97  
98  		public Object parseParameter(String string) {
99  			return null;
100 		}
101 
102 		public String nameFromLabel(Label label) {
103 			return label.toString();
104 		}
105 
106 		public Label labelFromName(String name) {
107 			try {
108 				return Label.valueOf(name);
109 			} catch (IllegalArgumentException e) {
110 				return null;
111 			}
112 		}
113 		
114 	}
115 
116 	
117 }