Browse Source

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
Backport-Commit: be8f23d756
3.1.x
Satyapal Reddy 14 years ago committed by Chris Beams
parent
commit
a94677e7b6
  1. 38
      org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/FloatLiteral.java
  2. 12
      org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/Literal.java
  3. 5
      org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/OpDivide.java
  4. 4
      org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/OpEQ.java
  5. 4
      org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/OpGE.java
  6. 4
      org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/OpLE.java
  7. 4
      org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/OpLT.java
  8. 6
      org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/OpMinus.java
  9. 4
      org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/OpModulus.java
  10. 3
      org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/OpMultiply.java
  11. 4
      org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/OpNE.java
  12. 66
      org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/OpPlus.java
  13. 4
      org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/OperatorPower.java
  14. 19
      org.springframework.expression/src/test/java/org/springframework/expression/spel/LiteralTests.java
  15. 21
      org.springframework.expression/src/test/java/org/springframework/expression/spel/OperatorTests.java
  16. 482
      org.springframework.expression/src/test/java/org/springframework/expression/spel/SpringEL300Tests.java

38
org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/FloatLiteral.java

@ -0,0 +1,38 @@ @@ -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;
}
}

12
org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/Literal.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -17,11 +17,7 @@ @@ -17,11 +17,7 @@
package org.springframework.expression.spel.ast;
import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.ExpressionState;
import org.springframework.expression.spel.SpelEvaluationException;
import org.springframework.expression.spel.SpelMessage;
import org.springframework.expression.spel.SpelParseException;
import org.springframework.expression.spel.InternalParseException;
import org.springframework.expression.spel.*;
/**
* Common superclass for nodes representing literals (boolean, string, number, etc).
@ -80,12 +76,12 @@ public abstract class Literal extends SpelNodeImpl { @@ -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) {
try {
if (isFloat) {
float value = Float.parseFloat(numberToken);
return new RealLiteral(numberToken, pos, value);
return new FloatLiteral(numberToken, pos, value);
} else {
double value = Double.parseDouble(numberToken);
return new RealLiteral(numberToken, pos, value);

5
org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/OpDivide.java

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

4
org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/OpEQ.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -41,6 +41,8 @@ public class OpEQ extends Operator { @@ -41,6 +41,8 @@ public class OpEQ extends Operator {
Number op2 = (Number) right;
if (op1 instanceof Double || op2 instanceof Double) {
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) {
return BooleanTypedValue.forValue(op1.longValue() == op2.longValue());
} else {

4
org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/OpGE.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -40,6 +40,8 @@ public class OpGE extends Operator { @@ -40,6 +40,8 @@ public class OpGE extends Operator {
Number rightNumber = (Number) right;
if (leftNumber instanceof Double || rightNumber instanceof Double) {
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) {
return BooleanTypedValue.forValue( leftNumber.longValue() >= rightNumber.longValue());
} else {

4
org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/OpLE.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -41,6 +41,8 @@ public class OpLE extends Operator { @@ -41,6 +41,8 @@ public class OpLE extends Operator {
Number rightNumber = (Number) right;
if (leftNumber instanceof Double || rightNumber instanceof Double) {
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) {
return BooleanTypedValue.forValue(leftNumber.longValue() <= rightNumber.longValue());
} else {

4
org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/OpLT.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -42,6 +42,8 @@ public class OpLT extends Operator { @@ -42,6 +42,8 @@ public class OpLT extends Operator {
Number rightNumber = (Number) right;
if (leftNumber instanceof Double || rightNumber instanceof Double) {
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) {
return BooleanTypedValue.forValue(leftNumber.longValue() < rightNumber.longValue());
} else {

6
org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/OpMinus.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -52,6 +52,8 @@ public class OpMinus extends Operator { @@ -52,6 +52,8 @@ public class OpMinus extends Operator {
Number n = (Number) operand;
if (operand instanceof Double) {
return new TypedValue(0 - n.doubleValue());
} else if (operand instanceof Float) {
return new TypedValue(0 - n.floatValue());
} else if (operand instanceof Long) {
return new TypedValue(0 - n.longValue());
} else {
@ -67,6 +69,8 @@ public class OpMinus extends Operator { @@ -67,6 +69,8 @@ public class OpMinus extends Operator {
Number op2 = (Number) right;
if (op1 instanceof Double || op2 instanceof Double) {
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) {
return new TypedValue(op1.longValue() - op2.longValue());
} else {

4
org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/OpModulus.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -42,6 +42,8 @@ public class OpModulus extends Operator { @@ -42,6 +42,8 @@ public class OpModulus extends Operator {
Number op2 = (Number) operandTwo;
if (op1 instanceof Double || op2 instanceof Double) {
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) {
return new TypedValue(op1.longValue() % op2.longValue());
} else {

3
org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/OpMultiply.java

@ -66,6 +66,9 @@ public class OpMultiply extends Operator { @@ -66,6 +66,9 @@ public class OpMultiply extends Operator {
if (leftNumber instanceof Double || rightNumber instanceof Double) {
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) {
return new TypedValue(leftNumber.longValue() * rightNumber.longValue());
}

4
org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/OpNE.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -41,6 +41,8 @@ public class OpNE extends Operator { @@ -41,6 +41,8 @@ public class OpNE extends Operator {
Number op2 = (Number) right;
if (op1 instanceof Double || op2 instanceof Double) {
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) {
return BooleanTypedValue.forValue(op1.longValue() != op2.longValue());
} else {

66
org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/OpPlus.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -16,10 +16,13 @@ @@ -16,10 +16,13 @@
package org.springframework.expression.spel.ast;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.Operation;
import org.springframework.expression.TypeConverter;
import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.ExpressionState;
import org.springframework.util.Assert;
/**
* The plus operator will:
@ -31,14 +34,16 @@ import org.springframework.expression.spel.ExpressionState; @@ -31,14 +34,16 @@ import org.springframework.expression.spel.ExpressionState;
* </ul>
* It can be used as a unary operator for numbers (double/long/int). The standard promotions are performed
* when the operand types vary (double+int=double). For other options it defers to the registered overloader.
*
*
* @author Andy Clement
* @author Ivo Smid
* @since 3.0
*/
public class OpPlus extends Operator {
public OpPlus(int pos, SpelNodeImpl... operands) {
super("+", pos, operands);
Assert.notEmpty(operands);
}
@Override
@ -48,24 +53,30 @@ public class OpPlus extends Operator { @@ -48,24 +53,30 @@ public class OpPlus extends Operator {
if (rightOp == null) { // If only one operand, then this is unary plus
Object operandOne = leftOp.getValueInternal(state).getValue();
if (operandOne instanceof Number) {
if (operandOne instanceof Double) {
return new TypedValue(((Double) operandOne).doubleValue());
} else if (operandOne instanceof Long) {
return new TypedValue(((Long) operandOne).longValue());
if (operandOne instanceof Double || operandOne instanceof Long) {
return new TypedValue(operandOne);
} else if (operandOne instanceof Float) {
return new TypedValue(((Number) operandOne).floatValue());
} else {
return new TypedValue(((Integer) operandOne).intValue());
return new TypedValue(((Number) operandOne).intValue());
}
}
return state.operate(Operation.ADD, operandOne, null);
}
else {
Object operandOne = leftOp.getValueInternal(state).getValue();
Object operandTwo = rightOp.getValueInternal(state).getValue();
final TypedValue operandOneValue = leftOp.getValueInternal(state);
final Object operandOne = operandOneValue.getValue();
final TypedValue operandTwoValue = rightOp.getValueInternal(state);
final Object operandTwo = operandTwoValue.getValue();
if (operandOne instanceof Number && operandTwo instanceof Number) {
Number op1 = (Number) operandOne;
Number op2 = (Number) operandTwo;
if (op1 instanceof Double || op2 instanceof Double) {
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) {
return new TypedValue(op1.longValue() + op2.longValue());
} else { // TODO what about overflow?
@ -74,13 +85,14 @@ public class OpPlus extends Operator { @@ -74,13 +85,14 @@ public class OpPlus extends Operator {
} else if (operandOne instanceof String && operandTwo instanceof String) {
return new TypedValue(new StringBuilder((String) operandOne).append((String) operandTwo).toString());
} else if (operandOne instanceof String) {
StringBuilder result = new StringBuilder((String)operandOne);
result.append((operandTwo==null?"null":operandTwo.toString()));
return new TypedValue(result.toString());
StringBuilder result = new StringBuilder((String) operandOne);
result.append((operandTwo == null ? "null" : convertTypedValueToString(operandTwoValue, state)));
return new TypedValue(result.toString());
} else if (operandTwo instanceof String) {
StringBuilder result = new StringBuilder((operandOne==null?"null":operandOne.toString()));
result.append((String)operandTwo);
return new TypedValue(result.toString());
StringBuilder result = new StringBuilder((operandOne == null ? "null" : convertTypedValueToString(
operandOneValue, state)));
result.append((String) operandTwo);
return new TypedValue(result.toString());
}
return state.operate(Operation.ADD, operandOne, operandTwo);
}
@ -94,10 +106,32 @@ public class OpPlus extends Operator { @@ -94,10 +106,32 @@ public class OpPlus extends Operator {
return super.toStringAST();
}
@Override
public SpelNodeImpl getRightOperand() {
if (children.length<2) {return null;}
if (children.length < 2) {
return null;
}
return children[1];
}
/**
* Convert operand value to string using registered converter or using
* {@code toString} method.
*
* @param value typed value to be converted
* @param state expression state
* @return {@code TypedValue} instance converted to {@code String}
*/
private static String convertTypedValueToString(TypedValue value, ExpressionState state) {
final TypeConverter typeConverter = state.getEvaluationContext().getTypeConverter();
final TypeDescriptor typeDescriptor = TypeDescriptor.valueOf(String.class);
if (typeConverter.canConvert(value.getTypeDescriptor(), typeDescriptor)) {
final Object obj = typeConverter.convertValue(value.getValue(), value.getTypeDescriptor(), typeDescriptor);
return String.valueOf(obj);
} else {
return String.valueOf(value.getValue());
}
}
}

4
org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/OperatorPower.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -45,6 +45,8 @@ public class OperatorPower extends Operator { @@ -45,6 +45,8 @@ public class OperatorPower extends Operator {
Number op2 = (Number) operandTwo;
if (op1 instanceof Double || op2 instanceof Double) {
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) {
double d= Math.pow(op1.longValue(), op2.longValue());
return new TypedValue((long)d);

19
org.springframework.expression/src/test/java/org/springframework/expression/spel/LiteralTests.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -17,10 +17,9 @@ @@ -17,10 +17,9 @@
package org.springframework.expression.spel;
import junit.framework.Assert;
import org.junit.Test;
import org.springframework.expression.spel.support.StandardEvaluationContext;
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
@ -126,12 +125,12 @@ public class LiteralTests extends ExpressionTestCase { @@ -126,12 +125,12 @@ public class LiteralTests extends ExpressionTestCase {
@Test
public void testLiteralReal02_CreatingFloats() {
// For now, everything becomes a double...
evaluate("1.25f", 1.25d, Double.class);
evaluate("2.5f", 2.5d, Double.class);
evaluate("-3.5f", -3.5d, Double.class);
evaluate("1.25F", 1.25d, Double.class);
evaluate("2.5F", 2.5d, Double.class);
evaluate("-3.5F", -3.5d, Double.class);
evaluate("1.25f", 1.25f, Float.class);
evaluate("2.5f", 2.5f, Float.class);
evaluate("-3.5f", -3.5f, Float.class);
evaluate("1.25F", 1.25f, Float.class);
evaluate("2.5F", 2.5f, Float.class);
evaluate("-3.5F", -3.5f, Float.class);
}
@Test
@ -140,7 +139,7 @@ public class LiteralTests extends ExpressionTestCase { @@ -140,7 +139,7 @@ public class LiteralTests extends ExpressionTestCase {
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("6E2f", 600.0d, Double.class);
evaluate("6E2f", 6E2f, Float.class);
}
@Test

21
org.springframework.expression/src/test/java/org/springframework/expression/spel/OperatorTests.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -17,7 +17,6 @@ @@ -17,7 +17,6 @@
package org.springframework.expression.spel;
import junit.framework.Assert;
import org.junit.Test;
import org.springframework.expression.spel.ast.Operator;
import org.springframework.expression.spel.standard.SpelExpression;
@ -187,7 +186,7 @@ public class OperatorTests extends ExpressionTestCase { @@ -187,7 +186,7 @@ public class OperatorTests extends ExpressionTestCase {
evaluate("5 - 4", "1", Integer.class);
evaluate("3 * 5", 15, Integer.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 % 2", 1, Integer.class);
evaluate("3 mod 2", 1, Integer.class);
@ -199,7 +198,7 @@ public class OperatorTests extends ExpressionTestCase { @@ -199,7 +198,7 @@ public class OperatorTests extends ExpressionTestCase {
@Test
public void testPlus() throws Exception {
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("'ab' + 2", "ab2", String.class);
@ -229,7 +228,7 @@ public class OperatorTests extends ExpressionTestCase { @@ -229,7 +228,7 @@ public class OperatorTests extends ExpressionTestCase {
@Test
public void testMinus() throws Exception {
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("2-'ab'",SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
SpelExpression expr = (SpelExpression)parser.parseExpression("-3");
@ -247,16 +246,16 @@ public class OperatorTests extends ExpressionTestCase { @@ -247,16 +246,16 @@ public class OperatorTests extends ExpressionTestCase {
public void testModulus() {
evaluate("3%2",1,Integer.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);
evaluateAndCheckError("'abc'%'def'",SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
}
@Test
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("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);
evaluateAndCheckError("'abc'/'def'",SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
}
@ -350,9 +349,9 @@ public class OperatorTests extends ExpressionTestCase { @@ -350,9 +349,9 @@ public class OperatorTests extends ExpressionTestCase {
public void testMixedOperands_DoublesAndInts() {
evaluate("3.0d + 5", 8.0d, Double.class);
evaluate("3.0D - 5", -2.0d, Double.class);
evaluate("3.0f * 5", 15.0d, Double.class);
evaluate("6.0f / 2", 3.0, Double.class);
evaluate("6.0f / 4", 1.5d, Double.class);
evaluate("3.0f * 5", 15.0f, Float.class);
evaluate("6.0f / 2", 3.0f, Float.class);
evaluate("6.0f / 4", 1.5f, Float.class);
evaluate("5.0D % 3", 2.0d, Double.class);
evaluate("5.5D % 3", 2.5, Double.class);
}

482
org.springframework.expression/src/test/java/org/springframework/expression/spel/SpringEL300Tests.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -16,34 +16,11 @@ @@ -16,34 +16,11 @@
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 org.junit.Ignore;
import org.junit.Test;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.AccessException;
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.*;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.ReflectiveMethodResolver;
@ -51,6 +28,13 @@ import org.springframework.expression.spel.support.ReflectivePropertyAccessor; @@ -51,6 +28,13 @@ import org.springframework.expression.spel.support.ReflectivePropertyAccessor;
import org.springframework.expression.spel.support.StandardEvaluationContext;
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
*
@ -1175,6 +1159,454 @@ public class SpringEL300Tests extends ExpressionTestCase { @@ -1175,6 +1159,454 @@ public class SpringEL300Tests extends ExpressionTestCase {
}
}
@Test
public void testArray() {
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
Expression expression = null;
Object result = null;
expression = parser.parseExpression("new java.lang.Long[0].class");
result = expression.getValue(context, "");
assertEquals("Equal assertion failed: ", "class [Ljava.lang.Long;", result.toString());
expression = parser.parseExpression("T(java.lang.Long[])");
result = expression.getValue(context, "");
assertEquals("Equal assertion failed: ", "class [Ljava.lang.Long;", result.toString());
expression = parser.parseExpression("T(java.lang.String[][][])");
result = expression.getValue(context, "");
assertEquals("Equal assertion failed: ", "class [[[Ljava.lang.String;", result.toString());
assertEquals("T(java.lang.String[][][])",((SpelExpression)expression).toStringAST());
expression = parser.parseExpression("new int[0].class");
result = expression.getValue(context, "");
assertEquals("Equal assertion failed: ", "class [I", result.toString());
expression = parser.parseExpression("T(int[][])");
result = expression.getValue(context, "");
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");
}
}
}

Loading…
Cancel
Save