35
35
import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
36
36
37
37
/**
38
- * Tests for SpEL's plus operator.
38
+ * Tests for SpEL's {@link OpPlus} operator.
39
39
*
40
40
* @author Ivo Smid
41
41
* @author Chris Beams
42
+ * @author Sam Brannen
42
43
* @since 3.2
43
- * @see OpPlus
44
44
*/
45
45
class OpPlusTests {
46
46
47
+ private final ExpressionState expressionState = new ExpressionState (new StandardEvaluationContext ());
48
+
49
+
47
50
@ Test
48
- void test_emptyOperands () {
49
- assertThatIllegalArgumentException ().isThrownBy (() ->
50
- new OpPlus (-1 , -1 ));
51
+ void emptyOperands () {
52
+ assertThatIllegalArgumentException ().isThrownBy (() -> new OpPlus (-1 , -1 ));
51
53
}
52
54
53
55
@ 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
+ }
56
74
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
+ }
58
85
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 ());
62
95
}
63
96
64
97
@ 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 );
97
107
}
98
108
99
109
@ 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 );
135
119
}
136
120
137
121
@ 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
+ }
140
132
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 );
145
139
146
140
assertThat (value .getTypeDescriptor ().getObjectType ()).isEqualTo (String .class );
147
141
assertThat (value .getTypeDescriptor ().getType ()).isEqualTo (String .class );
148
142
assertThat (value .getValue ()).isEqualTo ("foobar" );
149
143
}
150
144
151
145
@ 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 );
159
151
160
152
assertThat (value .getTypeDescriptor ().getObjectType ()).isEqualTo (String .class );
161
153
assertThat (value .getTypeDescriptor ().getType ()).isEqualTo (String .class );
162
154
assertThat (value .getValue ()).isEqualTo ("number is 123" );
163
155
}
164
156
165
157
@ 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 );
173
163
174
164
assertThat (value .getTypeDescriptor ().getObjectType ()).isEqualTo (String .class );
175
165
assertThat (value .getTypeDescriptor ().getType ()).isEqualTo (String .class );
176
166
assertThat (value .getValue ()).isEqualTo ("123 is a number" );
177
167
}
178
168
179
169
@ Test
180
- void test_binaryPlusWithTime_ToString () {
181
- ExpressionState expressionState = new ExpressionState (new StandardEvaluationContext ());
170
+ void binaryPlusWithSqlTimeToString () {
182
171
Time time = new Time (new Date ().getTime ());
183
172
184
173
VariableReference var = new VariableReference ("timeVar" , -1 , -1 );
185
174
var .setValue (expressionState , time );
186
175
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 );
190
179
191
180
assertThat (value .getTypeDescriptor ().getObjectType ()).isEqualTo (String .class );
192
181
assertThat (value .getTypeDescriptor ().getType ()).isEqualTo (String .class );
193
- assertThat (value .getValue ()).isEqualTo (( time + " is now" ) );
182
+ assertThat (value .getValue ()).isEqualTo (time + " is now" );
194
183
}
195
184
196
185
@ Test
197
- void test_binaryPlusWithTimeConverted () {
186
+ void binaryPlusWithTimeConverted () {
198
187
SimpleDateFormat format = new SimpleDateFormat ("hh :--: mm :--: ss" , Locale .ENGLISH );
199
188
200
189
GenericConversionService conversionService = new GenericConversionService ();
@@ -209,13 +198,13 @@ void test_binaryPlusWithTimeConverted() {
209
198
VariableReference var = new VariableReference ("timeVar" , -1 , -1 );
210
199
var .setValue (expressionState , time );
211
200
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 );
215
204
216
205
assertThat (value .getTypeDescriptor ().getObjectType ()).isEqualTo (String .class );
217
206
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" );
219
208
}
220
209
221
210
}
0 commit comments