PlayerLogicTest.java :  » Game » openfield » battlefield » Java Open Source

Java Open Source » Game » openfield 
openfield » battlefield » PlayerLogicTest.java
/*
 *--------------------------------------------------------------------------
 *   Battlefield - A Realtime Network Multiplayer Game
 *   =======================================================
 *   Developed by Group D02 - 2009/2010 Semester 4 - CS2103
 *     Harry Nguyen Duy Hoang <nnduyhoang@yahoo.co.uk>
 *     Kent Chng Siang Rong <fivefootway@gmail.com>
 *     Lim Yong Peng <limpeng1986@gmail.com>
 *     Loh Xiankun <u0807185@nus.edu.sg>
 *   Instructed by
 *     Dr. Damith C.Rajapakse <damith@gmail.com>
 *   =======================================================
 *   $Id: PlayerLogicTest.java 660 2010-07-30 08:33:12Z YongPeng $
 *   $LastChangedDate: 2010-07-30 01:33:12 -0700 (Fri, 30 Jul 2010) $
 *   $LastChangedBy: YongPeng $
 *--------------------------------------------------------------------------
 */

package battlefield;

import battlefield.entity.Unit;
import java.rmi.RemoteException;
import battlefield.entity.Game;
import battlefield.entity.Player;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Yong Peng
 */
public class PlayerLogicTest {
    BattlefieldServerInterface logic;
    Player player;
    Player opponent;
    Game game;

    public PlayerLogicTest() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Before
    public void setUp() {
        try {
            logic = new BattlefieldServerImp();
            player = logic.registerPlayer();
        } catch (RemoteException ex) {
            ex.printStackTrace();
        }
    }

    @After
    public void tearDown() {
    }

    /**
     * Test of registerPlayer method, of class BattlefieldServerImp.
     */
    @Test
    public void testRegisterPlayer() throws Exception {
        System.out.println("registerPlayer");
        Player result = logic.registerPlayer();
        assertNotNull("Player created",result);
        // TODO review the generated test code and remove the default call to fail.
        //fail("The test case is a prototype.");
    }

    /**
     * Test of getPlayer method, of class BattlefieldServerImp.
     */
    @Test
    public void testGetPlayer() throws Exception {
        System.out.println("getPlayer");
        Player result = logic.getPlayer(player.getId());
        assertNotNull("Player does not exist on server",result);
        assertEquals("Player object is equal", player, result);

    }

    /**
     * Test of getPlayer method, of class BattlefieldServerImp.
     */
    @Test
    public void testGetPlayerPlayerIdNotValid() throws Exception {
        System.out.println("getPlayer");
        int playerId = 0;
        Player result = logic.getPlayer(playerId);
        assertNull("Player does not exist",result);
    }

    /**
     * Test of topupGold method, of class BattlefieldServerImp.
     */
    @Test
    public void testTopupGoldReload() throws Exception {
        try{
            System.out.println("topupGold");
            int reloadQty = 1;
            logic.topupGold(player.getId(), reloadQty);
        }catch(Exception e){
            fail("Not suppose to throw exception");
            //Successful
        }
    }

    /**
     * Test of topupGold method, of class BattlefieldServerImp.
     */
    @Test
    public void testTopupGoldReloadQtyNegative() throws Exception {
        try{
            System.out.println("topupGold");
            int reloadQty = -1;
            logic.topupGold(player.getId(), reloadQty);
            fail("Suppose to throw exception");
        }catch(RemoteException e){
            //Successful
        }
    }

    /**
     * Test of topupGold method, of class BattlefieldServerImp.
     */
    @Test
    public void testTopupGoldPlayerDoesNotExist() throws Exception {
        try{
            System.out.println("topupGold");
            int reloadQty = -1;
            Player player = null;
            logic.topupGold(player.getId(), reloadQty);
            fail("Suppose to throw Exception");
        }catch(NullPointerException e){
            //Successful
        }
    }

    /**
     * Test of changePlayerName method, of class BattlefieldServerImp.
     */
    @Test
    public void testChangePlayerName() throws Exception {
        System.out.println("changePlayerName");
        String name = "player1";
        logic.changePlayerName(player.getId(), name);
        // TODO review the generated test code and remove the default call to fail.
        assertEquals("Player name changed", name, player.getName());
    }

    /**
     * Test of changePlayerName method, of class BattlefieldServerImp.
     */
    @Test
    public void testChangePlayerNamePlayerNotFound() throws Exception {
        try{
            System.out.println("changePlayerName");
            String name = "player1";
            int playerId = 2;
            logic.changePlayerName(playerId, name);
            // TODO review the generated test code and remove the default call to fail.
            fail("Player object is found.");
        }catch(RemoteException e){
            //Success            
        }
    }

    /**
     * Test of getOpponent method, of class BattlefieldServerImp.
     */
    @Test
    public void testGetOpponent() throws Exception {
        System.out.println("getOpponent");
        Player opponentPlayer = logic.registerPlayer();
        game = logic.createGame(player.getId());
        logic.joinGame(opponentPlayer.getId(), game.getId());
        Player resultPlayer = logic.getOpponent(player.getId());
        assertEquals("Able to get opponent player",opponentPlayer, resultPlayer);
    }

    /**
     * Test of getOpponent method, of class BattlefieldServerImp.
     */
    @Test
    public void testGetOpponentNotFound() throws Exception {
        System.out.println("getOpponent");
        Player opponentPlayer = logic.registerPlayer();
        game = logic.createGame(player.getId());
        //logic.joinGame(opponentPlayer.getId(), game.getId());
        Player resultPlayer = logic.getOpponent(player.getId());
        assertNotSame("Unable to get opponent player",opponentPlayer, resultPlayer);
    }

    /**
     * Test of isHost method, of class BattlefieldServerImp.
     */
    @Test
    public void testIsHost() throws Exception {
        game = logic.createGame(player.getId());
        boolean isHost = logic.isHost(player.getId());
        assertTrue("Player is host",isHost);
    }

    /**
     * Test of isHost method, of class BattlefieldServerImp.
     */
    @Test
    public void testIsHostNotHost() throws Exception {
        game = logic.createGame(player.getId());
        Player opponentPlayer = logic.registerPlayer();
        assertFalse(logic.isHost(opponentPlayer.getId()));
    }
    /**
     * Test of isAlive method, of class BattlefieldServerImp.
     */
    
    @Test
    public void testIsAlive() throws Exception {
        System.out.println("isAlive");
        Game game = logic.createGame(player.getId());
        logic.createMap(player.getId(), "/battlefield/images/map1.jpg");
        logic.updateUnitCounts(player.getId());
        logic.buyAndPlaceUnitOnMap(player.getId(), logic.getUnitModels().get(0).getId(), 0, 0);
        boolean isAlive = logic.isAlive(player.getId());
        assertTrue("Player is alive",isAlive);
    }

    /**
     * Test of updateUnitCounts method, of class BattlefieldServerImp.
     */
    @Test
    public void testUpdateUnitCounts() throws Exception {
        try{
            Game game = logic.createGame(player.getId());
            logic.createMap(player.getId(), "/battlefield/images/map1.jpg");
            logic.updateUnitCounts(player.getId());
        }catch(Exception e){
            fail("Not suppose to fail");
        }
        
    }

    /**
     * Test of updateUnitCounts method, of class BattlefieldServerImp.
     */
    @Test
    public void testUpdateUnitCountsMapNotFound() throws Exception {
        try{
            Game game = logic.createGame(player.getId());
            logic.updateUnitCounts(player.getId());
            fail("Map is suppose to be  null");
        }catch(NullPointerException e){
            //Success test case
        }
        
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.