summaryrefslogtreecommitdiff
path: root/modules/lang-painless/src/test/java/org/elasticsearch/painless/ScriptEngineTests.java
blob: 93b0d359a26c416457a1bf826275f9c40081f2e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.painless;

import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.ScriptContext;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class ScriptEngineTests extends ScriptTestCase {

    public void testSimpleEquation() {
        final Object value = exec("return 1 + 2;");
        assertEquals(3, ((Number)value).intValue());
    }

    @SuppressWarnings("unchecked") // We know its Map<String, Object> because we put them there in the test
    public void testMapAccess() {
        Map<String, Object> vars = new HashMap<>();
        Map<String, Object> obj2 = new HashMap<>();
        obj2.put("prop2", "value2");
        Map<String, Object> obj1 = new HashMap<>();
        obj1.put("prop1", "value1");
        obj1.put("obj2", obj2);
        obj1.put("l", Arrays.asList("2", "1"));
        vars.put("obj1", obj1);

        Object value = exec("return params['obj1'];", vars, true);
        obj1 = (Map<String, Object>)value;
        assertEquals("value1", obj1.get("prop1"));
        assertEquals("value2", ((Map<String, Object>) obj1.get("obj2")).get("prop2"));

        value = exec("return params.obj1.l.0;", vars, true);
        assertEquals("2", value);
    }

    @SuppressWarnings("unchecked") // We know its Map<String, Object> because we put them there ourselves
    public void testAccessListInScript() {
        Map<String, Object> vars = new HashMap<>();
        Map<String, Object> obj2 = new HashMap<>();
        obj2.put("prop2", "value2");
        Map<String, Object> obj1 = new HashMap<>();
        obj1.put("prop1", "value1");
        obj1.put("obj2", obj2);
        vars.put("l", Arrays.asList("1", "2", "3", obj1));

        assertEquals(4, exec("return params.l.size();", vars, true));
        assertEquals("1", exec("return params.l.0;", vars, true));

        Object value = exec("return params.l.3;", vars, true);
        obj1 = (Map<String, Object>)value;
        assertEquals("value1", obj1.get("prop1"));
        assertEquals("value2", ((Map<String, Object>)obj1.get("obj2")).get("prop2"));

        assertEquals("value1", exec("return params.l.3.prop1;", vars, true));
    }

    public void testChangingVarsCrossExecution1() {
        Map<String, Object> vars = new HashMap<>();
        Map<String, Object> ctx = new HashMap<>();
        vars.put("ctx", ctx);

        ExecutableScript.Compiled compiledScript =
            scriptEngine.compile(null, "return ctx.value;", ExecutableScript.CONTEXT, Collections.emptyMap());
        ExecutableScript script = compiledScript.newInstance(vars);

        ctx.put("value", 1);
        Object o = script.run();
        assertEquals(1, ((Number) o).intValue());

        ctx.put("value", 2);
        o = script.run();
        assertEquals(2, ((Number) o).intValue());
    }

    public void testChangingVarsCrossExecution2() {
        Map<String, Object> vars = new HashMap<>();
        ExecutableScript.Compiled compiledScript =
            scriptEngine.compile(null, "return params['value'];", ExecutableScript.CONTEXT, Collections.emptyMap());
        ExecutableScript script = compiledScript.newInstance(vars);

        script.setNextVar("value", 1);
        Object value = script.run();
        assertEquals(1, ((Number)value).intValue());

        script.setNextVar("value", 2);
        value = script.run();
        assertEquals(2, ((Number)value).intValue());
    }
}