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.parser;
18  
19  import org.apache.commons.jexl3.JexlException;
20  import org.apache.commons.jexl3.JexlFeatures;
21  import org.junit.Assert;
22  import org.junit.Test;
23  
24  /**
25   * @since 1.0
26   */
27  public class ParserTest {
28      static final JexlFeatures FEATURES = new JexlFeatures();
29      public ParserTest() {}
30  
31      /**
32       * See if we can parse simple scripts
33       */
34      @Test
35      public void testParse() throws Exception {
36          final Parser parser = new Parser(";");
37          JexlNode sn;
38          sn = parser.parse(null, FEATURES, "foo = 1;", null);
39          Assert.assertNotNull("parsed node is null", sn);
40  
41          sn = parser.parse(null, FEATURES, "foo = \"bar\";", null);
42          Assert.assertNotNull("parsed node is null", sn);
43  
44          sn = parser.parse(null, FEATURES, "foo = 'bar';", null);
45          Assert.assertNotNull("parsed node is null", sn);
46      }
47  
48      @Test
49      public void testErrorAssign() throws Exception {
50          final String[] ops = { "=", "+=", "-=", "/=", "*=", "^=", "&=", "|=" };
51          for(final String op : ops) {
52              final Parser parser = new Parser(";");
53              try {
54                  final JexlNode sn = parser.parse(null, FEATURES, "foo() "+op+" 1;", null);
55                  Assert.fail("should have failed on invalid assignment " + op);
56              } catch (final JexlException.Parsing xparse) {
57                  // ok
58                  final String ss = xparse.getDetail();
59                  final String sss = xparse.toString();
60              }
61          }
62      }
63  
64      @Test
65      public void testErrorAmbiguous() throws Exception {
66          final Parser parser = new Parser(";");
67          try {
68              final JexlNode sn = parser.parse(null, FEATURES, "x = 1 y = 5", null);
69              Assert.fail("should have failed on ambiguous statement");
70          } catch (final JexlException.Ambiguous xambiguous) {
71              // ok
72          } catch(final JexlException xother) {
73              Assert.fail(xother.toString());
74          }
75      }
76  
77      @Test
78      public void testIdentifierEscape() {
79          final String[] ids = new String[]{"a\\ b", "a\\ b\\ c", "a\\'b\\\"c", "a\\ \\ c"};
80          for(final String id : ids) {
81              final String esc0 = StringParser.unescapeIdentifier(id);
82              Assert.assertFalse(esc0.contains("\\"));
83              final String esc1 = StringParser.escapeIdentifier(esc0);
84              Assert.assertEquals(id, esc1);
85          }
86      }
87  
88      /**
89       * Test the escaped control characters.
90       */
91      @Test
92      public void testControlCharacters() {
93          // Both '' and "" are valid JEXL string
94          // The array of tuples where the first element is an expected result and the second element is a test string.
95          final String[][] strings = new String[][] {
96              new String[] {"a\nb\tc", "'a\nb\tc'"}, // we still honor the actual characters
97              new String[] {"a\nb\tc", "'a\\nb\\tc'"},
98              new String[] {"a\nb\tc", "\"a\\nb\\tc\""},
99              new String[] {"\b\t\n\f\r", "'\\b\\t\\n\\f\\r'"},
100             new String[] {"'hi'", "'\\'hi\\''"},
101             new String[] {"\"hi\"", "'\"hi\"'"},
102             new String[] {"\"hi\"", "'\"hi\"'"},
103         };
104         for(final String[] pair: strings) {
105             final String output = StringParser.buildString(pair[1], true);
106             Assert.assertEquals(pair[0], output);
107         }
108     }
109 }