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 java.util.Map;
20  import java.util.TreeMap;
21  import org.junit.Assert;
22  import org.junit.Before;
23  import org.junit.Test;
24  
25  
26  /**
27   * Test cases for synchronized calls.
28   * <p>May be a base for synchronized calls.
29   */
30  @SuppressWarnings({"boxing", "UnnecessaryBoxing", "AssertEqualsBetweenInconvertibleTypes"})
31  public class SynchronizedOverloadsTest extends JexlTestCase {
32      public SynchronizedOverloadsTest() {
33          super("SynchronizedOverloadsTest", null);
34      }
35  
36      @Before
37      @Override
38      public void setUp() throws Exception {
39          // ensure jul logging is only error to avoid warning in silent mode
40          java.util.logging.Logger.getLogger(JexlEngine.class.getName()).setLevel(java.util.logging.Level.SEVERE);
41      }
42  
43  
44      @Test
45      public void testSynchronizer() throws Exception {
46          final Map<String, Object> ns = new TreeMap<>();
47          ns.put("synchronized", SynchronizedContext.class);
48          final JexlContext jc = new MapContext();
49          final JexlEngine jexl = new JexlBuilder().namespaces(ns).create();
50          final JexlScript js0 = jexl.createScript("synchronized:call(x, (y)->{y.size()})", "x");
51          final Object size = js0.execute(jc, "foobar");
52          Assert.assertEquals(6, size);
53      }
54  
55      @Test
56      public void testSynchronized() throws Exception {
57          final Map<String, Object> ns = new TreeMap<>();
58          final JexlContext jc = new SynchronizedContext(new MapContext());
59          final JexlEngine jexl = new JexlBuilder().namespaces(ns).create();
60          final JexlScript js0 = jexl.createScript("@synchronized(y) {return y.size(); }", "y");
61          final Object size = js0.execute(jc, "foobar");
62          Assert.assertEquals(6, size);
63      }
64  
65      @Test
66      public void testUnsafeMonitor() throws Exception {
67          final SynchronizedArithmetic.Monitor monitor = new SynchronizedArithmetic.SafeMonitor();
68          final Map<String, Object> foo = new TreeMap<>();
69          foo.put("one", 1);
70          foo.put("two", 2);
71          foo.put("three", 3);
72          final JexlContext jc = new SynchronizedContext(new MapContext());
73          final JexlEngine jexl = new JexlBuilder().arithmetic(new SynchronizedArithmetic(monitor, true)).create();
74          final JexlScript js0 = jexl.createScript("x['four'] = 4; var t = 0.0; for(var z: x) { t += z; }; call(t, (y)->{return y});", "x");
75          Object t = js0.execute(jc, foo);
76          Assert.assertEquals(10.0d, t);
77          Assert.assertTrue(monitor.isBalanced());
78          Assert.assertEquals(2, monitor.getCount());
79          t = js0.execute(jc, foo);
80          Assert.assertEquals(10.0d, t);
81          Assert.assertTrue(monitor.isBalanced());
82          Assert.assertEquals(4, monitor.getCount());
83      }
84  }