/*
 * Copyright 2004 The Apache Software Foundation
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System;
/*using StandardAnalyzer = Lucene.Net.Analysis.Standard.StandardAnalyzer;
using Document = Lucene.Net.documents.document;
using Field = Lucene.Net.documents.Field;
using IndexReader = Lucene.Net.Index.IndexReader;
using IndexWriter = Lucene.Net.Index.IndexWriter;
using Term = Lucene.Net.Index.Term;
using QueryParser = Lucene.Net.QueryParsers.QueryParser;
using Directory = Lucene.Net.Store.Directory;
using RAMDirectory = Lucene.Net.Store.RAMDirectory;*/
using NUnit.Framework;
namespace clucene.net
{
	
	/// <summary> Tests {@link MultiSearcher} class.
	/// 
	/// </summary>
	/// <version>  $Id: TestMultiSearcher.cs,v 1.3 2006/01/11 05:15:07 ustramooner Exp $
	/// </version>
	[TestFixture]
    public class TestMultiSearcher
	{
		/// <summary> ReturnS a new instance of the concrete MultiSearcher class
		/// used in this test.
		/// </summary>
		protected internal virtual MultiSearcher GetMultiSearcherInstance(Searcher[] searchers)
		{
			return new MultiSearcher(searchers);
		}
		
        [Test]
		public virtual void  TestEmptyIndex()
		{
			// creating two directories for indices
			Directory indexStoreA = new RAMDirectory();
			Directory indexStoreB = new RAMDirectory();
			
			// creating a document to store
			Document lDoc = new Document();
			lDoc.add(Field.Text("fulltext", "Once upon a time....."));
			lDoc.add(Field.Keyword("id", "doc1"));
			lDoc.add(Field.Keyword("handle", "1"));
			
			// creating a document to store
			Document lDoc2 = new Document();
			lDoc2.add(Field.Text("fulltext", "in a galaxy far far away....."));
			lDoc2.add(Field.Keyword("id", "doc2"));
			lDoc2.add(Field.Keyword("handle", "1"));
			
			// creating a document to store
			Document lDoc3 = new Document();
			lDoc3.add(Field.Text("fulltext", "a bizarre bug manifested itself...."));
			lDoc3.add(Field.Keyword("id", "doc3"));
			lDoc3.add(Field.Keyword("handle", "1"));
			
			// creating an index writer for the first index
			IndexWriter writerA = new IndexWriter(indexStoreA, new StandardAnalyzer(), true);
			// creating an index writer for the second index, but writing nothing
			IndexWriter writerB = new IndexWriter(indexStoreB, new StandardAnalyzer(), true);
			
			//--------------------------------------------------------------------
			// scenario 1
			//--------------------------------------------------------------------
			
			// writing the documents to the first index
			writerA.addDocument(lDoc);
			writerA.addDocument(lDoc2);
			writerA.addDocument(lDoc3);
			writerA.optimize();
			writerA.close();
			
			// closing the second index
			writerB.close();
			
			// creating the query
			Query query = QueryParser.Parse("handle:1", "fulltext", new StandardAnalyzer());
			
			// building the searchables
			Searcher[] searchers = new Searcher[2];
			// VITAL STEP:adding the searcher for the empty index first, before the searcher for the populated index
			searchers[0] = new IndexSearcher(indexStoreB);
			searchers[1] = new IndexSearcher(indexStoreA);
			// creating the multiSearcher
			Searcher mSearcher = GetMultiSearcherInstance(searchers);
			// performing the search
			Hits hits = mSearcher.search(query);
			
			Assert.AreEqual(3, hits.length());
			
			try
			{
				// iterating over the hit documents
				for (int i = 0; i < hits.length(); i++)
				{
					Document d = hits.doc(i);
				}
			}
			catch (System.IndexOutOfRangeException e)
			{
				Assert.Fail("ArrayIndexOutOfBoundsException thrown: " + e.Message);
				System.Console.Error.WriteLine(e.Source);
			}
			finally
			{
				mSearcher.close();
			}
			
			
			//--------------------------------------------------------------------
			// scenario 2
			//--------------------------------------------------------------------
			
			// adding one document to the empty index
			writerB = new IndexWriter(indexStoreB, new StandardAnalyzer(), false);
			writerB.addDocument(lDoc);
			writerB.optimize();
			writerB.close();
			
			// building the searchables
			Searcher[] searchers2 = new Searcher[2];
			// VITAL STEP:adding the searcher for the empty index first, before the searcher for the populated index
			searchers2[0] = new IndexSearcher(indexStoreB);
			searchers2[1] = new IndexSearcher(indexStoreA);
			// creating the mulitSearcher
			Searcher mSearcher2 = GetMultiSearcherInstance(searchers2);
			// performing the same search
			Hits hits2 = mSearcher2.search(query);
			
			Assert.AreEqual(4, hits2.length());
			
			try
			{
				// iterating over the hit documents
				for (int i = 0; i < hits2.length(); i++)
				{
					// no exception should happen at this point
					Document d = hits2.doc(i);
				}
			}
			catch (System.Exception e)
			{
				Assert.Fail("Exception thrown: " + e.Message);
                System.Console.Error.WriteLine(e.Source);
            }
			finally
			{
				mSearcher2.close();
			}
			
			//--------------------------------------------------------------------
			// scenario 3
			//--------------------------------------------------------------------
			
			// deleting the document just added, this will cause a different exception to take place
			Term term = new Term("id", "doc1");
			IndexReader readerB = IndexReader.open(indexStoreB);
			readerB.Delete(term);
			readerB.close();
			
			// optimizing the index with the writer
			writerB = new IndexWriter(indexStoreB, new StandardAnalyzer(), false);
			writerB.optimize();
			writerB.close();
			
			// building the searchables
			Searcher[] searchers3 = new Searcher[2];
			
			searchers3[0] = new IndexSearcher(indexStoreB);
			searchers3[1] = new IndexSearcher(indexStoreA);
			// creating the mulitSearcher
			Searcher mSearcher3 = GetMultiSearcherInstance(searchers3);
			// performing the same search
			Hits hits3 = mSearcher3.search(query);
			
			Assert.AreEqual(3, hits3.length());
			
			try
			{
				// iterating over the hit documents
				for (int i = 0; i < hits3.length(); i++)
				{
					Document d = hits3.doc(i);
				}
			}
			catch (System.IO.IOException e)
			{
				Assert.Fail("IOException thrown: " + e.Message);
                System.Console.Error.WriteLine(e.Source);
            }
			finally
			{
				mSearcher3.close();
			}
		}
	}
}