Empire resources in XML dumps

* Added dump view for empire resources

* Added empire resource information storage class and associated row
mapper

* Integrated empire resource information into the summary generator
This commit is contained in:
Emmanuel BENOîT 2012-01-19 09:35:12 +01:00
parent ce6d86d344
commit 9b346a80c2
9 changed files with 542 additions and 15 deletions
legacyworlds-server-tests/src/test/java/com/deepclone/lw/beans/bt/es

View file

@ -0,0 +1,74 @@
package com.deepclone.lw.beans.bt.es;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import com.deepclone.lw.beans.bt.es.data.EmpireResourceInformation;
import com.deepclone.lw.testing.MockResultSet;
/**
* Tests for the {@link EmpireResourceInformationMapper} class.
*
* @author <a href="mailto:tseeker@legacyworlds.com">E. Benoît</a>
*/
public class TestEmpireResourceInformationMapper
{
/** Resource identifier used in tests */
private static final String TEST_ID = "Test";
/** "Owed" value used in tests */
private static final Double TEST_OWED = 1.0;
/** "Possessed" value used in tests */
private static final Double TEST_POSSESSED = 2.0;
/** The fake result set used in the tests */
private ResultSet resultSet;
/** The mapper used in the tests */
private EmpireResourceInformationMapper mapper;
/** Create the mapper and the contents of the fake result set */
@Before
@SuppressWarnings( "unchecked" )
public void setUp( )
{
this.mapper = new EmpireResourceInformationMapper( );
HashMap< String , Object > row = new HashMap< String , Object >( );
row.put( "resource_name" , TEST_ID );
row.put( "empres_possessed" , TEST_POSSESSED );
row.put( "empres_owed" , TEST_OWED );
this.resultSet = MockResultSet.create( new HashMap[] {
row
} );
}
/** Mapping a row */
@Test
public void testMapRow( )
throws SQLException
{
EmpireResourceInformation empRes;
this.resultSet.absolute( 1 );
empRes = this.mapper.mapRow( this.resultSet , 1 );
assertEquals( TEST_ID , empRes.getResource( ) );
assertEquals( TEST_POSSESSED , empRes.getPossessed( ) );
assertEquals( TEST_OWED , empRes.getOwed( ) );
}
}

View file

@ -0,0 +1,140 @@
package com.deepclone.lw.beans.bt.es.data;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import com.thoughtworks.xstream.XStream;
/**
* Tests for the {@link EmpireResourceInformation} class.
*
* @author <a href="mailto:tseeker@legacyworlds.com">E. Benoît</a>
*/
public class TestEmpireResourceInformation
{
/** Resource identifier used in tests */
private static final String TEST_ID = "Test";
/** "Owed" value used in tests */
private static final Double TEST_OWED = 1.0;
/** "Possessed" value used in tests */
private static final Double TEST_POSSESSED = 2.0;
/** Empire resource information instance used in tests */
private EmpireResourceInformation empRes;
/** Create the instance to be used in actual tests */
@Before
public void setUp( )
{
this.empRes = new EmpireResourceInformation( );
}
/** Default values are <code>null</code> */
@Test
public void testDefaultValues( )
{
assertNull( this.empRes.getResource( ) );
assertNull( this.empRes.getOwed( ) );
assertNull( this.empRes.getPossessed( ) );
}
/** Setting and reading the resource type */
@Test
public void testResource( )
{
this.empRes.setResource( TEST_ID );
assertEquals( TEST_ID , this.empRes.getResource( ) );
}
/** Setting the resource type to <code>null</code> */
@Test
public void testNullResource( )
{
try {
this.empRes.setResource( null );
fail( "InvalidDumpContentsException expected" );
} catch ( InvalidDumpContentsException exception ) {
assertEquals( EmpireResourceInformation.class , exception.getRecordType( ) );
assertEquals( "resource" , exception.getField( ) );
}
}
/** Setting and reading the amount of resources possessed */
@Test
public void testPossessed( )
{
this.empRes.setPossessed( TEST_POSSESSED );
assertEquals( TEST_POSSESSED , this.empRes.getPossessed( ) );
}
/** Setting and reading the amount of resources owed */
@Test
public void testOwed( )
{
this.empRes.setOwed( TEST_OWED );
assertEquals( TEST_OWED , this.empRes.getOwed( ) );
}
/** Serialising the instance to XML */
@Test
public void testXMLSerialisation( )
{
this.empRes.setResource( TEST_ID );
this.empRes.setOwed( TEST_OWED );
this.empRes.setPossessed( TEST_POSSESSED );
String serialised = this.createXStreamInstance( ).toXML( this.empRes );
assertNotNull( serialised );
assertTrue( serialised.startsWith( "<resource " ) );
assertTrue( serialised.endsWith( "/>" ) );
assertTrue( serialised.contains( " id=\"" + TEST_ID + "\"" ) );
assertTrue( serialised.contains( " owed=\"" + TEST_OWED + "\"" ) );
assertTrue( serialised.contains( " possessed=\"" + TEST_POSSESSED + "\"" ) );
}
/** Deserialising an instance from XML */
@Test
public void testXMLDeserialisation( )
{
String xml = "<resource id=\"" + TEST_ID + "\" owed=\"" + TEST_OWED.toString( ) + "\" possessed=\""
+ TEST_POSSESSED.toString( ) + "\" />";
Object deserialised = this.createXStreamInstance( ).fromXML( xml );
assertNotNull( deserialised );
assertEquals( EmpireResourceInformation.class , deserialised.getClass( ) );
this.empRes = (EmpireResourceInformation) deserialised;
assertEquals( TEST_ID , this.empRes.getResource( ) );
assertEquals( TEST_POSSESSED , this.empRes.getPossessed( ) );
assertEquals( TEST_OWED , this.empRes.getOwed( ) );
}
/**
* Create and set up the {@link XStream} instance used in the serialisation tests
*
* @return the {@link XStream} instance to use
*/
private XStream createXStreamInstance( )
{
XStream xstream = new XStream( );
xstream.processAnnotations( EmpireResourceInformation.class );
return xstream;
}
}