diff --git a/core/src/test/java/org/elasticsearch/index/query/AbstractQueryTestCase.java b/core/src/test/java/org/elasticsearch/index/query/AbstractQueryTestCase.java index 930dbd90af7..da32e38023c 100644 --- a/core/src/test/java/org/elasticsearch/index/query/AbstractQueryTestCase.java +++ b/core/src/test/java/org/elasticsearch/index/query/AbstractQueryTestCase.java @@ -69,7 +69,8 @@ import org.elasticsearch.indices.IndicesModule; import org.elasticsearch.indices.analysis.IndicesAnalysisService; import org.elasticsearch.indices.breaker.CircuitBreakerService; import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; -import org.elasticsearch.script.ScriptModule; +import org.elasticsearch.script.*; +import org.elasticsearch.script.mustache.MustacheScriptEngineService; import org.elasticsearch.search.internal.SearchContext; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.TestSearchContext; @@ -85,13 +86,11 @@ import java.io.IOException; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.ExecutionException; -import static org.hamcrest.Matchers.*; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.not; public abstract class AbstractQueryTestCase> extends ESTestCase { @@ -166,7 +165,38 @@ public abstract class AbstractQueryTestCase> bindQueryParsersExtension(); } }, - new ScriptModule(settings), + new ScriptModule(settings) { + @Override + protected void configure() { + Settings settings = Settings.builder() + .put("path.home", createTempDir()) + // no file watching, so we don't need a ResourceWatcherService + .put(ScriptService.SCRIPT_AUTO_RELOAD_ENABLED_SETTING, false) + .build(); + MockScriptEngine mockScriptEngine = new MockScriptEngine(); + Multibinder multibinder = Multibinder.newSetBinder(binder(), ScriptEngineService.class); + multibinder.addBinding().toInstance(mockScriptEngine); + try { + Class.forName("com.github.mustachejava.Mustache"); + } catch(ClassNotFoundException e) { + throw new IllegalStateException("error while loading mustache", e); + } + MustacheScriptEngineService mustacheScriptEngineService = new MustacheScriptEngineService(settings); + Set engines = new HashSet<>(); + engines.add(mockScriptEngine); + engines.add(mustacheScriptEngineService); + List customContexts = new ArrayList<>(); + bind(ScriptContextRegistry.class).toInstance(new ScriptContextRegistry(customContexts)); + try { + ScriptService scriptService = new ScriptService(settings, new Environment(settings), engines, null, new ScriptContextRegistry(customContexts)); + bind(ScriptService.class).toInstance(scriptService); + } catch(IOException e) { + throw new IllegalStateException("error while binding ScriptService", e); + } + + + } + }, new IndexSettingsModule(index, indexSettings), new IndexCacheModule(indexSettings), new AnalysisModule(indexSettings, new IndicesAnalysisService(indexSettings)), diff --git a/core/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java index 534542da0a7..87384f78d8f 100644 --- a/core/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java @@ -20,13 +20,13 @@ package org.elasticsearch.index.query; import org.apache.lucene.search.Query; +import org.elasticsearch.script.MockScriptEngine; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptService.ScriptType; -import org.elasticsearch.script.expression.ExpressionScriptEngineService; import org.junit.Test; import java.io.IOException; -import java.util.HashMap; +import java.util.Collections; import java.util.Map; import static org.hamcrest.Matchers.instanceOf; @@ -35,16 +35,9 @@ public class ScriptQueryBuilderTests extends AbstractQueryTestCase params = null; - if (randomBoolean()) { - script = "5 * 2 > param"; - params = new HashMap<>(); - params.put("param", 1); - } else { - script = "5 * 2 > 2"; - } - return new ScriptQueryBuilder(new Script(script, ScriptType.INLINE, ExpressionScriptEngineService.NAME, params)); + String script = "5"; + Map params = Collections.emptyMap(); + return new ScriptQueryBuilder(new Script(script, ScriptType.INLINE, MockScriptEngine.NAME, params)); } @Override diff --git a/core/src/test/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java index e1286c2468b..58193ab40d3 100644 --- a/core/src/test/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java @@ -35,14 +35,14 @@ import org.elasticsearch.index.query.functionscore.lin.LinearDecayFunctionBuilde import org.elasticsearch.index.query.functionscore.random.RandomScoreFunctionBuilder; import org.elasticsearch.index.query.functionscore.script.ScriptScoreFunctionBuilder; import org.elasticsearch.index.query.functionscore.weight.WeightBuilder; +import org.elasticsearch.script.MockScriptEngine; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptService; -import org.elasticsearch.script.expression.ExpressionScriptEngineService; import org.elasticsearch.search.MultiValueMode; import org.junit.Test; import java.io.IOException; -import java.util.HashMap; +import java.util.Collections; import java.util.Map; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; @@ -140,16 +140,9 @@ public class FunctionScoreQueryBuilderTests extends AbstractQueryTestCase params = null; - if (randomBoolean()) { - script = "5 * 2 > param"; - params = new HashMap<>(); - params.put("param", 1); - } else { - script = "5 * 2 > 2"; - } - functionBuilder = new ScriptScoreFunctionBuilder(new Script(script, ScriptService.ScriptType.INLINE, ExpressionScriptEngineService.NAME, params)); + String script = "5"; + Map params = Collections.emptyMap(); + functionBuilder = new ScriptScoreFunctionBuilder(new Script(script, ScriptService.ScriptType.INLINE, MockScriptEngine.NAME, params)); break; case 3: RandomScoreFunctionBuilder randomScoreFunctionBuilder = new RandomScoreFunctionBuilder(); @@ -420,7 +413,7 @@ public class FunctionScoreQueryBuilderTests extends AbstractQueryTestCase