138 lines
4.1 KiB
Java
138 lines
4.1 KiB
Java
package net.socialgamer.cah.data;
|
|
|
|
import static org.easymock.EasyMock.anyObject;
|
|
import static org.easymock.EasyMock.createMock;
|
|
import static org.easymock.EasyMock.eq;
|
|
import static org.easymock.EasyMock.expectLastCall;
|
|
import static org.easymock.EasyMock.replay;
|
|
import static org.easymock.EasyMock.verify;
|
|
import static org.junit.Assert.assertEquals;
|
|
import static org.junit.Assert.assertNotNull;
|
|
import static org.junit.Assert.assertNull;
|
|
|
|
import java.util.Collection;
|
|
import java.util.HashMap;
|
|
|
|
import net.socialgamer.cah.data.GameManager.GameId;
|
|
import net.socialgamer.cah.data.GameManager.MaxGames;
|
|
import net.socialgamer.cah.data.QueuedMessage.MessageType;
|
|
|
|
import org.junit.After;
|
|
import org.junit.Before;
|
|
import org.junit.Test;
|
|
|
|
import com.google.inject.AbstractModule;
|
|
import com.google.inject.Guice;
|
|
import com.google.inject.Injector;
|
|
import com.google.inject.Provides;
|
|
|
|
|
|
public class GameManagerTest {
|
|
|
|
private Injector injector;
|
|
private GameManager gameManager;
|
|
private ConnectedUsers cuMock;
|
|
private User userMock;
|
|
private int gameId;
|
|
|
|
@Before
|
|
public void setUp() throws Exception {
|
|
cuMock = createMock(ConnectedUsers.class);
|
|
userMock = createMock(User.class);
|
|
|
|
injector = Guice.createInjector(new AbstractModule() {
|
|
@Override
|
|
protected void configure() {
|
|
bind(ConnectedUsers.class).toInstance(cuMock);
|
|
}
|
|
|
|
@SuppressWarnings("unused")
|
|
@Provides
|
|
@MaxGames
|
|
Integer provideMaxGames() {
|
|
return 3;
|
|
}
|
|
|
|
@SuppressWarnings("unused")
|
|
@Provides
|
|
@GameId
|
|
Integer provideGameId() {
|
|
return gameId;
|
|
}
|
|
});
|
|
|
|
gameManager = injector.getInstance(GameManager.class);
|
|
}
|
|
|
|
@After
|
|
public void tearDown() {
|
|
verify(cuMock);
|
|
verify(userMock);
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
@Test
|
|
public void testGetAndDestroyGame() {
|
|
cuMock.broadcastToAll(eq(MessageType.GAME_EVENT), anyObject(HashMap.class));
|
|
expectLastCall().times(3);
|
|
replay(cuMock);
|
|
replay(userMock);
|
|
|
|
// fill it up with 3 games
|
|
assertEquals(0, gameManager.get().intValue());
|
|
gameManager.getGames().put(0, new Game(0, cuMock, gameManager));
|
|
assertEquals(1, gameManager.get().intValue());
|
|
gameManager.getGames().put(1, new Game(1, cuMock, gameManager));
|
|
assertEquals(2, gameManager.get().intValue());
|
|
gameManager.getGames().put(2, new Game(2, cuMock, gameManager));
|
|
// make sure it says it can't make any more
|
|
assertEquals(-1, gameManager.get().intValue());
|
|
|
|
// remove game 1 using its own method -- this should be how it always happens in production
|
|
gameManager.destroyGame(1);
|
|
// make sure it re-uses that id
|
|
assertEquals(1, gameManager.get().intValue());
|
|
gameManager.getGames().put(1, new Game(1, cuMock, gameManager));
|
|
assertEquals(-1, gameManager.get().intValue());
|
|
|
|
// remove game 1 out from under it, to make sure it'll fix itself
|
|
gameManager.getGames().remove(1);
|
|
assertEquals(1, gameManager.get().intValue());
|
|
gameManager.getGames().put(1, new Game(1, cuMock, gameManager));
|
|
assertEquals(-1, gameManager.get().intValue());
|
|
|
|
gameManager.destroyGame(2);
|
|
gameManager.destroyGame(0);
|
|
assertEquals(2, gameManager.get().intValue());
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
@Test
|
|
public void testCreateGame() {
|
|
cuMock.broadcastToAll(eq(MessageType.GAME_EVENT), anyObject(HashMap.class));
|
|
expectLastCall().times(3);
|
|
cuMock.broadcastToList(anyObject(Collection.class), eq(MessageType.GAME_PLAYER_EVENT),
|
|
anyObject(HashMap.class));
|
|
expectLastCall().times(3);
|
|
replay(cuMock);
|
|
|
|
userMock.joinGame(anyObject(Game.class));
|
|
expectLastCall().times(3);
|
|
userMock.getNickname();
|
|
expectLastCall().andReturn("test").times(3);
|
|
replay(userMock);
|
|
|
|
Game game = gameManager.createGameWithPlayer(userMock);
|
|
assertNotNull(game);
|
|
gameId = 1;
|
|
game = gameManager.createGameWithPlayer(userMock);
|
|
assertNotNull(game);
|
|
gameId = 2;
|
|
game = gameManager.createGameWithPlayer(userMock);
|
|
assertNotNull(game);
|
|
gameId = -1;
|
|
game = gameManager.createGameWithPlayer(userMock);
|
|
assertNull(game);
|
|
}
|
|
}
|