Skip to content

Commit 0882ca5

Browse files
committed
Polishing
1 parent 94bbf85 commit 0882ca5

File tree

2 files changed

+65
-69
lines changed

2 files changed

+65
-69
lines changed

spring-expression/src/test/java/org/springframework/expression/spel/EvaluationTests.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,6 @@ void safeNavigation() {
127127
evaluate("null?.null?.null", null, null);
128128
}
129129

130-
@Test // SPR-16731
131-
void matchesWithPatternAccessThreshold() {
132-
String pattern = "^(?=[a-z0-9-]{1,47})([a-z0-9]+[-]{0,1}){1,47}[a-z0-9]{1}$";
133-
String expression = "'abcde-fghijklmn-o42pasdfasdfasdf.qrstuvwxyz10x.xx.yyy.zasdfasfd' matches \'" + pattern + "\'";
134-
Expression expr = parser.parseExpression(expression);
135-
assertThatExceptionOfType(SpelEvaluationException.class)
136-
.isThrownBy(expr::getValue)
137-
.withCauseInstanceOf(IllegalStateException.class)
138-
.satisfies(ex -> assertThat(ex.getMessageCode()).isEqualTo(SpelMessage.FLAWED_PATTERN));
139-
}
140-
141130
// mixing operators
142131
@Test
143132
void mixingOperators() {
@@ -460,28 +449,35 @@ void relOperatorsInstanceof06() {
460449
}
461450

462451
@Test
463-
void relOperatorsMatches01() {
452+
void matchesTrue() {
453+
evaluate("'5.00' matches '^-?\\d+(\\.\\d{2})?$'", "true", Boolean.class);
454+
}
455+
456+
@Test
457+
void matchesFalse() {
464458
evaluate("'5.0067' matches '^-?\\d+(\\.\\d{2})?$'", "false", Boolean.class);
465459
}
466460

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

472466
@Test
473-
void relOperatorsMatches03() {
467+
void matchesWithNullInput() {
474468
evaluateAndCheckError("null matches '^.*$'", SpelMessage.INVALID_FIRST_OPERAND_FOR_MATCHES_OPERATOR, 0, null);
475469
}
476470

477471
@Test
478-
void relOperatorsMatches04() {
472+
void matchesWithNullPattern() {
479473
evaluateAndCheckError("'abc' matches null", SpelMessage.INVALID_SECOND_OPERAND_FOR_MATCHES_OPERATOR, 14, null);
480474
}
481475

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

487483
}

spring-expression/src/test/java/org/springframework/expression/spel/OperatorTests.java

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,7 +27,7 @@
2727
import static org.assertj.core.api.Assertions.assertThat;
2828

2929
/**
30-
* Tests the evaluation of expressions using relational operators.
30+
* Tests the evaluation of expressions using various operators.
3131
*
3232
* @author Andy Clement
3333
* @author Juergen Hoeller
@@ -36,7 +36,7 @@
3636
class OperatorTests extends AbstractExpressionTests {
3737

3838
@Test
39-
void testEqual() {
39+
void equal() {
4040
evaluate("3 == 5", false, Boolean.class);
4141
evaluate("5 == 3", false, Boolean.class);
4242
evaluate("6 == 6", true, Boolean.class);
@@ -85,7 +85,7 @@ void testEqual() {
8585
}
8686

8787
@Test
88-
void testNotEqual() {
88+
void notEqual() {
8989
evaluate("3 != 5", true, Boolean.class);
9090
evaluate("5 != 3", true, Boolean.class);
9191
evaluate("6 != 6", false, Boolean.class);
@@ -134,7 +134,7 @@ void testNotEqual() {
134134
}
135135

136136
@Test
137-
void testLessThan() {
137+
void lessThan() {
138138
evaluate("5 < 5", false, Boolean.class);
139139
evaluate("3 < 5", true, Boolean.class);
140140
evaluate("5 < 3", false, Boolean.class);
@@ -176,7 +176,7 @@ void testLessThan() {
176176
}
177177

178178
@Test
179-
void testLessThanOrEqual() {
179+
void lessThanOrEqual() {
180180
evaluate("3 <= 5", true, Boolean.class);
181181
evaluate("5 <= 3", false, Boolean.class);
182182
evaluate("6 <= 6", true, Boolean.class);
@@ -225,7 +225,7 @@ void testLessThanOrEqual() {
225225
}
226226

227227
@Test
228-
void testGreaterThan() {
228+
void greaterThan() {
229229
evaluate("3 > 5", false, Boolean.class);
230230
evaluate("5 > 3", true, Boolean.class);
231231
evaluate("3L > 5L", false, Boolean.class);
@@ -266,7 +266,7 @@ void testGreaterThan() {
266266
}
267267

268268
@Test
269-
void testGreaterThanOrEqual() {
269+
void greaterThanOrEqual() {
270270
evaluate("3 >= 5", false, Boolean.class);
271271
evaluate("5 >= 3", true, Boolean.class);
272272
evaluate("6 >= 6", true, Boolean.class);
@@ -315,27 +315,27 @@ void testGreaterThanOrEqual() {
315315
}
316316

317317
@Test
318-
void testIntegerLiteral() {
318+
void integerLiteral() {
319319
evaluate("3", 3, Integer.class);
320320
}
321321

322322
@Test
323-
void testRealLiteral() {
323+
void realLiteral() {
324324
evaluate("3.5", 3.5d, Double.class);
325325
}
326326

327327
@Test
328-
void testMultiplyStringInt() {
328+
void multiplyStringInt() {
329329
evaluate("'a' * 5", "aaaaa", String.class);
330330
}
331331

332332
@Test
333-
void testMultiplyDoubleDoubleGivesDouble() {
333+
void multiplyDoubleDoubleGivesDouble() {
334334
evaluate("3.0d * 5.0d", 15.0d, Double.class);
335335
}
336336

337337
@Test
338-
void testMixedOperandsBigDecimal() {
338+
void mixedOperandsBigDecimal() {
339339
evaluate("3 * new java.math.BigDecimal('5')", new BigDecimal("15"), BigDecimal.class);
340340
evaluate("3L * new java.math.BigDecimal('5')", new BigDecimal("15"), BigDecimal.class);
341341
evaluate("3.0d * new java.math.BigDecimal('5')", new BigDecimal("15.0"), BigDecimal.class);
@@ -361,19 +361,19 @@ void testMixedOperandsBigDecimal() {
361361
}
362362

363363
@Test
364-
void testMathOperatorAdd02() {
364+
void mathOperatorAdd02() {
365365
evaluate("'hello' + ' ' + 'world'", "hello world", String.class);
366366
}
367367

368368
@Test
369-
void testMathOperatorsInChains() {
369+
void mathOperatorsInChains() {
370370
evaluate("1+2+3",6,Integer.class);
371371
evaluate("2*3*4",24,Integer.class);
372372
evaluate("12-1-2",9,Integer.class);
373373
}
374374

375375
@Test
376-
void testIntegerArithmetic() {
376+
void integerArithmetic() {
377377
evaluate("2 + 4", "6", Integer.class);
378378
evaluate("5 - 4", "1", Integer.class);
379379
evaluate("3 * 5", 15, Integer.class);
@@ -388,7 +388,7 @@ void testIntegerArithmetic() {
388388
}
389389

390390
@Test
391-
void testPlus() {
391+
void plus() {
392392
evaluate("7 + 2", "9", Integer.class);
393393
evaluate("3.0f + 5.0f", 8.0f, Float.class);
394394
evaluate("3.0d + 5.0d", 8.0d, Double.class);
@@ -400,44 +400,44 @@ void testPlus() {
400400
evaluate("null + 'ab'", "nullab", String.class);
401401

402402
// AST:
403-
SpelExpression expr = (SpelExpression)parser.parseExpression("+3");
403+
SpelExpression expr = (SpelExpression) parser.parseExpression("+3");
404404
assertThat(expr.toStringAST()).isEqualTo("+3");
405-
expr = (SpelExpression)parser.parseExpression("2+3");
405+
expr = (SpelExpression) parser.parseExpression("2+3");
406406
assertThat(expr.toStringAST()).isEqualTo("(2 + 3)");
407407

408408
// use as a unary operator
409-
evaluate("+5d",5d,Double.class);
410-
evaluate("+5L",5L,Long.class);
411-
evaluate("+5",5,Integer.class);
412-
evaluate("+new java.math.BigDecimal('5')", new BigDecimal("5"),BigDecimal.class);
413-
evaluateAndCheckError("+'abc'",SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
409+
evaluate("+5d", 5d, Double.class);
410+
evaluate("+5L", 5L, Long.class);
411+
evaluate("+5", 5, Integer.class);
412+
evaluate("+new java.math.BigDecimal('5')", new BigDecimal("5"), BigDecimal.class);
413+
evaluateAndCheckError("+'abc'", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
414414

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

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

421421
@Test
422-
void testMinus() {
422+
void minus() {
423423
evaluate("'c' - 2", "a", String.class);
424424
evaluate("3.0f - 5.0f", -2.0f, Float.class);
425425
evaluateAndCheckError("'ab' - 2", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
426426
evaluateAndCheckError("2-'ab'", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
427-
SpelExpression expr = (SpelExpression)parser.parseExpression("-3");
427+
SpelExpression expr = (SpelExpression) parser.parseExpression("-3");
428428
assertThat(expr.toStringAST()).isEqualTo("-3");
429-
expr = (SpelExpression)parser.parseExpression("2-3");
429+
expr = (SpelExpression) parser.parseExpression("2-3");
430430
assertThat(expr.toStringAST()).isEqualTo("(2 - 3)");
431431

432-
evaluate("-5d",-5d,Double.class);
433-
evaluate("-5L",-5L,Long.class);
432+
evaluate("-5d", -5d, Double.class);
433+
evaluate("-5L", -5L, Long.class);
434434
evaluate("-5", -5, Integer.class);
435-
evaluate("-new java.math.BigDecimal('5')", new BigDecimal("-5"),BigDecimal.class);
435+
evaluate("-new java.math.BigDecimal('5')", new BigDecimal("-5"), BigDecimal.class);
436436
evaluateAndCheckError("-'abc'", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
437437
}
438438

439439
@Test
440-
void testModulus() {
440+
void modulus() {
441441
evaluate("3%2",1,Integer.class);
442442
evaluate("3L%2L",1L,Long.class);
443443
evaluate("3.0f%2.0f",1f,Float.class);
@@ -448,7 +448,7 @@ void testModulus() {
448448
}
449449

450450
@Test
451-
void testDivide() {
451+
void divide() {
452452
evaluate("3.0f / 5.0f", 0.6f, Float.class);
453453
evaluate("4L/2L",2L,Long.class);
454454
evaluate("3.0f div 5.0f", 0.6f, Float.class);
@@ -461,17 +461,17 @@ void testDivide() {
461461
}
462462

463463
@Test
464-
void testMathOperatorDivide_ConvertToDouble() {
464+
void mathOperatorDivide_ConvertToDouble() {
465465
evaluateAndAskForReturnType("8/4", 2.0, Double.class);
466466
}
467467

468468
@Test
469-
void testMathOperatorDivide04_ConvertToFloat() {
469+
void mathOperatorDivide04_ConvertToFloat() {
470470
evaluateAndAskForReturnType("8/4", 2.0F, Float.class);
471471
}
472472

473473
@Test
474-
void testDoubles() {
474+
void doubles() {
475475
evaluate("3.0d == 5.0d", false, Boolean.class);
476476
evaluate("3.0d == 3.0d", true, Boolean.class);
477477
evaluate("3.0d != 5.0d", true, Boolean.class);
@@ -484,7 +484,7 @@ void testDoubles() {
484484
}
485485

486486
@Test
487-
void testBigDecimals() {
487+
void bigDecimals() {
488488
evaluate("3 + new java.math.BigDecimal('5')", new BigDecimal("8"), BigDecimal.class);
489489
evaluate("3 - new java.math.BigDecimal('5')", new BigDecimal("-2"), BigDecimal.class);
490490
evaluate("3 * new java.math.BigDecimal('5')", new BigDecimal("15"), BigDecimal.class);
@@ -495,7 +495,7 @@ void testBigDecimals() {
495495
}
496496

497497
@Test
498-
void testOperatorNames() {
498+
void operatorNames() {
499499
Operator node = getOperatorNode((SpelExpression)parser.parseExpression("1==3"));
500500
assertThat(node.getOperatorName()).isEqualTo("==");
501501

@@ -534,22 +534,22 @@ void testOperatorNames() {
534534
}
535535

536536
@Test
537-
void testOperatorOverloading() {
537+
void operatorOverloading() {
538538
evaluateAndCheckError("'a' * '2'", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
539539
evaluateAndCheckError("'a' ^ '2'", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
540540
}
541541

542542
@Test
543-
void testPower() {
544-
evaluate("3^2",9,Integer.class);
545-
evaluate("3.0d^2.0d",9.0d,Double.class);
546-
evaluate("3L^2L",9L,Long.class);
543+
void power() {
544+
evaluate("3^2", 9, Integer.class);
545+
evaluate("3.0d^2.0d", 9.0d, Double.class);
546+
evaluate("3L^2L", 9L, Long.class);
547547
evaluate("(2^32)^2", 9223372036854775807L, Long.class);
548548
evaluate("new java.math.BigDecimal('5') ^ 3", new BigDecimal("125"), BigDecimal.class);
549549
}
550550

551551
@Test
552-
void testMixedOperands_FloatsAndDoubles() {
552+
void mixedOperands_FloatsAndDoubles() {
553553
evaluate("3.0d + 5.0f", 8.0d, Double.class);
554554
evaluate("3.0D - 5.0f", -2.0d, Double.class);
555555
evaluate("3.0f * 5.0d", 15.0d, Double.class);
@@ -558,7 +558,7 @@ void testMixedOperands_FloatsAndDoubles() {
558558
}
559559

560560
@Test
561-
void testMixedOperands_DoublesAndInts() {
561+
void mixedOperands_DoublesAndInts() {
562562
evaluate("3.0d + 5", 8.0d, Double.class);
563563
evaluate("3.0D - 5", -2.0d, Double.class);
564564
evaluate("3.0f * 5", 15.0f, Float.class);
@@ -569,15 +569,15 @@ void testMixedOperands_DoublesAndInts() {
569569
}
570570

571571
@Test
572-
void testStrings() {
572+
void strings() {
573573
evaluate("'abc' == 'abc'", true, Boolean.class);
574574
evaluate("'abc' == 'def'", false, Boolean.class);
575575
evaluate("'abc' != 'abc'", false, Boolean.class);
576576
evaluate("'abc' != 'def'", true, Boolean.class);
577577
}
578578

579579
@Test
580-
void testLongs() {
580+
void longs() {
581581
evaluate("3L == 4L", false, Boolean.class);
582582
evaluate("3L == 3L", true, Boolean.class);
583583
evaluate("3L != 4L", true, Boolean.class);
@@ -588,7 +588,7 @@ void testLongs() {
588588
}
589589

590590
@Test
591-
void testBigIntegers() {
591+
void bigIntegers() {
592592
evaluate("3 + new java.math.BigInteger('5')", new BigInteger("8"), BigInteger.class);
593593
evaluate("3 - new java.math.BigInteger('5')", new BigInteger("-2"), BigInteger.class);
594594
evaluate("3 * new java.math.BigInteger('5')", new BigInteger("15"), BigInteger.class);
@@ -619,7 +619,7 @@ private Operator findOperator(SpelNode node) {
619619
}
620620

621621

622-
public static class BaseComparable implements Comparable<BaseComparable> {
622+
static class BaseComparable implements Comparable<BaseComparable> {
623623

624624
@Override
625625
public int compareTo(BaseComparable other) {

0 commit comments

Comments
 (0)