net.craigstars.game.services.GameControllerTest.java Source code

Java tutorial

Introduction

Here is the source code for net.craigstars.game.services.GameControllerTest.java

Source

/**
 * 
 * CraigStars!
 * 
 * Copyright (c) 2010 Craig Post Licensed under the MIT license.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 * 
 */
package net.craigstars.game.services;

import net.craigstars.dao.DaoTestCase;
import net.craigstars.model.Game;
import net.craigstars.model.Player;
import net.craigstars.model.Race;
import net.craigstars.model.User;
import net.craigstars.model.enums.Density;
import net.craigstars.model.enums.Size;

import org.apache.commons.lang.time.StopWatch;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.slf4j.Logger;

public class GameControllerTest extends DaoTestCase {

    @Inject
    GameController gameController;

    @Inject
    Logger log;

    public void testCreateGame() {
        String token = getNextToken();
        User user = getUser();
        Game game = gameController.createGame(user, token, Size.Medium, Density.Normal, 1);
        assertNotNull(game);

        assertEquals(1, game.getNumPlayers());
    }

    public void testGenerateUniverse() {
        String token = getNextToken();
        User user = getUser();
        Game game = gameController.createGame(user, token, Size.Tiny, Density.Sparse, 1);

        Race race = getRace();
        gameController.joinGame(race.getUser(), race, game.getId());
        game = gameController.getGame(game.getId());

        assertNotNull(game);

        assertEquals(2400, game.getYear());
        assertEquals(24, game.getPlanets().size());
        assertEquals(1, game.getPlayers().size());

        Player player = gameController.getPlayerForGame(race.getUser(), game.getId());
        assertNotNull(player);

    }

    public void testGenerateTurn() {
        String token = getNextToken();
        User user = getUser();
        Game game = gameController.createGame(user, token, Size.Tiny, Density.Sparse, 2);

        Race race = getRace();
        gameController.joinGame(race.getUser(), race, game.getId());

        // join an ai player
        User aiUser = getUser(token + "-ai");
        aiUser.setAi(true);
        dao.getUserDao().save(aiUser);
        Race aiRace = getRace(user, token + "-ai");
        gameController.joinGame(aiUser, aiRace, game.getId());

        game = gameController.getGame(game.getId());

        log.debug("\n\n\n\nStarting generateTurn\n\n\n\n");
        StopWatch watch = new StopWatch();
        watch.start();

        int numYears = 50;
        // generate a bunch of turns
        for (int i = 0; i < numYears; i++) {
            gameController.submitTurn(user, game.getId());
        }
        watch.stop();
        log.debug("\n\n\nTime: {}ms", watch.getTime());

        game = gameController.getGame(game.getId());

        assertEquals(2400 + numYears, game.getYear());

    }

    public void testDelete() {
        String token = getNextToken();
        User user = getUser(token);
        Game game = dao.getGameDao().create(user, token, Size.Tiny, Density.Sparse, 1);
        Race race = getRace(user, token);
        gameController.joinGame(user, race, game.getId());
        resetSession();
        gameController.submitTurn(user, game.getId());

        resetSession();
        gameController.deleteGame(game.getId());
        resetSession();

        assertEquals(null, dao.getGameDao().get(game.getId()));
    }

}