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 static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Set;
28
29 import org.junit.jupiter.api.Test;
30
31
32
33
34 @SuppressWarnings({"UnnecessaryBoxing", "AssertEqualsBetweenInconvertibleTypes"})
35 class SetLiteralTest extends JexlTestCase {
36
37 private static Set<?> createSet(final Object... args) {
38 return new HashSet<>(Arrays.asList(args));
39 }
40
41 public SetLiteralTest() {
42 super("SetLiteralTest");
43 }
44
45 @Test
46 void testLiteralWithOneEntry() throws Exception {
47 final List<String> sources = Arrays.asList("{ 'foo' }", "{ 'foo', }");
48 for(final String src : sources) {
49 final JexlExpression e = JEXL.createExpression(src);
50 final JexlContext jc = new MapContext();
51
52 final Object o = e.evaluate(jc);
53 final Set<?> check = createSet("foo");
54 assertEquals(check, o);
55 }
56 }
57
58 @Test
59 void testNotEmptySimpleSetLiteral() throws Exception {
60 final JexlExpression e = JEXL.createExpression("empty({ 'foo' , 'bar' })");
61 final JexlContext jc = new MapContext();
62
63 final Object o = e.evaluate(jc);
64 assertFalse((Boolean) o);
65 }
66
67 @Test
68 void testSetLiteralWithNulls() throws Exception {
69 final String[] exprs = {
70 "{ }",
71 "{ 10 }",
72 "{ 10 , null }",
73 "{ 10 , null , 20}",
74 "{ '10' , null }",
75 "{ null, '10' , 20 }"
76 };
77 final Set<?>[] checks = {
78 Collections.emptySet(),
79 createSet(Integer.valueOf(10)),
80 createSet(Integer.valueOf(10), null),
81 createSet(Integer.valueOf(10), null, Integer.valueOf(20)),
82 createSet("10", null),
83 createSet(null, "10", Integer.valueOf(20))
84 };
85 final JexlContext jc = new MapContext();
86 for (int t = 0; t < exprs.length; ++t) {
87 final JexlScript e = JEXL.createScript(exprs[t]);
88 final Object o = e.execute(jc);
89 assertEquals(checks[t], o, exprs[t]);
90 }
91
92 }
93
94 @Test
95 void testSetLiteralWithNumbers() throws Exception {
96 final JexlExpression e = JEXL.createExpression("{ 5.0 , 10 }");
97 final JexlContext jc = new MapContext();
98
99 final Object o = e.evaluate(jc);
100 final Set<?> check = createSet(Double.valueOf(5.0), Integer.valueOf(10));
101 assertEquals(check, o);
102 }
103
104 @Test
105 void testSetLiteralWithOneEntryBlock() throws Exception {
106 final JexlScript e = JEXL.createScript("{ { 'foo' }; }");
107 final JexlContext jc = new MapContext();
108
109 final Object o = e.execute(jc);
110 final Set<?> check = createSet("foo");
111 assertEquals(check, o);
112 }
113
114 @Test
115 void testSetLiteralWithOneEntryScript() throws Exception {
116 final JexlScript e = JEXL.createScript("{ 'foo' }");
117 final JexlContext jc = new MapContext();
118
119 final Object o = e.execute(jc);
120 final Set<?> check = createSet("foo");
121 assertEquals(check, o);
122 }
123
124 @Test
125 void testSetLiteralWithOneNestedSet() throws Exception {
126 final JexlScript e = JEXL.createScript("{ { 'foo' } }");
127 final JexlContext jc = new MapContext();
128
129 final Object o = e.execute(jc);
130 final Set<?> check = createSet(createSet("foo"));
131 assertEquals(check, o);
132 }
133
134 @Test
135 void testSetLiteralWithStrings() throws Exception {
136 final List<String> sources = Arrays.asList("{ 'foo', 'bar' }", "{ 'foo', 'bar', ... }", "{ 'foo', 'bar', }");
137 for (final String src : sources) {
138 final JexlExpression e = JEXL.createExpression(src);
139 final JexlContext jc = new MapContext();
140 final Object o = e.evaluate(jc);
141 final Set<?> check = createSet("foo", "bar");
142 assertEquals(check, o);
143 }
144 assertThrows(JexlException.Parsing.class, () -> JEXL.createExpression("{ , }"), "syntax");
145 }
146
147 @Test
148 void testSetLiteralWithStringsScript() throws Exception {
149 final JexlScript e = JEXL.createScript("{ 'foo' , 'bar' }");
150 final JexlContext jc = new MapContext();
151
152 final Object o = e.execute(jc);
153 final Set<?> check = createSet("foo", "bar");
154 assertEquals(check, o);
155 }
156
157 @Test
158 void testSizeOfSimpleSetLiteral() throws Exception {
159 final JexlExpression e = JEXL.createExpression("size({ 'foo' , 'bar'})");
160 final JexlContext jc = new MapContext();
161
162 final Object o = e.evaluate(jc);
163 assertEquals(Integer.valueOf(2), o);
164 }
165
166 }