3535import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
3636
3737/**
38- * Tests for SpEL's plus operator.
38+ * Tests for SpEL's {@link OpPlus} operator.
3939 *
4040 * @author Ivo Smid
4141 * @author Chris Beams
42+ * @author Sam Brannen
4243 * @since 3.2
43- * @see OpPlus
4444 */
4545class OpPlusTests {
4646
47+ private final ExpressionState expressionState = new ExpressionState (new StandardEvaluationContext ());
48+
49+
4750 @ Test
48- void test_emptyOperands () {
49- assertThatIllegalArgumentException ().isThrownBy (() ->
50- new OpPlus (-1 , -1 ));
51+ void emptyOperands () {
52+ assertThatIllegalArgumentException ().isThrownBy (() -> new OpPlus (-1 , -1 ));
5153 }
5254
5355 @ Test
54- void test_unaryPlusWithStringLiteral () {
55- ExpressionState expressionState = new ExpressionState (new StandardEvaluationContext ());
56+ void unaryPlusWithStringLiteral () {
57+ StringLiteral stringLiteral = new StringLiteral ("word" , -1 , -1 , "word" );
58+
59+ OpPlus operator = new OpPlus (-1 , -1 , stringLiteral );
60+ assertThatExceptionOfType (SpelEvaluationException .class )
61+ .isThrownBy (() -> operator .getValueInternal (expressionState ));
62+ }
63+
64+ @ Test
65+ void unaryPlusWithIntegerOperand () {
66+ IntLiteral intLiteral = new IntLiteral ("123" , -1 , -1 , 123 );
67+ OpPlus operator = new OpPlus (-1 , -1 , intLiteral );
68+ TypedValue value = operator .getValueInternal (expressionState );
69+
70+ assertThat (value .getTypeDescriptor ().getObjectType ()).isEqualTo (Integer .class );
71+ assertThat (value .getTypeDescriptor ().getType ()).isEqualTo (Integer .class );
72+ assertThat (value .getValue ()).isEqualTo (intLiteral .getLiteralValue ().getValue ());
73+ }
5674
57- StringLiteral str = new StringLiteral ("word" , -1 , -1 , "word" );
75+ @ Test
76+ void unaryPlusWithLongOperand () {
77+ LongLiteral longLiteral = new LongLiteral ("123" , -1 , -1 , 123L );
78+ OpPlus operator = new OpPlus (-1 , -1 , longLiteral );
79+ TypedValue value = operator .getValueInternal (expressionState );
80+
81+ assertThat (value .getTypeDescriptor ().getObjectType ()).isEqualTo (Long .class );
82+ assertThat (value .getTypeDescriptor ().getType ()).isEqualTo (Long .class );
83+ assertThat (value .getValue ()).isEqualTo (longLiteral .getLiteralValue ().getValue ());
84+ }
5885
59- OpPlus o = new OpPlus (-1 , -1 , str );
60- assertThatExceptionOfType (SpelEvaluationException .class ).isThrownBy (() ->
61- o .getValueInternal (expressionState ));
86+ @ Test
87+ void unaryPlusWithRealOperand () {
88+ RealLiteral realLiteral = new RealLiteral ("123.00" , -1 , -1 , 123.0 );
89+ OpPlus operator = new OpPlus (-1 , -1 , realLiteral );
90+ TypedValue value = operator .getValueInternal (expressionState );
91+
92+ assertThat (value .getTypeDescriptor ().getObjectType ()).isEqualTo (Double .class );
93+ assertThat (value .getTypeDescriptor ().getType ()).isEqualTo (Double .class );
94+ assertThat (value .getValue ()).isEqualTo (realLiteral .getLiteralValue ().getValue ());
6295 }
6396
6497 @ Test
65- void test_unaryPlusWithNumberOperand () {
66- ExpressionState expressionState = new ExpressionState (new StandardEvaluationContext ());
67-
68- {
69- RealLiteral realLiteral = new RealLiteral ("123.00" , -1 , -1 , 123.0 );
70- OpPlus o = new OpPlus (-1 , -1 , realLiteral );
71- TypedValue value = o .getValueInternal (expressionState );
72-
73- assertThat (value .getTypeDescriptor ().getObjectType ()).isEqualTo (Double .class );
74- assertThat (value .getTypeDescriptor ().getType ()).isEqualTo (Double .class );
75- assertThat (value .getValue ()).isEqualTo (realLiteral .getLiteralValue ().getValue ());
76- }
77-
78- {
79- IntLiteral intLiteral = new IntLiteral ("123" , -1 , -1 , 123 );
80- OpPlus o = new OpPlus (-1 , -1 , intLiteral );
81- TypedValue value = o .getValueInternal (expressionState );
82-
83- assertThat (value .getTypeDescriptor ().getObjectType ()).isEqualTo (Integer .class );
84- assertThat (value .getTypeDescriptor ().getType ()).isEqualTo (Integer .class );
85- assertThat (value .getValue ()).isEqualTo (intLiteral .getLiteralValue ().getValue ());
86- }
87-
88- {
89- LongLiteral longLiteral = new LongLiteral ("123" , -1 , -1 , 123L );
90- OpPlus o = new OpPlus (-1 , -1 , longLiteral );
91- TypedValue value = o .getValueInternal (expressionState );
92-
93- assertThat (value .getTypeDescriptor ().getObjectType ()).isEqualTo (Long .class );
94- assertThat (value .getTypeDescriptor ().getType ()).isEqualTo (Long .class );
95- assertThat (value .getValue ()).isEqualTo (longLiteral .getLiteralValue ().getValue ());
96- }
98+ void binaryPlusWithIntegerOperands () {
99+ IntLiteral n1 = new IntLiteral ("123" , -1 , -1 , 123 );
100+ IntLiteral n2 = new IntLiteral ("456" , -1 , -1 , 456 );
101+ OpPlus operator = new OpPlus (-1 , -1 , n1 , n2 );
102+ TypedValue value = operator .getValueInternal (expressionState );
103+
104+ assertThat (value .getTypeDescriptor ().getObjectType ()).isEqualTo (Integer .class );
105+ assertThat (value .getTypeDescriptor ().getType ()).isEqualTo (Integer .class );
106+ assertThat (value .getValue ()).isEqualTo (123 + 456 );
97107 }
98108
99109 @ Test
100- void test_binaryPlusWithNumberOperands () {
101- ExpressionState expressionState = new ExpressionState (new StandardEvaluationContext ());
102-
103- {
104- RealLiteral n1 = new RealLiteral ("123.00" , -1 , -1 , 123.0 );
105- RealLiteral n2 = new RealLiteral ("456.00" , -1 , -1 , 456.0 );
106- OpPlus o = new OpPlus (-1 , -1 , n1 , n2 );
107- TypedValue value = o .getValueInternal (expressionState );
108-
109- assertThat (value .getTypeDescriptor ().getObjectType ()).isEqualTo (Double .class );
110- assertThat (value .getTypeDescriptor ().getType ()).isEqualTo (Double .class );
111- assertThat (value .getValue ()).isEqualTo (123.0 + 456.0 );
112- }
113-
114- {
115- LongLiteral n1 = new LongLiteral ("123" , -1 , -1 , 123L );
116- LongLiteral n2 = new LongLiteral ("456" , -1 , -1 , 456L );
117- OpPlus o = new OpPlus (-1 , -1 , n1 , n2 );
118- TypedValue value = o .getValueInternal (expressionState );
119-
120- assertThat (value .getTypeDescriptor ().getObjectType ()).isEqualTo (Long .class );
121- assertThat (value .getTypeDescriptor ().getType ()).isEqualTo (Long .class );
122- assertThat (value .getValue ()).isEqualTo (123L + 456L );
123- }
124-
125- {
126- IntLiteral n1 = new IntLiteral ("123" , -1 , -1 , 123 );
127- IntLiteral n2 = new IntLiteral ("456" , -1 , -1 , 456 );
128- OpPlus o = new OpPlus (-1 , -1 , n1 , n2 );
129- TypedValue value = o .getValueInternal (expressionState );
130-
131- assertThat (value .getTypeDescriptor ().getObjectType ()).isEqualTo (Integer .class );
132- assertThat (value .getTypeDescriptor ().getType ()).isEqualTo (Integer .class );
133- assertThat (value .getValue ()).isEqualTo (123 + 456 );
134- }
110+ void binaryPlusWithLongOperands () {
111+ LongLiteral n1 = new LongLiteral ("123" , -1 , -1 , 123L );
112+ LongLiteral n2 = new LongLiteral ("456" , -1 , -1 , 456L );
113+ OpPlus operator = new OpPlus (-1 , -1 , n1 , n2 );
114+ TypedValue value = operator .getValueInternal (expressionState );
115+
116+ assertThat (value .getTypeDescriptor ().getObjectType ()).isEqualTo (Long .class );
117+ assertThat (value .getTypeDescriptor ().getType ()).isEqualTo (Long .class );
118+ assertThat (value .getValue ()).isEqualTo (123L + 456L );
135119 }
136120
137121 @ Test
138- void test_binaryPlusWithStringOperands () {
139- ExpressionState expressionState = new ExpressionState (new StandardEvaluationContext ());
122+ void binaryPlusWithRealOperands () {
123+ RealLiteral n1 = new RealLiteral ("123.00" , -1 , -1 , 123.0 );
124+ RealLiteral n2 = new RealLiteral ("456.00" , -1 , -1 , 456.0 );
125+ OpPlus operator = new OpPlus (-1 , -1 , n1 , n2 );
126+ TypedValue value = operator .getValueInternal (expressionState );
127+
128+ assertThat (value .getTypeDescriptor ().getObjectType ()).isEqualTo (Double .class );
129+ assertThat (value .getTypeDescriptor ().getType ()).isEqualTo (Double .class );
130+ assertThat (value .getValue ()).isEqualTo (123.0 + 456.0 );
131+ }
140132
141- StringLiteral n1 = new StringLiteral ("\" foo\" " , -1 , -1 , "\" foo\" " );
142- StringLiteral n2 = new StringLiteral ("\" bar\" " , -1 , -1 , "\" bar\" " );
143- OpPlus o = new OpPlus (-1 , -1 , n1 , n2 );
144- TypedValue value = o .getValueInternal (expressionState );
133+ @ Test
134+ void binaryPlusWithStringOperands () {
135+ StringLiteral str1 = new StringLiteral ("\" foo\" " , -1 , -1 , "\" foo\" " );
136+ StringLiteral str2 = new StringLiteral ("\" bar\" " , -1 , -1 , "\" bar\" " );
137+ OpPlus operator = new OpPlus (-1 , -1 , str1 , str2 );
138+ TypedValue value = operator .getValueInternal (expressionState );
145139
146140 assertThat (value .getTypeDescriptor ().getObjectType ()).isEqualTo (String .class );
147141 assertThat (value .getTypeDescriptor ().getType ()).isEqualTo (String .class );
148142 assertThat (value .getValue ()).isEqualTo ("foobar" );
149143 }
150144
151145 @ Test
152- void test_binaryPlusWithLeftStringOperand () {
153- ExpressionState expressionState = new ExpressionState (new StandardEvaluationContext ());
154-
155- StringLiteral n1 = new StringLiteral ("\" number is \" " , -1 , -1 , "\" number is \" " );
156- LongLiteral n2 = new LongLiteral ("123" , -1 , -1 , 123 );
157- OpPlus o = new OpPlus (-1 , -1 , n1 , n2 );
158- TypedValue value = o .getValueInternal (expressionState );
146+ void binaryPlusWithLeftStringOperand () {
147+ StringLiteral stringLiteral = new StringLiteral ("\" number is \" " , -1 , -1 , "\" number is \" " );
148+ LongLiteral longLiteral = new LongLiteral ("123" , -1 , -1 , 123 );
149+ OpPlus operator = new OpPlus (-1 , -1 , stringLiteral , longLiteral );
150+ TypedValue value = operator .getValueInternal (expressionState );
159151
160152 assertThat (value .getTypeDescriptor ().getObjectType ()).isEqualTo (String .class );
161153 assertThat (value .getTypeDescriptor ().getType ()).isEqualTo (String .class );
162154 assertThat (value .getValue ()).isEqualTo ("number is 123" );
163155 }
164156
165157 @ Test
166- void test_binaryPlusWithRightStringOperand () {
167- ExpressionState expressionState = new ExpressionState (new StandardEvaluationContext ());
168-
169- LongLiteral n1 = new LongLiteral ("123" , -1 , -1 , 123 );
170- StringLiteral n2 = new StringLiteral ("\" is a number\" " , -1 , -1 , "\" is a number\" " );
171- OpPlus o = new OpPlus (-1 , -1 , n1 , n2 );
172- TypedValue value = o .getValueInternal (expressionState );
158+ void binaryPlusWithRightStringOperand () {
159+ LongLiteral longLiteral = new LongLiteral ("123" , -1 , -1 , 123 );
160+ StringLiteral stringLiteral = new StringLiteral ("\" is a number\" " , -1 , -1 , "\" is a number\" " );
161+ OpPlus operator = new OpPlus (-1 , -1 , longLiteral , stringLiteral );
162+ TypedValue value = operator .getValueInternal (expressionState );
173163
174164 assertThat (value .getTypeDescriptor ().getObjectType ()).isEqualTo (String .class );
175165 assertThat (value .getTypeDescriptor ().getType ()).isEqualTo (String .class );
176166 assertThat (value .getValue ()).isEqualTo ("123 is a number" );
177167 }
178168
179169 @ Test
180- void test_binaryPlusWithTime_ToString () {
181- ExpressionState expressionState = new ExpressionState (new StandardEvaluationContext ());
170+ void binaryPlusWithSqlTimeToString () {
182171 Time time = new Time (new Date ().getTime ());
183172
184173 VariableReference var = new VariableReference ("timeVar" , -1 , -1 );
185174 var .setValue (expressionState , time );
186175
187- StringLiteral n2 = new StringLiteral ("\" is now\" " , -1 , -1 , "\" is now\" " );
188- OpPlus o = new OpPlus (-1 , -1 , var , n2 );
189- TypedValue value = o .getValueInternal (expressionState );
176+ StringLiteral stringLiteral = new StringLiteral ("\" is now\" " , -1 , -1 , "\" is now\" " );
177+ OpPlus operator = new OpPlus (-1 , -1 , var , stringLiteral );
178+ TypedValue value = operator .getValueInternal (expressionState );
190179
191180 assertThat (value .getTypeDescriptor ().getObjectType ()).isEqualTo (String .class );
192181 assertThat (value .getTypeDescriptor ().getType ()).isEqualTo (String .class );
193- assertThat (value .getValue ()).isEqualTo (( time + " is now" ) );
182+ assertThat (value .getValue ()).isEqualTo (time + " is now" );
194183 }
195184
196185 @ Test
197- void test_binaryPlusWithTimeConverted () {
186+ void binaryPlusWithTimeConverted () {
198187 SimpleDateFormat format = new SimpleDateFormat ("hh :--: mm :--: ss" , Locale .ENGLISH );
199188
200189 GenericConversionService conversionService = new GenericConversionService ();
@@ -209,13 +198,13 @@ void test_binaryPlusWithTimeConverted() {
209198 VariableReference var = new VariableReference ("timeVar" , -1 , -1 );
210199 var .setValue (expressionState , time );
211200
212- StringLiteral n2 = new StringLiteral ("\" is now\" " , -1 , -1 , "\" is now\" " );
213- OpPlus o = new OpPlus (-1 , -1 , var , n2 );
214- TypedValue value = o .getValueInternal (expressionState );
201+ StringLiteral stringLiteral = new StringLiteral ("\" is now\" " , -1 , -1 , "\" is now\" " );
202+ OpPlus operator = new OpPlus (-1 , -1 , var , stringLiteral );
203+ TypedValue value = operator .getValueInternal (expressionState );
215204
216205 assertThat (value .getTypeDescriptor ().getObjectType ()).isEqualTo (String .class );
217206 assertThat (value .getTypeDescriptor ().getType ()).isEqualTo (String .class );
218- assertThat (value .getValue ()).isEqualTo (( format .format (time ) + " is now" ) );
207+ assertThat (value .getValue ()).isEqualTo (format .format (time ) + " is now" );
219208 }
220209
221210}
0 commit comments