diff --git a/spring-expression/src/test/java/org/springframework/expression/spel/EvaluationTests.java b/spring-expression/src/test/java/org/springframework/expression/spel/EvaluationTests.java index 56559883e0..2ee7734d93 100644 --- a/spring-expression/src/test/java/org/springframework/expression/spel/EvaluationTests.java +++ b/spring-expression/src/test/java/org/springframework/expression/spel/EvaluationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 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. @@ -127,17 +127,6 @@ class EvaluationTests extends AbstractExpressionTests { 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() { @@ -462,28 +451,35 @@ class EvaluationTests extends AbstractExpressionTests { } @Test - void relOperatorsMatches01() { - evaluate("'5.0067' matches '^-?\\d+(\\.\\d{2})?$'", "false", Boolean.class); - } - - @Test - void relOperatorsMatches02() { + void matchesTrue() { evaluate("'5.00' matches '^-?\\d+(\\.\\d{2})?$'", "true", Boolean.class); } @Test - void relOperatorsMatches03() { + void matchesFalse() { + evaluate("'5.0067' matches '^-?\\d+(\\.\\d{2})?$'", "false", Boolean.class); + } + + @Test + void matchesWithInputConversion() { + evaluate("27 matches '^.*2.*$'", true, Boolean.class); // conversion int --> string + } + + @Test + 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); } } diff --git a/spring-expression/src/test/java/org/springframework/expression/spel/OperatorTests.java b/spring-expression/src/test/java/org/springframework/expression/spel/OperatorTests.java index 28f446280b..cd914539d2 100644 --- a/spring-expression/src/test/java/org/springframework/expression/spel/OperatorTests.java +++ b/spring-expression/src/test/java/org/springframework/expression/spel/OperatorTests.java @@ -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. @@ -27,7 +27,7 @@ import org.springframework.expression.spel.standard.SpelExpression; 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 @@ -36,7 +36,7 @@ import static org.assertj.core.api.Assertions.assertThat; 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); @@ -87,7 +87,7 @@ class OperatorTests extends AbstractExpressionTests { } @Test - void testNotEqual() { + void notEqual() { evaluate("3 != 5", true, Boolean.class); evaluate("5 != 3", true, Boolean.class); evaluate("6 != 6", false, Boolean.class); @@ -136,7 +136,7 @@ class OperatorTests extends AbstractExpressionTests { } @Test - void testLessThan() { + void lessThan() { evaluate("5 < 5", false, Boolean.class); evaluate("3 < 5", true, Boolean.class); evaluate("5 < 3", false, Boolean.class); @@ -178,7 +178,7 @@ class OperatorTests extends AbstractExpressionTests { } @Test - void testLessThanOrEqual() { + void lessThanOrEqual() { evaluate("3 <= 5", true, Boolean.class); evaluate("5 <= 3", false, Boolean.class); evaluate("6 <= 6", true, Boolean.class); @@ -227,7 +227,7 @@ class OperatorTests extends AbstractExpressionTests { } @Test - void testGreaterThan() { + void greaterThan() { evaluate("3 > 5", false, Boolean.class); evaluate("5 > 3", true, Boolean.class); evaluate("3L > 5L", false, Boolean.class); @@ -268,7 +268,7 @@ class OperatorTests extends AbstractExpressionTests { } @Test - void testGreaterThanOrEqual() { + void greaterThanOrEqual() { evaluate("3 >= 5", false, Boolean.class); evaluate("5 >= 3", true, Boolean.class); evaluate("6 >= 6", true, Boolean.class); @@ -317,27 +317,27 @@ class OperatorTests extends AbstractExpressionTests { } @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); @@ -363,19 +363,19 @@ class OperatorTests extends AbstractExpressionTests { } @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); @@ -390,7 +390,7 @@ class OperatorTests extends AbstractExpressionTests { } @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); @@ -402,44 +402,44 @@ class OperatorTests extends AbstractExpressionTests { 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); @@ -450,7 +450,7 @@ class OperatorTests extends AbstractExpressionTests { } @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); @@ -463,17 +463,17 @@ class OperatorTests extends AbstractExpressionTests { } @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); @@ -486,7 +486,7 @@ class OperatorTests extends AbstractExpressionTests { } @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); @@ -497,7 +497,7 @@ class OperatorTests extends AbstractExpressionTests { } @Test - void testOperatorNames() { + void operatorNames() { Operator node = getOperatorNode((SpelExpression)parser.parseExpression("1==3")); assertThat(node.getOperatorName()).isEqualTo("=="); @@ -536,22 +536,22 @@ class OperatorTests extends AbstractExpressionTests { } @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); @@ -560,7 +560,7 @@ class OperatorTests extends AbstractExpressionTests { } @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); @@ -571,7 +571,7 @@ class OperatorTests extends AbstractExpressionTests { } @Test - void testStrings() { + void strings() { evaluate("'abc' == 'abc'", true, Boolean.class); evaluate("'abc' == 'def'", false, Boolean.class); evaluate("'abc' != 'abc'", false, Boolean.class); @@ -579,7 +579,7 @@ class OperatorTests extends AbstractExpressionTests { } @Test - void testLongs() { + void longs() { evaluate("3L == 4L", false, Boolean.class); evaluate("3L == 3L", true, Boolean.class); evaluate("3L != 4L", true, Boolean.class); @@ -590,7 +590,7 @@ class OperatorTests extends AbstractExpressionTests { } @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); @@ -607,8 +607,8 @@ class OperatorTests extends AbstractExpressionTests { } private Operator findOperator(SpelNode node) { - if (node instanceof Operator) { - return (Operator) node; + if (node instanceof Operator operator) { + return operator; } int childCount = node.getChildCount(); for (int i = 0; i < childCount; i++) { @@ -621,7 +621,7 @@ class OperatorTests extends AbstractExpressionTests { } - public static class BaseComparable implements Comparable { + static class BaseComparable implements Comparable { private int id; @@ -640,7 +640,7 @@ class OperatorTests extends AbstractExpressionTests { } - public static class SubComparable extends BaseComparable { + static class SubComparable extends BaseComparable { public SubComparable() { } @@ -650,7 +650,7 @@ class OperatorTests extends AbstractExpressionTests { } - public static class OtherSubComparable extends BaseComparable { + static class OtherSubComparable extends BaseComparable { public OtherSubComparable() { }