View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.jexl3;
18  
19  import static org.junit.jupiter.api.Assertions.assertThrows;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.junit.jupiter.api.Test;
24  
25  /**
26   * Tests for malformed expressions and scripts. ({@link org.apache.commons.jexl3.JexlEngine#createExpression(String)} and
27   * {@link org.apache.commons.jexl3.JexlEngine#createScript(String)} should throw {@link org.apache.commons.jexl3.JexlException}s).
28   */
29  public class ParseFailuresTest extends JexlTestCase {
30  
31      static final Log LOGGER = LogFactory.getLog(ParseFailuresTest.class.getName());
32  
33      /**
34       * Create the test.
35       */
36      public ParseFailuresTest() {
37          super("ParseFailuresTest");
38      }
39  
40      @Test
41      public void testMalformedExpression1() throws Exception {
42          // this will throw a JexlException
43          final String badExpression = "eq";
44          final JexlException pe = assertThrows(JexlException.class, () -> JEXL.createExpression(badExpression),
45                  () -> "Parsing \"" + badExpression + "\" should result in a JexlException");
46          LOGGER.debug(pe);
47      }
48  
49      @Test
50      public void testMalformedExpression2() throws Exception {
51          // this will throw a TokenMgrErr, which we rethrow as a JexlException
52          final String badExpression = "?";
53          final JexlException pe = assertThrows(JexlException.class, () -> JEXL.createExpression(badExpression),
54                  () -> "Parsing \"" + badExpression + "\" should result in a JexlException");
55          LOGGER.debug(pe);
56      }
57  
58      @Test
59      public void testMalformedScript1() throws Exception {
60          // this will throw a TokenMgrErr, which we rethrow as a JexlException
61          final String badScript = "eq";
62          final JexlException pe = assertThrows(JexlException.class, () -> JEXL.createExpression(badScript),
63                  () -> "Parsing \"" + badScript + "\" should result in a JexlException");
64          LOGGER.debug(pe);
65      }
66  
67      @Test
68      public void testMalformedScript2() throws Exception {
69          // this will throw a TokenMgrErr, which we rethrow as a JexlException
70          final String badScript = "?";
71          final JexlException pe = assertThrows(JexlException.class, () -> JEXL.createExpression(badScript),
72                  () -> "Parsing \"" + badScript + "\" should result in a JexlException");
73          LOGGER.debug(pe);
74      }
75  
76      @Test
77      public void testMalformedScript3() throws Exception {
78          // this will throw a TokenMgrErr, which we rethrow as a JexlException
79          final String badScript = "foo=1;bar=2;a?b:c:d;";
80          final JexlException pe = assertThrows(JexlException.class, () -> JEXL.createExpression(badScript),
81                  () -> "Parsing \"" + badScript + "\" should result in a JexlException");
82          LOGGER.debug(pe);
83      }
84  
85  }