Planet resources and resource providers in XML dumps

* Dump version bumped up to 2

* Added SQL view that shows resource delta and provider information for
all empire-owned planets

* Added new storage classes for resource providers and resource deltas

* Added row mapper which extracts all planet resources information
(providers and deltas)

* Modified dump generator to include planet resources / resource
providers
This commit is contained in:
Emmanuel BENOîT 2012-01-18 09:28:35 +01:00
parent 95e6019c12
commit ce6d86d344
17 changed files with 1607 additions and 50 deletions

View file

@ -16,6 +16,8 @@ import com.deepclone.lw.testing.MockResultSet;
/**
* Tests for the {@link BuildingsInformationMapper} class.
*
* @author <a href="mailto:tseeker@legacyworlds.com">E. Benoît</a>
*/
public class TestBuildingsInformationMapper
{

View file

@ -0,0 +1,102 @@
package com.deepclone.lw.beans.bt.es;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import com.deepclone.lw.beans.bt.es.data.ResourceProviderInformation;
/**
* Tests for the {@link PlanetResourceRow} class
*
* @author <a href="mailto:tseeker@legacyworlds.com">E. Benoît</a>
*/
public class TestPlanetResourceRow
{
/** Identifier of the planet in the test object */
private static final int TEST_IDENTIFIER = 42;
/** Resource name used in the tests */
private static final String TEST_NAME = "Test";
/** The planet resource row instance used in tests */
private PlanetResourceRow prr;
/**
* Create a planet resource row instance with the planet identifier specified by
* {@link #TEST_IDENTIFIER}.
*/
@Before
public void setUp( )
{
this.prr = new PlanetResourceRow( TEST_IDENTIFIER );
}
/**
* Planet identifier has been initialised correctly.
*/
@Test
public void testPlanetIdentifier( )
{
assertEquals( TEST_IDENTIFIER , this.prr.getPlanetId( ) );
}
/**
* By default, the resource delta exists, the provider doesn't.
*/
@Test
public void testDefaultValues( )
{
assertNotNull( this.prr.getDelta( ) );
assertNull( this.prr.getProvider( ) );
}
/**
* Setting and reading the resource provider record.
*/
@Test
public void testSetProvider( )
{
ResourceProviderInformation rpi = new ResourceProviderInformation( );
rpi.setResource( TEST_NAME );
this.prr.setProvider( rpi );
assertNotNull( this.prr.getProvider( ) );
assertEquals( TEST_NAME , this.prr.getProvider( ).getResource( ) );
}
/**
* Setting the resource name when there is no resource provider record.
*/
@Test
public void testSetNameDeltaOnly( )
{
this.prr.setResource( TEST_NAME );
assertEquals( TEST_NAME , this.prr.getDelta( ).getResource( ) );
assertNull( this.prr.getProvider( ) );
}
/**
* Setting the resource name when there are both a resource delta record and a resource provider
* record.
*/
@Test
public void testSetNameFull( )
{
this.prr.setProvider( new ResourceProviderInformation( ) );
this.prr.setResource( TEST_NAME );
assertEquals( TEST_NAME , this.prr.getDelta( ).getResource( ) );
assertNotNull( this.prr.getProvider( ) );
assertEquals( TEST_NAME , this.prr.getProvider( ).getResource( ) );
}
}

View file

@ -0,0 +1,129 @@
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.testing.MockResultSet;
/**
* Tests for the {@link ResourceRowMapper} class.
*
* @author <a href="mailto:tseeker@legacyworlds.com">E. Benoît</a>
*/
public class TestResourceRowMapper
{
/** Planet identifiers found in the "results" */
private static final int[] PLANET_IDS = new int[] {
1 , 2
};
/** Resource names found in the two "results", respectively */
private static final String[] RESOURCE_NAMES = new String[] {
"Test1" , "Test2"
};
/** Income values found in the two results */
private static final double[] INCOME_VALUES = new double[] {
3.0 , 4.0
};
/** Upkeep values found in the two results */
private static final double[] UPKEEP_VALUES = new double[] {
5.0 , 6.0
};
/** Resource provider quantity for the second row */
private static final double RP_QUANTITY = 7.0;
/** Resource provider capacity for the second row */
private static final double RP_CAPACITY = 8.0;
/** Resource provider extraction difficulty for the second row */
private static final double RP_DIFFICULTY = 9.0;
/** Resource provider recovery rate for the second row */
private static final double RP_RECOVERY = 10.0;
/** The fake result set used in the tests */
private ResultSet resultSet;
/** The mapper used in the tests */
private ResourceRowMapper mapper;
/** Create the mapper and the contents of the fake result set */
@Before
public void setUp( )
{
this.mapper = new ResourceRowMapper( );
@SuppressWarnings( "unchecked" )
HashMap< String , Object > rows[] = new HashMap[ 2 ];
for ( int i = 0 ; i < 2 ; i++ ) {
HashMap< String , Object > row = new HashMap< String , Object >( );
row.put( "planet_id" , PLANET_IDS[ i ] );
row.put( "resource_name" , RESOURCE_NAMES[ i ] );
row.put( "pres_income" , INCOME_VALUES[ i ] );
row.put( "pres_upkeep" , UPKEEP_VALUES[ i ] );
if ( i == 1 ) {
row.put( "resprov_quantity_max" , RP_CAPACITY );
row.put( "resprov_quantity" , RP_QUANTITY );
row.put( "resprov_difficulty" , RP_DIFFICULTY );
row.put( "resprov_recovery" , RP_RECOVERY );
}
rows[ i ] = row;
}
this.resultSet = MockResultSet.create( rows );
}
/**
* Mapping a row with no provider information
*/
@Test
public void testMapNoProvider( )
throws SQLException
{
this.resultSet.absolute( 1 );
PlanetResourceRow row = this.mapper.mapRow( this.resultSet , 0 );
assertNotNull( row );
assertEquals( PLANET_IDS[ 0 ] , row.getPlanetId( ) );
assertEquals( RESOURCE_NAMES[ 0 ] , row.getDelta( ).getResource( ) );
assertEquals( INCOME_VALUES[ 0 ] , row.getDelta( ).getIncome( ) , 0 );
assertEquals( UPKEEP_VALUES[ 0 ] , row.getDelta( ).getUpkeep( ) , 0 );
assertNull( row.getProvider( ) );
}
/**
* Mapping a row with a provider information record
*/
@Test
public void testMapWithProvider( )
throws SQLException
{
this.resultSet.absolute( 2 );
PlanetResourceRow row = this.mapper.mapRow( this.resultSet , 0 );
assertNotNull( row );
assertEquals( PLANET_IDS[ 1 ] , row.getPlanetId( ) );
assertEquals( RESOURCE_NAMES[ 1 ] , row.getDelta( ).getResource( ) );
assertEquals( INCOME_VALUES[ 1 ] , row.getDelta( ).getIncome( ) , 0 );
assertEquals( UPKEEP_VALUES[ 1 ] , row.getDelta( ).getUpkeep( ) , 0 );
assertNotNull( row.getProvider( ) );
assertEquals( RP_CAPACITY , row.getProvider( ).getMaximalQuantity( ) , 0 );
assertEquals( RP_QUANTITY , row.getProvider( ).getCurrentQuantity( ) , 0 );
assertEquals( RP_DIFFICULTY , row.getProvider( ).getDifficulty( ) , 0 );
assertEquals( RP_RECOVERY , row.getProvider( ).getRecovery( ) , 0 );
}
}

View file

@ -0,0 +1,85 @@
package com.deepclone.lw.beans.bt.es.data;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.Serializable;
import org.junit.Before;
import org.junit.Test;
import com.deepclone.lw.beans.bt.es.data.InvalidDumpContentsException;
/**
* Tests for the {@link InvalidDumpContentsException} class
*
* @author <a href="mailto:tseeker@legacyworlds.com">E. Benoît</a>
*/
public class TestInvalidDumpContentsException
{
/**
* A dummy class used to test the {@link InvalidDumpContentsException} class.
*
* @author <a href="mailto:tseeker@legacyworlds.com">E. Benoît</a>
*/
@SuppressWarnings( "serial" )
private static final class TestRecord
implements Serializable
{
// EMPTY
}
/** Test string used as the "field" parameter */
private static final String TEST_STRING = "Test";
/** Exception instance used to run tests */
private InvalidDumpContentsException exception;
/**
* Set up the test by creating an exception instance using {@link TestRecord} as the target
* class and {@link #TEST_STRING} as the field name.
*/
@Before
public void setUp( )
{
this.exception = new InvalidDumpContentsException( TestRecord.class , TEST_STRING );
}
/**
* The constructor must set the record's class and field name.
*/
@Test
public void testExceptionData( )
{
assertEquals( TestRecord.class , this.exception.getRecordType( ) );
assertEquals( TEST_STRING , this.exception.getField( ) );
assertNotNull( this.exception.getMessage( ) );
}
/**
* The exception's message must end with the name of the class.
*/
@Test
public void testClassNameInMessage( )
{
assertTrue( this.exception.getMessage( ).endsWith( " " + TestRecord.class.getSimpleName( ) ) );
}
/**
* The exception's message must contain the name of the field.
*/
@Test
public void testFieldNameInMessage( )
{
assertTrue( this.exception.getMessage( ).contains( " " + TEST_STRING + " " ) );
}
}

View file

@ -0,0 +1,174 @@
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 ResourceDeltaInformation} XML dump storage class
*
* @author <a href="mailto:tseeker@legacyworlds.com">E. Benoît</a>
*/
public class TestResourceDeltaInformation
{
/** A string used in tests */
private static final String TEST_STRING = "This is a test";
/** A real number used in tests */
private static final Double TEST_DOUBLE = 4.2;
/** A record used in the tests */
private ResourceDeltaInformation rdi;
/**
* Set up the test by creating a new resource delta information record.
*/
@Before
public void setUp( )
{
this.rdi = new ResourceDeltaInformation( );
}
/**
* All fields are set to <code>null</code> by default
*/
@Test
public void testDefaultValues( )
{
assertNull( this.rdi.getResource( ) );
assertNull( this.rdi.getIncome( ) );
assertNull( this.rdi.getUpkeep( ) );
}
/**
* Setting and reading the resource's name
*/
@Test
public void testResourceName( )
{
this.rdi.setResource( TEST_STRING );
assertEquals( TEST_STRING , this.rdi.getResource( ) );
}
/**
* Setting the resource name to <code>null</code> throws {@link InvalidDumpContentsException}
*/
@Test
public void testNullResourceName( )
{
try {
this.rdi.setResource( null );
fail( "No InvalidDumpContentsException thrown" );
} catch ( InvalidDumpContentsException exception ) {
assertEquals( ResourceDeltaInformation.class , exception.getRecordType( ) );
assertEquals( "resource" , exception.getField( ) );
}
}
/**
* Setting and reading the income
*/
@Test
public void testIncome( )
{
this.rdi.setIncome( TEST_DOUBLE );
assertEquals( TEST_DOUBLE , this.rdi.getIncome( ) );
}
/**
* Setting and reading the upkeep
*/
@Test
public void testUpkeep( )
{
this.rdi.setUpkeep( TEST_DOUBLE );
assertEquals( TEST_DOUBLE , this.rdi.getUpkeep( ) );
}
/**
* Serialising the instance to XML
*/
@Test
public void testXMLSerialisation( )
{
this.rdi.setResource( TEST_STRING );
this.rdi.setIncome( 0.1 );
this.rdi.setUpkeep( 0.2 );
XStream xstream = this.createXStreamInstance( );
String serialised = xstream.toXML( this.rdi );
assertNotNull( serialised );
assertTrue( serialised.startsWith( "<resource-delta " ) );
assertTrue( serialised.endsWith( "/>" ) );
assertTrue( serialised.contains( " id=\"" + TEST_STRING + "\"" ) );
assertTrue( serialised.contains( " income=\"0.1\"" ) );
assertTrue( serialised.contains( " upkeep=\"0.2\"" ) );
}
/**
* Deserialising an instance that contains data
*/
@Test
public void testXMLDeserialisation( )
{
String xml = "<resource-delta id=\"Test\" income=\"0.1\" upkeep=\"0.2\" />";
XStream xstream = this.createXStreamInstance( );
Object deserialised = xstream.fromXML( xml );
assertNotNull( deserialised );
assertEquals( ResourceDeltaInformation.class , deserialised.getClass( ) );
this.rdi = (ResourceDeltaInformation) deserialised;
assertEquals( "Test" , this.rdi.getResource( ) );
assertEquals( (Double) 0.1 , this.rdi.getIncome( ) );
assertEquals( (Double) 0.2 , this.rdi.getUpkeep( ) );
}
/**
* Deserialising an instance that contains no data
*/
@Test
public void testXMLDeserialisationNoData( )
{
String xml = "<resource-delta />";
XStream xstream = this.createXStreamInstance( );
Object deserialised = xstream.fromXML( xml );
assertNotNull( deserialised );
assertEquals( ResourceDeltaInformation.class , deserialised.getClass( ) );
this.rdi = (ResourceDeltaInformation) deserialised;
assertNull( this.rdi.getResource( ) );
assertNull( this.rdi.getIncome( ) );
assertNull( this.rdi.getUpkeep( ) );
}
/**
* 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( ResourceDeltaInformation.class );
return xstream;
}
}

View file

@ -0,0 +1,207 @@
package com.deepclone.lw.beans.bt.es.data;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Before;
import org.junit.Test;
import com.thoughtworks.xstream.XStream;
/**
* Tests for the {@link ResourceProviderInformation} XML dump storage class
*
* @author <a href="mailto:tseeker@legacyworlds.com">E. Benoît</a>
*/
public class TestResourceProviderInformation
{
/** A string used in tests */
private static final String TEST_STRING = "This is a test";
/** A real number used in tests */
private static final Double TEST_DOUBLE = 4.2;
/** A resource provider information record */
private ResourceProviderInformation rpi;
@Before
public void setUp( )
{
this.rpi = new ResourceProviderInformation( );
}
/**
* A new record's fields are all set to <code>null</code>.
*/
@Test
public void testDefaultValues( )
{
assertNull( this.rpi.getResource( ) );
assertNull( this.rpi.getCurrentQuantity( ) );
assertNull( this.rpi.getMaximalQuantity( ) );
assertNull( this.rpi.getDifficulty( ) );
assertNull( this.rpi.getRecovery( ) );
}
/**
* Setting and reading the resource's name
*/
@Test
public void testResourceName( )
{
this.rpi.setResource( TEST_STRING );
assertEquals( TEST_STRING , this.rpi.getResource( ) );
}
/**
* Setting the resource name to <code>null</code> throws {@link InvalidDumpContentsException}
*/
@Test
public void testNullResourceName( )
{
try {
this.rpi.setResource( null );
fail( "No InvalidDumpContentsException thrown" );
} catch ( InvalidDumpContentsException exception ) {
assertEquals( ResourceProviderInformation.class , exception.getRecordType( ) );
assertEquals( "resource" , exception.getField( ) );
}
}
/**
* Setting and reading the current quantity
*/
@Test
public void testCurrentQuantity( )
{
this.rpi.setCurrentQuantity( TEST_DOUBLE );
assertEquals( TEST_DOUBLE , this.rpi.getCurrentQuantity( ) );
}
/**
* Setting and reading the maximal quantity
*/
@Test
public void testMaximalQuantity( )
{
this.rpi.setMaximalQuantity( TEST_DOUBLE );
assertEquals( TEST_DOUBLE , this.rpi.getMaximalQuantity( ) );
}
/**
* Setting and reading the extraction difficulty
*/
@Test
public void testDifficulty( )
{
this.rpi.setDifficulty( TEST_DOUBLE );
assertEquals( TEST_DOUBLE , this.rpi.getDifficulty( ) );
}
/**
* Setting and reading the recovery rate
*/
@Test
public void testRecovery( )
{
this.rpi.setRecovery( TEST_DOUBLE );
assertEquals( TEST_DOUBLE , this.rpi.getRecovery( ) );
}
/**
* Serialising the instance to XML
*/
@Test
public void testXMLSerialisation( )
{
this.rpi.setResource( TEST_STRING );
this.rpi.setCurrentQuantity( 0.1 );
this.rpi.setMaximalQuantity( 0.2 );
this.rpi.setDifficulty( 0.3 );
this.rpi.setRecovery( 0.4 );
XStream xstream = this.createXStreamInstance( );
String serialised = xstream.toXML( this.rpi );
assertNotNull( serialised );
assertTrue( serialised.startsWith( "<resource-provider " ) );
assertTrue( serialised.endsWith( "/>" ) );
assertTrue( serialised.contains( " resource=\"" + TEST_STRING + "\"" ) );
assertTrue( serialised.contains( " current=\"0.1\"" ) );
assertTrue( serialised.contains( " max=\"0.2\"" ) );
assertTrue( serialised.contains( " difficulty=\"0.3\"" ) );
assertTrue( serialised.contains( " recovery=\"0.4\"" ) );
}
/**
* Deserialising an instance that contains data
*/
@Test
public void testXMLDeserialisation( )
{
String xml = "<resource-provider resource=\"Test\" current=\"0.1\" max=\"0.2\" difficulty=\"0.3\" recovery=\"0.4\" />";
XStream xstream = this.createXStreamInstance( );
Object deserialised = xstream.fromXML( xml );
assertNotNull( deserialised );
assertEquals( ResourceProviderInformation.class , deserialised.getClass( ) );
this.rpi = (ResourceProviderInformation) deserialised;
assertEquals( "Test" , this.rpi.getResource( ) );
assertEquals( (Double) 0.1 , this.rpi.getCurrentQuantity( ) );
assertEquals( (Double) 0.2 , this.rpi.getMaximalQuantity( ) );
assertEquals( (Double) 0.3 , this.rpi.getDifficulty( ) );
assertEquals( (Double) 0.4 , this.rpi.getRecovery( ) );
}
/**
* Deserialising an instance that contains no data
*/
@Test
public void testXMLDeserialisationNoData( )
{
String xml = "<resource-provider />";
XStream xstream = this.createXStreamInstance( );
Object deserialised = xstream.fromXML( xml );
assertNotNull( deserialised );
assertEquals( ResourceProviderInformation.class , deserialised.getClass( ) );
this.rpi = (ResourceProviderInformation) deserialised;
assertNull( this.rpi.getResource( ) );
assertNull( this.rpi.getCurrentQuantity( ) );
assertNull( this.rpi.getMaximalQuantity( ) );
assertNull( this.rpi.getDifficulty( ) );
assertNull( this.rpi.getRecovery( ) );
}
/**
* 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( ResourceProviderInformation.class );
xstream.alias( "resource-provider" , ResourceProviderInformation.class );
return xstream;
}
}