1 package cz.cuni.amis.pogamut.emohawk.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.emohawk.utils.UCCWrapperConfEmohawk;
24 import cz.cuni.amis.pogamut.ut2004.agent.params.UT2004AgentParameters;
25 import cz.cuni.amis.pogamut.ut2004.bot.IUT2004BotController;
26 import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
27 import cz.cuni.amis.pogamut.ut2004.bot.params.UT2004BotParameters;
28 import cz.cuni.amis.pogamut.ut2004.factory.guice.remoteagent.UT2004BotFactory;
29 import cz.cuni.amis.pogamut.ut2004.factory.guice.remoteagent.UT2004BotModule;
30 import cz.cuni.amis.pogamut.ut2004.observer.IUT2004Observer;
31 import cz.cuni.amis.pogamut.ut2004.server.IUT2004Server;
32 import cz.cuni.amis.pogamut.ut2004.server.exception.UCCStartException;
33 import cz.cuni.amis.pogamut.ut2004.utils.PogamutUT2004Property;
34 import cz.cuni.amis.pogamut.ut2004.utils.UCCWrapper;
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.tests.BaseTest;
39 import cz.cuni.amis.utils.exception.PogamutException;
40
41
42
43
44
45
46
47
48
49
50
51
52 public class EmohawkTest extends BaseTest {
53
54 public static String[] MAPS = new String[] {
55 "EmohawkVille_Runtime_04"
56 };
57
58 protected IAgentId testId;
59
60 protected LogCategory log;
61
62 protected UCCWrapper ucc = null;
63
64
65
66
67
68
69 protected boolean useInternalUcc = false;
70
71
72
73
74
75
76 protected String emohawkHome = "D:/Games/UE2-Emohawk";
77
78
79 public EmohawkTest() {
80 this.testId = new AgentId("Test");
81 this.log = new LogCategory("UT2004Test");
82 this.log.addHandler(new LogPublisher.ConsolePublisher(testId));
83 }
84
85
86
87
88
89
90 public void startUCC(UCCWrapperConfEmohawk uccConf) throws UCCStartException {
91 if (emohawkHome != null) {
92 uccConf.setUnrealHome(emohawkHome);
93 }
94 if (useInternalUcc) {
95 ucc = new UCCWrapper(uccConf);
96 }
97 }
98
99 public void endUcc() {
100 if (useInternalUcc) {
101 ucc.stop();
102 }
103 }
104
105
106
107
108
109 @Before
110 public void beforeTest() throws UCCStartException {
111 startUCC(new UCCWrapperConfEmohawk());
112 }
113
114
115
116
117 @After
118 public void afterTest() {
119 endUcc();
120 Pogamut.getPlatform().close();
121 }
122
123
124
125
126
127
128
129
130
131 protected boolean awaitAgentUp(AbstractAgent agent) {
132 System.out.println("Awaiting server UP(timeout 60s)...");
133 IAgentState state = new WaitForAgentStateChange(agent.getState(), IAgentStateUp.class).await(60000, TimeUnit.MILLISECONDS);
134 return state != null && state instanceof IAgentStateUp;
135 }
136
137
138
139
140
141
142
143
144
145 protected boolean awaitAgentDown(AbstractAgent agent) {
146 System.out.println("Awaiting server DOWN (timeout 60s)...");
147 IAgentState state = new WaitForAgentStateChange(agent.getState(), IAgentStateDown.class).await(120000, TimeUnit.MILLISECONDS);
148 return state != null && state instanceof IAgentStateDown;
149 }
150
151
152
153
154
155
156
157 protected <T extends IUT2004BotController> UT2004Bot startUTBot(Class<T> controller) {
158 return startUTBot(controller, null);
159 }
160
161
162
163
164
165
166
167 protected <T extends IUT2004BotController> UT2004Bot startUTBot(Class<T> controller, UT2004AgentParameters params) {
168
169 UT2004BotFactory factory = new UT2004BotFactory(new UT2004BotModule(controller));
170
171 String host = Pogamut.getPlatform().getProperty(PogamutUT2004Property.POGAMUT_UT2004_BOT_HOST.getKey());
172 int port = Pogamut.getPlatform().getIntProperty(PogamutUT2004Property.POGAMUT_UT2004_BOT_PORT.getKey());
173 if (useInternalUcc) {
174 host = ucc.getHost();
175 port = ucc.getBotPort();
176 }
177 UT2004BotRunner botRunner = new UT2004BotRunner(factory, "TestBot", host, port);
178 UT2004Bot bot =
179 params == null ? (UT2004Bot) botRunner.startAgent()
180 : (UT2004Bot) botRunner.startAgents(params).get(0);
181 return bot;
182 }
183
184 protected <T extends IUT2004BotController> List<UT2004Bot> startAllUTBots(Class<T> controller, UT2004BotParameters... params) {
185
186 UT2004BotFactory factory = new UT2004BotFactory(new UT2004BotModule(controller));
187
188 String host = Pogamut.getPlatform().getProperty(PogamutUT2004Property.POGAMUT_UT2004_BOT_HOST.getKey());
189 int port = Pogamut.getPlatform().getIntProperty(PogamutUT2004Property.POGAMUT_UT2004_BOT_PORT.getKey());
190 if (host == null) host = "localhost";
191 if (port == 0) port = 3000;
192
193 if (useInternalUcc) {
194 host = ucc.getHost();
195 port = ucc.getBotPort();
196 }
197 UT2004BotRunner botRunner = new UT2004BotRunner(factory, "TestBot", host, port);
198 botRunner.setPausing(true);
199 return botRunner.startAgents(params);
200 }
201
202
203
204
205
206
207 protected IUT2004Server startUTServer(IAgentFactory<IUT2004Server, IRemoteAgentParameters> factory) {
208 String host = Pogamut.getPlatform().getProperty(PogamutUT2004Property.POGAMUT_UT2004_SERVER_HOST.getKey());
209 int port = Pogamut.getPlatform().getIntProperty(PogamutUT2004Property.POGAMUT_UT2004_SERVER_PORT.getKey());
210 if (useInternalUcc) {
211 host = ucc.getHost();
212 port = ucc.getControlPort();
213 }
214
215 UT2004ServerRunner runner = new UT2004ServerRunner(factory,
216 "TEST server",
217 host, port) {
218 @Override
219 protected void preStartHook(IAgent agent) throws PogamutException {
220 super.preStartHook(agent);
221 agent.getLogger().setLevel(Level.ALL);
222 }
223 };
224 return runner.startAgent();
225 }
226
227
228
229
230
231
232 protected IUT2004Observer startUTObserver(IAgentFactory<IUT2004Observer, IRemoteAgentParameters> factory) {
233 String host = Pogamut.getPlatform().getProperty(PogamutUT2004Property.POGAMUT_UT2004_SERVER_HOST.getKey());
234 int port = Pogamut.getPlatform().getIntProperty(PogamutUT2004Property.POGAMUT_UT2004_SERVER_PORT.getKey());
235 if (useInternalUcc) {
236 host = ucc.getHost();
237 port = ucc.getObserverPort();
238 }
239
240 UT2004ObserverRunner runner = new UT2004ObserverRunner(factory,
241 "TEST observer",
242 host, port) {
243 @Override
244 protected void preStartHook(IAgent agent) throws PogamutException {
245 super.preStartHook(agent);
246 agent.getLogger().setLevel(Level.ALL);
247 }
248 };
249 return runner.startAgent();
250 }
251
252 }