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 org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.junit.Assert;
22  import org.junit.Test;
23  
24  /**
25   * Tests for malformed expressions and scripts.
26   * ({@link org.apache.commons.jexl3.JexlEngine#createExpression(String)} and
27   * {@link org.apache.commons.jexl3.JexlEngine#createScript(String)} should throw
28   * {@link org.apache.commons.jexl3.JexlException}s).
29   *
30   * @since 1.1
31   */
32  public class ParseFailuresTest extends JexlTestCase {
33  
34      static final Log LOGGER = LogFactory.getLog(ParseFailuresTest.class.getName());
35  
36      /**
37       * Create the test.
38       */
39      public ParseFailuresTest() {
40          super("ParseFailuresTest");
41      }
42  
43      @Test
44      public void testMalformedExpression1() throws Exception {
45          // this will throw a JexlException
46          final String badExpression = "eq";
47          try {
48              JEXL.createExpression(badExpression);
49              Assert.fail("Parsing \"" + badExpression
50                      + "\" should result in a JexlException");
51          } catch (final JexlException pe) {
52              // expected
53              LOGGER.debug(pe);
54          }
55      }
56  
57      @Test
58      public void testMalformedExpression2() throws Exception {
59          // this will throw a TokenMgrErr, which we rethrow as a JexlException
60          final String badExpression = "?";
61          try {
62              JEXL.createExpression(badExpression);
63              Assert.fail("Parsing \"" + badExpression
64                      + "\" should result in a JexlException");
65          } catch (final JexlException pe) {
66              // expected
67              LOGGER.debug(pe);
68          }
69      }
70  
71      @Test
72      public void testMalformedScript1() throws Exception {
73          // this will throw a TokenMgrErr, which we rethrow as a JexlException
74          final String badScript = "eq";
75          try {
76              JEXL.createScript(badScript);
77              Assert.fail("Parsing \"" + badScript
78                      + "\" should result in a JexlException");
79          } catch (final JexlException pe) {
80              // expected
81              LOGGER.debug(pe);
82          }
83      }
84  
85      @Test
86      public void testMalformedScript2() throws Exception {
87          // this will throw a TokenMgrErr, which we rethrow as a JexlException
88          final String badScript = "?";
89          try {
90              JEXL.createScript(badScript);
91              Assert.fail("Parsing \"" + badScript
92                      + "\" should result in a JexlException");
93          } catch (final JexlException pe) {
94              // expected
95              LOGGER.debug(pe);
96          }
97      }
98  
99      @Test
100     public void testMalformedScript3() throws Exception {
101         // this will throw a TokenMgrErr, which we rethrow as a JexlException
102         final String badScript = "foo=1;bar=2;a?b:c:d;";
103         try {
104             JEXL.createScript(badScript);
105             Assert.fail("Parsing \"" + badScript
106                     + "\" should result in a JexlException");
107         } catch (final JexlException pe) {
108             // expected
109             LOGGER.debug(pe);
110         }
111     }
112 
113 }