/*
 * 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 WhitespaceAnalyzer = Lucene.Net.Analysis.WhitespaceAnalyzer;
using Document = Lucene.Net.Documents.Document;
using Field = Lucene.Net.Documents.Field;
using IndexWriter = Lucene.Net.Index.IndexWriter;
using Term = Lucene.Net.Index.Term;
using RAMDirectory = Lucene.Net.Store.RAMDirectory;*/
using NUnit.Framework;
namespace clucene.net
{
	
	/// <author>  goller
	/// </author>
	[TestFixture]
    public class TestRangeQuery
	{
		
		private int docCount = 0;
		private RAMDirectory dir;
		
        [TestFixtureSetUp]
		public virtual void  SetUp()
		{
			dir = new RAMDirectory();
		}
		
        [Test]
		public virtual void  TestExclusive()
		{
			Query query = new RangeQuery(new Term("content", "A"), new Term("content", "C"), false);
			InitializeIndex(new System.String[]{"A", "B", "C", "D"});
			IndexSearcher searcher = new IndexSearcher(dir);
			Hits hits = searcher.search(query);
			Assert.AreEqual(1, hits.length(), "A,B,C,D, only B in range");
			searcher.close();
			
			InitializeIndex(new System.String[]{"A", "B", "D"});
			searcher = new IndexSearcher(dir);
			hits = searcher.search(query);
			Assert.AreEqual(1, hits.length(), "A,B,D, only B in range");
			searcher.close();
			
			AddDoc("C");
			searcher = new IndexSearcher(dir);
			hits = searcher.search(query);
			Assert.AreEqual(1, hits.length(), "C added, still only B in range");
			searcher.close();
		}
		
        [Test]
		public virtual void  TestInclusive()
		{
			Query query = new RangeQuery(new Term("content", "A"), new Term("content", "C"), true);
			
			InitializeIndex(new System.String[]{"A", "B", "C", "D"});
			IndexSearcher searcher = new IndexSearcher(dir);
			Hits hits = searcher.search(query);
			Assert.AreEqual(3, hits.length(), "A,B,C,D - A,B,C in range");
			searcher.close();
			
			InitializeIndex(new System.String[]{"A", "B", "D"});
			searcher = new IndexSearcher(dir);
			hits = searcher.search(query);
			Assert.AreEqual(2, hits.length(), "A,B,D - A and B in range");
			searcher.close();
			
			AddDoc("C");
			searcher = new IndexSearcher(dir);
			hits = searcher.search(query);
			Assert.AreEqual(3, hits.length(), "C added - A, B, C in range");
			searcher.close();
		}
		
		private void  InitializeIndex(System.String[] values)
		{
			IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
			for (int i = 0; i < values.Length; i++)
			{
				InsertDoc(writer, values[i]);
			}
			writer.close();
		}
		
		private void  AddDoc(System.String content)
		{
			IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), false);
			InsertDoc(writer, content);
			writer.close();
		}
		
		private void  InsertDoc(IndexWriter writer, System.String content)
		{
			Document doc = new Document();
			
			doc.add(Field.Keyword("id", "id" + docCount));
			doc.add(Field.UnStored("content", content));
			
			writer.addDocument(doc);
			docCount++;
		}
	}
}