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
/**
34
35
* @author Andy Clement
35
36
* @author Juergen Hoeller
36
37
* @author Giovanni Dall'Oglio Risso
38
+ * @author Sam Brannen
37
39
*/
38
40
class OperatorTests extends AbstractExpressionTests {
39
41
@@ -392,11 +394,7 @@ void plus() {
392
394
evaluate ("3.0f + 5.0f" , 8.0f , Float .class );
393
395
evaluate ("3.0d + 5.0d" , 8.0d , Double .class );
394
396
evaluate ("3 + new java.math.BigDecimal('5')" , new BigDecimal ("8" ), BigDecimal .class );
395
-
396
- evaluate ("'ab' + 2" , "ab2" , String .class );
397
- evaluate ("2 + 'a'" , "2a" , String .class );
398
- evaluate ("'ab' + null" , "abnull" , String .class );
399
- evaluate ("null + 'ab'" , "nullab" , String .class );
397
+ evaluate ("5 + new Integer('37')" , 42 , Integer .class );
400
398
401
399
// AST:
402
400
SpelExpression expr = (SpelExpression ) parser .parseExpression ("+3" );
@@ -410,11 +408,6 @@ void plus() {
410
408
evaluate ("+5" , 5 , Integer .class );
411
409
evaluate ("+new java.math.BigDecimal('5')" , new BigDecimal ("5" ), BigDecimal .class );
412
410
evaluateAndCheckError ("+'abc'" , SpelMessage .OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES );
413
-
414
- // string concatenation
415
- evaluate ("'abc'+'def'" , "abcdef" , String .class );
416
-
417
- evaluate ("5 + new Integer('37')" , 42 , Integer .class );
418
411
}
419
412
420
413
@ Test
@@ -588,6 +581,59 @@ void stringRepeat() {
588
581
evaluateAndCheckError ("'a' * 257" , String .class , MAX_REPEATED_TEXT_SIZE_EXCEEDED , 4 );
589
582
}
590
583
584
+ @ Test
585
+ void stringConcatenation () {
586
+ evaluate ("'' + ''" , "" , String .class );
587
+ evaluate ("'' + null" , "null" , String .class );
588
+ evaluate ("null + ''" , "null" , String .class );
589
+ evaluate ("'ab' + null" , "abnull" , String .class );
590
+ evaluate ("null + 'ab'" , "nullab" , String .class );
591
+ evaluate ("'ab' + 2" , "ab2" , String .class );
592
+ evaluate ("2 + 'ab'" , "2ab" , String .class );
593
+ evaluate ("'abc' + 'def'" , "abcdef" , String .class );
594
+
595
+ // Text is big but not too big
596
+ final int maxSize = 100_000 ;
597
+ context .setVariable ("text1" , createString (maxSize ));
598
+ Expression expr = parser .parseExpression ("#text1 + ''" );
599
+ assertThat (expr .getValue (context , String .class )).hasSize (maxSize );
600
+
601
+ expr = parser .parseExpression ("'' + #text1" );
602
+ assertThat (expr .getValue (context , String .class )).hasSize (maxSize );
603
+
604
+ context .setVariable ("text1" , createString (maxSize / 2 ));
605
+ expr = parser .parseExpression ("#text1 + #text1" );
606
+ assertThat (expr .getValue (context , String .class )).hasSize (maxSize );
607
+
608
+ // Text is too big
609
+ context .setVariable ("text1" , createString (maxSize + 1 ));
610
+ evaluateAndCheckError ("#text1 + ''" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 7 );
611
+ evaluateAndCheckError ("#text1 + true" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 7 );
612
+ evaluateAndCheckError ("'' + #text1" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 3 );
613
+ evaluateAndCheckError ("true + #text1" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 5 );
614
+
615
+ context .setVariable ("text1" , createString (maxSize / 2 ));
616
+ context .setVariable ("text2" , createString ((maxSize / 2 ) + 1 ));
617
+ evaluateAndCheckError ("#text1 + #text2" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 7 );
618
+ evaluateAndCheckError ("#text1 + #text2 + true" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 7 );
619
+ evaluateAndCheckError ("#text1 + true + #text2" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 14 );
620
+ evaluateAndCheckError ("true + #text1 + #text2" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 14 );
621
+
622
+ evaluateAndCheckError ("#text2 + #text1" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 7 );
623
+ evaluateAndCheckError ("#text2 + #text1 + true" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 7 );
624
+ evaluateAndCheckError ("#text2 + true + #text1" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 14 );
625
+ evaluateAndCheckError ("true + #text2 + #text1" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 14 );
626
+
627
+ context .setVariable ("text1" , createString ((maxSize / 3 ) + 1 ));
628
+ evaluateAndCheckError ("#text1 + #text1 + #text1" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 16 );
629
+ evaluateAndCheckError ("(#text1 + #text1) + #text1" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 18 );
630
+ evaluateAndCheckError ("#text1 + (#text1 + #text1)" , String .class , MAX_CONCATENATED_STRING_LENGTH_EXCEEDED , 7 );
631
+ }
632
+
633
+ private static String createString (int size ) {
634
+ return new String (new char [size ]);
635
+ }
636
+
591
637
@ Test
592
638
void longs () {
593
639
evaluate ("3L == 4L" , false , Boolean .class );
0 commit comments