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