1
1
/*
2
- * Copyright 2002-2022 the original author or authors.
2
+ * Copyright 2002-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
34
34
/**
35
35
* @author Andy Clement
36
36
* @author Juergen Hoeller
37
+ * @author Sam Brannen
37
38
*/
38
- public class TemplateExpressionParsingTests extends AbstractExpressionTests {
39
-
40
- public static final ParserContext DEFAULT_TEMPLATE_PARSER_CONTEXT = new ParserContext () {
41
- @ Override
42
- public String getExpressionPrefix () {
43
- return "${" ;
44
- }
45
- @ Override
46
- public String getExpressionSuffix () {
47
- return "}" ;
48
- }
49
- @ Override
50
- public boolean isTemplate () {
51
- return true ;
52
- }
53
- };
54
-
55
- public static final ParserContext HASH_DELIMITED_PARSER_CONTEXT = new ParserContext () {
56
- @ Override
57
- public String getExpressionPrefix () {
58
- return "#{" ;
59
- }
60
- @ Override
61
- public String getExpressionSuffix () {
62
- return "}" ;
63
- }
64
- @ Override
65
- public boolean isTemplate () {
66
- return true ;
67
- }
68
- };
39
+ class TemplateExpressionParsingTests extends AbstractExpressionTests {
40
+
41
+ static final ParserContext DOLLAR_SIGN_TEMPLATE_PARSER_CONTEXT = new TemplateParserContext ("${" , "}" );
69
42
70
43
71
44
@ Test
72
- public void testParsingSimpleTemplateExpression01 () throws Exception {
45
+ void parsingSimpleTemplateExpression01 () throws Exception {
73
46
SpelExpressionParser parser = new SpelExpressionParser ();
74
- Expression expr = parser .parseExpression ("hello ${'world'}" , DEFAULT_TEMPLATE_PARSER_CONTEXT );
47
+ Expression expr = parser .parseExpression ("hello ${'world'}" , DOLLAR_SIGN_TEMPLATE_PARSER_CONTEXT );
75
48
Object o = expr .getValue ();
76
49
assertThat (o .toString ()).isEqualTo ("hello world" );
77
50
}
78
51
79
52
@ Test
80
- public void testParsingSimpleTemplateExpression02 () throws Exception {
53
+ void parsingSimpleTemplateExpression02 () throws Exception {
81
54
SpelExpressionParser parser = new SpelExpressionParser ();
82
- Expression expr = parser .parseExpression ("hello ${'to'} you" , DEFAULT_TEMPLATE_PARSER_CONTEXT );
55
+ Expression expr = parser .parseExpression ("hello ${'to'} you" , DOLLAR_SIGN_TEMPLATE_PARSER_CONTEXT );
83
56
Object o = expr .getValue ();
84
57
assertThat (o .toString ()).isEqualTo ("hello to you" );
85
58
}
86
59
87
60
@ Test
88
- public void testParsingSimpleTemplateExpression03 () throws Exception {
61
+ void parsingSimpleTemplateExpression03 () throws Exception {
89
62
SpelExpressionParser parser = new SpelExpressionParser ();
90
63
Expression expr = parser .parseExpression ("The quick ${'brown'} fox jumped over the ${'lazy'} dog" ,
91
- DEFAULT_TEMPLATE_PARSER_CONTEXT );
64
+ DOLLAR_SIGN_TEMPLATE_PARSER_CONTEXT );
92
65
Object o = expr .getValue ();
93
66
assertThat (o .toString ()).isEqualTo ("The quick brown fox jumped over the lazy dog" );
94
67
}
95
68
96
69
@ Test
97
- public void testParsingSimpleTemplateExpression04 () throws Exception {
70
+ void parsingSimpleTemplateExpression04 () throws Exception {
98
71
SpelExpressionParser parser = new SpelExpressionParser ();
99
- Expression expr = parser .parseExpression ("${'hello'} world" , DEFAULT_TEMPLATE_PARSER_CONTEXT );
72
+ Expression expr = parser .parseExpression ("${'hello'} world" , DOLLAR_SIGN_TEMPLATE_PARSER_CONTEXT );
100
73
Object o = expr .getValue ();
101
74
assertThat (o .toString ()).isEqualTo ("hello world" );
102
75
103
- expr = parser .parseExpression ("" , DEFAULT_TEMPLATE_PARSER_CONTEXT );
76
+ expr = parser .parseExpression ("" , DOLLAR_SIGN_TEMPLATE_PARSER_CONTEXT );
104
77
o = expr .getValue ();
105
78
assertThat (o .toString ()).isEqualTo ("" );
106
79
107
- expr = parser .parseExpression ("abc" , DEFAULT_TEMPLATE_PARSER_CONTEXT );
80
+ expr = parser .parseExpression ("abc" , DOLLAR_SIGN_TEMPLATE_PARSER_CONTEXT );
108
81
o = expr .getValue ();
109
82
assertThat (o .toString ()).isEqualTo ("abc" );
110
83
111
- expr = parser .parseExpression ("abc" , DEFAULT_TEMPLATE_PARSER_CONTEXT );
84
+ expr = parser .parseExpression ("abc" , DOLLAR_SIGN_TEMPLATE_PARSER_CONTEXT );
112
85
o = expr .getValue ((Object )null );
113
86
assertThat (o .toString ()).isEqualTo ("abc" );
114
87
}
115
88
116
89
@ Test
117
- public void testCompositeStringExpression () throws Exception {
90
+ void compositeStringExpression () throws Exception {
118
91
SpelExpressionParser parser = new SpelExpressionParser ();
119
- Expression ex = parser .parseExpression ("hello ${'world'}" , DEFAULT_TEMPLATE_PARSER_CONTEXT );
92
+ Expression ex = parser .parseExpression ("hello ${'world'}" , DOLLAR_SIGN_TEMPLATE_PARSER_CONTEXT );
120
93
assertThat (ex .getValue ()).isInstanceOf (String .class ).isEqualTo ("hello world" );
121
94
assertThat (ex .getValue (String .class )).isInstanceOf (String .class ).isEqualTo ("hello world" );
122
95
assertThat (ex .getValue ((Object )null , String .class )).isInstanceOf (String .class ).isEqualTo ("hello world" );
@@ -154,15 +127,15 @@ public void testCompositeStringExpression() throws Exception {
154
127
static class Rooty {}
155
128
156
129
@ Test
157
- public void testNestedExpressions () throws Exception {
130
+ void nestedExpressions () throws Exception {
158
131
SpelExpressionParser parser = new SpelExpressionParser ();
159
132
// treat the nested ${..} as a part of the expression
160
- Expression ex = parser .parseExpression ("hello ${listOfNumbersUpToTen.$[#this<5]} world" ,DEFAULT_TEMPLATE_PARSER_CONTEXT );
133
+ Expression ex = parser .parseExpression ("hello ${listOfNumbersUpToTen.$[#this<5]} world" ,DOLLAR_SIGN_TEMPLATE_PARSER_CONTEXT );
161
134
String s = ex .getValue (TestScenarioCreator .getTestEvaluationContext (),String .class );
162
135
assertThat (s ).isEqualTo ("hello 4 world" );
163
136
164
137
// not a useful expression but tests nested expression syntax that clashes with template prefix/suffix
165
- ex = parser .parseExpression ("hello ${listOfNumbersUpToTen.$[#root.listOfNumbersUpToTen.$[#this%2==1]==3]} world" ,DEFAULT_TEMPLATE_PARSER_CONTEXT );
138
+ ex = parser .parseExpression ("hello ${listOfNumbersUpToTen.$[#root.listOfNumbersUpToTen.$[#this%2==1]==3]} world" ,DOLLAR_SIGN_TEMPLATE_PARSER_CONTEXT );
166
139
assertThat (ex .getClass ()).isEqualTo (CompositeStringExpression .class );
167
140
CompositeStringExpression cse = (CompositeStringExpression )ex ;
168
141
Expression [] exprs = cse .getExpressions ();
@@ -171,60 +144,59 @@ public void testNestedExpressions() throws Exception {
171
144
s = ex .getValue (TestScenarioCreator .getTestEvaluationContext (),String .class );
172
145
assertThat (s ).isEqualTo ("hello world" );
173
146
174
- ex = parser .parseExpression ("hello ${listOfNumbersUpToTen.$[#this<5]} ${listOfNumbersUpToTen.$[#this>5]} world" ,DEFAULT_TEMPLATE_PARSER_CONTEXT );
147
+ ex = parser .parseExpression ("hello ${listOfNumbersUpToTen.$[#this<5]} ${listOfNumbersUpToTen.$[#this>5]} world" ,DOLLAR_SIGN_TEMPLATE_PARSER_CONTEXT );
175
148
s = ex .getValue (TestScenarioCreator .getTestEvaluationContext (),String .class );
176
149
assertThat (s ).isEqualTo ("hello 4 10 world" );
177
150
178
151
assertThatExceptionOfType (ParseException .class ).isThrownBy (() ->
179
- parser .parseExpression ("hello ${listOfNumbersUpToTen.$[#this<5]} ${listOfNumbersUpToTen.$[#this>5] world" ,DEFAULT_TEMPLATE_PARSER_CONTEXT ))
152
+ parser .parseExpression ("hello ${listOfNumbersUpToTen.$[#this<5]} ${listOfNumbersUpToTen.$[#this>5] world" ,DOLLAR_SIGN_TEMPLATE_PARSER_CONTEXT ))
180
153
.satisfies (pex -> assertThat (pex .getSimpleMessage ()).isEqualTo ("No ending suffix '}' for expression starting at character 41: ${listOfNumbersUpToTen.$[#this>5] world" ));
181
154
182
155
assertThatExceptionOfType (ParseException .class ).isThrownBy (() ->
183
- parser .parseExpression ("hello ${listOfNumbersUpToTen.$[#root.listOfNumbersUpToTen.$[#this%2==1==3]} world" ,DEFAULT_TEMPLATE_PARSER_CONTEXT ))
156
+ parser .parseExpression ("hello ${listOfNumbersUpToTen.$[#root.listOfNumbersUpToTen.$[#this%2==1==3]} world" ,DOLLAR_SIGN_TEMPLATE_PARSER_CONTEXT ))
184
157
.satisfies (pex -> assertThat (pex .getSimpleMessage ()).isEqualTo ("Found closing '}' at position 74 but most recent opening is '[' at position 30" ));
185
158
}
186
159
187
160
@ Test
188
-
189
- public void testClashingWithSuffixes () throws Exception {
161
+ void clashingWithSuffixes () throws Exception {
190
162
// Just wanting to use the prefix or suffix within the template:
191
- Expression ex = parser .parseExpression ("hello ${3+4} world" ,DEFAULT_TEMPLATE_PARSER_CONTEXT );
163
+ Expression ex = parser .parseExpression ("hello ${3+4} world" ,DOLLAR_SIGN_TEMPLATE_PARSER_CONTEXT );
192
164
String s = ex .getValue (TestScenarioCreator .getTestEvaluationContext (),String .class );
193
165
assertThat (s ).isEqualTo ("hello 7 world" );
194
166
195
- ex = parser .parseExpression ("hello ${3+4} wo${'${'}rld" ,DEFAULT_TEMPLATE_PARSER_CONTEXT );
167
+ ex = parser .parseExpression ("hello ${3+4} wo${'${'}rld" ,DOLLAR_SIGN_TEMPLATE_PARSER_CONTEXT );
196
168
s = ex .getValue (TestScenarioCreator .getTestEvaluationContext (),String .class );
197
169
assertThat (s ).isEqualTo ("hello 7 wo${rld" );
198
170
199
- ex = parser .parseExpression ("hello ${3+4} wo}rld" ,DEFAULT_TEMPLATE_PARSER_CONTEXT );
171
+ ex = parser .parseExpression ("hello ${3+4} wo}rld" ,DOLLAR_SIGN_TEMPLATE_PARSER_CONTEXT );
200
172
s = ex .getValue (TestScenarioCreator .getTestEvaluationContext (),String .class );
201
173
assertThat (s ).isEqualTo ("hello 7 wo}rld" );
202
174
}
203
175
204
176
@ Test
205
- public void testParsingNormalExpressionThroughTemplateParser () throws Exception {
177
+ void parsingNormalExpressionThroughTemplateParser () throws Exception {
206
178
Expression expr = parser .parseExpression ("1+2+3" );
207
179
assertThat (expr .getValue ()).isEqualTo (6 );
208
180
}
209
181
210
182
@ Test
211
- public void testErrorCases () throws Exception {
183
+ void errorCases () throws Exception {
212
184
assertThatExceptionOfType (ParseException .class ).isThrownBy (() ->
213
- parser .parseExpression ("hello ${'world'" , DEFAULT_TEMPLATE_PARSER_CONTEXT ))
185
+ parser .parseExpression ("hello ${'world'" , DOLLAR_SIGN_TEMPLATE_PARSER_CONTEXT ))
214
186
.satisfies (pex -> {
215
187
assertThat (pex .getSimpleMessage ()).isEqualTo ("No ending suffix '}' for expression starting at character 6: ${'world'" );
216
188
assertThat (pex .getExpressionString ()).isEqualTo ("hello ${'world'" );
217
189
});
218
190
assertThatExceptionOfType (ParseException .class ).isThrownBy (() ->
219
- parser .parseExpression ("hello ${'wibble'${'world'}" , DEFAULT_TEMPLATE_PARSER_CONTEXT ))
191
+ parser .parseExpression ("hello ${'wibble'${'world'}" , DOLLAR_SIGN_TEMPLATE_PARSER_CONTEXT ))
220
192
.satisfies (pex -> assertThat (pex .getSimpleMessage ()).isEqualTo ("No ending suffix '}' for expression starting at character 6: ${'wibble'${'world'}" ));
221
193
assertThatExceptionOfType (ParseException .class ).isThrownBy (() ->
222
- parser .parseExpression ("hello ${} world" , DEFAULT_TEMPLATE_PARSER_CONTEXT ))
194
+ parser .parseExpression ("hello ${} world" , DOLLAR_SIGN_TEMPLATE_PARSER_CONTEXT ))
223
195
.satisfies (pex -> assertThat (pex .getSimpleMessage ()).isEqualTo ("No expression defined within delimiter '${}' at character 6" ));
224
196
}
225
197
226
198
@ Test
227
- public void testTemplateParserContext () {
199
+ void templateParserContext () {
228
200
TemplateParserContext tpc = new TemplateParserContext ("abc" ,"def" );
229
201
assertThat (tpc .getExpressionPrefix ()).isEqualTo ("abc" );
230
202
assertThat (tpc .getExpressionSuffix ()).isEqualTo ("def" );
0 commit comments