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