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    *      https://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.assertArrayEquals;
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertInstanceOf;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import java.util.Arrays;
27  import java.util.List;
28  
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Tests for array literals.
33   */
34  @SuppressWarnings({"UnnecessaryBoxing", "AssertEqualsBetweenInconvertibleTypes"})
35  class ArrayLiteralTest extends JexlTestCase {
36  
37      public ArrayLiteralTest() {
38          super("ArrayLiteralTest");
39      }
40  
41      @Test
42      void testChangeThroughVariables() throws Exception {
43          final JexlContext jc = new MapContext();
44          final JexlExpression e147 = JEXL.createExpression("quux = [one, two]");
45  
46          jc.set("one", 1);
47          jc.set("two", 2);
48          final int[] o1 = (int[]) e147.evaluate(jc);
49          assertEquals(1, o1[0]);
50          assertEquals(2, o1[1]);
51  
52          jc.set("one", 10);
53          jc.set("two", 20);
54          final int[] o2 = (int[]) e147.evaluate(jc);
55          assertEquals(10, o2[0]);
56          assertEquals(20, o2[1]);
57      }
58  
59      @Test
60      void testEmptyArrayLiteral() throws Exception {
61          final JexlContext jc = new MapContext();
62          Object o;
63          o = JEXL.createExpression("[]").evaluate(jc);
64          assertInstanceOf(Object[].class, o);
65          assertEquals(0, ((Object[]) o).length);
66          o = JEXL.createExpression("[...]").evaluate(jc);
67          assertInstanceOf(List.class, o);
68          assertEquals(0, ((List<?>) o).size());
69          assertThrows(JexlException.Parsing.class, () -> JEXL.createExpression("[ , ]"));
70          assertThrows(JexlException.Parsing.class, () -> JEXL.createExpression("[ ... , ]"));
71      }
72  
73      @Test
74      void testLiteralWithElipsis() throws Exception {
75          final JexlExpression e = JEXL.createExpression("[ 'foo' , 'bar', ... ]");
76          final JexlContext jc = new MapContext();
77  
78          final Object o = e.evaluate(jc);
79          final Object[] check = { "foo", "bar" };
80          assertEquals(Arrays.asList(check), o);
81          assertEquals(2, ((List<?>) o).size());
82          assertThrows(JexlException.Parsing.class, () -> JEXL.createExpression("[ 'foo' , 'bar', ... , ]"));
83      }
84  
85      @Test
86      void testLiteralWithIntegers() throws Exception {
87          final JexlExpression e = JEXL.createExpression("[ 5 , 10 ]");
88          final JexlContext jc = new MapContext();
89  
90          final Object o = e.evaluate(jc);
91          final int[] check = {5, 10};
92          assertArrayEquals(check, (int[]) o);
93      }
94  
95      @Test
96      void testLiteralWithNulls() throws Exception {
97          final String[] exprs = {
98              "[ null , 10 ]",
99              "[ 10 , null ]",
100             "[ 10 , null , 10]",
101             "[ '10' , null ]",
102             "[ null, '10' , null ]"
103         };
104         final Object[][] checks = {
105             {null, Integer.valueOf(10)},
106             {Integer.valueOf(10), null},
107             {Integer.valueOf(10), null, Integer.valueOf(10)},
108             {"10", null},
109             {null, "10", null}
110         };
111         final JexlContext jc = new MapContext();
112         for (int t = 0; t < exprs.length; ++t) {
113             final JexlExpression e = JEXL.createExpression(exprs[t]);
114             final Object o = e.evaluate(jc);
115             assertArrayEquals(checks[t], (Object[]) o, exprs[t]);
116         }
117 
118     }
119 
120     @Test
121     void testLiteralWithNumbers() throws Exception {
122         final JexlExpression e = JEXL.createExpression("[ 5.0 , 10 ]");
123         final JexlContext jc = new MapContext();
124 
125         final Object o = e.evaluate(jc);
126         final Object[] check = {Double.valueOf(5), Integer.valueOf(10)};
127         assertArrayEquals(check, (Object[]) o);
128         assertTrue(o.getClass().isArray() && o.getClass().getComponentType().equals(Number.class));
129     }
130 
131     @Test
132     void testLiteralWithOneEntry() throws Exception {
133         final Object[] check = {"foo"};
134         final List<String> sources = Arrays.asList("[ 'foo']", "[ 'foo' , ]");
135         for(final String src : sources) {
136             final JexlExpression e = JEXL.createExpression(src);
137             final Object o = e.evaluate(null);
138             assertArrayEquals(check, (Object[]) o);
139         }
140     }
141 
142     @Test
143     void testLiteralWithStrings() throws Exception {
144         final Object[] check = {"foo", "bar"};
145         final List<String> sources = Arrays.asList("[ 'foo' , 'bar' ]", "[ 'foo' , 'bar', ]");
146         for(final String src : sources) {
147             final JexlExpression e = JEXL.createExpression(src);
148             final Object o = e.evaluate(null);
149             assertArrayEquals(check, (Object[]) o);
150         }
151     }
152 
153     @Test
154     void testNotEmptySimpleArrayLiteral() throws Exception {
155         final JexlExpression e = JEXL.createExpression("empty([ 'foo' , 'bar' ])");
156         final JexlContext jc = new MapContext();
157 
158         final Object o = e.evaluate(jc);
159         assertFalse((Boolean) o);
160     }
161 
162     @Test
163     void testNotestCallingMethodsOnNewMapLiteral() throws Exception {
164         final JexlExpression e = JEXL.createExpression("size({ 'foo' : 'bar' }.values())");
165         final JexlContext jc = new MapContext();
166 
167         final Object o = e.evaluate(jc);
168         assertEquals(Integer.valueOf(1), o);
169     }
170 
171     @Test
172     void testSizeOfSimpleArrayLiteral() throws Exception {
173         final JexlExpression e = JEXL.createExpression("size([ 'foo' , 'bar' ])");
174         final JexlContext jc = new MapContext();
175 
176         final Object o = e.evaluate(jc);
177         assertEquals(Integer.valueOf(2), o);
178     }
179 }