Resource Definition Loader

* Implemented resource definition loader including tests

* Added resource definition xml file and style definition

* Made a small style change to i18n loader
This commit is contained in:
Tim Rosser 2012-01-10 22:38:29 +11:00
parent 3637b6e1d1
commit f4a16aa431
20 changed files with 2275 additions and 3 deletions

View file

@ -0,0 +1,155 @@
package com.deepclone.lw.cli.xmlimport;
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 java.io.File;
import java.io.IOException;
import org.junit.Test;
import com.deepclone.lw.cli.xmlimport.data.DataImportException;
import com.deepclone.lw.cli.xmlimport.data.resources.BasicResource;
import com.deepclone.lw.cli.xmlimport.data.resources.NaturalResource;
import com.deepclone.lw.cli.xmlimport.data.resources.Resources;
import com.thoughtworks.xstream.converters.ConversionException;
import com.thoughtworks.xstream.io.StreamException;
/**
* Unit tests for the {@link ResourceLoader} class.
*
* @author <a href="mailto:tim@mitheren.com">T. Rosser</a>
*
*/
public class TestResourceLoader
{
/**
* Try initialising the loader with a <code>null</code> file instance.
*
* @throws NullPointerException
* when the constructor is executed
*/
@Test( expected = NullPointerException.class )
public void testNullFile( )
throws NullPointerException
{
new ResourceLoader( null );
}
/** Try loading a file that does not exist */
@Test
public void testMissingFile( )
{
ResourceLoader loader = new ResourceLoader( new File( "does-not-exist" ) );
try {
loader.load( );
} catch ( DataImportException e ) {
assertTrue( "cause is a" + e.getCause( ).getClass( ).getName( ) , e.getCause( ) instanceof IOException );
return;
}
fail( "no exception after trying to load a missing file" );
}
/** Try loading a file that contains something that is not XML. */
@Test
public void testBadXML( )
{
ResourceLoader loader = new ResourceLoader( new File( "TestFiles/resource-loader/bad-xml.xml" ) );
try {
loader.load( );
} catch ( DataImportException e ) {
assertTrue( "cause is a " + e.getCause( ).getClass( ).getName( ) , e.getCause( ) instanceof StreamException );
return;
}
fail( "no exception after loading stuff that isn't XML" );
}
/**
* Test loading a file that contains XML but which cannot be deserialised to an
* {@link Resources} instance.
*/
@Test
public void testBadContents( )
{
ResourceLoader loader = new ResourceLoader( new File( "TestFiles/resource-loader/bad-contents.xml" ) );
try {
loader.load( );
} catch ( DataImportException e ) {
assertTrue( "cause is a " + e.getCause( ).getClass( ).getName( ) ,
e.getCause( ) instanceof ConversionException );
return;
}
fail( "no exception after loading bad XML" );
}
/**
* Try loading a file that contains valid XML for a {@link ResourceLoader} instance with
* semantic errors.
*/
@Test
public void testBadData( )
{
ResourceLoader loader = new ResourceLoader( new File( "TestFiles/resource-loader/bad-data.xml" ) );
try {
loader.load( );
} catch ( DataImportException e ) {
assertNull( e.getCause( ) );
return;
}
fail( "no exception after loading bad data" );
}
/** Try loading valid data, make sure that it contains exactly what is expected */
@Test
public void testGoodData( )
{
ResourceLoader loader = new ResourceLoader( new File( "TestFiles/resource-loader/good-data.xml" ) );
Resources resources;
try {
resources = loader.load( );
} catch ( DataImportException e ) {
fail( "could not load valid file" );
return;
}
assertNotNull( resources );
// Not sure if this is the best way to code for the two different resource types...
int rCount = 0;
for ( BasicResource br : resources ) {
if ( rCount == 0 ) {
assertEquals( "money" , br.getName( ) );
assertEquals( "moneyDescription" , br.getDescription( ) );
assertEquals( new Integer( 0 ) , br.getWeight( ) );
assertEquals( null , br.getCategory( ) );
} else if ( rCount == 1 ) {
// This isn't retarded is it?
NaturalResource nr = (NaturalResource) br;
assertEquals( "titanium" , nr.getName( ) );
assertEquals( "titaniumDescription" , nr.getDescription( ) );
assertEquals( new Integer( 1 ) , nr.getWeight( ) );
assertEquals( "minerals" , nr.getCategory( ) );
assertEquals( new Double( 0.8 ) , nr.getPresenceProbability( ) );
assertEquals( new Double( 5000 ) , nr.getQuantity( ).getAverage( ) );
assertEquals( new Double( 1500 ) , nr.getQuantity( ).getDeviation( ) );
assertEquals( new Double( 0.1 ) , nr.getDifficulty( ).getAverage( ) );
assertEquals( new Double( 0.05 ) , nr.getDifficulty( ).getDeviation( ) );
assertEquals( new Double( 0.4 ) , nr.getRecovery( ).getAverage( ) );
assertEquals( new Double( 0.05 ) , nr.getRecovery( ).getDeviation( ) );
}
rCount++;
}
assertEquals( 2 , rCount );
}
}

View file

@ -0,0 +1,267 @@
package com.deepclone.lw.cli.xmlimport.resources;
import com.deepclone.lw.cli.xmlimport.data.ImportableData;
import com.deepclone.lw.cli.xmlimport.data.resources.BasicResource;
import com.deepclone.lw.cli.xmlimport.data.resources.NaturalResource;
import com.deepclone.lw.cli.xmlimport.data.resources.ResourceParameter;
import com.deepclone.lw.cli.xmlimport.data.resources.Resources;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.XStreamException;
/**
* Base class for testing resource importation
*
* <p>
* This class is used as a parent for tests of the resource import structures. It includes the code
* used to actually create these resources, as this can normally only be done through XStream.
*
* @author <a href="mailto:tim@mitheren.com">T. Rosser</a>
*
*/
abstract public class BaseTest
{
/**
* Escape &amp;, &lt; and &gt; in XML strings
*
* @param string
* the string to escape
*
* @return the escaped string
*/
private String xmlString( String string )
{
return string.replace( "&" , "&amp;" ).replace( "<" , "&lt;" ).replace( ">" , "&gt;" );
}
/**
* Escape &amp;, &lt; and &gt;, ' and " in XML strings
*
* @param string
* the string to escape
*
* @return the escaped string
*/
private String quoteString( String string )
{
return "\"" + this.xmlString( string ).replace( "\"" , "&quot;" ).replace( "'" , "&apos;" ) + "\"";
}
/**
* Create a {@link BasicResource} definition.
*
* <p>
* Generate the XML code that corresponds to a basic resource definition.
*
* @param name
* The resources name, or <code>null</code> to omit the name.
* @param description
* The resources description, or <code>null</code> to omit the description.
* @param weight
* The resources weight, or <code>null</code> to omit the weight.
* @param category
* The resources category, or <code>null</code> to omit the category.
*
* @return the XML code corresponding to the resource definition.
*/
protected String createBasicResourceDefinition( String name , String description , String weight , String category )
{
StringBuilder str = new StringBuilder( );
str.append( "<basic-resource" );
if ( name != null ) {
str.append( " name=" ).append( this.quoteString( name ) );
}
if ( description != null ) {
str.append( " description=" ).append( this.quoteString( description ) );
}
if ( weight != null ) {
str.append( " weight=" ).append( this.quoteString( weight ) );
}
if ( category != null ) {
str.append( " category=" ).append( this.quoteString( category ) );
}
str.append( " />" );
return str.toString( );
}
/**
* Create the a {@link NaturalResource} definition.
*
* <p>
* Create the XML code that corresponds to a natural resource definition.
*
* @param name
* The resources name, or <code>null</code> to omit the name.
* @param description
* The resources description, or <code>null</code> to omit the description.
* @param weight
* The resources weight, or <code>null</code> to omit the weight.
* @param category
* The resources category, or <code>null</code> to omit the category.
* @param probability
* The resources presence-probability, or <code>null</code> to omit the probability.
* @param quantity
* The XML code representing the {@link ResourceParameter} giving the resources
* quantity average and deviation or <code>null</code> to omit the quantity.
* @param difficulty
* The XML code representing the {@link ResourceParameter} giving the resources
* difficulty average and deviation or <code>null</code> to omit the difficulty.
* @param recovery
* The XML code representing the {@link ResourceParameter} giving the resources
* recovery average and deviation or <code>null</code> to omit the recovery.
*
* @return the XML code corresponding to the natural resource definition
*/
protected String createNaturalResourceDefinition( String name , String description , String weight ,
String category , String probability , String quantity , String difficulty , String recovery )
{
StringBuilder str = new StringBuilder( );
str.append( "<natural-resource" );
if ( name != null ) {
str.append( " name=" ).append( this.quoteString( name ) );
}
if ( description != null ) {
str.append( " description=" ).append( this.quoteString( description ) );
}
if ( weight != null ) {
str.append( " weight=" ).append( this.quoteString( weight ) );
}
if ( category != null ) {
str.append( " category=" ).append( this.quoteString( category ) );
}
if ( probability != null ) {
str.append( " presence-probability=" ).append( this.quoteString( probability ) );
}
str.append( ">" );
if ( quantity != null ) {
str.append( quantity );
}
if ( difficulty != null ) {
str.append( difficulty );
}
if ( recovery != null ) {
str.append( recovery );
}
str.append( "</natural-resource>" );
return str.toString( );
}
/**
* Create a {@link ResourceParameter} definition
*
* <p>
* Generates the XML code the corresponds to a resource parameter definition.
*
* @param type
* Gives the type of resource parameter to create (quantity/difficulty/recovery)
* @param average
* The parameters average.
* @param deviation
* The parameters deviation.
*
* @return the XML code corresponding to the resource parameter definition
*/
protected String createResourceParameterDefinition( String type , String average , String deviation )
{
StringBuilder str = new StringBuilder( );
str.append( "<" );
if ( type != null ) {
str.append( type );
}
if ( average != null ) {
str.append( " average=" ).append( this.quoteString( average ) );
}
if ( deviation != null ) {
str.append( " deviation=" ).append( this.quoteString( deviation ) );
}
str.append( " />" );
return str.toString( );
}
/**
* Creates the XML code for a top-level resource definition element
*
* @param resources
* XML definitions of resources
*
* @return the top-level elements XML code
*/
protected String createTopLevel( String... resources )
{
StringBuilder str = new StringBuilder( );
str.append( "<lw-resources>" );
for ( String resource : resources ) {
str.append( resource );
}
str.append( "</lw-resources>" );
return str.toString( );
}
/**
* Create the necessary XStream instance
*
* <p>
* Initialise an XStream instance and set it up so it can process resource data definitions.
* Unlike {@link ResourceLoader}, this version also registers multiple aliases for
* ResourceParameter. This is because ResourceParameter is usually only used inside a
* NaturalResource, but when testing may need to be created independently.
*/
private XStream createXStreamInstance( )
{
XStream xstream = new XStream( );
xstream.processAnnotations( Resources.class );
xstream.processAnnotations( NaturalResource.class );
xstream.alias( "quantity" , ResourceParameter.class );
xstream.alias( "difficulty" , ResourceParameter.class );
xstream.alias( "recovery" , ResourceParameter.class );
xstream.alias( "test" , ResourceParameter.class ); // Just for unit tests
xstream.alias( "lw-resources" , Resources.class );
return xstream;
}
/**
* Create a resource object from its XML code
*
* @param xml
* the XML code
* @param cls
* the class of the object
*
* @return the object that was created from the code
*
* @throws ClassCastException
* if the code corresponds to some other type of object
* @throws XStreamException
* if some error occurred while deserialising the object
*/
protected < T extends ImportableData > T createObject( String xml , Class< T > cls )
throws ClassCastException , XStreamException
{
return cls.cast( this.createXStreamInstance( ).fromXML( xml ) );
}
}

View file

@ -0,0 +1,203 @@
package com.deepclone.lw.cli.xmlimport.resources;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.deepclone.lw.cli.xmlimport.data.DataImportException;
import com.deepclone.lw.cli.xmlimport.data.resources.BasicResource;
import com.thoughtworks.xstream.converters.ConversionException;
/**
* Unit tests for {@link BasicResource}.
*
* @author <a href="mailto:tim@mitheren.com>T. Rosser</a>
*
*/
public class TestBasicResource
extends BaseTest
{
/** Test loading a valid basic resource */
@Test
public void testLoadBasicResourceOK( )
{
String resource = this.createBasicResourceDefinition( "test" , "test" , "0" , "test" );
BasicResource instance = this.createObject( resource , BasicResource.class );
assertEquals( "test" , instance.getName( ) );
assertEquals( "test" , instance.getDescription( ) );
assertEquals( new Integer( 0 ) , instance.getWeight( ) );
assertEquals( "test" , instance.getCategory( ) );
}
/** Test loading a basic resource without name */
@Test
public void testLoadResourceNoName( )
{
String resource = this.createBasicResourceDefinition( null , "test" , "0" , "test" );
BasicResource instance = this.createObject( resource , BasicResource.class );
assertEquals( null , instance.getName( ) );
assertEquals( "test" , instance.getDescription( ) );
assertEquals( new Integer( 0 ) , instance.getWeight( ) );
assertEquals( "test" , instance.getCategory( ) );
}
/** Test loading a basic resource without description */
@Test
public void testLoadResourceNoDescription( )
{
String resource = this.createBasicResourceDefinition( "test" , null , "0" , "test" );
BasicResource instance = this.createObject( resource , BasicResource.class );
assertEquals( "test" , instance.getName( ) );
assertEquals( null , instance.getDescription( ) );
assertEquals( new Integer( 0 ) , instance.getWeight( ) );
assertEquals( "test" , instance.getCategory( ) );
}
/** Test loading a basic resource without weight */
@Test
public void testLoadBasicResourceNoWeight( )
{
String resource = this.createBasicResourceDefinition( "test" , "test" , null , "test" );
BasicResource instance = this.createObject( resource , BasicResource.class );
assertEquals( "test" , instance.getName( ) );
assertEquals( "test" , instance.getDescription( ) );
assertEquals( null , instance.getWeight( ) );
assertEquals( "test" , instance.getCategory( ) );
}
/** Test loading a basic resource without category */
@Test
public void testLoadBasicResourceNoCategory( )
{
String resource = this.createBasicResourceDefinition( "test" , "test" , "0" , null );
BasicResource instance = this.createObject( resource , BasicResource.class );
assertEquals( "test" , instance.getName( ) );
assertEquals( "test" , instance.getDescription( ) );
assertEquals( new Integer( 0 ) , instance.getWeight( ) );
assertEquals( null , instance.getCategory( ) );
}
/**
* Test validating a valid basic resource
*
* @throws DataImportException
* if the data is not valid
*/
@Test
public void testValidateBasicResourceOK( )
throws DataImportException
{
String resource = this.createBasicResourceDefinition( "test" , "test" , "0" , "test" );
BasicResource instance = this.createObject( resource , BasicResource.class );
instance.verifyData( );
}
/**
* Test validating a basic resource without name
*
* @throws DataImportException
* if the data is not valid
*/
@Test( expected = DataImportException.class )
public void testValidateResourceNoName( )
throws DataImportException
{
String resource = this.createBasicResourceDefinition( null , "test" , "0" , "test" );
BasicResource instance = this.createObject( resource , BasicResource.class );
instance.verifyData( );
}
/**
* Test validating a basic resource without description
*
* @throws DataImportException
* if the data is not valid
*/
@Test( expected = DataImportException.class )
public void testValidateResourceNoDescription( )
throws DataImportException
{
String resource = this.createBasicResourceDefinition( "test" , null , "0" , "test" );
BasicResource instance = this.createObject( resource , BasicResource.class );
instance.verifyData( );
}
/**
* Test validating a basic resource without weight
*
* @throws DataImportException
* if the data is not valid
*/
@Test( expected = DataImportException.class )
public void testValidateBasicResourceNoWeight( )
throws DataImportException
{
String resource = this.createBasicResourceDefinition( "test" , "test" , null , "test" );
BasicResource instance = this.createObject( resource , BasicResource.class );
instance.verifyData( );
}
/**
* Test validating a basic resource without category
*
* @throws DataImportException
* if the data is not valid
*/
@Test
public void testValidateBasicResourceNoCategory( )
throws DataImportException
{
String resource = this.createBasicResourceDefinition( "test" , "test" , "0" , null );
BasicResource instance = this.createObject( resource , BasicResource.class );
instance.verifyData( );
}
/**
* Test validating a basic resource with a non-numeric string weight
*
* @throws DataImportException
* if the data is not valid
* @throws ConversionException
* if XStream cannot convert the text to an object
*/
@Test( expected = ConversionException.class )
public void testValidateBasicResourceStringWeight( )
throws DataImportException , ConversionException
{
String resource = this.createBasicResourceDefinition( "test" , "test" , "test" , "test" );
BasicResource instance = this.createObject( resource , BasicResource.class );
instance.verifyData( );
}
/**
* Test validating a basic resource with a non-integer weight
*
* @throws DataImportException
* if the data is not valid
* @throws ConversionException
* if XStream cannot convert the text to an object
*/
@Test( expected = ConversionException.class )
public void testValidateBasicResourceStringFloat( )
throws DataImportException , ConversionException
{
String resource = this.createBasicResourceDefinition( "test" , "test" , "1.2" , null );
BasicResource instance = this.createObject( resource , BasicResource.class );
instance.verifyData( );
}
}

View file

@ -0,0 +1,329 @@
package com.deepclone.lw.cli.xmlimport.resources;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.Before;
import org.junit.Test;
import com.deepclone.lw.cli.xmlimport.data.DataImportException;
import com.deepclone.lw.cli.xmlimport.data.resources.NaturalResource;
import com.deepclone.lw.cli.xmlimport.data.resources.ResourceParameter;
import com.thoughtworks.xstream.converters.ConversionException;
/**
* Unit tests for {@link NaturalResource}.
*
* @author <a href="mailto:tim@mitheren.com>T. Rosser</a>
*
*/
public class TestNaturalResource
extends BaseTest
{
/** The XML definition for a {@link ResourceParameter} representing a resources quantity */
private String quantity;
/** The XML definition for a {@link ResourceParameter} representing a resources quantity */
private String difficulty;
/** The XML definition for a {@link ResourceParameter} representing a resources quantity */
private String recovery;
/**
* Create valid definitions for the ResourceParameters to be used in the NaturalResource tests
*/
@Before
public void setup( )
{
this.quantity = this.createResourceParameterDefinition( "quantity" , "0" , "0" );
this.difficulty = this.createResourceParameterDefinition( "difficulty" , "0" , "0" );
this.recovery = this.createResourceParameterDefinition( "recovery" , "0" , "0" );
}
/** Test loading a valid natural resource */
@Test
public void testLoadNaturalResourceOK( )
{
String resource = this.createNaturalResourceDefinition( "test" , "test" , "0" , "test" , "0" , this.quantity ,
this.difficulty , this.recovery );
NaturalResource instance = this.createObject( resource , NaturalResource.class );
assertEquals( new Double( 0 ) , instance.getPresenceProbability( ) );
if ( instance.getQuantity( ) != null ) {
assertEquals( new Double( 0 ) , instance.getQuantity( ).getAverage( ) );
assertEquals( new Double( 0 ) , instance.getQuantity( ).getDeviation( ) );
} else {
fail( "Quantity is null" );
}
if ( instance.getDifficulty( ) != null ) {
assertEquals( new Double( 0 ) , instance.getDifficulty( ).getAverage( ) );
assertEquals( new Double( 0 ) , instance.getDifficulty( ).getDeviation( ) );
} else {
fail( "Difficulty is null" );
}
if ( instance.getRecovery( ) != null ) {
assertEquals( new Double( 0 ) , instance.getRecovery( ).getAverage( ) );
assertEquals( new Double( 0 ) , instance.getRecovery( ).getDeviation( ) );
} else {
fail( "Recovery is null" );
}
}
/** Test loading a valid natural resource with presence-probability giving a Double value */
@Test
public void testLoadNaturalResourceWithDoubleOK( )
{
String resource = this.createNaturalResourceDefinition( "test" , "test" , "0" , "test" , "1.2" , this.quantity ,
this.difficulty , this.recovery );
NaturalResource instance = this.createObject( resource , NaturalResource.class );
assertEquals( new Double( 1.2 ) , instance.getPresenceProbability( ) );
if ( instance.getQuantity( ) != null ) {
assertEquals( new Double( 0 ) , instance.getQuantity( ).getAverage( ) );
assertEquals( new Double( 0 ) , instance.getQuantity( ).getDeviation( ) );
} else {
fail( "Quantity is null" );
}
if ( instance.getDifficulty( ) != null ) {
assertEquals( new Double( 0 ) , instance.getDifficulty( ).getAverage( ) );
assertEquals( new Double( 0 ) , instance.getDifficulty( ).getDeviation( ) );
} else {
fail( "Difficulty is null" );
}
if ( instance.getRecovery( ) != null ) {
assertEquals( new Double( 0 ) , instance.getRecovery( ).getAverage( ) );
assertEquals( new Double( 0 ) , instance.getRecovery( ).getDeviation( ) );
} else {
fail( "Recovery is null" );
}
}
/** Test loading a natural resource with no presence-probability */
@Test
public void testLoadNaturalResourceNoProbability( )
{
String resource = this.createNaturalResourceDefinition( "test" , "test" , "0" , "test" , null , this.quantity ,
this.difficulty , this.recovery );
NaturalResource instance = this.createObject( resource , NaturalResource.class );
assertEquals( null , instance.getPresenceProbability( ) );
if ( instance.getQuantity( ) != null ) {
assertEquals( new Double( 0 ) , instance.getQuantity( ).getAverage( ) );
assertEquals( new Double( 0 ) , instance.getQuantity( ).getDeviation( ) );
} else {
fail( "Quantity is null" );
}
if ( instance.getDifficulty( ) != null ) {
assertEquals( new Double( 0 ) , instance.getDifficulty( ).getAverage( ) );
assertEquals( new Double( 0 ) , instance.getDifficulty( ).getDeviation( ) );
} else {
fail( "Difficulty is null" );
}
if ( instance.getRecovery( ) != null ) {
assertEquals( new Double( 0 ) , instance.getRecovery( ).getAverage( ) );
assertEquals( new Double( 0 ) , instance.getRecovery( ).getDeviation( ) );
} else {
fail( "Recovery is null" );
}
}
/** Test loading a natural resource with no quantity */
@Test
public void testLoadNaturalResourceNoQuantity( )
{
String resource = this.createNaturalResourceDefinition( "test" , "test" , "0" , "test" , "0" , null ,
this.difficulty , this.recovery );
NaturalResource instance = this.createObject( resource , NaturalResource.class );
assertEquals( new Double( 0 ) , instance.getPresenceProbability( ) );
assertEquals( null , instance.getQuantity( ) );
if ( instance.getDifficulty( ) != null ) {
assertEquals( new Double( 0 ) , instance.getDifficulty( ).getAverage( ) );
assertEquals( new Double( 0 ) , instance.getDifficulty( ).getDeviation( ) );
} else {
fail( "Difficulty is null" );
}
if ( instance.getRecovery( ) != null ) {
assertEquals( new Double( 0 ) , instance.getRecovery( ).getAverage( ) );
assertEquals( new Double( 0 ) , instance.getRecovery( ).getDeviation( ) );
} else {
fail( "Recovery is null" );
}
}
/** Test loading a natural resource with no difficulty */
@Test
public void testLoadNaturalResourceNoDifficulty( )
{
String resource = this.createNaturalResourceDefinition( "test" , "test" , "0" , "test" , "0" , this.quantity ,
null , this.recovery );
NaturalResource instance = this.createObject( resource , NaturalResource.class );
assertEquals( new Double( 0 ) , instance.getPresenceProbability( ) );
if ( instance.getQuantity( ) != null ) {
assertEquals( new Double( 0 ) , instance.getQuantity( ).getAverage( ) );
assertEquals( new Double( 0 ) , instance.getQuantity( ).getDeviation( ) );
} else {
fail( "Quantity is null" );
}
assertEquals( null , instance.getDifficulty( ) );
if ( instance.getRecovery( ) != null ) {
assertEquals( new Double( 0 ) , instance.getRecovery( ).getAverage( ) );
assertEquals( new Double( 0 ) , instance.getRecovery( ).getDeviation( ) );
} else {
fail( "Recovery is null" );
}
}
/** Test loading a natural resource with no recovery */
@Test
public void testLoadNaturalResourceNoRecovery( )
{
String resource = this.createNaturalResourceDefinition( "test" , "test" , "0" , "test" , "0" , this.quantity ,
this.difficulty , null );
NaturalResource instance = this.createObject( resource , NaturalResource.class );
assertEquals( new Double( 0 ) , instance.getPresenceProbability( ) );
if ( instance.getQuantity( ) != null ) {
assertEquals( new Double( 0 ) , instance.getQuantity( ).getAverage( ) );
assertEquals( new Double( 0 ) , instance.getQuantity( ).getDeviation( ) );
} else {
fail( "Quantity is null" );
}
if ( instance.getDifficulty( ) != null ) {
assertEquals( new Double( 0 ) , instance.getDifficulty( ).getAverage( ) );
assertEquals( new Double( 0 ) , instance.getDifficulty( ).getDeviation( ) );
} else {
fail( "Difficulty is null" );
}
assertEquals( null , instance.getRecovery( ) );
}
/**
* Test validating a valid natural resource
*
* @throws DataImportException
* if the data is not valid
*/
@Test
public void testValidateNaturalResourceOK( )
throws DataImportException
{
String resource = this.createNaturalResourceDefinition( "test" , "test" , "0" , "test" , "0" , this.quantity ,
this.difficulty , this.recovery );
NaturalResource instance = this.createObject( resource , NaturalResource.class );
instance.verifyData( );
}
/**
* Test validating a valid natural resource with presence-probability containing a Double value
*
* @throws DataImportException
* if the data is not valid
*/
@Test
public void testValidateNaturalResourceWithDoubleOK( )
throws DataImportException
{
String resource = this.createNaturalResourceDefinition( "test" , "test" , "0" , "test" , "1.2" , this.quantity ,
this.difficulty , this.recovery );
NaturalResource instance = this.createObject( resource , NaturalResource.class );
instance.verifyData( );
}
/**
* Test validating a natural resource with no presence-probability
*
* @throws DataImportException
* if the data is not valid
*/
@Test( expected = DataImportException.class )
public void testValidateNaturalResourceNoProbability( )
throws DataImportException
{
String resource = this.createNaturalResourceDefinition( "test" , "test" , "0" , "test" , null , this.quantity ,
this.difficulty , this.recovery );
NaturalResource instance = this.createObject( resource , NaturalResource.class );
instance.verifyData( );
}
/**
* Test validating a natural resource with no quantity
*
* @throws DataImportException
* if the data is not valid
*/
@Test( expected = DataImportException.class )
public void testValidateNaturalResourceNoQuantity( )
throws DataImportException
{
String resource = this.createNaturalResourceDefinition( "test" , "test" , "0" , "test" , "0" , null ,
this.difficulty , this.recovery );
NaturalResource instance = this.createObject( resource , NaturalResource.class );
instance.verifyData( );
}
/**
* Test validating a natural resource with no difficulty
*
* @throws DataImportException
* if the data is not valid
*/
@Test( expected = DataImportException.class )
public void testValidateNaturalResourceNoDifficulty( )
throws DataImportException
{
String resource = this.createNaturalResourceDefinition( "test" , "test" , "0" , "test" , "0" , this.quantity ,
null , this.recovery );
NaturalResource instance = this.createObject( resource , NaturalResource.class );
instance.verifyData( );
}
/**
* Test validating a natural resource with no recovery
*
* @throws DataImportException
* if the data is not valid
*/
@Test( expected = DataImportException.class )
public void testValidateNaturalResourceNoRecovery( )
throws DataImportException
{
String resource = this.createNaturalResourceDefinition( "test" , "test" , "0" , "test" , "0" , this.quantity ,
this.difficulty , null );
NaturalResource instance = this.createObject( resource , NaturalResource.class );
instance.verifyData( );
}
/**
* Test validating a natural resource with a non-numerical string as presence-probability
*
* @throws DataImportException
* if the data is not valid
* @throws ConversionException
* if XStream cannot convert the text into an object
*/
@Test( expected = ConversionException.class )
public void testValidateNaturalResourceStringProbability( )
throws DataImportException , ConversionException
{
String resource = this.createNaturalResourceDefinition( "test" , "test" , "0" , "test" , "test" ,
this.quantity , this.difficulty , this.recovery );
NaturalResource instance = this.createObject( resource , NaturalResource.class );
instance.verifyData( );
}
}

View file

@ -0,0 +1,192 @@
package com.deepclone.lw.cli.xmlimport.resources;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.deepclone.lw.cli.xmlimport.data.DataImportException;
import com.deepclone.lw.cli.xmlimport.data.resources.ResourceParameter;
import com.thoughtworks.xstream.converters.ConversionException;
/**
* Unit tests for {@link ResourceParameter}.
*
* @author <a href="mailto:tim@mitheren.com>T. Rosser</a>
*
*/
public class TestResourceParameter
extends BaseTest
{
/** Test loading a valid resource parameter */
@Test
public void testLoadResourceParameterOK( )
{
String rp = this.createResourceParameterDefinition( "test" , "0" , "0" );
ResourceParameter instance = this.createObject( rp , ResourceParameter.class );
assertEquals( new Double( 0 ) , instance.getAverage( ) );
assertEquals( new Double( 0 ) , instance.getDeviation( ) );
}
/** Test loading a valid resource parameter with average containing a Double value */
@Test
public void testLoadResourceParameterDoubleAverageOK( )
{
String rp = this.createResourceParameterDefinition( "test" , "1.2" , "0" );
ResourceParameter instance = this.createObject( rp , ResourceParameter.class );
assertEquals( new Double( 1.2 ) , instance.getAverage( ) );
assertEquals( new Double( 0 ) , instance.getDeviation( ) );
}
/** Test loading a valid resource parameter with deviation containing a Double value */
@Test
public void testLoadResourceParameterDoubleDeviationOK( )
{
String rp = this.createResourceParameterDefinition( "test" , "0" , "1.2" );
ResourceParameter instance = this.createObject( rp , ResourceParameter.class );
assertEquals( new Double( 0 ) , instance.getAverage( ) );
assertEquals( new Double( 1.2 ) , instance.getDeviation( ) );
}
/** Test loading a valid resource parameter with no average */
@Test
public void testLoadResourceParameterNoAverage( )
{
String rp = this.createResourceParameterDefinition( "test" , null , "0" );
ResourceParameter instance = this.createObject( rp , ResourceParameter.class );
assertEquals( null , instance.getAverage( ) );
assertEquals( new Double( 0 ) , instance.getDeviation( ) );
}
/** Test loading a valid resource parameter with no deviation */
@Test
public void testLoadResourceParameterNoDeviation( )
{
String rp = this.createResourceParameterDefinition( "test" , "0" , null );
ResourceParameter instance = this.createObject( rp , ResourceParameter.class );
assertEquals( new Double( 0 ) , instance.getAverage( ) );
assertEquals( null , instance.getDeviation( ) );
}
/**
* Test validating a valid resource parameter
*
* @throws DataImportException
* if the data is not valid
*/
@Test
public void testValidateResourceParameterOK( )
throws DataImportException
{
String rp = this.createResourceParameterDefinition( "test" , "0" , "0" );
ResourceParameter instance = this.createObject( rp , ResourceParameter.class );
instance.verifyData( "test" );
}
/**
* Test validating a valid resource parameter with average containing a Double value
*
* @throws DataImportException
* if the data is not valid
*/
@Test
public void testValidateResourceParameterDoubleAverageOK( )
throws DataImportException
{
String rp = this.createResourceParameterDefinition( "test" , "1.2" , "0" );
ResourceParameter instance = this.createObject( rp , ResourceParameter.class );
instance.verifyData( "test" );
}
/**
* Test validating a valid resource parameter with deviation containing a Double value
*
* @throws DataImportException
* if the data is not valid
*/
@Test
public void testValidateResourceParameterDoubleDeviationOK( )
throws DataImportException
{
String rp = this.createResourceParameterDefinition( "test" , "0" , "1.2" );
ResourceParameter instance = this.createObject( rp , ResourceParameter.class );
instance.verifyData( "test" );
}
/**
* Test validating a resource parameter with no average
*
* @throws DataImportException
* if the data is not valid
*/
@Test( expected = DataImportException.class )
public void testValidateResourceParameterNoAverage( )
throws DataImportException
{
String rp = this.createResourceParameterDefinition( "test" , null , "0" );
ResourceParameter instance = this.createObject( rp , ResourceParameter.class );
instance.verifyData( "test" );
}
/**
* Test validating a resource parameter with no deviation
*
* @throws DataImportException
* if the data is not valid
*/
@Test( expected = DataImportException.class )
public void testValidateResourceParameterNoDeviation( )
throws DataImportException
{
String rp = this.createResourceParameterDefinition( "test" , "0" , null );
ResourceParameter instance = this.createObject( rp , ResourceParameter.class );
instance.verifyData( "test" );
}
/**
* Test validating a resource parameter with a non-numerical string average
*
* @throws DataImportException
* if the data is not valid
* @throws ConversionException
* if XStream cannot convert the text into an object
*/
@Test( expected = ConversionException.class )
public void testValidateResourceParameterStringAverage( )
throws DataImportException , ConversionException
{
String rp = this.createResourceParameterDefinition( "test" , "test" , "0" );
ResourceParameter instance = this.createObject( rp , ResourceParameter.class );
instance.verifyData( "test" );
}
/**
* Test validating a resource parameter with a non-numerical string deviation
*
* @throws DataImportException
* if the data is not valid
* @throws ConversionException
* if XStream cannot convert the text into an object
*/
@Test( expected = ConversionException.class )
public void testValidateResourceParameterStringDeviation( )
throws DataImportException , ConversionException
{
String rp = this.createResourceParameterDefinition( "test" , "0" , "test" );
ResourceParameter instance = this.createObject( rp , ResourceParameter.class );
instance.verifyData( "test" );
}
}

View file

@ -0,0 +1,137 @@
package com.deepclone.lw.cli.xmlimport.resources;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
import com.deepclone.lw.cli.xmlimport.data.DataImportException;
import com.deepclone.lw.cli.xmlimport.data.resources.BasicResource;
import com.deepclone.lw.cli.xmlimport.data.resources.Resources;
/**
* Unit tests for {@link Resources}.
*
* @author <a href="mailto:tim@mitheren.com>T. Rosser</a>
*
*/
public class TestResources
extends BaseTest
{
/** The basic resource definition used in most tests */
private String basicResourceDefinition;
/** Initialise the basic resource definition used in tests */
@Before
public void setUp( )
{
this.basicResourceDefinition = this.createBasicResourceDefinition( "test" , "test" , "0" , "test" );
}
/**
* Try loading a valid top-level resource definition and make sure it's contents are correct
*
* @throws DataImportException
* if the resource definition check fails
*/
@Test
public void testLoadValidResources( )
throws DataImportException
{
String xml = this.createTopLevel( this.basicResourceDefinition );
Resources resources = this.createObject( xml , Resources.class );
int count = 0;
for ( BasicResource resource : resources ) {
resource.verifyData( );
count++;
}
assertEquals( 1 , count );
}
/**
* Try loading an empty top-level element
*
* @throws NullPointerException
* if the resource definition check fails
*/
@Test( expected = NullPointerException.class )
public void testLoadEmpty( )
throws NullPointerException
{
String xml = this.createTopLevel( );
Resources resources = this.createObject( xml , Resources.class );
resources.iterator( );
}
/**
* Test validating a valid top-level resource definition
*
* @throws DataImportException
* if the resource definition check fails
*/
@Test
public void testValidateOK( )
throws DataImportException
{
String xml = this.createTopLevel( this.basicResourceDefinition );
Resources resources = this.createObject( xml , Resources.class );
resources.verifyData( );
}
/**
* Try rejecting a top-level element that does not contain any resource definitions
*
* @throws DataImportException
* if the resource definition check fails
*/
@Test( expected = DataImportException.class )
public void testValidateEmpty( )
throws DataImportException
{
String xml = this.createTopLevel( );
Resources resources = this.createObject( xml , Resources.class );
resources.verifyData( );
}
/**
* Try rejecting a top-level element that contains an invalid resource definition
*
* @throws DataImportException
* if the resource definition check fails
*/
@Test( expected = DataImportException.class )
public void testValidateBadDefinition( )
throws DataImportException
{
String xml = this.createTopLevel( this.createBasicResourceDefinition( null , null , null , null ) );
Resources resources = this.createObject( xml , Resources.class );
resources.verifyData( );
}
/**
* Try rejecting a top-level element that contains two valid but duplicate resource definitions
*
* @throws DataImportException
* if the resource definition check fails
*/
@Test( expected = DataImportException.class )
public void testValidateDuplicateDefinition( )
throws DataImportException
{
String xml = this.createTopLevel( this.basicResourceDefinition , this.basicResourceDefinition );
Resources resources = this.createObject( xml , Resources.class );
resources.verifyData( );
}
}