From aae7cf0b0fd1389635cd20bf88e9ac485c6b432f Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Wed, 24 May 2017 20:06:51 -0700 Subject: [PATCH] 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@1779afa6bc6ed9a20b3dfb62a984fe7c66c20dff --- .../script/MockMustacheScriptEngine.java | 8 +++++--- .../monitoring/test/MockPainlessScriptEngine.java | 13 ++++++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/plugin/src/test/java/org/elasticsearch/script/MockMustacheScriptEngine.java b/plugin/src/test/java/org/elasticsearch/script/MockMustacheScriptEngine.java index 57df1936053..42fb5a249d9 100644 --- a/plugin/src/test/java/org/elasticsearch/script/MockMustacheScriptEngine.java +++ b/plugin/src/test/java/org/elasticsearch/script/MockMustacheScriptEngine.java @@ -39,11 +39,13 @@ public class MockMustacheScriptEngine extends MockScriptEngine { } @Override - public Object compile(String name, String script, Map params) { + public T compile(String name, String script, ScriptContext context, Map 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)); } } diff --git a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/test/MockPainlessScriptEngine.java b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/test/MockPainlessScriptEngine.java index 5bbc5e16c4e..bf90f89989b 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/test/MockPainlessScriptEngine.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/test/MockPainlessScriptEngine.java @@ -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 params) { - // We always return the script's source as it is - return new MockCompiledScript(name, params, script, null); + public T compile(String name, String script, ScriptContext context, Map 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 + "]"); } }