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