public class ExpressionParserTest extends TestCase {
public void testReturnValue()
{
ExpressionParser parser = null;
Boolean boolResult = null;
Integer intResult = null;
Double dblResult = null;
String strResult = null;
//boolean value
//Note: assignment from integer/double to boolean is not supported
//Note: Only the following constant is supported (case-sensitive): "true" and "false"
//set-constant-value (string)
parser = new ExpressionParser("true", Boolean.class);
try
{
boolResult = (Boolean)parser.evaluate();
Assert.assertNotNull(boolResult);
Assert.assertEquals(true, boolResult.booleanValue());
}
catch(Exception ex)
{
Assert.fail(ex.toString());
}
//set-constant-value (string)
parser = new ExpressionParser("false", Boolean.class);
try
{
boolResult = (Boolean)parser.evaluate();
Assert.assertNotNull(boolResult);
Assert.assertEquals(false, boolResult.booleanValue());
}
catch(Exception ex)
{
Assert.fail(ex.toString());
}
//number/integer value
//Note: no conversion from double is supported.
//set-constant-value (positive value)
parser = new ExpressionParser("15", Integer.class);
try
{
intResult = (Integer)parser.evaluate();
Assert.assertNotNull(intResult);
Assert.assertEquals(15, intResult.intValue());
}
catch(Exception ex)
{
Assert.fail(ex.toString());
}
//set-constant-value (zero)
parser = new ExpressionParser("0", Integer.class);
try
{
intResult = (Integer)parser.evaluate();
Assert.assertNotNull(intResult);
Assert.assertEquals(0, intResult.intValue());
}
catch(Exception ex)
{
Assert.fail(ex.toString());
}
//set-constant-value (negative value)
parser = new ExpressionParser("-15", Integer.class);
try
{
intResult = (Integer)parser.evaluate();
Assert.assertNotNull(intResult);
Assert.assertEquals(-15, intResult.intValue());
}
catch(Exception ex)
{
Assert.fail(ex.toString());
}
//double value
//Note: When doing double calculation, we must explicitly specify double value
// by using the decimal notation
//set-constant-value (positive value)
parser = new ExpressionParser("15.0", Double.class);
try
{
dblResult = (Double)parser.evaluate();
Assert.assertNotNull(dblResult);
Assert.assertEquals(15.0, dblResult.doubleValue());
}
catch(Exception ex)
{
Assert.fail(ex.toString());
}
//set-constant-value (zero)
parser = new ExpressionParser("0.0", Double.class);
try
{
dblResult = (Double)parser.evaluate();
Assert.assertNotNull(dblResult);
Assert.assertEquals(0.0, dblResult.doubleValue());
}
catch(Exception ex)
{
Assert.fail(ex.toString());
}
//set-constant-value (negative value)
parser = new ExpressionParser("-15.0", Double.class);
try
{
dblResult = (Double)parser.evaluate();
Assert.assertNotNull(dblResult);
Assert.assertEquals(-15.0, dblResult.doubleValue());
}
catch(Exception ex)
{
Assert.fail(ex.toString());
}
//string value
//Note: We must always escape the string argument with double quote
//set-constant-value
parser = new ExpressionParser("\"any-string\"", String.class);
try
{
strResult = (String)parser.evaluate();
Assert.assertNotNull(strResult);
Assert.assertTrue(strResult.compareTo("any-string") == 0);
}
catch(Exception ex)
{