/**
 * 
 * APDPlat - Application Product Development Platform
 * Copyright (c) 2013, 杨尚川, yang-shangchuan@qq.com
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package org.apdplat.word.elasticsearch;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.Tokenizer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apdplat.word.lucene.ChineseWordAnalyzer;
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.index.IndexNameModule;
import org.elasticsearch.index.analysis.AnalysisModule;
import org.elasticsearch.index.analysis.AnalysisService;
import org.elasticsearch.index.analysis.TokenizerFactory;
import org.elasticsearch.index.settings.IndexSettingsModule;
import org.elasticsearch.indices.analysis.IndicesAnalysisService;
import org.junit.Test;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * ElasticSearch中文分词索引分析单元测试
 * @author 杨尚川
 */
public class ChineseWordIndicesAnalysisTest {
    private static final Settings SETTINGS = Settings.settingsBuilder()
            .put(IndexMetaData.SETTING_VERSION_CREATED, "2010099")
            .put("path.home", "/Users/apple/elasticsearch-2.0.0-beta1")
            .build();
    @Test
    public void testChineseWordIndicesAnalysis() throws IOException {
        Index index = new Index("test");
        
        Injector parentInjector = new ModulesBuilder()
                .add(new SettingsModule(SETTINGS),
                        new EnvironmentModule(new Environment(SETTINGS)),
                        new ChineseWordIndicesAnalysisModule())
                .createInjector();
        
        Injector injector = new ModulesBuilder().add(
                                new IndexSettingsModule(index, SETTINGS),
                                new IndexNameModule(index),
                                new AnalysisModule(SETTINGS, parentInjector.getInstance(IndicesAnalysisService.class))
                                    .addProcessor(new ChineseWordAnalysisBinderProcessor()))
                            .createChildInjector(parentInjector);

        AnalysisService analysisService = injector.getInstance(AnalysisService.class);

        TokenizerFactory tokenizerFactory = analysisService.tokenizer("word");
        boolean match = (tokenizerFactory instanceof ChineseWordTokenizerFactory);
        assertTrue(match);

        Tokenizer tokenizer = tokenizerFactory.create();
        tokenizer.setReader(new StringReader("他说的确实在理"));
        String exp = "[确实, 在理]";
        List<String> result = new ArrayList<>();
        while(tokenizer.incrementToken()){
            CharTermAttribute charTermAttribute = tokenizer.getAttribute(CharTermAttribute.class);
            result.add(charTermAttribute.toString());
        }
        assertEquals(exp, result.toString());

        Analyzer analyzer = analysisService.analyzer("word").analyzer();
        match = (analyzer instanceof ChineseWordAnalyzer);
        assertTrue(match);
        
        TokenStream tokenStream = analyzer.tokenStream("text", "杨尚川是APDPlat应用级产品开发平台的作者");
        exp = "[杨尚川, apdplat, 应用级, 产品开发, 平台, 作者]";
        result = new ArrayList<>();
        tokenizer.reset();
        while(tokenStream.incrementToken()){
            CharTermAttribute charTermAttribute = tokenStream.getAttribute(CharTermAttribute.class);
            result.add(charTermAttribute.toString());
        }
        tokenizer.close();
        assertEquals(exp, result.toString());
    }
}
@@ -1823,18 +1823,655 @@
 org.
-junit.Test
+elasticsearch.test.ESTestCase;%0Aimport org.junit.Test;%0A%0Aimport org.elasticsearch.Version;%0Aimport org.elasticsearch.cluster.metadata.IndexMetaData;%0Aimport org.elasticsearch.common.inject.Injector;%0Aimport org.elasticsearch.common.inject.ModulesBuilder;%0Aimport org.elasticsearch.common.settings.Settings;%0Aimport org.elasticsearch.common.settings.SettingsFilter;%0Aimport org.elasticsearch.common.settings.SettingsModule;%0Aimport org.elasticsearch.env.Environment;%0Aimport org.elasticsearch.env.EnvironmentModule;%0Aimport org.elasticsearch.index.Index;%0Aimport org.elasticsearch.test.ESTestCase;%0Aimport org.hamcrest.MatcherAssert;%0A%0Aimport java.io.IOException
 ;%0A%0Ai
@@ -2763,16 +2763,35 @@
 sisTest 
+extends ESTestCase 
 %7B%0A    pr
@@ -2916,17 +2916,23 @@
 ED, 
-%222010099%22
+Version.CURRENT
 )%0A  
@@ -2993,19 +2993,140 @@
 h-2.
-0.0-beta1
+1.1%22)%0A            .build();%0A%0A    public void testDefaultsIcuAnalysis() throws IOException %7B%0A        Index index = new Index(%22test
 %22)
+;
 %0A   
@@ -3130,29 +3130,902 @@
         
-    .build();
+AnalysisModule analysisModule = new AnalysisModule(new Environment(settings));%0A        new AnalysisSmartChinesePlugin().onModule(analysisModule);%0A        SettingsModule settingsModule = new SettingsModule(settings, new SettingsFilter(settings));%0A        settingsModule.registerSetting(InternalSettingsPlugin.VERSION_CREATED);%0A        Injector parentInjector = new ModulesBuilder().add(settingsModule,%0A                new EnvironmentModule(new Environment(settings)), analysisModule)%0A                .createInjector();%0A        final AnalysisService analysisService = parentInjector.getInstance(AnalysisRegistry.class).build(IndexSettingsModule.newIndexSettings(index, settings));%0A        TokenizerFactory tokenizerFactory = analysisService.tokenizer(%22smartcn_tokenizer%22);%0A        MatcherAssert.assertThat(tokenizerFactory, instanceOf(SmartChineseTokenizerTokenizerFactory.class));%0A    %7D%0A
 %0A    @Te

sentence identify