File: TestELParser.java

package info (click to toggle)
tomcat9 9.0.115-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 48,140 kB
  • sloc: java: 383,595; xml: 71,225; jsp: 4,682; sh: 1,228; perl: 324; makefile: 18; ansic: 14
file content (322 lines) | stat: -rw-r--r-- 8,620 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jasper.compiler;

import javax.el.ELContext;
import javax.el.ELException;
import javax.el.ELManager;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;

import org.junit.Assert;
import org.junit.Test;

import org.apache.jasper.JasperException;
import org.apache.jasper.compiler.ELNode.Nodes;
import org.apache.jasper.compiler.ELParser.TextBuilder;

/**
 * You will need to keep your wits about you when working with this class. Keep in mind the following:
 * <ul>
 * <li>If in doubt, read the EL and JSP specifications. Twice.</li>
 * <li>The escaping rules are complex and subtle. The explanation below (as well as the tests and the implementation)
 * may have missed an edge case despite trying hard not to.
 * <li>The strings passed to {@link #doTestParser(String,String)} are Java escaped in the source code and will be
 * unescaped before being used.</li>
 * <li>LiteralExpressions always occur outside of "${...}" and "#{...}". Literal expressions escape '$' and '#' with
 * '\\'</li>
 * <li>LiteralStrings always occur inside "${...}" or "#{...}". Literal strings escape '\'', '\"' and '\\' with '\\'.
 * Escaping '\"' is optional if the literal string is delimited by '\''. Escaping '\'' is optional if the literal string
 * is delimited by '\"'.</li>
 * </ul>
 */
public class TestELParser {

    @Test
    public void testText() throws JasperException {
        doTestParser("foo", "foo");
    }


    @Test
    public void testLiteral() throws JasperException {
        doTestParser("${'foo'}", "foo");
    }


    @Test
    public void testVariable() throws JasperException {
        doTestParser("${test}", null);
    }


    @Test
    public void testFunction01() throws JasperException {
        doTestParser("${do(x)}", null);
    }


    @Test
    public void testFunction02() throws JasperException {
        doTestParser("${do:it(x)}", null);
    }


    @Test
    public void testFunction03() throws JasperException {
        doTestParser("${do:it(x,y)}", null);
    }


    @Test
    public void testFunction04() throws JasperException {
        doTestParser("${do:it(x,y,z)}", null);
    }


    @Test
    public void testFunction05() throws JasperException {
        doTestParser("${do:it(x, '\\\\y',z)}", null);
    }


    @Test
    public void testCompound01() throws JasperException {
        doTestParser("1${'foo'}1", "1foo1");
    }


    @Test
    public void testCompound02() throws JasperException {
        doTestParser("1${test}1", null);
    }


    @Test
    public void testCompound03() throws JasperException {
        doTestParser("${foo}${bar}", null);
    }


    @Test
    public void testTernary01() throws JasperException {
        doTestParser("${true?true:false}", "true");
    }


    @Test
    public void testTernary02() throws JasperException {
        doTestParser("${a==1?true:false}", null);
    }


    @Test
    public void testTernary03() throws JasperException {
        doTestParser("${a eq1?true:false}", null);
    }


    @Test
    public void testTernary04() throws JasperException {
        doTestParser(" ${ a eq 1 ? true : false } ", null);
    }


    @Test
    public void testTernary05() throws JasperException {
        // Note this is invalid EL
        doTestParser("${aeq1?true:false}", null);
    }


    @Test
    public void testTernary06() throws JasperException {
        doTestParser("${do:it(a eq1?true:false,y)}", null);
    }


    @Test
    public void testTernary07() throws JasperException {
        doTestParser(" ${ do:it( a eq 1 ? true : false, y ) } ", null);
    }


    @Test
    public void testTernary08() throws JasperException {
        doTestParser(" ${ do:it ( a eq 1 ? true : false, y ) } ", null);
    }


    @Test
    public void testTernary09() throws JasperException {
        doTestParser(" ${ do : it ( a eq 1 ? true : false, y ) } ", null);
    }


    @Test
    public void testTernary10() throws JasperException {
        doTestParser(" ${!empty my:link(foo)} ", null);
    }


    @Test
    public void testTernary11() throws JasperException {
        doTestParser("${true?'true':'false'}", "true");
    }


    @Test
    public void testTernary12() throws JasperException {
        doTestParser("${true?'tr\"ue':'false'}", "tr\"ue");
    }


    @Test
    public void testTernary13() throws JasperException {
        doTestParser("${true?'tr\\'ue':'false'}", "tr'ue");
    }


    @Test
    public void testTernaryBug56031() throws JasperException {
        doTestParser("${my:link(!empty registration ? registration : '/test/registration')}", null);
    }


    @Test
    public void testQuotes01() throws JasperException {
        doTestParser("'", "'");
    }


    @Test
    public void testQuotes02() throws JasperException {
        doTestParser("'${foo}'", null);
    }


    @Test
    public void testQuotes03() throws JasperException {
        doTestParser("'${'foo'}'", "'foo'");
    }


    @Test
    public void testEscape01() throws JasperException {
        doTestParser("${'\\\\'}", "\\");
    }


    @Test
    public void testEscape02() throws JasperException {
        doTestParser("\\\\x${'\\\\'}", "\\\\x\\");
    }


    @Test
    public void testEscape03() throws JasperException {
        doTestParser("\\\\", "\\\\");
    }


    @Test
    public void testEscape04() throws JasperException {
        // When parsed as EL in JSP the escaping of $ as \$ is optional
        doTestParser("\\$", "\\$", "$");
    }


    @Test
    public void testEscape05() throws JasperException {
        // When parsed as EL in JSP the escaping of # as \# is optional
        doTestParser("\\#", "\\#", "#");
    }


    @Test
    public void testEscape07() throws JasperException {
        doTestParser("${'\\\\$'}", "\\$");
    }


    @Test
    public void testEscape08() throws JasperException {
        doTestParser("${'\\\\#'}", "\\#");
    }


    @Test
    public void testEscape09() throws JasperException {
        doTestParser("\\${", "${");
    }


    @Test
    public void testEscape10() throws JasperException {
        doTestParser("\\#{", "#{");
    }


    @Test
    public void testEscape11() throws JasperException {
        // Bug 56612
        doTestParser("${'\\'\\''}", "''");
    }


    private void doTestParser(String input, String expected) throws JasperException {
        doTestParser(input, expected, input);
    }

    private void doTestParser(String input, String expectedResult, String expectedBuilderOutput)
            throws JasperException {

        ELException elException = null;
        String elResult = null;

        // Don't try and evaluate expressions that depend on variables or functions
        if (expectedResult != null) {
            try {
                ELManager manager = new ELManager();
                ELContext context = manager.getELContext();
                ExpressionFactory factory = ELManager.getExpressionFactory();
                ValueExpression ve = factory.createValueExpression(context, input, String.class);
                elResult = ve.getValue(context).toString();
                Assert.assertEquals(expectedResult, elResult);
            } catch (ELException ele) {
                elException = ele;
            }
        }

        Nodes nodes = null;
        try {
            nodes = ELParser.parse(input, false);
            Assert.assertNull(elException);
        } catch (IllegalArgumentException iae) {
            Assert.assertNotNull(elResult, elException);
            // Not strictly true but enables us to report both
            iae.initCause(elException);
            throw iae;
        }

        TextBuilder textBuilder = new TextBuilder(false);

        nodes.visit(textBuilder);

        Assert.assertEquals(expectedBuilderOutput, textBuilder.getText());
    }
}