Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Mar 19, 2023
1 parent 94bbf85 commit 0882ca5
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,6 @@ void safeNavigation() {
evaluate("null?.null?.null", null, null);
}

@Test // SPR-16731
void matchesWithPatternAccessThreshold() {
String pattern = "^(?=[a-z0-9-]{1,47})([a-z0-9]+[-]{0,1}){1,47}[a-z0-9]{1}$";
String expression = "'abcde-fghijklmn-o42pasdfasdfasdf.qrstuvwxyz10x.xx.yyy.zasdfasfd' matches \'" + pattern + "\'";
Expression expr = parser.parseExpression(expression);
assertThatExceptionOfType(SpelEvaluationException.class)
.isThrownBy(expr::getValue)
.withCauseInstanceOf(IllegalStateException.class)
.satisfies(ex -> assertThat(ex.getMessageCode()).isEqualTo(SpelMessage.FLAWED_PATTERN));
}

// mixing operators
@Test
void mixingOperators() {
Expand Down Expand Up @@ -460,28 +449,35 @@ void relOperatorsInstanceof06() {
}

@Test
void relOperatorsMatches01() {
void matchesTrue() {
evaluate("'5.00' matches '^-?\\d+(\\.\\d{2})?$'", "true", Boolean.class);
}

@Test
void matchesFalse() {
evaluate("'5.0067' matches '^-?\\d+(\\.\\d{2})?$'", "false", Boolean.class);
}

@Test
void relOperatorsMatches02() {
evaluate("'5.00' matches '^-?\\d+(\\.\\d{2})?$'", "true", Boolean.class);
void matchesWithInputConversion() {
evaluate("27 matches '^.*2.*$'", true, Boolean.class); // conversion int --> string
}

@Test
void relOperatorsMatches03() {
void matchesWithNullInput() {
evaluateAndCheckError("null matches '^.*$'", SpelMessage.INVALID_FIRST_OPERAND_FOR_MATCHES_OPERATOR, 0, null);
}

@Test
void relOperatorsMatches04() {
void matchesWithNullPattern() {
evaluateAndCheckError("'abc' matches null", SpelMessage.INVALID_SECOND_OPERAND_FOR_MATCHES_OPERATOR, 14, null);
}

@Test
void relOperatorsMatches05() {
evaluate("27 matches '^.*2.*$'", true, Boolean.class); // conversion int>string
@Test // SPR-16731
void matchesWithPatternAccessThreshold() {
String pattern = "^(?=[a-z0-9-]{1,47})([a-z0-9]+[-]{0,1}){1,47}[a-z0-9]{1}$";
String expression = "'abcde-fghijklmn-o42pasdfasdfasdf.qrstuvwxyz10x.xx.yyy.zasdfasfd' matches '" + pattern + "'";
evaluateAndCheckError(expression, SpelMessage.FLAWED_PATTERN);
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 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.
Expand Down Expand Up @@ -27,7 +27,7 @@
import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests the evaluation of expressions using relational operators.
* Tests the evaluation of expressions using various operators.
*
* @author Andy Clement
* @author Juergen Hoeller
Expand All @@ -36,7 +36,7 @@
class OperatorTests extends AbstractExpressionTests {

@Test
void testEqual() {
void equal() {
evaluate("3 == 5", false, Boolean.class);
evaluate("5 == 3", false, Boolean.class);
evaluate("6 == 6", true, Boolean.class);
Expand Down Expand Up @@ -85,7 +85,7 @@ void testEqual() {
}

@Test
void testNotEqual() {
void notEqual() {
evaluate("3 != 5", true, Boolean.class);
evaluate("5 != 3", true, Boolean.class);
evaluate("6 != 6", false, Boolean.class);
Expand Down Expand Up @@ -134,7 +134,7 @@ void testNotEqual() {
}

@Test
void testLessThan() {
void lessThan() {
evaluate("5 < 5", false, Boolean.class);
evaluate("3 < 5", true, Boolean.class);
evaluate("5 < 3", false, Boolean.class);
Expand Down Expand Up @@ -176,7 +176,7 @@ void testLessThan() {
}

@Test
void testLessThanOrEqual() {
void lessThanOrEqual() {
evaluate("3 <= 5", true, Boolean.class);
evaluate("5 <= 3", false, Boolean.class);
evaluate("6 <= 6", true, Boolean.class);
Expand Down Expand Up @@ -225,7 +225,7 @@ void testLessThanOrEqual() {
}

@Test
void testGreaterThan() {
void greaterThan() {
evaluate("3 > 5", false, Boolean.class);
evaluate("5 > 3", true, Boolean.class);
evaluate("3L > 5L", false, Boolean.class);
Expand Down Expand Up @@ -266,7 +266,7 @@ void testGreaterThan() {
}

@Test
void testGreaterThanOrEqual() {
void greaterThanOrEqual() {
evaluate("3 >= 5", false, Boolean.class);
evaluate("5 >= 3", true, Boolean.class);
evaluate("6 >= 6", true, Boolean.class);
Expand Down Expand Up @@ -315,27 +315,27 @@ void testGreaterThanOrEqual() {
}

@Test
void testIntegerLiteral() {
void integerLiteral() {
evaluate("3", 3, Integer.class);
}

@Test
void testRealLiteral() {
void realLiteral() {
evaluate("3.5", 3.5d, Double.class);
}

@Test
void testMultiplyStringInt() {
void multiplyStringInt() {
evaluate("'a' * 5", "aaaaa", String.class);
}

@Test
void testMultiplyDoubleDoubleGivesDouble() {
void multiplyDoubleDoubleGivesDouble() {
evaluate("3.0d * 5.0d", 15.0d, Double.class);
}

@Test
void testMixedOperandsBigDecimal() {
void mixedOperandsBigDecimal() {
evaluate("3 * new java.math.BigDecimal('5')", new BigDecimal("15"), BigDecimal.class);
evaluate("3L * new java.math.BigDecimal('5')", new BigDecimal("15"), BigDecimal.class);
evaluate("3.0d * new java.math.BigDecimal('5')", new BigDecimal("15.0"), BigDecimal.class);
Expand All @@ -361,19 +361,19 @@ void testMixedOperandsBigDecimal() {
}

@Test
void testMathOperatorAdd02() {
void mathOperatorAdd02() {
evaluate("'hello' + ' ' + 'world'", "hello world", String.class);
}

@Test
void testMathOperatorsInChains() {
void mathOperatorsInChains() {
evaluate("1+2+3",6,Integer.class);
evaluate("2*3*4",24,Integer.class);
evaluate("12-1-2",9,Integer.class);
}

@Test
void testIntegerArithmetic() {
void integerArithmetic() {
evaluate("2 + 4", "6", Integer.class);
evaluate("5 - 4", "1", Integer.class);
evaluate("3 * 5", 15, Integer.class);
Expand All @@ -388,7 +388,7 @@ void testIntegerArithmetic() {
}

@Test
void testPlus() {
void plus() {
evaluate("7 + 2", "9", Integer.class);
evaluate("3.0f + 5.0f", 8.0f, Float.class);
evaluate("3.0d + 5.0d", 8.0d, Double.class);
Expand All @@ -400,44 +400,44 @@ void testPlus() {
evaluate("null + 'ab'", "nullab", String.class);

// AST:
SpelExpression expr = (SpelExpression)parser.parseExpression("+3");
SpelExpression expr = (SpelExpression) parser.parseExpression("+3");
assertThat(expr.toStringAST()).isEqualTo("+3");
expr = (SpelExpression)parser.parseExpression("2+3");
expr = (SpelExpression) parser.parseExpression("2+3");
assertThat(expr.toStringAST()).isEqualTo("(2 + 3)");

// use as a unary operator
evaluate("+5d",5d,Double.class);
evaluate("+5L",5L,Long.class);
evaluate("+5",5,Integer.class);
evaluate("+new java.math.BigDecimal('5')", new BigDecimal("5"),BigDecimal.class);
evaluateAndCheckError("+'abc'",SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
evaluate("+5d", 5d, Double.class);
evaluate("+5L", 5L, Long.class);
evaluate("+5", 5, Integer.class);
evaluate("+new java.math.BigDecimal('5')", new BigDecimal("5"), BigDecimal.class);
evaluateAndCheckError("+'abc'", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);

// string concatenation
evaluate("'abc'+'def'","abcdef",String.class);
evaluate("'abc'+'def'", "abcdef", String.class);

evaluate("5 + new Integer('37')",42,Integer.class);
evaluate("5 + new Integer('37')", 42, Integer.class);
}

@Test
void testMinus() {
void minus() {
evaluate("'c' - 2", "a", String.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");
SpelExpression expr = (SpelExpression) parser.parseExpression("-3");
assertThat(expr.toStringAST()).isEqualTo("-3");
expr = (SpelExpression)parser.parseExpression("2-3");
expr = (SpelExpression) parser.parseExpression("2-3");
assertThat(expr.toStringAST()).isEqualTo("(2 - 3)");

evaluate("-5d",-5d,Double.class);
evaluate("-5L",-5L,Long.class);
evaluate("-5d", -5d, Double.class);
evaluate("-5L", -5L, Long.class);
evaluate("-5", -5, Integer.class);
evaluate("-new java.math.BigDecimal('5')", new BigDecimal("-5"),BigDecimal.class);
evaluate("-new java.math.BigDecimal('5')", new BigDecimal("-5"), BigDecimal.class);
evaluateAndCheckError("-'abc'", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
}

@Test
void testModulus() {
void modulus() {
evaluate("3%2",1,Integer.class);
evaluate("3L%2L",1L,Long.class);
evaluate("3.0f%2.0f",1f,Float.class);
Expand All @@ -448,7 +448,7 @@ void testModulus() {
}

@Test
void testDivide() {
void divide() {
evaluate("3.0f / 5.0f", 0.6f, Float.class);
evaluate("4L/2L",2L,Long.class);
evaluate("3.0f div 5.0f", 0.6f, Float.class);
Expand All @@ -461,17 +461,17 @@ void testDivide() {
}

@Test
void testMathOperatorDivide_ConvertToDouble() {
void mathOperatorDivide_ConvertToDouble() {
evaluateAndAskForReturnType("8/4", 2.0, Double.class);
}

@Test
void testMathOperatorDivide04_ConvertToFloat() {
void mathOperatorDivide04_ConvertToFloat() {
evaluateAndAskForReturnType("8/4", 2.0F, Float.class);
}

@Test
void testDoubles() {
void doubles() {
evaluate("3.0d == 5.0d", false, Boolean.class);
evaluate("3.0d == 3.0d", true, Boolean.class);
evaluate("3.0d != 5.0d", true, Boolean.class);
Expand All @@ -484,7 +484,7 @@ void testDoubles() {
}

@Test
void testBigDecimals() {
void bigDecimals() {
evaluate("3 + new java.math.BigDecimal('5')", new BigDecimal("8"), BigDecimal.class);
evaluate("3 - new java.math.BigDecimal('5')", new BigDecimal("-2"), BigDecimal.class);
evaluate("3 * new java.math.BigDecimal('5')", new BigDecimal("15"), BigDecimal.class);
Expand All @@ -495,7 +495,7 @@ void testBigDecimals() {
}

@Test
void testOperatorNames() {
void operatorNames() {
Operator node = getOperatorNode((SpelExpression)parser.parseExpression("1==3"));
assertThat(node.getOperatorName()).isEqualTo("==");

Expand Down Expand Up @@ -534,22 +534,22 @@ void testOperatorNames() {
}

@Test
void testOperatorOverloading() {
void operatorOverloading() {
evaluateAndCheckError("'a' * '2'", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
evaluateAndCheckError("'a' ^ '2'", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
}

@Test
void testPower() {
evaluate("3^2",9,Integer.class);
evaluate("3.0d^2.0d",9.0d,Double.class);
evaluate("3L^2L",9L,Long.class);
void power() {
evaluate("3^2", 9, Integer.class);
evaluate("3.0d^2.0d", 9.0d, Double.class);
evaluate("3L^2L", 9L, Long.class);
evaluate("(2^32)^2", 9223372036854775807L, Long.class);
evaluate("new java.math.BigDecimal('5') ^ 3", new BigDecimal("125"), BigDecimal.class);
}

@Test
void testMixedOperands_FloatsAndDoubles() {
void mixedOperands_FloatsAndDoubles() {
evaluate("3.0d + 5.0f", 8.0d, Double.class);
evaluate("3.0D - 5.0f", -2.0d, Double.class);
evaluate("3.0f * 5.0d", 15.0d, Double.class);
Expand All @@ -558,7 +558,7 @@ void testMixedOperands_FloatsAndDoubles() {
}

@Test
void testMixedOperands_DoublesAndInts() {
void mixedOperands_DoublesAndInts() {
evaluate("3.0d + 5", 8.0d, Double.class);
evaluate("3.0D - 5", -2.0d, Double.class);
evaluate("3.0f * 5", 15.0f, Float.class);
Expand All @@ -569,15 +569,15 @@ void testMixedOperands_DoublesAndInts() {
}

@Test
void testStrings() {
void strings() {
evaluate("'abc' == 'abc'", true, Boolean.class);
evaluate("'abc' == 'def'", false, Boolean.class);
evaluate("'abc' != 'abc'", false, Boolean.class);
evaluate("'abc' != 'def'", true, Boolean.class);
}

@Test
void testLongs() {
void longs() {
evaluate("3L == 4L", false, Boolean.class);
evaluate("3L == 3L", true, Boolean.class);
evaluate("3L != 4L", true, Boolean.class);
Expand All @@ -588,7 +588,7 @@ void testLongs() {
}

@Test
void testBigIntegers() {
void bigIntegers() {
evaluate("3 + new java.math.BigInteger('5')", new BigInteger("8"), BigInteger.class);
evaluate("3 - new java.math.BigInteger('5')", new BigInteger("-2"), BigInteger.class);
evaluate("3 * new java.math.BigInteger('5')", new BigInteger("15"), BigInteger.class);
Expand Down Expand Up @@ -619,7 +619,7 @@ private Operator findOperator(SpelNode node) {
}


public static class BaseComparable implements Comparable<BaseComparable> {
static class BaseComparable implements Comparable<BaseComparable> {

@Override
public int compareTo(BaseComparable other) {
Expand Down

0 comments on commit 0882ca5

Please sign in to comment.