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.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
28
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
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 }