26
26
import org .springframework .expression .spel .standard .SpelExpression ;
27
27
28
28
import static org .assertj .core .api .Assertions .assertThat ;
29
+ import static org .springframework .expression .spel .SpelMessage .MAX_CONCATENATED_STRING_LENGTH_EXCEEDED ;
29
30
import static org .springframework .expression .spel .SpelMessage .MAX_REPEATED_TEXT_SIZE_EXCEEDED ;
30
31
31
32
/**
@@ -391,11 +392,7 @@ public void testPlus() throws Exception {
391
392
evaluate ("3.0f + 5.0f" , 8.0f , Float .class );
392
393
evaluate ("3.0d + 5.0d" , 8.0d , Double .class );
393
394
evaluate ("3 + new java.math.BigDecimal('5')" , new BigDecimal ("8" ), BigDecimal .class );
394
-
395
- evaluate ("'ab' + 2" , "ab2" , String .class );
396
- evaluate ("2 + 'a'" , "2a" , String .class );
397
- evaluate ("'ab' + null" , "abnull" , String .class );
398
- evaluate ("null + 'ab'" , "nullab" , String .class );
395
+ evaluate ("5 + new Integer('37')" , 42 , Integer .class );
399
396
400
397
// AST:
401
398
SpelExpression expr = (SpelExpression )parser .parseExpression ("+3" );
@@ -404,11 +401,11 @@ public void testPlus() throws Exception {
404
401
assertThat (expr .toStringAST ()).isEqualTo ("(2 + 3)" );
405
402
406
403
// use as a unary operator
407
- evaluate ("+5d" ,5d ,Double .class );
408
- evaluate ("+5L" ,5L ,Long .class );
409
- evaluate ("+5" ,5 , Integer .class );
410
- evaluate ("+new java.math.BigDecimal('5')" , new BigDecimal ("5" ),BigDecimal .class );
411
- evaluateAndCheckError ("+'abc'" ,SpelMessage .OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES );
404
+ evaluate ("+5d" , 5d , Double .class );
405
+ evaluate ("+5L" , 5L , Long .class );
406
+ evaluate ("+5" , 5 , Integer .class );
407
+ evaluate ("+new java.math.BigDecimal('5')" , new BigDecimal ("5" ), BigDecimal .class );
408
+ evaluateAndCheckError ("+'abc'" , SpelMessage .OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES );
412
409
413
410
// string concatenation
414
411
evaluate ("'abc'+'def'" ,"abcdef" ,String .class );
@@ -587,6 +584,59 @@ void stringRepeat() {
587
584
evaluateAndCheckError ("'a' * 257" , String .class , MAX_REPEATED_TEXT_SIZE_EXCEEDED , 4 );
588
585
}
589
586
587
+ @ Test
588
+ void stringConcatenation () {
589
+ evaluate ("'' + ''" , "" , String .class );
590
+ evaluate ("'' + null" , "null" , String .class );
591
+ evaluate ("null + ''" , "null" , String .class );
592
+ evaluate ("'ab' + null" , "abnull" , String .class );
593
+ evaluate ("null + 'ab'" , "nullab" , String .class );
594
+ evaluate ("'ab' + 2" , "ab2" , String .class );
595
+ evaluate ("2 + 'ab'" , "2ab" , String .class );
596
+ evaluate ("'abc' + 'def'" , "abcdef" , String .class );
597
+
598
+ // Text is big but not too big
599
+ final int maxSize = 100_000 ;
600
+ context .setVariable ("text1" , createString (maxSize ));
601
+ Expression expr = parser .parseExpression ("#text1 + ''" );
602
+ assertThat (expr .getValue (context , String .class )).hasSize (maxSize );
603
+
604
+ expr = parser .parseExpression ("'' + #text1" );
605
+ assertThat (expr .getValue (context , String .class )).hasSize (maxSize );
606
+
607
+ context .setVariable ("text1" , createString (maxSize / 2 ));
608
+ expr = parser .parseExpression ("#text1 + #text1" );
609
+ assertThat (expr .getValue (context , String .class )).hasSize (maxSize );
610
+
611
+ // Text is too big
612
+ context .setVariable ("text1" , createString (maxSize + 1 ));
613
+ evaluateAndCheckError ("#text1 + ''" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 7 );
614
+ evaluateAndCheckError ("#text1 + true" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 7 );
615
+ evaluateAndCheckError ("'' + #text1" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 3 );
616
+ evaluateAndCheckError ("true + #text1" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 5 );
617
+
618
+ context .setVariable ("text1" , createString (maxSize / 2 ));
619
+ context .setVariable ("text2" , createString ((maxSize / 2 ) + 1 ));
620
+ evaluateAndCheckError ("#text1 + #text2" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 7 );
621
+ evaluateAndCheckError ("#text1 + #text2 + true" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 7 );
622
+ evaluateAndCheckError ("#text1 + true + #text2" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 14 );
623
+ evaluateAndCheckError ("true + #text1 + #text2" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 14 );
624
+
625
+ evaluateAndCheckError ("#text2 + #text1" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 7 );
626
+ evaluateAndCheckError ("#text2 + #text1 + true" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 7 );
627
+ evaluateAndCheckError ("#text2 + true + #text1" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 14 );
628
+ evaluateAndCheckError ("true + #text2 + #text1" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 14 );
629
+
630
+ context .setVariable ("text1" , createString ((maxSize / 3 ) + 1 ));
631
+ evaluateAndCheckError ("#text1 + #text1 + #text1" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 16 );
632
+ evaluateAndCheckError ("(#text1 + #text1) + #text1" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 18 );
633
+ evaluateAndCheckError ("#text1 + (#text1 + #text1)" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 7 );
634
+ }
635
+
636
+ private static String createString (int size ) {
637
+ return new String (new char [size ]);
638
+ }
639
+
590
640
@ Test
591
641
public void testLongs () {
592
642
evaluate ("3L == 4L" , false , Boolean .class );
0 commit comments