package io.bdrc.lucene.bo;

import static org.junit.Assert.*;

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

import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.Tokenizer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.*;

/**
 * Unit test for simple App.
 */
public class TibetanAnalyzerTest
{
	static TokenStream tokenize(String input, Tokenizer tokenizer) throws IOException {
	      tokenizer.close();
	      tokenizer.end();
	      Reader reader = new StringReader(input);
	      tokenizer.setReader(reader);
	      tokenizer.reset();
	      return tokenizer;
	}
	
	private void assertTokenStream(String string, List<String> expected, Tokenizer tokenizer) {
		TokenStream tokenStream = null;
		try {
			tokenStream = tokenize(string, tokenizer);
			List<String> termList = new ArrayList<String>();
			CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
			while (tokenStream.incrementToken()) {
			    termList.add(charTermAttribute.toString());
			}
			/** prints out the list of terms */
			System.out.println(string + " => " + String.join(" ", termList));
			assertThat(termList, is(expected));
		} catch (IOException e) {
			assertTrue(false);
		}
	}
	
	@BeforeClass
	public static void init() {
	    System.out.println("before the test sequence");
	}
	
	@Test
    public void test1() throws IOException
    {
		System.out.println("Test1: TibSyllableTokenizer()");
		String input = "བཀྲ་ཤིས། བདེ་ལེགས།";
		List<String> expected = Arrays.asList("བཀྲ", "ཤིས", "བདེ" ,"ལེགས");
		assertTokenStream(input, expected, new TibSyllableTokenizer());
    }

	@Test
    public void test2()
    {
		System.out.println("test 2");
		assertFalse(false);
    }
	
	@AfterClass
	public static void finish() {
	    System.out.println("after the test sequence");
	}
	
}

@@ -846,18 +846,26 @@
 er;%0A%09%7D%0A%09
+%09
 %0A%09
+static 
 private 
@@ -891,21 +891,29 @@
 eam(
-String string
+TokenStream tokenizer
 , Li
@@ -935,120 +935,19 @@
 cted
-, Tokenizer tokenizer) %7B%0A%09%09TokenStream tokenStream = null;%0A%09%09try %7B%0A%09%09%09tokenStream = tokenize(string, tokenizer);
+) %7B%0A%09%09try %7B
 %0A%09%09%09
@@ -1041,22 +1041,20 @@
  = token
-Stream
+izer
 .addAttr
@@ -1100,22 +1100,20 @@
 e (token
-Stream
+izer
 .increme
@@ -1246,17 +1246,8 @@
 tln(
-string + 
 %22 =%3E
@@ -1629,16 +1629,43 @@
 %E0%BD%A3%E0%BD%BA%E0%BD%82%E0%BD%A6%E0%BC%8D%22;%0A
+%09%09System.out.print(input);%0A
 %09%09List%3CS
@@ -1728,22 +1728,16 @@
 %E0%BD%82%E0%BD%A6%22);%0A%09%09
-assert
 TokenStr
@@ -1743,24 +1743,34 @@
 ream
-(input, expected
+ tokenizr = tokenize(input
 , ne
@@ -1796,16 +1796,57 @@
 zer());%0A
+%09%09assertTokenStream(tokenizr, expected);%0A
     %7D%0A%0A%09

update assertTokenStream()