1   /*
2    * Copyright 2006 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jexl;
17  
18  import junit.framework.TestCase;
19  
20  import org.apache.commons.jexl.parser.ParseException;
21  
22  /***
23   * Tests for malformed expressions and scripts.
24   * ({@link ExpressionFactory} and {@link ScriptFactory} should throw
25   * {@link ParseException}s).
26   *
27   * @since 1.1
28   */
29  public class ParseFailuresTest extends TestCase {
30  
31      /***
32       * Create the test.
33       *
34       * @param testName name of the test
35       */
36      public ParseFailuresTest(String testName) {
37          super(testName);
38      }
39  
40      public void testMalformedExpression1() throws Exception {
41          // this will throw a ParseException
42          String badExpression = "eq";
43          try {
44              Expression e = ExpressionFactory.createExpression(badExpression);
45              fail("Parsing \"" + badExpression
46                  + "\" should result in a ParseException");
47          } catch (ParseException pe) {
48              // expected
49          }
50      }
51  
52      public void testMalformedExpression2() throws Exception {
53          // this will throw a TokenMgrErr, which we rethrow as a ParseException
54          String badExpression = "?";
55          try {
56              Expression e = ExpressionFactory.createExpression(badExpression);
57              fail("Parsing \"" + badExpression
58                  + "\" should result in a ParseException");
59          } catch (ParseException pe) {
60              // expected
61          }
62      }
63  
64      public void testMalformedScript1() throws Exception {
65          // this will throw a TokenMgrErr, which we rethrow as a ParseException
66          String badScript = "eq";
67          try {
68              Script s = ScriptFactory.createScript(badScript);
69              fail("Parsing \"" + badScript
70                  + "\" should result in a ParseException");
71          } catch (ParseException pe) {
72              // expected
73          }
74      }
75  
76  
77      public void testMalformedScript2() throws Exception {
78          // this will throw a TokenMgrErr, which we rethrow as a ParseException
79          String badScript = "?";
80          try {
81              Script s = ScriptFactory.createScript(badScript);
82              fail("Parsing \"" + badScript
83                  + "\" should result in a ParseException");
84          } catch (ParseException pe) {
85              // expected
86          }
87      }
88  
89      public void testMalformedScript3() throws Exception {
90          // this will throw a TokenMgrErr, which we rethrow as a ParseException
91          String badScript = "foo=1;bar=2;a?b:c;";
92          try {
93              Script s = ScriptFactory.createScript(badScript);
94              fail("Parsing \"" + badScript
95                  + "\" should result in a ParseException");
96          } catch (ParseException pe) {
97              // expected
98          }
99      }
100 
101 }