19
19
20
20
import java .util .Map ;
21
21
22
+ import org .jetbrains .annotations .Nullable ;
22
23
import org .junit .jupiter .api .BeforeEach ;
23
24
import org .junit .jupiter .api .Test ;
24
25
38
39
*/
39
40
public class ValueEvaluationUnitTests {
40
41
42
+ private ValueExpressionParser parser = ValueExpressionParser .create ();
41
43
private ValueEvaluationContext evaluationContext ;
42
44
43
45
@ BeforeEach
@@ -47,6 +49,10 @@ void setUp() {
47
49
StandardEnvironment environment = new StandardEnvironment ();
48
50
environment .getPropertySources ().addFirst (propertySource );
49
51
52
+ record MyRecord (String foo , @ org .springframework .lang .Nullable String bar ) {
53
+
54
+ }
55
+
50
56
this .evaluationContext = new ValueEvaluationContext () {
51
57
@ Override
52
58
public Environment getEnvironment () {
@@ -57,6 +63,8 @@ public Environment getEnvironment() {
57
63
public EvaluationContext getEvaluationContext () {
58
64
StandardEvaluationContext context = new StandardEvaluationContext ();
59
65
context .setVariable ("contextVar" , "contextVal" );
66
+ context .setVariable ("nullVar" , null );
67
+ context .setVariable ("someRecord" , new MyRecord ("foo" , null ));
60
68
61
69
return context ;
62
70
}
@@ -82,6 +90,20 @@ void shouldParseAndEvaluateExpressions() {
82
90
.withMessageContaining ("Could not resolve placeholder 'env.does.not.exist'" );
83
91
}
84
92
93
+ @ Test // GH-3169
94
+ void shouldReturnValueType () {
95
+
96
+ assertThat (getValueType ("foo" )).isEqualTo (String .class );
97
+ assertThat (getValueType ("${env.key.one}" )).isEqualTo (String .class );
98
+ assertThat (getValueType ("#{'foo'}" )).isEqualTo (String .class );
99
+ assertThat (getValueType ("#{1+1}" )).isEqualTo (Integer .class );
100
+ assertThat (getValueType ("#{null}" )).isNull ();
101
+ assertThat (getValueType ("#{#contextVar}" )).isEqualTo (String .class );
102
+ assertThat (getValueType ("#{#nullVar}" )).isNull ();
103
+ assertThat (getValueType ("#{#someRecord.foo}" )).isEqualTo (String .class );
104
+ assertThat (getValueType ("#{#someRecord.bar}" )).isEqualTo (String .class );
105
+ }
106
+
85
107
@ Test // GH-2369
86
108
void shouldParseLiteral () {
87
109
@@ -128,10 +150,14 @@ void shouldParseQuoted() {
128
150
assertThat (eval ("#{(1+1) + \" -foo'}\" + '-bar}'}" )).isEqualTo ("2-foo'}-bar}" );
129
151
}
130
152
153
+ @ SuppressWarnings ("DataFlowIssue" )
131
154
private String eval (String expressionString ) {
132
-
133
- ValueExpressionParser parser = ValueExpressionParser .create ();
134
155
return (String ) parser .parse (expressionString ).evaluate (evaluationContext );
135
156
}
136
157
158
+ @ Nullable
159
+ private Class <?> getValueType (String expressionString ) {
160
+ return parser .parse (expressionString ).getValueType (evaluationContext );
161
+ }
162
+
137
163
}
0 commit comments