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.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertNotEquals;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  import static org.junit.jupiter.api.Assertions.assertNull;
24  
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import org.apache.commons.jexl3.internal.Engine;
29  import org.apache.commons.jexl3.introspection.JexlPropertyGet;
30  import org.apache.commons.jexl3.introspection.JexlPropertySet;
31  import org.apache.commons.jexl3.introspection.JexlUberspect;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Test cases for the if statement.
36   */
37  @SuppressWarnings({"UnnecessaryBoxing", "AssertEqualsBetweenInconvertibleTypes"})
38  class StrategyTest extends JexlTestCase {
39      // JEXL-174
40      public static class MapArithmetic extends JexlArithmetic {
41          public MapArithmetic(final boolean flag) {
42              super(flag);
43          }
44  
45          public Object arrayGet(final Map<?,?> map, final Object identifier) {
46              return map.get(identifier);
47          }
48  
49          public Object arraySet(final Map<Object, Object> map, final Object identifier, final Object value) {
50               map.put(identifier, value);
51               return value;
52          }
53  
54          public Object propertyGet(final Map<?,?> map, final Object identifier) {
55              return arrayGet(map, identifier);
56          }
57  
58          public Object propertySet(final Map<Object, Object> map, final Object identifier, final Object value) {
59               return arraySet(map, identifier, value);
60          }
61      }
62  
63      public StrategyTest() {
64          super("StrategyTest");
65      }
66  
67      public void run171(final JexlEngine jexl, final boolean std) throws Exception {
68          Object result;
69          final Map<String, Object> i = new HashMap<>();
70  
71          i.put("class", 42);
72          result = jexl.createScript("i['class'] ", "i").execute((JexlContext)null, i);
73          assertEquals(42, result);
74          result = jexl.createScript("i['class'] = 28", "i").execute((JexlContext)null, i);
75          assertEquals(28, result);
76          assertEquals(28, i.get("class"));
77          result = jexl.createScript("i.class", "i").execute((JexlContext)null, i);
78          if (std) {
79              assertEquals(HashMap.class, result);
80          } else {
81              assertEquals(28, result);
82          }
83          result = jexl.createScript("i.'class'", "i").execute((JexlContext)null, i);
84          if (std) {
85              assertEquals(HashMap.class, result);
86          } else {
87              assertEquals(28, result);
88          }
89  
90          i.put("size", 4242);
91          result = jexl.createScript("i['size'] ", "i").execute((JexlContext)null, i);
92          assertEquals(4242 ,result);
93          result = jexl.createScript("i['size'] = 2828", "i").execute((JexlContext)null, i);
94          assertEquals(2828, result);
95          assertEquals(2828, i.get("size"));
96          result = jexl.createScript("i.'size'", "i").execute((JexlContext)null, i);
97          assertEquals(2828, result);
98          result = jexl.createScript("size i", "i").execute((JexlContext)null, i);
99          assertEquals(2, result);
100 
101         i.put("empty", 424242);
102         result = jexl.createScript("i['empty'] ", "i").execute((JexlContext)null, i);
103         assertEquals(424242, result);
104         result = jexl.createScript("i['empty'] = 282828", "i").execute((JexlContext)null, i);
105         assertEquals(282828, result);
106         assertEquals(282828, i.get("empty"));
107         result = jexl.createScript("i.'empty'", "i").execute((JexlContext)null, i);
108         if (std) {
109         assertNotEquals(282828, result);
110         } else {
111             assertEquals(282828, result);
112         }
113         result = jexl.createScript("empty i", "i").execute((JexlContext)null, i);
114         assertFalse((Boolean) result);
115     }
116 
117     @Test
118     void testJexlStrategy() throws Exception {
119         final JexlEngine jexl = new Engine();
120         run171(jexl, true);
121     }
122 
123     @Test
124     void testMapStrategy() throws Exception {
125         final JexlEngine jexl = new JexlBuilder().strategy(JexlUberspect.MAP_STRATEGY).create();
126         run171(jexl, false);
127     }
128 
129     @Test
130     void testMyMapStrategy() throws Exception {
131         final JexlEngine jexl = new JexlBuilder().arithmetic(new MapArithmetic(true)).create();
132         run171(jexl, false);
133     }
134 
135     @Test
136     void testRawResolvers() throws Exception {
137         final Object map  = new HashMap<String, Object>();
138         final JexlEngine jexl = new JexlBuilder().create();
139         final JexlUberspect uberspect = jexl.getUberspect();
140         final JexlUberspect.PropertyResolver rfieldp = JexlUberspect.JexlResolver.FIELD;
141         final JexlPropertyGet fget = rfieldp.getPropertyGet(uberspect, map, "key");
142         assertNull(fget);
143         final JexlPropertySet fset = rfieldp.getPropertySet(uberspect, map, "key", "value");
144         assertNull(fset);
145         final JexlUberspect.PropertyResolver rmap = JexlUberspect.JexlResolver.MAP;
146         final JexlPropertyGet mget = rmap.getPropertyGet(uberspect, map, "key");
147         assertNotNull(mget);
148         final JexlPropertySet mset = rmap.getPropertySet(uberspect, map, "key", "value");
149         assertNotNull(mset);
150     }
151 }