View Javadoc

1   package cz.cuni.amis.pogamut.ut2004.test;
2   
3   import java.util.List;
4   import java.util.concurrent.TimeUnit;
5   import java.util.logging.Level;
6   
7   import org.junit.After;
8   import org.junit.Before;
9   
10  import cz.cuni.amis.pogamut.base.agent.IAgent;
11  import cz.cuni.amis.pogamut.base.agent.IAgentId;
12  import cz.cuni.amis.pogamut.base.agent.impl.AbstractAgent;
13  import cz.cuni.amis.pogamut.base.agent.impl.AgentId;
14  import cz.cuni.amis.pogamut.base.agent.params.IRemoteAgentParameters;
15  import cz.cuni.amis.pogamut.base.agent.state.WaitForAgentStateChange;
16  import cz.cuni.amis.pogamut.base.agent.state.level0.IAgentState;
17  import cz.cuni.amis.pogamut.base.agent.state.level1.IAgentStateDown;
18  import cz.cuni.amis.pogamut.base.agent.state.level1.IAgentStateUp;
19  import cz.cuni.amis.pogamut.base.factory.IAgentFactory;
20  import cz.cuni.amis.pogamut.base.utils.Pogamut;
21  import cz.cuni.amis.pogamut.base.utils.logging.LogCategory;
22  import cz.cuni.amis.pogamut.base.utils.logging.LogPublisher;
23  import cz.cuni.amis.pogamut.ut2004.agent.params.UT2004AgentParameters;
24  import cz.cuni.amis.pogamut.ut2004.bot.IUT2004BotController;
25  import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
26  import cz.cuni.amis.pogamut.ut2004.bot.params.UT2004BotParameters;
27  import cz.cuni.amis.pogamut.ut2004.factory.guice.remoteagent.UT2004BotFactory;
28  import cz.cuni.amis.pogamut.ut2004.factory.guice.remoteagent.UT2004BotModule;
29  import cz.cuni.amis.pogamut.ut2004.observer.IUT2004Observer;
30  import cz.cuni.amis.pogamut.ut2004.server.IUT2004Server;
31  import cz.cuni.amis.pogamut.ut2004.server.exception.UCCStartException;
32  import cz.cuni.amis.pogamut.ut2004.utils.PogamutUT2004Property;
33  import cz.cuni.amis.pogamut.ut2004.utils.UCCWrapper;
34  import cz.cuni.amis.pogamut.ut2004.utils.UCCWrapperConf;
35  import cz.cuni.amis.pogamut.ut2004.utils.UT2004BotRunner;
36  import cz.cuni.amis.pogamut.ut2004.utils.UT2004ObserverRunner;
37  import cz.cuni.amis.pogamut.ut2004.utils.UT2004ServerRunner;
38  import cz.cuni.amis.utils.exception.PogamutException;
39  
40  /**
41   * Base class for tests that need a running UCC instance. If you inherit from
42   * this class then before the first @Test method will be called the UCC server
43   * will be executed. The ports where it is listening can be obtained by
44   * <code>ucc.getGbPort()</code> and <code>ucc.getControlPort()</code> calls.
45   * Don't forget to specify pogamut.unreal.home and pogamut.unreal.serverexec
46   * variables.
47   * 
48   * @author ik
49   */
50  public class UT2004Test {
51  	
52  	public static final String[] CTF_MAPS = new String[] {
53  		"CTF-1on1-Joust",
54  		"CTF-AbsoluteZero",
55  		"CTF-Avaris",
56  		"CTF-BridgeOfFate",
57  		"CTF-Chrome",
58  		"CTF-Citadel",
59  		"CTF-Colossus",
60  		"CTF-December",
61  		"CTF-DE-ElecFields",
62  		"CTF-DoubleDammage",
63  		"CTF-Face3",
64  		"CTF-FaceClassic",
65  		"CTF-Geothermal",
66  		"CTF-Grassyknoll",
67  		"CTF-Grendelkeep",
68  		"CTF-January",
69  		"CTF-Lostfaith",
70  		"CTF-Magma",
71  		"CTF-Maul",
72  		"CTF-MoonDragon",
73  		"CTF-Orbital2",
74  		"CTF-Smote",
75  		"CTF-TwinTombs"
76  	};
77  	
78  	public static String[] DM_MAPS = new String[] {
79  		"DM-1on1-Albatross",
80  		"DM-1on1-Crash",
81  		"DM-1on1-Desolation",
82  		"DM-1on1-Idoma",
83  		"DM-1on1-Irondust",
84  		"DM-1on1-Mixer",
85  		"DM-1on1-Roughinery",
86  		"DM-1on1-Serpen-tine",
87  		"DM-1on1-Spirit",
88  		"DM-1on1-Squader",
89  		"DM-1on1-Trite",
90  		"DM-Antalus",
91  		"DM-Asbestos",
92  		"DM-Compressed",
93  		"DM-Corrugation",
94  		"DM-Curse4",
95  		"DM-Deck17",
96  		"DM-DE-Grendelkeep",
97  		"DM-DE-Ironic",
98  		"DM-DE-Osiris2",
99  		"DM-DesertIsle",
100 		"DM-Flux2",
101 		"DM-Gael",
102 		"DM-Gestalt",
103 		"DM-Goliath",
104 		"DM-HyperBlast2",
105 		"DM-Icetomb",
106 		"DM-Inferno",
107 		"DM-Injector",
108 		"DM-Insidious",
109 		"DM-IronDeity",
110 		"DM-Junkyard",
111 		"DM-Leviathan",
112 		"DM-Metallurgy",
113 		"DM-Morpheus3",
114 		"DM-Oceanic",
115 		"DM-Phobos2",
116 		"DM-Plunge",
117 		"DM-Rankin",
118 		"DM-Rrajigar",
119 		"DM-Rustatorium",
120 		"DM-Sulphur",
121 		"DM-TokaraForest",
122 		"DM-TrainingDay"
123 	};
124 
125 	protected IAgentId testId;
126 	
127 	protected LogCategory log;
128 
129     protected UCCWrapper ucc = null;
130     
131     /**
132      * TRUE == use ucc executed through uccwrapper
133      * FALSE == use externaly executed instance
134      */
135     //protected boolean useInternalUcc = !Pogamut.getPlatform().getBooleanProperty(PogamutUT2004Property.POGAMUT_UNREAL_TEST_EXT_SERVER.getKey());
136     protected boolean useInternalUcc = false;
137     //protected boolean useInternalUcc = true;
138     
139     /**
140      * If not null will be used.
141      */
142     protected String unrealHome = null;
143     //protected String unrealHome = "D:/Games/UT2004-Devel";
144 
145     public UT2004Test() {
146     	this.testId = new AgentId("Test");
147     	this.log = new LogCategory("UT2004Test");
148     	this.log.addHandler(new LogPublisher.ConsolePublisher(testId));
149     }
150         
151     /**
152      * Starts UCC server.
153      *
154      * @throws cz.cuni.amis.pogamut.ut2004.server.exceptions.UCCStartException
155      */
156     public void startUCC(UCCWrapperConf uccConf) throws UCCStartException {
157     	if (unrealHome != null) {
158     		uccConf.setUnrealHome(unrealHome);
159     	}
160         if (useInternalUcc) {            
161             ucc = new UCCWrapper(uccConf);
162         }
163     }
164     
165     public void endUcc() {
166     	if (useInternalUcc) {
167             ucc.stop();
168         }
169     }
170 
171     /**
172      * Initialize UCC server.
173      * @throws UCCStartException
174      */
175     @Before
176     public void beforeTest() throws UCCStartException {
177     	startUCC(new UCCWrapperConf());    	
178     }
179 
180     /**
181      * Kills the UCC server and closes PogamutPlatform.
182      */
183     @After
184     public void afterTest() {
185     	endUcc();
186         Pogamut.getPlatform().close();
187     }    
188 
189     /**
190      * Waits till 'agent' changes its state to {@link IAgentStateUp}.
191      * <p><p>
192      * 60s timeout.
193      * 
194      * @param agent
195      * @return
196      */
197     protected boolean awaitAgentUp(AbstractAgent agent) {
198     	System.out.println("Awaiting server UP(timeout 60s)...");
199     	IAgentState state = new WaitForAgentStateChange(agent.getState(), IAgentStateUp.class).await(60000, TimeUnit.MILLISECONDS);
200     	return state != null && state instanceof IAgentStateUp;
201     }
202     
203     /**
204      * Waits till 'agent' changes its state to {@link IAgentStateDown}.
205      * <p><p>
206      * 60s timeout.
207      * 
208      * @param agent
209      * @return
210      */
211     protected boolean awaitAgentDown(AbstractAgent agent) {
212     	System.out.println("Awaiting server DOWN (timeout 60s)...");
213     	IAgentState state = new WaitForAgentStateChange(agent.getState(), IAgentStateDown.class).await(120000, TimeUnit.MILLISECONDS);
214     	return state != null && state instanceof IAgentStateDown;
215     }
216     
217     /**
218      * Starts new bot in the environment.
219      * @param <T>
220      * @param controller controller that will be used for newly created bot
221      * @return running bot with the given controller
222      */
223     protected <T extends IUT2004BotController> UT2004Bot startUTBot(Class<T> controller) {
224     	return startUTBot(controller, null);
225     }
226     
227     /**
228      * Starts new bot in the environment with specified 'params'.
229      * @param <T>
230      * @param controller controller that will be used for newly created bot
231      * @return running bot with the given controller
232      */
233     protected <T extends IUT2004BotController> UT2004Bot startUTBot(Class<T> controller, UT2004AgentParameters params) {
234 
235         UT2004BotFactory factory = new UT2004BotFactory(new UT2004BotModule(controller));
236 
237         String host = Pogamut.getPlatform().getProperty(PogamutUT2004Property.POGAMUT_UT2004_BOT_HOST.getKey());
238         int port = Pogamut.getPlatform().getIntProperty(PogamutUT2004Property.POGAMUT_UT2004_BOT_PORT.getKey());
239         if (useInternalUcc) {
240             host = ucc.getHost();
241             port = ucc.getBotPort();
242         }
243         UT2004BotRunner botRunner = new UT2004BotRunner(factory, "TestBot", host, port);
244         UT2004Bot bot = 
245         	params == null ? (UT2004Bot) botRunner.startAgent()
246                            : (UT2004Bot) botRunner.startAgents(params).get(0);
247         return bot;
248     }
249     
250     protected <T extends IUT2004BotController> List<UT2004Bot> startAllUTBots(Class<T> controller, UT2004BotParameters... params) {
251 
252         UT2004BotFactory factory = new UT2004BotFactory(new UT2004BotModule(controller));
253 
254         String host = Pogamut.getPlatform().getProperty(PogamutUT2004Property.POGAMUT_UT2004_BOT_HOST.getKey());
255         int port = Pogamut.getPlatform().getIntProperty(PogamutUT2004Property.POGAMUT_UT2004_BOT_PORT.getKey());
256         if (host == null) host = "localhost"; 
257         if (port == 0) port = 3000;
258         
259         if (useInternalUcc) {
260             host = ucc.getHost();
261             port = ucc.getBotPort();
262         }
263         UT2004BotRunner botRunner = new UT2004BotRunner(factory, "TestBot", host, port);
264         botRunner.setPausing(true);
265         return botRunner.startAgents(params);
266     }
267     
268     /**
269      * Starts new UTServer.
270      * @param <T>
271      * @return running server connected to UCC instance.
272      */
273     protected IUT2004Server startUTServer(IAgentFactory<IUT2004Server, IRemoteAgentParameters> factory) {
274         String host = Pogamut.getPlatform().getProperty(PogamutUT2004Property.POGAMUT_UT2004_SERVER_HOST.getKey());
275         int port = Pogamut.getPlatform().getIntProperty(PogamutUT2004Property.POGAMUT_UT2004_SERVER_PORT.getKey());
276         if (useInternalUcc) {
277             host = ucc.getHost();
278             port = ucc.getControlPort();
279         }
280 
281         UT2004ServerRunner runner = new UT2004ServerRunner(factory,
282                 "TEST server",
283                 host, port) {
284             @Override
285             protected void preStartHook(IAgent agent) throws PogamutException {
286             	super.preStartHook(agent);
287             	agent.getLogger().setLevel(Level.ALL);                
288             }
289         };
290         return runner.startAgent();
291     }
292     
293     /**
294      * Starts new UTServer.
295      * @param <T>
296      * @return running server connected to UCC instance.
297      */
298     protected IUT2004Observer startUTObserver(IAgentFactory<IUT2004Observer, IRemoteAgentParameters> factory) {
299         String host = Pogamut.getPlatform().getProperty(PogamutUT2004Property.POGAMUT_UT2004_SERVER_HOST.getKey());
300         int port = Pogamut.getPlatform().getIntProperty(PogamutUT2004Property.POGAMUT_UT2004_SERVER_PORT.getKey());
301         if (useInternalUcc) {
302             host = ucc.getHost();
303             port = ucc.getObserverPort();
304         }
305 
306         UT2004ObserverRunner runner = new UT2004ObserverRunner(factory,
307                 "TEST observer",
308                 host, port) {
309             @Override
310             protected void preStartHook(IAgent agent) throws PogamutException {
311             	super.preStartHook(agent);
312             	agent.getLogger().setLevel(Level.ALL);
313             }
314         };
315         return runner.startAgent();
316     }
317 
318 }