diff --git a/plugins/analysis-kuromoji/src/test/java/org/elasticsearch/index/analysis/KuromojiAnalysisTests.java b/plugins/analysis-kuromoji/src/test/java/org/elasticsearch/index/analysis/KuromojiAnalysisTests.java index 04d8d64cc7540..93aeddeee5f32 100644 --- a/plugins/analysis-kuromoji/src/test/java/org/elasticsearch/index/analysis/KuromojiAnalysisTests.java +++ b/plugins/analysis-kuromoji/src/test/java/org/elasticsearch/index/analysis/KuromojiAnalysisTests.java @@ -24,24 +24,13 @@ import org.apache.lucene.analysis.ja.JapaneseAnalyzer; import org.apache.lucene.analysis.ja.JapaneseTokenizer; import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; -import org.apache.lucene.analysis.tokenattributes.OffsetAttribute; -import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute; -import org.apache.lucene.analysis.tokenattributes.TypeAttribute; import org.elasticsearch.Version; -import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse; import org.elasticsearch.cluster.metadata.IndexMetaData; -import org.elasticsearch.common.inject.Injector; -import org.elasticsearch.common.inject.ModulesBuilder; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.settings.SettingsModule; import org.elasticsearch.env.Environment; -import org.elasticsearch.env.EnvironmentModule; import org.elasticsearch.index.Index; -import org.elasticsearch.indices.analysis.AnalysisModule; import org.elasticsearch.plugin.analysis.kuromoji.AnalysisKuromojiPlugin; import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.IndexSettingsModule; -import org.elasticsearch.test.InternalSettingsPlugin; import java.io.IOException; import java.io.InputStream; @@ -194,34 +183,22 @@ public void testJapaneseStopFilterFactory() throws IOException { assertSimpleTSOutput(tokenFilter.create(tokenizer), expected); } - public AnalysisService createAnalysisService() throws IOException { - InputStream empty_dict = getClass().getResourceAsStream("empty_user_dict.txt"); - InputStream dict = getClass().getResourceAsStream("user_dict.txt"); + private static AnalysisService createAnalysisService() throws IOException { + InputStream empty_dict = KuromojiAnalysisTests.class.getResourceAsStream("empty_user_dict.txt"); + InputStream dict = KuromojiAnalysisTests.class.getResourceAsStream("user_dict.txt"); Path home = createTempDir(); Path config = home.resolve("config"); Files.createDirectory(config); Files.copy(empty_dict, config.resolve("empty_user_dict.txt")); Files.copy(dict, config.resolve("user_dict.txt")); - String json = "/org/elasticsearch/index/analysis/kuromoji_analysis.json"; + Settings settings = Settings.settingsBuilder() - .loadFromStream(json, getClass().getResourceAsStream(json)) - .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) - .build(); - Settings nodeSettings = Settings.settingsBuilder() - .put(Environment.PATH_HOME_SETTING.getKey(), home).build(); - final SettingsModule settingsModule = new SettingsModule(nodeSettings); - settingsModule.registerSetting(InternalSettingsPlugin.VERSION_CREATED); - Index index = new Index("test", "_na_"); - - Environment environment = new Environment(nodeSettings); - AnalysisModule analysisModule = new AnalysisModule(environment); - new AnalysisKuromojiPlugin().onModule(analysisModule); - Injector parentInjector = new ModulesBuilder().add(settingsModule, - new EnvironmentModule(environment), analysisModule) - .createInjector(); - - return parentInjector.getInstance(AnalysisRegistry.class).build(IndexSettingsModule.newIndexSettings(index, settings)); + .loadFromStream(json, KuromojiAnalysisTests.class.getResourceAsStream(json)) + .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) + .build(); + Settings nodeSettings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), home).build(); + return createAnalysisService(new Index("test", "_na_"), nodeSettings, settings, new AnalysisKuromojiPlugin()::onModule); } public static void assertSimpleTSOutput(TokenStream stream, diff --git a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java index 23e58b0ed17be..d2cc23349fcef 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java @@ -690,10 +690,18 @@ protected static long spinForAtLeastOneMillisecond() { */ @SafeVarargs public static AnalysisService createAnalysisService(Index index, Settings settings, Consumer... moduleConsumers) throws IOException { + Settings nodeSettings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()).build(); + return createAnalysisService(index, nodeSettings, settings, moduleConsumers); + } + + /** + * Creates an AnalysisService to test analysis factories and analyzers. + */ + @SafeVarargs + public static AnalysisService createAnalysisService(Index index, Settings nodeSettings, Settings settings, Consumer... moduleConsumers) throws IOException { Settings indexSettings = settingsBuilder().put(settings) .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) .build(); - Settings nodeSettings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()).build(); Environment env = new Environment(nodeSettings); AnalysisModule analysisModule = new AnalysisModule(env); for (Consumer consumer : moduleConsumers) {