/*
 * 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 NUnit.Framework;
namespace clucene.net //.document
{
	/// <summary> Tests {@link Document} class.
	/// 
	/// </summary>
	/// <author>  Otis Gospodnetic
	/// </author>
	/// <version>  $Id: TestDocument.cs,v 1.3 2006/01/11 05:15:07 ustramooner Exp $
	/// </version>
	[TestFixture]
	public class TestDocument
	{
		
		/// <summary> Tests {@link Document#remove()} method for a brand new Document
		/// that has not been indexed yet.
		/// 
		/// </summary>
		/// <throws>  Exception on error </throws>
		[Test]
		public virtual void  TestRemoveForNewDocument()
		{
			Document doc = MakeDocumentWithFields();
			Assert.AreEqual(8, countFields(doc.fields()));
			doc.removeFields("keyword");
			Assert.AreEqual(6, countFields(doc.fields()));
			doc.removeFields("doesnotexists"); // removing non-existing fields is siltenlty ignored
			doc.removeFields("keyword"); // removing a Field more than once
			Assert.AreEqual(6, countFields(doc.fields()));
			doc.removeField("text");
			Assert.AreEqual(5, countFields(doc.fields()));
			doc.removeField("text");
			Assert.AreEqual(4, countFields(doc.fields()));
			doc.removeField("text");
			Assert.AreEqual(4, countFields(doc.fields()));
			doc.removeField("doesnotexists"); // removing non-existing fields is siltenlty ignored
			Assert.AreEqual(4, countFields(doc.fields()));
			doc.removeFields("unindexed");
			Assert.AreEqual(2, countFields(doc.fields()));
			doc.removeFields("unstored");
			Assert.AreEqual(0, countFields(doc.fields()));
			doc.removeFields("doesnotexists"); // removing non-existing fields is siltenlty ignored
			Assert.AreEqual(0, countFields(doc.fields()));
		}

		public int countFields(DocumentFieldEnumeration fields)
		{
			int ret = 0;
			while (fields.hasMoreElements())
			{
				ret++;
				fields.nextElement();
			}
			return ret;
		}

		/// <summary> Tests {@link Document#getValues()} method for a brand new Document
		/// that has not been indexed yet.
		/// 
		/// </summary>
		/// <throws>  Exception on error </throws>
		[Test]
		public virtual void  TestGetValuesForNewDocument()
		{
			DoAssert(MakeDocumentWithFields(), false);
		}
		
		/// <summary> Tests {@link Document#getValues()} method for a Document retrieved from
		/// an index.
		/// 
		/// </summary>
		/// <throws>  Exception on error </throws>
		[Test]
        public virtual void  TestGetValuesForIndexedDocument()
		{
			RAMDirectory dir = new RAMDirectory();
			IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true);
			Document doc = MakeDocumentWithFields();
			writer.addDocument(doc);
			writer.close();
			
			Searcher searcher = new IndexSearcher(dir);
			
			// search for something that does exists
			Query query = new TermQuery(new Term("keyword", "test1"));
			
			// ensure that queries return expected results without DateFilter first
			Hits hits = searcher.search(query);
			Assert.AreEqual(1, hits.length());
			
			try
			{
				DoAssert(hits.doc(0), true);
			}
			catch (System.Exception e)
			{
                System.Console.Error.WriteLine(e.StackTrace);
				System.Console.Error.Write("\n");
			}
			finally
			{
				searcher.close();
			}
		}
		
		private Document MakeDocumentWithFields()
		{
			Document doc = new Document();
			doc.add(Field.Keyword("keyword", "test1"));
			doc.add(Field.Keyword("keyword", "test2"));
			doc.add(Field.Text("text", "test1"));
			doc.add(Field.Text("text", "test2"));
			doc.add(Field.UnIndexed("unindexed", "test1"));
			doc.add(Field.UnIndexed("unindexed", "test2"));
			doc.add(Field.UnStored("unstored", "test1"));
			doc.add(Field.UnStored("unstored", "test2"));
			return doc;
		}
		
		private void  DoAssert(Document doc, bool fromIndex)
		{
			System.String[] keywordFieldValues = doc.getValues("keyword");
			System.String[] textFieldValues = doc.getValues("text");
			System.String[] unindexedFieldValues = doc.getValues("unindexed");
			System.String[] unstoredFieldValues = doc.getValues("unstored");
			
			/*Assert.IsTrue(keywordFieldValues.length == 2);
			Assert.IsTrue(textFieldValues.length == 2);
			Assert.IsTrue(unindexedFieldValues.length == 2);
			// this test cannot work for documents retrieved from the index
			// since unstored fields will obviously not be returned
			if (!fromIndex)
			{
				Assert.IsTrue(unstoredFieldValues.length == 2);
			}

			Assert.IsTrue(keywordFieldValues[0].Equals("test1"));
			Assert.IsTrue(keywordFieldValues[1].Equals("test2"));
			Assert.IsTrue(textFieldValues[0].Equals("test1"));
			Assert.IsTrue(textFieldValues[1].Equals("test2"));
			Assert.IsTrue(unindexedFieldValues[0].Equals("test1"));
			Assert.IsTrue(unindexedFieldValues[1].Equals("test2"));
			// this test cannot work for documents retrieved from the index
			// since unstored fields will obviously not be returned
			if (!fromIndex)
			{
				Assert.IsTrue(unstoredFieldValues[0].Equals("test1"));
				Assert.IsTrue(unstoredFieldValues[1].Equals("test2"));
			}*/
		}
	}
}