package org.elasticsearch.index.analysis;

import org.apache.lucene.analysis.vi.VietnameseAnalyzer;
import org.apache.lucene.analysis.vi.VietnameseTokenizer;
import org.elasticsearch.Version;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.inject.Injector;
import org.elasticsearch.common.inject.ModulesBuilder;
import org.elasticsearch.common.settings.ImmutableSettings;
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.settings.IndexSettingsModule;
import org.elasticsearch.indices.analysis.IndicesAnalysisModule;
import org.elasticsearch.indices.analysis.IndicesAnalysisService;
import org.elasticsearch.plugin.analysis.vi.AnalysisVietnamesePlugin;
import org.elasticsearch.test.ElasticsearchTestCase;
import org.junit.Test;

import java.io.IOException;
import java.io.StringReader;

import static org.hamcrest.Matchers.instanceOf;

/**
 * @author duydo
 */
public class VietnameseAnalysisTests extends ElasticsearchTestCase {
    @Test
    public void testDefaultsVietnameseAnalysis() throws IOException {
        AnalysisService analysisService = createAnalysisService();

        TokenizerFactory tokenizerFactory = analysisService.tokenizer("vi_tokenizer");
        assertThat(tokenizerFactory, instanceOf(VietnameseTokenizerFactory.class));

        NamedAnalyzer analyzer = analysisService.analyzer("my_analyzer");
        assertThat(analyzer.analyzer(), instanceOf(CustomAnalyzer.class));
        assertThat(analyzer.analyzer().tokenStream(null, new StringReader("")), instanceOf(VietnameseTokenizer.class));
    }


    public AnalysisService createAnalysisService() {
        Settings settings = ImmutableSettings.settingsBuilder()
                .loadFromClasspath("org/elasticsearch/index/analysis/vi_analysis.json")
                .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)
                .build();
        Index index = new Index("test");
        Injector parentInjector = new ModulesBuilder().add(new SettingsModule(settings),
                new EnvironmentModule(new Environment(settings)),
                new IndicesAnalysisModule())
                .createInjector();
        AnalysisModule analysisModule = new AnalysisModule(settings, parentInjector.getInstance(IndicesAnalysisService.class));
        new AnalysisVietnamesePlugin().onModule(analysisModule);
        Injector injector = new ModulesBuilder().add(
                new IndexSettingsModule(index, settings),
                new IndexNameModule(index),
                analysisModule)
                .createChildInjector(parentInjector);
        return injector.getInstance(AnalysisService.class);
    }
}

@@ -36,16 +36,177 @@
 lysis;%0A%0A
+import org.apache.lucene.analysis.TokenStream;%0Aimport org.apache.lucene.analysis.Tokenizer;%0Aimport org.apache.lucene.analysis.tokenattributes.CharTermAttribute;%0A
 import o
@@ -1284,26 +1284,17 @@
 atchers.
-instanceOf
+*
 ;%0A%0A/**%0A 
@@ -1542,41 +1542,30 @@
     
-TokenizerFactory tokenizerFactory
+NamedAnalyzer analyzer
  = a
@@ -1583,22 +1583,21 @@
 ice.
-tokeni
+analy
 zer(%22vi_
 toke
@@ -1592,22 +1592,21 @@
 zer(%22vi_
-tokeni
+analy
 zer%22);%0A 
@@ -1627,24 +1627,27 @@
 hat(
-tokenizerFactory
+analyzer.analyzer()
 , in
@@ -1669,24 +1669,16 @@
 mese
-TokenizerFactory
+Analyzer
 .cla
@@ -1688,38 +1688,24 @@
 );%0A%0A        
-NamedAnalyzer 
 analyzer = a
@@ -1880,29 +1880,176 @@
 ll, 
-new StringReader(%22%22))
+%22%22), instanceOf(VietnameseTokenizer.class));%0A%0A        TokenizerFactory tokenizerFactory = analysisService.tokenizer(%22vi_tokenizer%22);%0A        assertThat(tokenizerFactory
 , in
@@ -2068,32 +2068,39 @@
 tnameseTokenizer
+Factory
 .class));%0A    %7D%0A
@@ -2093,22 +2093,299 @@
 lass));%0A
+%0A 
     
+   String source = %22c%C3%B4ng ngh%E1%BB%87 th%C3%B4ng tin Vi%E1%BB%87t Nam%22;%0A        String%5B%5D exptected = new String%5B%5D%7B%22c%C3%B4ng ngh%E1%BB%87 th%C3%B4ng tin%22, %22Vi%E1%BB%87t Nam%22%7D;%0A%0A        Tokenizer tokenizer = tokenizerFactory.create(new StringReader(source));%0A        assertSimpleTokenStreamOutput(tokenizer, exptected);%0A    
 %7D
-%0A
 %0A%0A    pu
@@ -3462,11 +3462,697 @@
 );%0A    %7D
+%0A%0A    public static void assertSimpleTokenStreamOutput(TokenStream stream,%0A                                                     String%5B%5D expected) throws IOException %7B%0A        stream.reset();%0A        CharTermAttribute termAttr = stream.getAttribute(CharTermAttribute.class);%0A        assertThat(termAttr, notNullValue());%0A        int i = 0;%0A        while (stream.incrementToken()) %7B%0A            assertThat(expected.length, greaterThan(i));%0A            assertThat(%22expected different term at index %22 + i, expected%5Bi++%5D, equalTo(termAttr.toString()));%0A        %7D%0A        assertThat(%22not all tokens produced%22, i, equalTo(expected.length));%0A        stream.end();%0A        stream.close();%0A    %7D
 %0A%7D%0A

add unit test