package com.typeahead.reader;

import java.io.IOException;

import org.junit.Assert;
import org.junit.Test;

import com.typeahead.exceptions.IndexAlreadyExistException;
import com.typeahead.exceptions.IndexDoesNotExistException;
import com.typeahead.index.Document;
import com.typeahead.index.Index;
import com.typeahead.util.TestSet;
import com.typeahead.util.TestUtil;
import com.typeahead.writer.IndexWriterUtil;

public class IndexReaderTest {
	
	IndexReader	reader;
	
	public IndexReaderTest() {
		reader = new IndexReader();
	}
	
	/**
	 * Test mostly related to {@link IndexReader#_readDataMapFile}, <br>
	 * which then can be extended to test other fields like {@link Index#fieldFSTMap}
	 * @throws IndexAlreadyExistException
	 * @throws IndexDoesNotExistException
	 * @throws IOException 
	 */
	@Test
	public void readingSegmentTest() throws IndexAlreadyExistException, IndexDoesNotExistException, IOException {
		String indexName = "_reader_segment_test";
		IndexReader reader = new IndexReader();
		
		//making sure index does not exist already.
		try {
			reader.deleteIndex(indexName);
		} catch (IndexDoesNotExistException e) {}
		
		Index test = reader.createIndex(indexName);
		test.setMergeFactor(3);
		
		TestSet testSet = TestUtil.getTestSet(1);
		
		//Loading test data
		for(Document d: testSet.getDocuments()) {
			test.add(d);
		}
		test.getMergePolicy().flushIndex();
		
		
		test = null;
		
		test = reader.openIndex(indexName);
		Assert.assertEquals(testSet.getDocuments().size(), test.getDataMap().size());
		
		//Cleaning the test index.
		try {
			reader.deleteIndex(indexName);
		} catch (IndexDoesNotExistException e) {}
	}
	
	
	@Test
	public void createIndexTest() throws IndexDoesNotExistException, IOException {
		String indexName = "_create_test";
		Index index = new Index(indexName);
		IndexWriterUtil writerUtil = new IndexWriterUtil(index);

		//TEST 1: create test
		try {
			index = reader.createIndex(indexName);
			Assert.assertTrue(writerUtil.doesIndexExistance());
		} catch (IndexAlreadyExistException e) {
			
		}finally{
			reader.deleteIndex(indexName);
		}
	}
	
	@Test
	public void deleteIndexTest() throws IOException {
		
		String indexName = "_del_test";
		Index index = new Index(indexName);
		IndexWriterUtil writerUtil = new IndexWriterUtil(index);
		
		//TEST 1: Exception test
		try {
			reader.deleteIndex("_dummy_index");
		} catch (IndexDoesNotExistException e) {
			Assert.assertTrue("Index: _dummy_index does not exist.".equals(e.getMessage()));
		}
				
		//TEST 2: deletion test
		try {
			reader.createIndex(indexName);
		} catch (IndexAlreadyExistException e) {
			
		}
		
		try {
			reader.deleteIndex(indexName);
			Assert.assertFalse(writerUtil.doesIndexExistance());
		} catch (IndexDoesNotExistException e) {}
		
	}
}

@@ -53,16 +53,45 @@
 ption;%0A%0A
+import org.hamcrest.Matcher;%0A
 import o
@@ -124,20 +124,126 @@
 g.junit.
-Test
+Rule;%0Aimport org.junit.Test;%0Aimport org.junit.matchers.JUnitMatchers;%0Aimport org.junit.rules.ExpectedException
 ;%0A%0Aimpor
@@ -601,16 +601,89 @@
 ader;%0A%09%0A
+%09@Rule%0A%09public ExpectedException exception = ExpectedException.none();%0A%09%0A
 %09public 
@@ -2312,16 +2312,638 @@
 %09%7D%0A%09%7D%0A%09%0A
+%09public void openIndexTest() throws IndexDoesNotExistException %7B%0A%09%09String indexName = %22_open_test%22;%0A%09%09Index index = new Index(indexName);%0A%09%09IndexWriterUtil writerUtil = new IndexWriterUtil(index);%0A%0A%09%09//TEST 1: open test%0A%09%09//TODO: Need to think of test case for IndexReader#openIndex() method%0A%09%09%0A%09%09//TEST 2: Exception test%0A%09%09try %7B%0A%09%09%09index = reader.openIndex(indexName);%0A%09%09%09exception.expect(IndexDoesNotExistException.class);%0A%09%09    exception.expectMessage(indexName);%0A%09%09%09Assert.assertTrue(writerUtil.doesIndexExistance());%0A%09%09%7D catch (IndexDoesNotExistException e) %7B%7D%0A%09%09finally%7B%0A%09%09%09reader.deleteIndex(indexName);%0A%09%09%7D%0A%09%7D%0A%09%0A%09%0A
 %09@Test%0A%09

test case for openIndex() method.