Empire mining settings
* Modified mining settings stored procedures to use text identifiers instead of numeric identifiers * Added DAO for mining settings and controller for resource operations * Added UpdateEmpireMiningSettingsCommand and associated command delegate. The command always returns NullResponse. * Overview page templates split into multiple files for clarity, added priority update form to the empire economy view and associated web server handler
This commit is contained in:
parent
92dd01ffce
commit
d38576a5cf
24 changed files with 1024 additions and 160 deletions
legacyworlds-server-tests/src/test/java/com/deepclone/lw/beans/game/resources
|
@ -0,0 +1,121 @@
|
|||
package com.deepclone.lw.beans.game.resources;
|
||||
|
||||
|
||||
import com.deepclone.lw.interfaces.game.resources.MiningSettingsDAO;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Mock mining settings DAO which can be used to simulate failures and trace which methods were
|
||||
* called.
|
||||
*
|
||||
* @author <a href="mailto:tseeker@legacyworlds.com">E. Benoît</a>
|
||||
*
|
||||
*/
|
||||
public class MockMiningSettingsDAO
|
||||
implements MiningSettingsDAO
|
||||
{
|
||||
/** The empire identifier with which {@link #startUpdate(int)} was called */
|
||||
private Integer updateEmpire = null;
|
||||
|
||||
/** The amount of calls to {@link #setNewPriority(String, int)} */
|
||||
private int callsToSet = 0;
|
||||
|
||||
/** Whether {@link #applyUpdate()} was called */
|
||||
private boolean applyCalled = false;
|
||||
|
||||
/** Whether {@link #startUpdate(int)} will succeed or fail */
|
||||
private boolean startUpdateSucceeds = true;
|
||||
|
||||
/** Whether applyUpdate will succeed */
|
||||
private boolean applyUpdateSucceeds = true;
|
||||
|
||||
/**
|
||||
* Amount of calls to {@link #setNewPriority(String, int)} that will succeed, or
|
||||
* <code>null</code> if they will all succeed.
|
||||
*/
|
||||
private Integer maxSetCalls = null;
|
||||
|
||||
|
||||
/** @return the empire identifier to update */
|
||||
public Integer getUpdateEmpire( )
|
||||
{
|
||||
return this.updateEmpire;
|
||||
}
|
||||
|
||||
|
||||
/** @return the amount of calls to {@link #setNewPriority(String, int)} */
|
||||
public int getCallsToSet( )
|
||||
{
|
||||
return this.callsToSet;
|
||||
}
|
||||
|
||||
|
||||
/** @return <code>true</code> if {@link #applyUpdate()} was called */
|
||||
public boolean wasApplyCalled( )
|
||||
{
|
||||
return this.applyCalled;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determine whether calls to {@link #startUpdate(int)} will succeed
|
||||
*
|
||||
* @param startUpdateSucceeds
|
||||
* <code>true</code> if the call is to succeed, <code>false</code> if it is to fail
|
||||
*/
|
||||
public void setStartUpdateSucceeds( boolean startUpdateSucceeds )
|
||||
{
|
||||
this.startUpdateSucceeds = startUpdateSucceeds;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determine whether calls to {@link #applyUpdate()} will succeed
|
||||
*
|
||||
* @param applyUpdateSucceeds
|
||||
* <code>true</code> if the call is to succeed, <code>false</code> if it is to fail
|
||||
*/
|
||||
public void setApplyUpdateSucceeds( boolean applyUpdateSucceeds )
|
||||
{
|
||||
this.applyUpdateSucceeds = applyUpdateSucceeds;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the amount of calls to {@link #setNewPriority(String, int)} that will succeed
|
||||
*
|
||||
* @param maxSetCalls
|
||||
* the amount of calls that will succeed, or <code>null</code> if the method must
|
||||
* always succeed
|
||||
*/
|
||||
public void setMaxSetCalls( Integer maxSetCalls )
|
||||
{
|
||||
this.maxSetCalls = maxSetCalls;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean startUpdate( int empireId )
|
||||
{
|
||||
this.updateEmpire = empireId;
|
||||
return this.startUpdateSucceeds;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean setNewPriority( String resource , int priority )
|
||||
{
|
||||
this.callsToSet++;
|
||||
return ( this.maxSetCalls == null || this.maxSetCalls > this.callsToSet - 1 );
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean applyUpdate( )
|
||||
{
|
||||
this.applyCalled = true;
|
||||
return this.applyUpdateSucceeds;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,121 @@
|
|||
package com.deepclone.lw.beans.game.resources;
|
||||
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.deepclone.lw.interfaces.game.resources.MiningSettingsDAO;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Tests for {@link ResourcesControllerBean}
|
||||
*
|
||||
* @author <a href="mailto:tseeker@legacyworlds.com">E. Benoît</a>
|
||||
*
|
||||
*/
|
||||
public class TestResourcesControllerBean
|
||||
{
|
||||
|
||||
/** Empire identifier used in the tests */
|
||||
private static final Integer EMPIRE_ID = 42;
|
||||
|
||||
/** Mining settings used in the tests */
|
||||
private static final Map< String , Integer > MINING_SETTINGS;
|
||||
|
||||
/**
|
||||
* Initialise the mining settings map
|
||||
*/
|
||||
static {
|
||||
HashMap< String , Integer > tempMap = new HashMap< String , Integer >( );
|
||||
tempMap.put( "test 1" , 1 );
|
||||
tempMap.put( "test 2" , 2 );
|
||||
tempMap.put( "test 3" , 3 );
|
||||
tempMap.put( "test 4" , 4 );
|
||||
MINING_SETTINGS = Collections.unmodifiableMap( tempMap );
|
||||
}
|
||||
|
||||
/** The mock database access object */
|
||||
private MockMiningSettingsDAO miningSettingsDAO;
|
||||
|
||||
/** The instance to test on */
|
||||
private ResourcesControllerBean ctrl;
|
||||
|
||||
|
||||
/**
|
||||
* Create the resource controller that will be used in the tests and its various (usually fake)
|
||||
* dependencies
|
||||
*/
|
||||
@Before
|
||||
public void setUp( )
|
||||
{
|
||||
this.miningSettingsDAO = new MockMiningSettingsDAO( );
|
||||
this.ctrl = new ResourcesControllerBean( );
|
||||
this.ctrl.setMiningSettingsDAO( this.miningSettingsDAO );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* When calling {@link MiningSettingsDAO#startUpdate(int)} fails, the empire settings update is
|
||||
* interrupted.
|
||||
*/
|
||||
@Test
|
||||
public void testEmpireStartUpdateFails( )
|
||||
{
|
||||
this.miningSettingsDAO.setStartUpdateSucceeds( false );
|
||||
this.ctrl.updateEmpireSettings( EMPIRE_ID , MINING_SETTINGS );
|
||||
assertEquals( EMPIRE_ID , this.miningSettingsDAO.getUpdateEmpire( ) );
|
||||
assertEquals( 0 , this.miningSettingsDAO.getCallsToSet( ) );
|
||||
assertFalse( this.miningSettingsDAO.wasApplyCalled( ) );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* When calling {@link MiningSettingsDAO#startUpdate(int)} succeeds but one of the calls to
|
||||
* {@link MiningSettingsDAO#setNewPriority(String, int)} fails, the update is interrupted.
|
||||
*/
|
||||
@Test
|
||||
public void testSetMiningPriorityFails( )
|
||||
{
|
||||
this.miningSettingsDAO.setMaxSetCalls( 2 );
|
||||
this.ctrl.updateEmpireSettings( EMPIRE_ID , MINING_SETTINGS );
|
||||
assertEquals( EMPIRE_ID , this.miningSettingsDAO.getUpdateEmpire( ) );
|
||||
assertEquals( 3 , this.miningSettingsDAO.getCallsToSet( ) );
|
||||
assertFalse( this.miningSettingsDAO.wasApplyCalled( ) );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* If both {@link MiningSettingsDAO#startUpdate(int)} and
|
||||
* {@link MiningSettingsDAO#setNewPriority(String, int)} succeed,
|
||||
* {@link MiningSettingsDAO#applyUpdate()} is called.
|
||||
*/
|
||||
@Test
|
||||
public void testSettingsSuccess( )
|
||||
{
|
||||
this.ctrl.updateEmpireSettings( EMPIRE_ID , MINING_SETTINGS );
|
||||
assertEquals( EMPIRE_ID , this.miningSettingsDAO.getUpdateEmpire( ) );
|
||||
assertEquals( 4 , this.miningSettingsDAO.getCallsToSet( ) );
|
||||
assertTrue( this.miningSettingsDAO.wasApplyCalled( ) );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A failure of {@link MiningSettingsDAO#applyUpdate()} has no influence on the call.
|
||||
*/
|
||||
@Test
|
||||
public void testSettingsApplyFail( )
|
||||
{
|
||||
this.miningSettingsDAO.setApplyUpdateSucceeds( false );
|
||||
this.ctrl.updateEmpireSettings( EMPIRE_ID , MINING_SETTINGS );
|
||||
assertEquals( EMPIRE_ID , this.miningSettingsDAO.getUpdateEmpire( ) );
|
||||
assertEquals( 4 , this.miningSettingsDAO.getCallsToSet( ) );
|
||||
assertTrue( this.miningSettingsDAO.wasApplyCalled( ) );
|
||||
}
|
||||
}
|
Reference in a new issue