Add SpEL support for float literals

This change ensures that SpEL expressions involving floats are
supported natively as opposed to the previous behavior which required
conversion to double, leading to potential downstream conversion
ambiguities.

Issue: SPR-9486
This commit is contained in:
Satyapal Reddy 2012-08-06 11:55:36 -07:00 committed by Chris Beams
parent 2ab2c2e9ce
commit be8f23d756
16 changed files with 524 additions and 63 deletions

View File

@ -0,0 +1,38 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.expression.spel.ast;
import org.springframework.expression.TypedValue;
/**
* Expression language AST node that represents a float literal.
*
* @author Satyapal Reddy
* @since 3.2
*/
public class FloatLiteral extends Literal {
private final TypedValue value;
FloatLiteral(String payload, int pos, float value) {
super(payload, pos);
this.value = new TypedValue(value);
}
@Override
public TypedValue getLiteralValue() {
return this.value;
}
}

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,11 +17,7 @@
package org.springframework.expression.spel.ast; package org.springframework.expression.spel.ast;
import org.springframework.expression.TypedValue; import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.ExpressionState; import org.springframework.expression.spel.*;
import org.springframework.expression.spel.SpelEvaluationException;
import org.springframework.expression.spel.SpelMessage;
import org.springframework.expression.spel.SpelParseException;
import org.springframework.expression.spel.InternalParseException;
/** /**
* Common superclass for nodes representing literals (boolean, string, number, etc). * Common superclass for nodes representing literals (boolean, string, number, etc).
@ -80,12 +76,12 @@ public abstract class Literal extends SpelNodeImpl {
} }
} }
// TODO should allow for 'f' for float, not just double
public static Literal getRealLiteral(String numberToken, int pos, boolean isFloat) { public static Literal getRealLiteral(String numberToken, int pos, boolean isFloat) {
try { try {
if (isFloat) { if (isFloat) {
float value = Float.parseFloat(numberToken); float value = Float.parseFloat(numberToken);
return new RealLiteral(numberToken, pos, value); return new FloatLiteral(numberToken, pos, value);
} else { } else {
double value = Double.parseDouble(numberToken); double value = Double.parseDouble(numberToken);
return new RealLiteral(numberToken, pos, value); return new RealLiteral(numberToken, pos, value);

View File

@ -43,8 +43,9 @@ public class OpDivide extends Operator {
Number op2 = (Number) operandTwo; Number op2 = (Number) operandTwo;
if (op1 instanceof Double || op2 instanceof Double) { if (op1 instanceof Double || op2 instanceof Double) {
return new TypedValue(op1.doubleValue() / op2.doubleValue()); return new TypedValue(op1.doubleValue() / op2.doubleValue());
} } else if (op1 instanceof Float || op2 instanceof Float) {
else if (op1 instanceof Long || op2 instanceof Long) { return new TypedValue(op1.floatValue() / op2.floatValue());
} else if (op1 instanceof Long || op2 instanceof Long) {
return new TypedValue(op1.longValue() / op2.longValue()); return new TypedValue(op1.longValue() / op2.longValue());
} }
else { else {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -41,6 +41,8 @@ public class OpEQ extends Operator {
Number op2 = (Number) right; Number op2 = (Number) right;
if (op1 instanceof Double || op2 instanceof Double) { if (op1 instanceof Double || op2 instanceof Double) {
return BooleanTypedValue.forValue(op1.doubleValue() == op2.doubleValue()); return BooleanTypedValue.forValue(op1.doubleValue() == op2.doubleValue());
} else if (op1 instanceof Float || op2 instanceof Float) {
return BooleanTypedValue.forValue(op1.floatValue() == op2.floatValue());
} else if (op1 instanceof Long || op2 instanceof Long) { } else if (op1 instanceof Long || op2 instanceof Long) {
return BooleanTypedValue.forValue(op1.longValue() == op2.longValue()); return BooleanTypedValue.forValue(op1.longValue() == op2.longValue());
} else { } else {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -40,6 +40,8 @@ public class OpGE extends Operator {
Number rightNumber = (Number) right; Number rightNumber = (Number) right;
if (leftNumber instanceof Double || rightNumber instanceof Double) { if (leftNumber instanceof Double || rightNumber instanceof Double) {
return BooleanTypedValue.forValue(leftNumber.doubleValue() >= rightNumber.doubleValue()); return BooleanTypedValue.forValue(leftNumber.doubleValue() >= rightNumber.doubleValue());
} else if (leftNumber instanceof Float || rightNumber instanceof Float) {
return BooleanTypedValue.forValue(leftNumber.floatValue() >= rightNumber.floatValue());
} else if (leftNumber instanceof Long || rightNumber instanceof Long) { } else if (leftNumber instanceof Long || rightNumber instanceof Long) {
return BooleanTypedValue.forValue( leftNumber.longValue() >= rightNumber.longValue()); return BooleanTypedValue.forValue( leftNumber.longValue() >= rightNumber.longValue());
} else { } else {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -41,6 +41,8 @@ public class OpLE extends Operator {
Number rightNumber = (Number) right; Number rightNumber = (Number) right;
if (leftNumber instanceof Double || rightNumber instanceof Double) { if (leftNumber instanceof Double || rightNumber instanceof Double) {
return BooleanTypedValue.forValue(leftNumber.doubleValue() <= rightNumber.doubleValue()); return BooleanTypedValue.forValue(leftNumber.doubleValue() <= rightNumber.doubleValue());
} else if (leftNumber instanceof Float || rightNumber instanceof Float) {
return BooleanTypedValue.forValue(leftNumber.floatValue() <= rightNumber.floatValue());
} else if (leftNumber instanceof Long || rightNumber instanceof Long) { } else if (leftNumber instanceof Long || rightNumber instanceof Long) {
return BooleanTypedValue.forValue(leftNumber.longValue() <= rightNumber.longValue()); return BooleanTypedValue.forValue(leftNumber.longValue() <= rightNumber.longValue());
} else { } else {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -42,6 +42,8 @@ public class OpLT extends Operator {
Number rightNumber = (Number) right; Number rightNumber = (Number) right;
if (leftNumber instanceof Double || rightNumber instanceof Double) { if (leftNumber instanceof Double || rightNumber instanceof Double) {
return BooleanTypedValue.forValue(leftNumber.doubleValue() < rightNumber.doubleValue()); return BooleanTypedValue.forValue(leftNumber.doubleValue() < rightNumber.doubleValue());
} else if (leftNumber instanceof Float || rightNumber instanceof Float) {
return BooleanTypedValue.forValue(leftNumber.floatValue() < rightNumber.floatValue());
} else if (leftNumber instanceof Long || rightNumber instanceof Long) { } else if (leftNumber instanceof Long || rightNumber instanceof Long) {
return BooleanTypedValue.forValue(leftNumber.longValue() < rightNumber.longValue()); return BooleanTypedValue.forValue(leftNumber.longValue() < rightNumber.longValue());
} else { } else {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -52,6 +52,8 @@ public class OpMinus extends Operator {
Number n = (Number) operand; Number n = (Number) operand;
if (operand instanceof Double) { if (operand instanceof Double) {
return new TypedValue(0 - n.doubleValue()); return new TypedValue(0 - n.doubleValue());
} else if (operand instanceof Float) {
return new TypedValue(0 - n.floatValue());
} else if (operand instanceof Long) { } else if (operand instanceof Long) {
return new TypedValue(0 - n.longValue()); return new TypedValue(0 - n.longValue());
} else { } else {
@ -67,6 +69,8 @@ public class OpMinus extends Operator {
Number op2 = (Number) right; Number op2 = (Number) right;
if (op1 instanceof Double || op2 instanceof Double) { if (op1 instanceof Double || op2 instanceof Double) {
return new TypedValue(op1.doubleValue() - op2.doubleValue()); return new TypedValue(op1.doubleValue() - op2.doubleValue());
} else if (op1 instanceof Float || op2 instanceof Float) {
return new TypedValue(op1.floatValue() - op2.floatValue());
} else if (op1 instanceof Long || op2 instanceof Long) { } else if (op1 instanceof Long || op2 instanceof Long) {
return new TypedValue(op1.longValue() - op2.longValue()); return new TypedValue(op1.longValue() - op2.longValue());
} else { } else {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -42,6 +42,8 @@ public class OpModulus extends Operator {
Number op2 = (Number) operandTwo; Number op2 = (Number) operandTwo;
if (op1 instanceof Double || op2 instanceof Double) { if (op1 instanceof Double || op2 instanceof Double) {
return new TypedValue(op1.doubleValue() % op2.doubleValue()); return new TypedValue(op1.doubleValue() % op2.doubleValue());
} else if (op1 instanceof Float || op2 instanceof Float) {
return new TypedValue(op1.floatValue() % op2.floatValue());
} else if (op1 instanceof Long || op2 instanceof Long) { } else if (op1 instanceof Long || op2 instanceof Long) {
return new TypedValue(op1.longValue() % op2.longValue()); return new TypedValue(op1.longValue() % op2.longValue());
} else { } else {

View File

@ -66,6 +66,9 @@ public class OpMultiply extends Operator {
if (leftNumber instanceof Double || rightNumber instanceof Double) { if (leftNumber instanceof Double || rightNumber instanceof Double) {
return new TypedValue(leftNumber.doubleValue() * rightNumber.doubleValue()); return new TypedValue(leftNumber.doubleValue() * rightNumber.doubleValue());
} }
else if (leftNumber instanceof Float || rightNumber instanceof Float) {
return new TypedValue(leftNumber.floatValue() * rightNumber.floatValue());
}
else if (leftNumber instanceof Long || rightNumber instanceof Long) { else if (leftNumber instanceof Long || rightNumber instanceof Long) {
return new TypedValue(leftNumber.longValue() * rightNumber.longValue()); return new TypedValue(leftNumber.longValue() * rightNumber.longValue());
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -41,6 +41,8 @@ public class OpNE extends Operator {
Number op2 = (Number) right; Number op2 = (Number) right;
if (op1 instanceof Double || op2 instanceof Double) { if (op1 instanceof Double || op2 instanceof Double) {
return BooleanTypedValue.forValue(op1.doubleValue() != op2.doubleValue()); return BooleanTypedValue.forValue(op1.doubleValue() != op2.doubleValue());
} else if (op1 instanceof Float || op2 instanceof Float) {
return BooleanTypedValue.forValue(op1.floatValue() != op2.floatValue());
} else if (op1 instanceof Long || op2 instanceof Long) { } else if (op1 instanceof Long || op2 instanceof Long) {
return BooleanTypedValue.forValue(op1.longValue() != op2.longValue()); return BooleanTypedValue.forValue(op1.longValue() != op2.longValue());
} else { } else {

View File

@ -55,6 +55,8 @@ public class OpPlus extends Operator {
if (operandOne instanceof Number) { if (operandOne instanceof Number) {
if (operandOne instanceof Double || operandOne instanceof Long) { if (operandOne instanceof Double || operandOne instanceof Long) {
return new TypedValue(operandOne); return new TypedValue(operandOne);
} else if (operandOne instanceof Float) {
return new TypedValue(((Number) operandOne).floatValue());
} else { } else {
return new TypedValue(((Number) operandOne).intValue()); return new TypedValue(((Number) operandOne).intValue());
} }
@ -73,6 +75,8 @@ public class OpPlus extends Operator {
Number op2 = (Number) operandTwo; Number op2 = (Number) operandTwo;
if (op1 instanceof Double || op2 instanceof Double) { if (op1 instanceof Double || op2 instanceof Double) {
return new TypedValue(op1.doubleValue() + op2.doubleValue()); return new TypedValue(op1.doubleValue() + op2.doubleValue());
} else if (op1 instanceof Float || op2 instanceof Float) {
return new TypedValue(op1.floatValue() + op2.floatValue());
} else if (op1 instanceof Long || op2 instanceof Long) { } else if (op1 instanceof Long || op2 instanceof Long) {
return new TypedValue(op1.longValue() + op2.longValue()); return new TypedValue(op1.longValue() + op2.longValue());
} else { // TODO what about overflow? } else { // TODO what about overflow?

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -45,6 +45,8 @@ public class OperatorPower extends Operator {
Number op2 = (Number) operandTwo; Number op2 = (Number) operandTwo;
if (op1 instanceof Double || op2 instanceof Double) { if (op1 instanceof Double || op2 instanceof Double) {
return new TypedValue(Math.pow(op1.doubleValue(),op2.doubleValue())); return new TypedValue(Math.pow(op1.doubleValue(),op2.doubleValue()));
} else if (op1 instanceof Float || op2 instanceof Float) {
return new TypedValue(Math.pow(op1.floatValue(), op2.floatValue()));
} else if (op1 instanceof Long || op2 instanceof Long) { } else if (op1 instanceof Long || op2 instanceof Long) {
double d= Math.pow(op1.longValue(), op2.longValue()); double d= Math.pow(op1.longValue(), op2.longValue());
return new TypedValue((long)d); return new TypedValue((long)d);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,10 +17,9 @@
package org.springframework.expression.spel; package org.springframework.expression.spel;
import junit.framework.Assert; import junit.framework.Assert;
import org.junit.Test; import org.junit.Test;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.standard.SpelExpression; import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.expression.spel.support.StandardEvaluationContext;
/** /**
* Tests the evaluation of basic literals: boolean, integer, hex integer, long, real, null, date * Tests the evaluation of basic literals: boolean, integer, hex integer, long, real, null, date
@ -126,12 +125,12 @@ public class LiteralTests extends ExpressionTestCase {
@Test @Test
public void testLiteralReal02_CreatingFloats() { public void testLiteralReal02_CreatingFloats() {
// For now, everything becomes a double... // For now, everything becomes a double...
evaluate("1.25f", 1.25d, Double.class); evaluate("1.25f", 1.25f, Float.class);
evaluate("2.5f", 2.5d, Double.class); evaluate("2.5f", 2.5f, Float.class);
evaluate("-3.5f", -3.5d, Double.class); evaluate("-3.5f", -3.5f, Float.class);
evaluate("1.25F", 1.25d, Double.class); evaluate("1.25F", 1.25f, Float.class);
evaluate("2.5F", 2.5d, Double.class); evaluate("2.5F", 2.5f, Float.class);
evaluate("-3.5F", -3.5d, Double.class); evaluate("-3.5F", -3.5f, Float.class);
} }
@Test @Test
@ -140,7 +139,7 @@ public class LiteralTests extends ExpressionTestCase {
evaluate("6.0221415e+23", "6.0221415E23", Double.class); evaluate("6.0221415e+23", "6.0221415E23", Double.class);
evaluate("6.0221415E+23d", "6.0221415E23", Double.class); evaluate("6.0221415E+23d", "6.0221415E23", Double.class);
evaluate("6.0221415e+23D", "6.0221415E23", Double.class); evaluate("6.0221415e+23D", "6.0221415E23", Double.class);
evaluate("6E2f", 600.0d, Double.class); evaluate("6E2f", 6E2f, Float.class);
} }
@Test @Test

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,7 +17,6 @@
package org.springframework.expression.spel; package org.springframework.expression.spel;
import junit.framework.Assert; import junit.framework.Assert;
import org.junit.Test; import org.junit.Test;
import org.springframework.expression.spel.ast.Operator; import org.springframework.expression.spel.ast.Operator;
import org.springframework.expression.spel.standard.SpelExpression; import org.springframework.expression.spel.standard.SpelExpression;
@ -187,7 +186,7 @@ public class OperatorTests extends ExpressionTestCase {
evaluate("5 - 4", "1", Integer.class); evaluate("5 - 4", "1", Integer.class);
evaluate("3 * 5", 15, Integer.class); evaluate("3 * 5", 15, Integer.class);
evaluate("3.2d * 5", 16.0d, Double.class); evaluate("3.2d * 5", 16.0d, Double.class);
evaluate("3 * 5f", 15d, Double.class); evaluate("3 * 5f", 15f, Float.class);
evaluate("3 / 1", 3, Integer.class); evaluate("3 / 1", 3, Integer.class);
evaluate("3 % 2", 1, Integer.class); evaluate("3 % 2", 1, Integer.class);
evaluate("3 mod 2", 1, Integer.class); evaluate("3 mod 2", 1, Integer.class);
@ -199,7 +198,7 @@ public class OperatorTests extends ExpressionTestCase {
@Test @Test
public void testPlus() throws Exception { public void testPlus() throws Exception {
evaluate("7 + 2", "9", Integer.class); evaluate("7 + 2", "9", Integer.class);
evaluate("3.0f + 5.0f", 8.0d, Double.class); evaluate("3.0f + 5.0f", 8.0f, Float.class);
evaluate("3.0d + 5.0d", 8.0d, Double.class); evaluate("3.0d + 5.0d", 8.0d, Double.class);
evaluate("'ab' + 2", "ab2", String.class); evaluate("'ab' + 2", "ab2", String.class);
@ -229,7 +228,7 @@ public class OperatorTests extends ExpressionTestCase {
@Test @Test
public void testMinus() throws Exception { public void testMinus() throws Exception {
evaluate("'c' - 2", "a", String.class); evaluate("'c' - 2", "a", String.class);
evaluate("3.0f - 5.0f", -2.0d, Double.class); evaluate("3.0f - 5.0f", -2.0f, Float.class);
evaluateAndCheckError("'ab' - 2", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES); evaluateAndCheckError("'ab' - 2", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
evaluateAndCheckError("2-'ab'",SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES); evaluateAndCheckError("2-'ab'",SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
SpelExpression expr = (SpelExpression)parser.parseExpression("-3"); SpelExpression expr = (SpelExpression)parser.parseExpression("-3");
@ -247,16 +246,16 @@ public class OperatorTests extends ExpressionTestCase {
public void testModulus() { public void testModulus() {
evaluate("3%2",1,Integer.class); evaluate("3%2",1,Integer.class);
evaluate("3L%2L",1L,Long.class); evaluate("3L%2L",1L,Long.class);
evaluate("3.0f%2.0f",1d,Double.class); evaluate("3.0f%2.0f",1f,Float.class);
evaluate("5.0d % 3.1d", 1.9d, Double.class); evaluate("5.0d % 3.1d", 1.9d, Double.class);
evaluateAndCheckError("'abc'%'def'",SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES); evaluateAndCheckError("'abc'%'def'",SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
} }
@Test @Test
public void testDivide() { public void testDivide() {
evaluate("3.0f / 5.0f", 0.6d, Double.class); evaluate("3.0f / 5.0f", 0.6f, Float.class);
evaluate("4L/2L",2L,Long.class); evaluate("4L/2L",2L,Long.class);
evaluate("3.0f div 5.0f", 0.6d, Double.class); evaluate("3.0f div 5.0f", 0.6f, Float.class);
evaluate("4L DIV 2L",2L,Long.class); evaluate("4L DIV 2L",2L,Long.class);
evaluateAndCheckError("'abc'/'def'",SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES); evaluateAndCheckError("'abc'/'def'",SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
} }
@ -350,9 +349,9 @@ public class OperatorTests extends ExpressionTestCase {
public void testMixedOperands_DoublesAndInts() { public void testMixedOperands_DoublesAndInts() {
evaluate("3.0d + 5", 8.0d, Double.class); evaluate("3.0d + 5", 8.0d, Double.class);
evaluate("3.0D - 5", -2.0d, Double.class); evaluate("3.0D - 5", -2.0d, Double.class);
evaluate("3.0f * 5", 15.0d, Double.class); evaluate("3.0f * 5", 15.0f, Float.class);
evaluate("6.0f / 2", 3.0, Double.class); evaluate("6.0f / 2", 3.0f, Float.class);
evaluate("6.0f / 4", 1.5d, Double.class); evaluate("6.0f / 4", 1.5f, Float.class);
evaluate("5.0D % 3", 2.0d, Double.class); evaluate("5.0D % 3", 2.0d, Double.class);
evaluate("5.5D % 3", 2.5, Double.class); evaluate("5.5D % 3", 2.5, Double.class);
} }

View File

@ -16,34 +16,11 @@
package org.springframework.expression.spel; package org.springframework.expression.spel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import junit.framework.Assert; import junit.framework.Assert;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.AccessException; import org.springframework.expression.*;
import org.springframework.expression.BeanResolver;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.MethodExecutor;
import org.springframework.expression.MethodResolver;
import org.springframework.expression.ParserContext;
import org.springframework.expression.PropertyAccessor;
import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.standard.SpelExpression; import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.ReflectiveMethodResolver; import org.springframework.expression.spel.support.ReflectiveMethodResolver;
@ -51,6 +28,13 @@ import org.springframework.expression.spel.support.ReflectivePropertyAccessor;
import org.springframework.expression.spel.support.StandardEvaluationContext; import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.support.StandardTypeLocator; import org.springframework.expression.spel.support.StandardTypeLocator;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
/** /**
* Tests based on Jiras up to the release of Spring 3.0.0 * Tests based on Jiras up to the release of Spring 3.0.0
* *
@ -1204,6 +1188,425 @@ public class SpringEL300Tests extends ExpressionTestCase {
assertEquals("Equal assertion failed: ", "class [[I", result.toString()); assertEquals("Equal assertion failed: ", "class [[I", result.toString());
} }
@Test
public void SPR_9486_floatFunctionResolverTest() {
try {
Number expectedResult = Math.abs(-10.2f);
ExpressionParser parser = new SpelExpressionParser();
SPR_9486_FunctionsClass testObject = new SPR_9486_FunctionsClass();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("abs(-10.2f)");
Number result = expression.getValue(context, testObject, Number.class);
Assert.assertEquals("Equal assertion failed for SPR_9486_floatFunctionResolverTest Test: ", expectedResult, result);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Test failed - SPR_9486_floatFunctionResolverTest");
}
}
class SPR_9486_FunctionsClass {
public int abs(int value) {
return Math.abs(value);
}
public float abs(float value) {
return Math.abs(value);
}
}
@Test
public void SPR_9486_addFloatWithDoubleTest() {
try {
Number expectedNumber = 10.21f + 10.2;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.21f + 10.2");
Number result = expression.getValue(context, null, Number.class);
Assert.assertEquals("Equal assertion failed for SPR_9486_addFloatWithDoubleTest Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Test failed - SPR_9486_addFloatWithDoubleTest");
}
}
@Test
public void SPR_9486_addFloatWithFloatTest() {
try {
Number expectedNumber = 10.21f + 10.2f;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.21f + 10.2f");
Number result = expression.getValue(context, null, Number.class);
Assert.assertEquals("Equal assertion failed for SPR_9486_addFloatWithFloatTest Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Test failed - SPR_9486_addFloatWithFloatTest");
}
}
@Test
public void SPR_9486_subtractFloatWithDoubleTest() {
try {
Number expectedNumber = 10.21f - 10.2;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.21f - 10.2");
Number result = expression.getValue(context, null, Number.class);
Assert.assertEquals("Equal assertion failed for SPR_9486_subtractFloatWithDoubleTest Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Test failed - SPR_9486_subtractFloatWithDoubleTest");
}
}
@Test
public void SPR_9486_subtractFloatWithFloatTest() {
try {
Number expectedNumber = 10.21f - 10.2f;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.21f - 10.2f");
Number result = expression.getValue(context, null, Number.class);
Assert.assertEquals("Equal assertion failed for SPR_9486_subtractFloatWithFloatTest Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Test failed - SPR_9486_subtractFloatWithFloatTest");
}
}
@Test
public void SPR_9486_multiplyFloatWithDoubleTest() {
try {
Number expectedNumber = 10.21f * 10.2;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.21f * 10.2");
Number result = expression.getValue(context, null, Number.class);
Assert.assertEquals("Equal assertion failed for float multiplied by double Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Test failed - SPR_9486_multiplyFloatWithDoubleTest");
}
}
@Test
public void SPR_9486_multiplyFloatWithFloatTest() {
try {
Number expectedNumber = 10.21f * 10.2f;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.21f * 10.2f");
Number result = expression.getValue(context, null, Number.class);
Assert.assertEquals("Equal assertion failed for float multiply by another float Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Test failed - SPR_9486_multiplyFloatWithFloatTest");
}
}
@Test
public void SPR_9486_floatDivideByFloatTest() {
try {
Number expectedNumber = -10.21f/-10.2f;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("-10.21f / -10.2f");
Number result = expression.getValue(context, null, Number.class);
Assert.assertEquals("Equal assertion failed for float divide Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Test failed - SPR_9486_floatDivideByFloatTest");
}
}
@Test
public void SPR_9486_floatDivideByDoubleTest() {
try {
Number expectedNumber = -10.21f/-10.2;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("-10.21f / -10.2");
Number result = expression.getValue(context, null, Number.class);
Assert.assertEquals("Equal assertion failed for float divide Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Test failed - SPR_9486_floatDivideByDoubleTest");
}
}
@Test
public void SPR_9486_floatEqFloatUnaryMinusTest() {
try {
Boolean expectedResult = -10.21f == -10.2f;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("-10.21f == -10.2f");
Boolean result = expression.getValue(context, null, Boolean.class);
Assert.assertEquals("Equal assertion failed for SPR_9486_floatEqFloatUnaryMinusTest Test: ", expectedResult, result);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Test failed - SPR_9486_floatEqFloatUnaryMinusTest");
}
}
@Test
public void SPR_9486_floatEqDoubleUnaryMinusTest() {
try {
Boolean expectedResult = -10.21f == -10.2;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("-10.21f == -10.2");
Boolean result = expression.getValue(context, null, Boolean.class);
Assert.assertEquals("Equal assertion failed for SPR_9486_floatEqDoubleUnaryMinusTest Test: ", expectedResult, result);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Test failed - SPR_9486_floatEqDoubleUnaryMinusTest");
}
}
@Test
public void SPR_9486_floatEqFloatTest() {
try {
Boolean expectedResult = 10.215f == 10.2109f;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.215f == 10.2109f");
Boolean result = expression.getValue(context, null, Boolean.class);
Assert.assertEquals("Equal assertion failed for SPR_9486_floatEqFloatTest Test: ", expectedResult, result);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Test failed - SPR_9486_floatEqFloatTest");
}
}
@Test
public void SPR_9486_floatEqDoubleTest() {
try {
Boolean expectedResult = 10.215f == 10.2109;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.215f == 10.2109");
Boolean result = expression.getValue(context, null, Boolean.class);
Assert.assertEquals("Equal assertion failed for SPR_9486_floatEqDoubleTest() Test: ", expectedResult, result);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Test failed - SPR_9486_floatEqDoubleTest()");
}
}
@Test
public void SPR_9486_floatNotEqFloatTest() {
try {
Boolean expectedResult = 10.215f != 10.2109f;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.215f != 10.2109f");
Boolean result = expression.getValue(context, null, Boolean.class);
Assert.assertEquals("Equal assertion failed for SPR_9486_floatEqFloatTest Test: ", expectedResult, result);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Test failed - SPR_9486_floatEqFloatTest");
}
}
@Test
public void SPR_9486_floatNotEqDoubleTest() {
try {
Boolean expectedResult = 10.215f != 10.2109;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.215f != 10.2109");
Boolean result = expression.getValue(context, null, Boolean.class);
Assert.assertEquals("Equal assertion failed for SPR_9486_floatNotEqDoubleTest Test: ", expectedResult, result);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Test failed - SPR_9486_floatNotEqDoubleTest");
}
}
@Test
public void SPR_9486_floatLessThanFloatTest() {
try {
Boolean expectedNumber = -10.21f < -10.2f;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("-10.21f < -10.2f");
Boolean result = expression.getValue(context, null, Boolean.class);
Assert.assertEquals("Equal assertion failed for SPR_9486_floatLessThanFloatTest Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Test failed - SPR_9486_floatLessThanFloatTest()");
}
}
@Test
public void SPR_9486_floatLessThanDoubleTest() {
try {
Boolean expectedNumber = -10.21f < -10.2;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("-10.21f < -10.2");
Boolean result = expression.getValue(context, null, Boolean.class);
Assert.assertEquals("Equal assertion failed for SPR_9486_floatLessThanDoubleTest Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Test failed - SPR_9486_floatLessThanDoubleTest()");
}
}
@Test
public void SPR_9486_floatLessThanOrEqualFloatTest() {
try {
Boolean expectedNumber = -10.21f <= -10.22f;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("-10.21f <= -10.22f");
Boolean result = expression.getValue(context, null, Boolean.class);
Assert.assertEquals("Equal assertion failed for SPR_9486_floatLessThanOrEqualFloatTest Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Test failed - SPR_9486_floatLessThanOrEqualFloatTest");
}
}
@Test
public void SPR_9486_floatLessThanOrEqualDoubleTest() {
try {
Boolean expectedNumber = -10.21f <= -10.2;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("-10.21f <= -10.2");
Boolean result = expression.getValue(context, null, Boolean.class);
Assert.assertEquals("Equal assertion failed for SPR_9486_floatLessThanOrEqualDoubleTest Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Test failed - SPR_9486_floatLessThanOrEqualDoubleTest");
}
}
@Test
public void SPR_9486_floatGreaterThanFloatTest() {
try {
Boolean expectedNumber = -10.21f > -10.2f;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("-10.21f > -10.2f");
Boolean result = expression.getValue(context, null, Boolean.class);
Assert.assertEquals("Equal assertion failed for SPR_9486_floatGreaterThanFloatTest Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Test failed - SPR_9486_floatGreaterThanTest");
}
}
@Test
public void SPR_9486_floatGreaterThanDoubleTest() {
try {
Boolean expectedResult = -10.21f > -10.2;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("-10.21f > -10.2");
Boolean result = expression.getValue(context, null, Boolean.class);
Assert.assertEquals("Equal assertion failed for SPR_9486_floatGreaterThanDoubleTest Test: ", expectedResult, result);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Test failed - SPR_9486_floatGreaterThanTest");
}
}
@Test
public void SPR_9486_floatGreaterThanOrEqualFloatTest() {
try {
Boolean expectedNumber = -10.21f >= -10.2f;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("-10.21f >= -10.2f");
Boolean result = expression.getValue(context, null, Boolean.class);
Assert.assertEquals("Equal assertion failed for SPR_9486_floatGreaterThanFloatTest Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Test failed - SPR_9486_floatGreaterThanTest");
}
}
@Test
public void SPR_9486_floatGreaterThanEqualDoubleTest() {
try {
Boolean expectedResult = -10.21f >= -10.2;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("-10.21f >= -10.2");
Boolean result = expression.getValue(context, null, Boolean.class);
Assert.assertEquals("Equal assertion failed for SPR_9486_floatGreaterThanDoubleTest Test: ", expectedResult, result);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Test failed - SPR_9486_floatGreaterThanTest");
}
}
@Test
public void SPR_9486_floatModulusFloatTest() {
try {
Number expectedResult = 10.21f % 10.2f;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.21f % 10.2f");
Number result = expression.getValue(context, null, Number.class);
Assert.assertEquals("Equal assertion failed for SPR_9486_floatModulusFloatTest Test: ", expectedResult, result);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Test failed - SPR_9486_floatModulusFloatTest");
}
}
@Test
public void SPR_9486_floatModulusDoubleTest() {
try {
Number expectedResult = 10.21f % 10.2;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.21f % 10.2");
Number result = expression.getValue(context, null, Number.class);
Assert.assertEquals("Equal assertion failed for SPR_9486_floatModulusDoubleTest Test: ", expectedResult, result);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Test failed - SPR_9486_floatModulusDoubleTest");
}
}
@Test
public void SPR_9486_floatPowerFloatTest() {
try {
Number expectedResult = Math.pow(10.21f, -10.2f);
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.21f ^ -10.2f");
Number result = expression.getValue(context, null, Number.class);
Assert.assertEquals("Equal assertion failed for SPR_9486_floatPowerFloatTest Test: ", expectedResult, result);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Test failed - SPR_9486_floatPowerFloatTest");
}
}
@Test
public void SPR_9486_floatPowerDoubleTest() {
try {
Number expectedResult = Math.pow(10.21f, 10.2);
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.21f ^ 10.2");
Number result = expression.getValue(context, null, Number.class);
Assert.assertEquals("Equal assertion failed for SPR_9486_floatPowerDoubleTest Test: ", expectedResult, result);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Test failed - SPR_9486_floatPowerDoubleTest");
}
}
} }