package org.apache.lucene.index;

/**
 * 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.
 */

import junit.framework.TestCase;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.search.Similarity;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;

import java.io.IOException;

public class TestDocumentWriter extends TestCase {
  private RAMDirectory dir = new RAMDirectory();
  private Document testDoc = new Document();


  public TestDocumentWriter(String s) {
    super(s);
  }

  protected void setUp() {
    DocHelper.setupDoc(testDoc);
  }

  protected void tearDown() {

  }

  public void test() {
    assertTrue(dir != null);

  }

  public void testAddDocument() {
    Analyzer analyzer = new WhitespaceAnalyzer();
    Similarity similarity = Similarity.getDefault();
    DocumentWriter writer = new DocumentWriter(dir, analyzer, similarity, 50);
    assertTrue(writer != null);
    try {
      writer.addDocument("test", testDoc);
      //After adding the document, we should be able to read it back in
      SegmentReader reader = SegmentReader.get(new SegmentInfo("test", 1, dir));
      assertTrue(reader != null);
      Document doc = reader.document(0);
      assertTrue(doc != null);
      
      //System.out.println("Document: " + doc);
      Field [] fields = doc.getFields("textField2");
      assertTrue(fields != null && fields.length == 1);
      assertTrue(fields[0].stringValue().equals(DocHelper.FIELD_2_TEXT));
      assertTrue(fields[0].isTermVectorStored() == true);
      
      fields = doc.getFields("textField1");
      assertTrue(fields != null && fields.length == 1);
      assertTrue(fields[0].stringValue().equals(DocHelper.FIELD_1_TEXT));
      assertTrue(fields[0].isTermVectorStored() == false);
      
      fields = doc.getFields("keyField");
      assertTrue(fields != null && fields.length == 1);
      assertTrue(fields[0].stringValue().equals(DocHelper.KEYWORD_TEXT));

      fields = doc.getFields(DocHelper.NO_NORMS_KEY);
      assertTrue(fields != null && fields.length == 1);
      assertTrue(fields[0].stringValue().equals(DocHelper.NO_NORMS_TEXT));

      fields = doc.getFields(DocHelper.TEXT_FIELD_3_KEY);
      assertTrue(fields != null && fields.length == 1);
      assertTrue(fields[0].stringValue().equals(DocHelper.FIELD_3_TEXT));

    } catch (IOException e) {
      e.printStackTrace();
      assertTrue(false);
    }
  }
}

@@ -1601,16 +1601,45 @@
   try %7B%0A
+      String segName=%22test%22;%0A
       wr
@@ -1655,22 +1655,23 @@
 ocument(
-%22test%22
+segName
 , testDo
@@ -1809,22 +1809,23 @@
 entInfo(
-%22test%22
+segName
 , 1, dir
@@ -3021,24 +3021,325 @@
 _3_TEXT));%0A%0A
+      // test that the norm file is not present if omitNorms is true%0A      for (int i=0; i%3Creader.fieldInfos.size(); i++) %7B%0A        FieldInfo fi = reader.fieldInfos.fieldInfo(i);%0A        if (fi.isIndexed) %7B%0A          assertTrue(fi.omitNorms == !dir.fileExists(segName + %22.f%22 + i));%0A        %7D%0A      %7D%0A%0A
     %7D catch 

test for presence/absence of physical norms file