2015-04-14 04:27:11 +02:00
package com.pahimar.ee3.test ;
2015-05-07 19:45:06 +02:00
import com.pahimar.ee3.api.exchange.EnergyValue ;
import com.pahimar.ee3.api.exchange.EnergyValueRegistryProxy ;
2015-04-14 04:27:11 +02:00
import com.pahimar.ee3.exchange.WrappedStack ;
import com.pahimar.ee3.util.LogHelper ;
import com.pahimar.ee3.util.SerializationHelper ;
2015-11-19 21:24:40 +01:00
import org.apache.logging.log4j.Marker ;
import org.apache.logging.log4j.MarkerManager ;
2015-04-14 04:27:11 +02:00
import java.io.File ;
import java.util.ArrayList ;
import java.util.List ;
import java.util.Map ;
import java.util.TreeMap ;
2015-11-19 21:24:40 +01:00
public class EnergyValueMappingsTestSuite {
public static final Marker TEST_MARKER = MarkerManager . getMarker ( " EE3_TEST " , LogHelper . MOD_MARKER ) ;
private static final Marker SUCCESS_MARKER = MarkerManager . getMarker ( " EE3_TEST_SUCCESS " , TEST_MARKER ) ;
private static final Marker FAILURE_MARKER = MarkerManager . getMarker ( " EE3_TEST_FAILURE " , TEST_MARKER ) ;
2015-04-14 04:27:11 +02:00
Map < WrappedStack , EnergyValue > testSuiteValueMap ;
public EnergyValueMappingsTestSuite ( )
{
testSuiteValueMap = new TreeMap < WrappedStack , EnergyValue > ( ) ;
}
public EnergyValueMappingsTestSuite ( File jsonFile )
{
if ( jsonFile ! = null & & jsonFile . exists ( ) & & jsonFile . isFile ( ) )
{
testSuiteValueMap = SerializationHelper . readEnergyValueStackMapFromJsonFile ( jsonFile ) ;
}
}
public void add ( Object object , Object value )
{
2015-04-19 21:01:35 +02:00
if ( WrappedStack . canBeWrapped ( object ) )
2015-04-14 04:27:11 +02:00
{
2015-04-19 21:01:35 +02:00
if ( value instanceof Number )
{
Number number = ( Number ) value ;
WrappedStack wrappedStack = WrappedStack . wrap ( object ) ;
wrappedStack . setStackSize ( 1 ) ;
testSuiteValueMap . put ( wrappedStack , new EnergyValue ( number . floatValue ( ) ) ) ;
}
else if ( value = = null )
{
WrappedStack wrappedStack = WrappedStack . wrap ( object ) ;
wrappedStack . setStackSize ( 1 ) ;
testSuiteValueMap . put ( wrappedStack , null ) ;
}
2015-04-14 04:27:11 +02:00
}
}
public void remove ( Object object )
{
if ( WrappedStack . canBeWrapped ( object ) )
{
2015-04-19 21:01:35 +02:00
WrappedStack wrappedStack = WrappedStack . wrap ( object ) ;
2015-04-14 04:27:11 +02:00
wrappedStack . setStackSize ( 1 ) ;
testSuiteValueMap . remove ( wrappedStack ) ;
}
}
public void loadTestSuite ( File jsonFile )
{
if ( jsonFile ! = null & & jsonFile . exists ( ) & & jsonFile . isFile ( ) )
{
testSuiteValueMap = SerializationHelper . readEnergyValueStackMapFromJsonFile ( jsonFile ) ;
}
}
public void saveTestSuite ( File jsonFile )
{
if ( jsonFile ! = null )
{
SerializationHelper . writeEnergyValueStackMapToJsonFile ( jsonFile , testSuiteValueMap ) ;
}
}
public void runTestSuite ( )
{
runTestSuite ( false ) ;
}
public void runTestSuite ( boolean strict )
{
List < String > successMessages = new ArrayList < String > ( ) ;
List < String > failureMessages = new ArrayList < String > ( ) ;
for ( WrappedStack wrappedStack : testSuiteValueMap . keySet ( ) )
{
EnergyValue registryEnergyValue = EnergyValueRegistryProxy . getEnergyValue ( wrappedStack , strict ) ;
EnergyValue testSuiteEnergryValue = testSuiteValueMap . get ( wrappedStack ) ;
if ( registryEnergyValue = = null & & testSuiteEnergryValue = = null )
{
/ * *
* Success - anticipated that no value was found and no value was found
* /
successMessages . add ( String . format ( " SUCCESS: Object '%s' had the expected energy value [Expected (%s), Found (%s)] " , wrappedStack , testSuiteEnergryValue , registryEnergyValue ) ) ;
}
else if ( registryEnergyValue = = null )
{
/ * *
* Failure - anticipated that a value would be found but no value was found
* /
failureMessages . add ( String . format ( " FAILURE: Object '%s' did not have the expected energy value [Expected (%s), Found (%s)] " , wrappedStack , testSuiteEnergryValue , registryEnergyValue ) ) ;
}
else if ( registryEnergyValue ! = null & & testSuiteEnergryValue ! = null )
{
if ( registryEnergyValue . equals ( testSuiteEnergryValue ) )
{
/ * *
* Success - anticipated that a specific value would be found and the anticipated value was found
* /
successMessages . add ( String . format ( " SUCCESS: Object '%s' had the expected energy value [Expected (%s), Found (%s)] " , wrappedStack , testSuiteEnergryValue , registryEnergyValue ) ) ;
}
else
{
/ * *
* Failure - anticipated that a specific value would be found and while a value was found it was not the anticipated one
* /
failureMessages . add ( String . format ( " FAILURE: Object '%s' did not have the expected energy value [Expected (%s), Found (%s)] " , wrappedStack , testSuiteEnergryValue , registryEnergyValue ) ) ;
}
}
}
for ( String successMessage : successMessages )
{
2015-11-19 21:24:40 +01:00
LogHelper . info ( SUCCESS_MARKER , successMessage ) ;
2015-04-14 04:27:11 +02:00
}
for ( String failureMessage : failureMessages )
{
2015-11-19 21:24:40 +01:00
LogHelper . warn ( FAILURE_MARKER , failureMessage ) ;
2015-04-14 04:27:11 +02:00
}
}
}