pmap.put( "f.fl.int" , "100" );
pmap.put( "f.fl.float" , "10.6" );
pmap.put( "f.bad.bool" , "notbool" );
pmap.put( "f.bad.int" , "notint" );
pmap.put( "f.bad.float", "notfloat" );
final SolrParams params = new MapSolrParams( pmap );
// Test the string values we put in directly
assertEquals( "string" , params.get( "str" ) );
assertEquals( "true" , params.get( "bool" ) );
assertEquals( "100" , params.get( "int" ) );
assertEquals( "10.6" , params.get( "float" ) );
assertEquals( "string" , params.get( "f.fl.str" ) );
assertEquals( "true" , params.get( "f.fl.bool" ) );
assertEquals( "100" , params.get( "f.fl.int" ) );
assertEquals( "10.6" , params.get( "f.fl.float" ) );
assertEquals( "notbool" , params.get( "f.bad.bool" ) );
assertEquals( "notint" , params.get( "f.bad.int" ) );
assertEquals( "notfloat" , params.get( "f.bad.float" ) );
final String pstr = "string";
final Boolean pbool = Boolean.TRUE;
final Integer pint = new Integer( 100 );
final Float pfloat = new Float( 10.6f );
// Make sure they parse ok
assertEquals( pstr , params.get( "str" ) );
assertEquals( pbool , params.getBool( "bool" ) );
assertEquals( pint , params.getInt( "int" ) );
assertEquals( pfloat , params.getFloat( "float" ) );
assertEquals( pbool , params.getBool( "f.fl.bool" ) );
assertEquals( pint , params.getInt( "f.fl.int" ) );
assertEquals( pfloat , params.getFloat( "f.fl.float" ) );
assertEquals( pstr , params.getFieldParam( "fl", "str" ) );
assertEquals( pbool , params.getFieldBool( "fl", "bool" ) );
assertEquals( pint , params.getFieldInt( "fl", "int" ) );
assertEquals( pfloat , params.getFieldFloat( "fl", "float" ) );
// Test field defaulting (fall through to non-field-specific value)
assertEquals( pint , params.getFieldInt( "fff", "int" ) );
// test boolean parsing
for( int i=0; i<3; i++ ) {
// Must use Boolean rather than boolean reference value to prevent
// auto-unboxing ambiguity
assertEquals( Boolean.TRUE, params.getBool( "true-"+i ) );
assertEquals( Boolean.FALSE, params.getBool( "false-"+i ) );
}
// Malformed params: These should throw a 400
assertEquals( 400, getReturnCode( new Runnable() { public void run() { params.getInt( "f.bad.int" ); } } ) );
assertEquals( 400, getReturnCode( new Runnable() { public void run() { params.getBool( "f.bad.bool" ); } } ) );
assertEquals( 400, getReturnCode( new Runnable() { public void run() { params.getFloat( "f.bad.float" ); } } ) );
// Ask for params that arent there
assertNull( params.get( "asagdsaga" ) );
assertNull( params.getBool( "asagdsaga" ) );
assertNull( params.getInt( "asagdsaga" ) );
assertNull( params.getFloat( "asagdsaga" ) );
// Get things with defaults
assertEquals( pstr , params.get( "xxx", pstr ) );
assertEquals( pbool.booleanValue() , params.getBool( "xxx", pbool ) );
assertEquals( pint.intValue() , params.getInt( "xxx", pint ) );
assertEquals( pfloat.floatValue() , params.getFloat( "xxx", pfloat ) );
assertEquals( pbool.booleanValue() , params.getFieldBool( "xxx", "bool", pbool ) );
assertEquals( pint.intValue() , params.getFieldInt( "xxx", "int", pint ) );
assertEquals( pfloat.floatValue() , params.getFieldFloat("xxx", "float", pfloat ) );
assertEquals( pstr , params.getFieldParam("xxx", "str", pstr ) );
// Required params testing uses decorator
final SolrParams required = params.required();
// Required params which are present should test same as above
assertEquals( pstr , required.get( "str" ) );
assertEquals( pbool , required.getBool( "bool" ) );
assertEquals( pint , required.getInt( "int" ) );
assertEquals( pfloat , required.getFloat( "float" ) );
// field value present
assertEquals( pbool , required.getFieldBool( "fl", "bool" ) );
// field defaulting (fall through to non-field-specific value)
assertEquals( pstr , required.getFieldParams("fakefield", "str")[0] );
assertEquals( pstr , required.getFieldParam( "fakefield", "str" ) );
assertEquals( pbool , required.getFieldBool( "fakefield", "bool" ) );
assertEquals( pint , required.getFieldInt( "fakefield", "int" ) );
assertEquals( pfloat , required.getFieldFloat( "fakefield", "float" ) );
// Required params which are missing: These should throw a 400
assertEquals( 400, getReturnCode( new Runnable() { public void run() { required.get( "aaaa" ); } } ) );
assertEquals( 400, getReturnCode( new Runnable() { public void run() { required.getInt( "f.bad.int" ); } } ) );
assertEquals( 400, getReturnCode( new Runnable() { public void run() { required.getBool( "f.bad.bool" ); } } ) );
assertEquals( 400, getReturnCode( new Runnable() { public void run() { required.getFloat( "f.bad.float" ); } } ) );
assertEquals( 400, getReturnCode( new Runnable() { public void run() { required.getInt( "aaa" ); } } ) );
assertEquals( 400, getReturnCode( new Runnable() { public void run() { required.getBool( "aaa" ); } } ) );
assertEquals( 400, getReturnCode( new Runnable() { public void run() { required.getFloat( "aaa" ); } } ) );
assertEquals( 400, getReturnCode( new Runnable() { public void run() { params.getFieldBool( "bad", "bool" ); } } ) );
assertEquals( 400, getReturnCode( new Runnable() { public void run() { params.getFieldInt( "bad", "int" ); } } ) );
// Fields with default use their parent value:
assertEquals(
params.get( "aaaa", "str" ),
required.get( "aaaa", "str" ) );
assertEquals(
params.getInt( "f.bad.nnnn", pint ),
required.getInt( "f.bad.nnnn", pint ) );
// Check default SolrParams
Map<String,String> dmap = new HashMap<String, String>();
// these are not defined in params
dmap.put( "dstr" , "default" );
dmap.put( "dint" , "123" );
// these are defined in params
dmap.put( "int" , "456" );
SolrParams defaults = new DefaultSolrParams( params, new MapSolrParams( dmap ) );
// in params, not in default
assertEquals( pstr , defaults.get( "str" ) );
// in default, not in params
assertEquals( "default" , defaults.get( "dstr" ) );