Update test script engines to use generic compile method (elastic/x-pack-elasticsearch#1546)

This is the xpack side of https://github.com/elastic/elasticsearch/pull/24873

Original commit: elastic/x-pack-elasticsearch@1779afa6bc
This commit is contained in:
Ryan Ernst 2017-05-24 20:06:51 -07:00 committed by GitHub
parent 3b58334efb
commit aae7cf0b0f
2 changed files with 15 additions and 6 deletions

View File

@ -39,11 +39,13 @@ public class MockMustacheScriptEngine extends MockScriptEngine {
}
@Override
public Object compile(String name, String script, Map<String, String> params) {
public <T> T compile(String name, String script, ScriptContext<T> context, Map<String, String> params) {
if (script.contains("{{") && script.contains("}}")) {
throw new IllegalArgumentException("Fix your test to not rely on mustache");
}
// We always return the script's source as it is
return new MockCompiledScript(name, params, script, null);
if (context.instanceClazz.equals(ExecutableScript.class) == false) {
throw new IllegalArgumentException("mock mustache only understands template scripts, not [" + context.name + "]");
}
return context.compiledClazz.cast((ExecutableScript.Compiled) vars -> new MockExecutableScript(vars, p -> script));
}
}

View File

@ -6,9 +6,12 @@
package org.elasticsearch.xpack.monitoring.test;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.MockScriptEngine;
import org.elasticsearch.script.MockScriptPlugin;
import org.elasticsearch.script.ScriptContext;
import org.elasticsearch.script.ScriptEngine;
import org.elasticsearch.script.SearchScript;
import java.util.Collections;
import java.util.Map;
@ -39,8 +42,12 @@ public class MockPainlessScriptEngine extends MockScriptEngine {
}
@Override
public Object compile(String name, String script, Map<String, String> params) {
// We always return the script's source as it is
return new MockCompiledScript(name, params, script, null);
public <T> T compile(String name, String script, ScriptContext<T> context, Map<String, String> params) {
if (context.instanceClazz.equals(ExecutableScript.class)) {
return context.compiledClazz.cast((ExecutableScript.Compiled) vars -> new MockExecutableScript(vars, p -> script));
} else if (context.instanceClazz.equals(SearchScript.class)) {
return context.compiledClazz.cast((SearchScript.Compiled) (vars, lookup) -> new MockSearchScript(lookup, vars, p -> script));
}
throw new IllegalArgumentException("mock painless does not know how to handle context [" + context.name + "]");
}
}