[TEST] Reuse EsTestCase#createAnalysisService in KuromojiAnalysisTests

This commit is contained in:
Simon Willnauer 2016-03-22 13:44:25 +01:00
parent 75d5b83367
commit 1988b8b387
2 changed files with 18 additions and 33 deletions

View File

@ -24,24 +24,13 @@ import org.apache.lucene.analysis.Tokenizer;
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 class KuromojiAnalysisTests extends ESTestCase {
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,

View File

@ -690,10 +690,18 @@ public abstract class ESTestCase extends LuceneTestCase {
*/
@SafeVarargs
public static AnalysisService createAnalysisService(Index index, Settings settings, Consumer<AnalysisModule>... 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<AnalysisModule>... 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<AnalysisModule> consumer : moduleConsumers) {