Skip to content

Commit

Permalink
[TEST] Reuse EsTestCase#createAnalysisService in KuromojiAnalysisTests
Browse files Browse the repository at this point in the history
  • Loading branch information
s1monw committed Mar 22, 2016
1 parent 75d5b83 commit 1988b8b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -690,10 +690,18 @@ protected static long spinForAtLeastOneMillisecond() {
*/
@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) {
Expand Down

0 comments on commit 1988b8b

Please sign in to comment.