Polishing
This commit is contained in:
parent
d4719493bd
commit
8202282975
|
@ -35,113 +35,107 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for SpEL's plus operator.
|
* Tests for SpEL's {@link OpPlus} operator.
|
||||||
*
|
*
|
||||||
* @author Ivo Smid
|
* @author Ivo Smid
|
||||||
* @author Chris Beams
|
* @author Chris Beams
|
||||||
|
* @author Sam Brannen
|
||||||
* @since 3.2
|
* @since 3.2
|
||||||
* @see OpPlus
|
|
||||||
*/
|
*/
|
||||||
class OpPlusTests {
|
class OpPlusTests {
|
||||||
|
|
||||||
|
private final ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test_emptyOperands() {
|
void emptyOperands() {
|
||||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
assertThatIllegalArgumentException().isThrownBy(() -> new OpPlus(-1, -1));
|
||||||
new OpPlus(-1, -1));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test_unaryPlusWithStringLiteral() {
|
void unaryPlusWithStringLiteral() {
|
||||||
ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
|
StringLiteral stringLiteral = new StringLiteral("word", -1, -1, "word");
|
||||||
|
|
||||||
StringLiteral str = new StringLiteral("word", -1, -1, "word");
|
OpPlus operator = new OpPlus(-1, -1, stringLiteral);
|
||||||
|
assertThatExceptionOfType(SpelEvaluationException.class)
|
||||||
OpPlus o = new OpPlus(-1, -1, str);
|
.isThrownBy(() -> operator.getValueInternal(expressionState));
|
||||||
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
|
|
||||||
o.getValueInternal(expressionState));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test_unaryPlusWithNumberOperand() {
|
void unaryPlusWithIntegerOperand() {
|
||||||
ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
|
IntLiteral intLiteral = new IntLiteral("123", -1, -1, 123);
|
||||||
|
OpPlus operator = new OpPlus(-1, -1, intLiteral);
|
||||||
|
TypedValue value = operator.getValueInternal(expressionState);
|
||||||
|
|
||||||
{
|
assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(Integer.class);
|
||||||
RealLiteral realLiteral = new RealLiteral("123.00", -1, -1, 123.0);
|
assertThat(value.getTypeDescriptor().getType()).isEqualTo(Integer.class);
|
||||||
OpPlus o = new OpPlus(-1, -1, realLiteral);
|
assertThat(value.getValue()).isEqualTo(intLiteral.getLiteralValue().getValue());
|
||||||
TypedValue value = o.getValueInternal(expressionState);
|
|
||||||
|
|
||||||
assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(Double.class);
|
|
||||||
assertThat(value.getTypeDescriptor().getType()).isEqualTo(Double.class);
|
|
||||||
assertThat(value.getValue()).isEqualTo(realLiteral.getLiteralValue().getValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
IntLiteral intLiteral = new IntLiteral("123", -1, -1, 123);
|
|
||||||
OpPlus o = new OpPlus(-1, -1, intLiteral);
|
|
||||||
TypedValue value = o.getValueInternal(expressionState);
|
|
||||||
|
|
||||||
assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(Integer.class);
|
|
||||||
assertThat(value.getTypeDescriptor().getType()).isEqualTo(Integer.class);
|
|
||||||
assertThat(value.getValue()).isEqualTo(intLiteral.getLiteralValue().getValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
LongLiteral longLiteral = new LongLiteral("123", -1, -1, 123L);
|
|
||||||
OpPlus o = new OpPlus(-1, -1, longLiteral);
|
|
||||||
TypedValue value = o.getValueInternal(expressionState);
|
|
||||||
|
|
||||||
assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(Long.class);
|
|
||||||
assertThat(value.getTypeDescriptor().getType()).isEqualTo(Long.class);
|
|
||||||
assertThat(value.getValue()).isEqualTo(longLiteral.getLiteralValue().getValue());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test_binaryPlusWithNumberOperands() {
|
void unaryPlusWithLongOperand() {
|
||||||
ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
|
LongLiteral longLiteral = new LongLiteral("123", -1, -1, 123L);
|
||||||
|
OpPlus operator = new OpPlus(-1, -1, longLiteral);
|
||||||
|
TypedValue value = operator.getValueInternal(expressionState);
|
||||||
|
|
||||||
{
|
assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(Long.class);
|
||||||
RealLiteral n1 = new RealLiteral("123.00", -1, -1, 123.0);
|
assertThat(value.getTypeDescriptor().getType()).isEqualTo(Long.class);
|
||||||
RealLiteral n2 = new RealLiteral("456.00", -1, -1, 456.0);
|
assertThat(value.getValue()).isEqualTo(longLiteral.getLiteralValue().getValue());
|
||||||
OpPlus o = new OpPlus(-1, -1, n1, n2);
|
|
||||||
TypedValue value = o.getValueInternal(expressionState);
|
|
||||||
|
|
||||||
assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(Double.class);
|
|
||||||
assertThat(value.getTypeDescriptor().getType()).isEqualTo(Double.class);
|
|
||||||
assertThat(value.getValue()).isEqualTo(123.0 + 456.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
LongLiteral n1 = new LongLiteral("123", -1, -1, 123L);
|
|
||||||
LongLiteral n2 = new LongLiteral("456", -1, -1, 456L);
|
|
||||||
OpPlus o = new OpPlus(-1, -1, n1, n2);
|
|
||||||
TypedValue value = o.getValueInternal(expressionState);
|
|
||||||
|
|
||||||
assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(Long.class);
|
|
||||||
assertThat(value.getTypeDescriptor().getType()).isEqualTo(Long.class);
|
|
||||||
assertThat(value.getValue()).isEqualTo(123L + 456L);
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
IntLiteral n1 = new IntLiteral("123", -1, -1, 123);
|
|
||||||
IntLiteral n2 = new IntLiteral("456", -1, -1, 456);
|
|
||||||
OpPlus o = new OpPlus(-1, -1, n1, n2);
|
|
||||||
TypedValue value = o.getValueInternal(expressionState);
|
|
||||||
|
|
||||||
assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(Integer.class);
|
|
||||||
assertThat(value.getTypeDescriptor().getType()).isEqualTo(Integer.class);
|
|
||||||
assertThat(value.getValue()).isEqualTo(123 + 456);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test_binaryPlusWithStringOperands() {
|
void unaryPlusWithRealOperand() {
|
||||||
ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
|
RealLiteral realLiteral = new RealLiteral("123.00", -1, -1, 123.0);
|
||||||
|
OpPlus operator = new OpPlus(-1, -1, realLiteral);
|
||||||
|
TypedValue value = operator.getValueInternal(expressionState);
|
||||||
|
|
||||||
StringLiteral n1 = new StringLiteral("\"foo\"", -1, -1, "\"foo\"");
|
assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(Double.class);
|
||||||
StringLiteral n2 = new StringLiteral("\"bar\"", -1, -1, "\"bar\"");
|
assertThat(value.getTypeDescriptor().getType()).isEqualTo(Double.class);
|
||||||
OpPlus o = new OpPlus(-1, -1, n1, n2);
|
assertThat(value.getValue()).isEqualTo(realLiteral.getLiteralValue().getValue());
|
||||||
TypedValue value = o.getValueInternal(expressionState);
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void binaryPlusWithIntegerOperands() {
|
||||||
|
IntLiteral n1 = new IntLiteral("123", -1, -1, 123);
|
||||||
|
IntLiteral n2 = new IntLiteral("456", -1, -1, 456);
|
||||||
|
OpPlus operator = new OpPlus(-1, -1, n1, n2);
|
||||||
|
TypedValue value = operator.getValueInternal(expressionState);
|
||||||
|
|
||||||
|
assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(Integer.class);
|
||||||
|
assertThat(value.getTypeDescriptor().getType()).isEqualTo(Integer.class);
|
||||||
|
assertThat(value.getValue()).isEqualTo(123 + 456);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void binaryPlusWithLongOperands() {
|
||||||
|
LongLiteral n1 = new LongLiteral("123", -1, -1, 123L);
|
||||||
|
LongLiteral n2 = new LongLiteral("456", -1, -1, 456L);
|
||||||
|
OpPlus operator = new OpPlus(-1, -1, n1, n2);
|
||||||
|
TypedValue value = operator.getValueInternal(expressionState);
|
||||||
|
|
||||||
|
assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(Long.class);
|
||||||
|
assertThat(value.getTypeDescriptor().getType()).isEqualTo(Long.class);
|
||||||
|
assertThat(value.getValue()).isEqualTo(123L + 456L);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void binaryPlusWithRealOperands() {
|
||||||
|
RealLiteral n1 = new RealLiteral("123.00", -1, -1, 123.0);
|
||||||
|
RealLiteral n2 = new RealLiteral("456.00", -1, -1, 456.0);
|
||||||
|
OpPlus operator = new OpPlus(-1, -1, n1, n2);
|
||||||
|
TypedValue value = operator.getValueInternal(expressionState);
|
||||||
|
|
||||||
|
assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(Double.class);
|
||||||
|
assertThat(value.getTypeDescriptor().getType()).isEqualTo(Double.class);
|
||||||
|
assertThat(value.getValue()).isEqualTo(123.0 + 456.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void binaryPlusWithStringOperands() {
|
||||||
|
StringLiteral str1 = new StringLiteral("\"foo\"", -1, -1, "\"foo\"");
|
||||||
|
StringLiteral str2 = new StringLiteral("\"bar\"", -1, -1, "\"bar\"");
|
||||||
|
OpPlus operator = new OpPlus(-1, -1, str1, str2);
|
||||||
|
TypedValue value = operator.getValueInternal(expressionState);
|
||||||
|
|
||||||
assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(String.class);
|
assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(String.class);
|
||||||
assertThat(value.getTypeDescriptor().getType()).isEqualTo(String.class);
|
assertThat(value.getTypeDescriptor().getType()).isEqualTo(String.class);
|
||||||
|
@ -149,13 +143,11 @@ class OpPlusTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test_binaryPlusWithLeftStringOperand() {
|
void binaryPlusWithLeftStringOperand() {
|
||||||
ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
|
StringLiteral stringLiteral = new StringLiteral("\"number is \"", -1, -1, "\"number is \"");
|
||||||
|
LongLiteral longLiteral = new LongLiteral("123", -1, -1, 123);
|
||||||
StringLiteral n1 = new StringLiteral("\"number is \"", -1, -1, "\"number is \"");
|
OpPlus operator = new OpPlus(-1, -1, stringLiteral, longLiteral);
|
||||||
LongLiteral n2 = new LongLiteral("123", -1, -1, 123);
|
TypedValue value = operator.getValueInternal(expressionState);
|
||||||
OpPlus o = new OpPlus(-1, -1, n1, n2);
|
|
||||||
TypedValue value = o.getValueInternal(expressionState);
|
|
||||||
|
|
||||||
assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(String.class);
|
assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(String.class);
|
||||||
assertThat(value.getTypeDescriptor().getType()).isEqualTo(String.class);
|
assertThat(value.getTypeDescriptor().getType()).isEqualTo(String.class);
|
||||||
|
@ -163,13 +155,11 @@ class OpPlusTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test_binaryPlusWithRightStringOperand() {
|
void binaryPlusWithRightStringOperand() {
|
||||||
ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
|
LongLiteral longLiteral = new LongLiteral("123", -1, -1, 123);
|
||||||
|
StringLiteral stringLiteral = new StringLiteral("\" is a number\"", -1, -1, "\" is a number\"");
|
||||||
LongLiteral n1 = new LongLiteral("123", -1, -1, 123);
|
OpPlus operator = new OpPlus(-1, -1, longLiteral, stringLiteral);
|
||||||
StringLiteral n2 = new StringLiteral("\" is a number\"", -1, -1, "\" is a number\"");
|
TypedValue value = operator.getValueInternal(expressionState);
|
||||||
OpPlus o = new OpPlus(-1, -1, n1, n2);
|
|
||||||
TypedValue value = o.getValueInternal(expressionState);
|
|
||||||
|
|
||||||
assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(String.class);
|
assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(String.class);
|
||||||
assertThat(value.getTypeDescriptor().getType()).isEqualTo(String.class);
|
assertThat(value.getTypeDescriptor().getType()).isEqualTo(String.class);
|
||||||
|
@ -177,24 +167,23 @@ class OpPlusTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test_binaryPlusWithTime_ToString() {
|
void binaryPlusWithSqlTimeToString() {
|
||||||
ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
|
|
||||||
Time time = new Time(new Date().getTime());
|
Time time = new Time(new Date().getTime());
|
||||||
|
|
||||||
VariableReference var = new VariableReference("timeVar", -1, -1);
|
VariableReference var = new VariableReference("timeVar", -1, -1);
|
||||||
var.setValue(expressionState, time);
|
var.setValue(expressionState, time);
|
||||||
|
|
||||||
StringLiteral n2 = new StringLiteral("\" is now\"", -1, -1, "\" is now\"");
|
StringLiteral stringLiteral = new StringLiteral("\" is now\"", -1, -1, "\" is now\"");
|
||||||
OpPlus o = new OpPlus(-1, -1, var, n2);
|
OpPlus operator = new OpPlus(-1, -1, var, stringLiteral);
|
||||||
TypedValue value = o.getValueInternal(expressionState);
|
TypedValue value = operator.getValueInternal(expressionState);
|
||||||
|
|
||||||
assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(String.class);
|
assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(String.class);
|
||||||
assertThat(value.getTypeDescriptor().getType()).isEqualTo(String.class);
|
assertThat(value.getTypeDescriptor().getType()).isEqualTo(String.class);
|
||||||
assertThat(value.getValue()).isEqualTo((time + " is now"));
|
assertThat(value.getValue()).isEqualTo(time + " is now");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test_binaryPlusWithTimeConverted() {
|
void binaryPlusWithTimeConverted() {
|
||||||
SimpleDateFormat format = new SimpleDateFormat("hh :--: mm :--: ss", Locale.ENGLISH);
|
SimpleDateFormat format = new SimpleDateFormat("hh :--: mm :--: ss", Locale.ENGLISH);
|
||||||
|
|
||||||
GenericConversionService conversionService = new GenericConversionService();
|
GenericConversionService conversionService = new GenericConversionService();
|
||||||
|
@ -209,13 +198,13 @@ class OpPlusTests {
|
||||||
VariableReference var = new VariableReference("timeVar", -1, -1);
|
VariableReference var = new VariableReference("timeVar", -1, -1);
|
||||||
var.setValue(expressionState, time);
|
var.setValue(expressionState, time);
|
||||||
|
|
||||||
StringLiteral n2 = new StringLiteral("\" is now\"", -1, -1, "\" is now\"");
|
StringLiteral stringLiteral = new StringLiteral("\" is now\"", -1, -1, "\" is now\"");
|
||||||
OpPlus o = new OpPlus(-1, -1, var, n2);
|
OpPlus operator = new OpPlus(-1, -1, var, stringLiteral);
|
||||||
TypedValue value = o.getValueInternal(expressionState);
|
TypedValue value = operator.getValueInternal(expressionState);
|
||||||
|
|
||||||
assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(String.class);
|
assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(String.class);
|
||||||
assertThat(value.getTypeDescriptor().getType()).isEqualTo(String.class);
|
assertThat(value.getTypeDescriptor().getType()).isEqualTo(String.class);
|
||||||
assertThat(value.getValue()).isEqualTo((format.format(time) + " is now"));
|
assertThat(value.getValue()).isEqualTo(format.format(time) + " is now");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue