View Javadoc

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   * Base class for tests that need a running UCC instance. If you inherit from
43   * this class then before the first @Test method will be called the UCC server
44   * will be executed. The ports where it is listening can be obtained by
45   * <code>{@link UCCWrapper#getBotPort()}</code>.
46   * 
47   * Don't forget to specify pogamut.unreal.home and pogamut.unreal.serverexec
48   * variables.
49   * 
50   * @author ik
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       * TRUE == use ucc executed through uccwrapper
66       * FALSE == use externaly executed instance
67       */
68      //protected boolean useInternalUcc = !Pogamut.getPlatform().getBooleanProperty(PogamutUT2004Property.POGAMUT_UNREAL_TEST_EXT_SERVER.getKey());
69      protected boolean useInternalUcc = false;
70      //protected boolean useInternalUcc = true;
71  
72      /**
73       * If this is NOT null, it will be used.
74       */
75      //protected String emohawkHome = null;
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       * Starts UCC server.
87       *
88       * @throws cz.cuni.amis.pogamut.ut2004.server.exceptions.UCCStartException
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      * Initialize UCC server.
107      * @throws UCCStartException
108      */
109     @Before
110     public void beforeTest() throws UCCStartException {
111     	startUCC(new UCCWrapperConfEmohawk());    	
112     }
113 
114     /**
115      * Kills the UCC server and closes PogamutPlatform.
116      */
117     @After
118     public void afterTest() {
119     	endUcc();
120         Pogamut.getPlatform().close();
121     }    
122 
123     /**
124      * Waits till 'agent' changes its state to {@link IAgentStateUp}.
125      * <p><p>
126      * 60s timeout.
127      * 
128      * @param agent
129      * @return
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      * Waits till 'agent' changes its state to {@link IAgentStateDown}.
139      * <p><p>
140      * 60s timeout.
141      * 
142      * @param agent
143      * @return
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      * Starts new bot in the environment.
153      * @param <T>
154      * @param controller controller that will be used for newly created bot
155      * @return running bot with the given controller
156      */
157     protected <T extends IUT2004BotController> UT2004Bot startUTBot(Class<T> controller) {
158     	return startUTBot(controller, null);
159     }
160     
161     /**
162      * Starts new bot in the environment with specified 'params'.
163      * @param <T>
164      * @param controller controller that will be used for newly created bot
165      * @return running bot with the given controller
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      * Starts new UTServer.
204      * @param <T>
205      * @return running server connected to UCC instance.
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      * Starts new UTServer.
229      * @param <T>
230      * @return running server connected to UCC instance.
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 }