KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > document > TestDocument


1 package org.apache.lucene.document;
2
3 import junit.framework.TestCase;
4
5 import org.apache.lucene.store.RAMDirectory;
6 import org.apache.lucene.document.Document;
7 import org.apache.lucene.document.Field;
8 import org.apache.lucene.analysis.standard.StandardAnalyzer;
9 import org.apache.lucene.index.IndexWriter;
10 import org.apache.lucene.index.Term;
11 import org.apache.lucene.search.Query;
12 import org.apache.lucene.search.TermQuery;
13 import org.apache.lucene.search.IndexSearcher;
14 import org.apache.lucene.search.Searcher;
15 import org.apache.lucene.search.Hits;
16
17 import java.io.IOException JavaDoc;
18
19 /**
20  * Copyright 2004 The Apache Software Foundation
21  *
22  * Licensed under the Apache License, Version 2.0 (the "License");
23  * you may not use this file except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  * http://www.apache.org/licenses/LICENSE-2.0
27  *
28  * Unless required by applicable law or agreed to in writing, software
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  */

34
35 /**
36  * Tests {@link Document} class.
37  *
38  * @author Otis Gospodnetic
39  * @version $Id: TestDocument.java,v 1.4 2004/04/20 17:26:16 goller Exp $
40  */

41 public class TestDocument extends TestCase
42 {
43
44   /**
45    * Tests {@link Document#remove()} method for a brand new Document
46    * that has not been indexed yet.
47    *
48    * @throws Exception on error
49    */

50   public void testRemoveForNewDocument() throws Exception JavaDoc
51   {
52     Document doc = makeDocumentWithFields();
53     assertEquals(8, doc.fields.size());
54     doc.removeFields("keyword");
55     assertEquals(6, doc.fields.size());
56     doc.removeFields("doesnotexists"); // removing non-existing fields is siltenlty ignored
57
doc.removeFields("keyword"); // removing a field more than once
58
assertEquals(6, doc.fields.size());
59     doc.removeField("text");
60     assertEquals(5, doc.fields.size());
61     doc.removeField("text");
62     assertEquals(4, doc.fields.size());
63     doc.removeField("text");
64     assertEquals(4, doc.fields.size());
65     doc.removeField("doesnotexists"); // removing non-existing fields is siltenlty ignored
66
assertEquals(4, doc.fields.size());
67     doc.removeFields("unindexed");
68     assertEquals(2, doc.fields.size());
69     doc.removeFields("unstored");
70     assertEquals(0, doc.fields.size());
71     doc.removeFields("doesnotexists"); // removing non-existing fields is siltenlty ignored
72
assertEquals(0, doc.fields.size());
73   }
74
75     /**
76      * Tests {@link Document#getValues()} method for a brand new Document
77      * that has not been indexed yet.
78      *
79      * @throws Exception on error
80      */

81     public void testGetValuesForNewDocument() throws Exception JavaDoc
82     {
83         doAssert(makeDocumentWithFields(), false);
84     }
85
86     /**
87      * Tests {@link Document#getValues()} method for a Document retrieved from
88      * an index.
89      *
90      * @throws Exception on error
91      */

92     public void testGetValuesForIndexedDocument() throws Exception JavaDoc
93     {
94         RAMDirectory dir = new RAMDirectory();
95         IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true);
96         writer.addDocument(makeDocumentWithFields());
97         writer.close();
98
99         Searcher searcher = new IndexSearcher(dir);
100
101     // search for something that does exists
102
Query query = new TermQuery(new Term("keyword", "test1"));
103
104     // ensure that queries return expected results without DateFilter first
105
Hits hits = searcher.search(query);
106     assertEquals(1, hits.length());
107
108         try
109         {
110             doAssert(hits.doc(0), true);
111         }
112         catch (Exception JavaDoc e)
113         {
114             e.printStackTrace(System.err);
115             System.err.print("\n");
116         }
117         finally
118         {
119             searcher.close();
120         }
121     }
122
123     private Document makeDocumentWithFields() throws IOException JavaDoc
124     {
125         Document doc = new Document();
126         doc.add(Field.Keyword( "keyword", "test1"));
127         doc.add(Field.Keyword( "keyword", "test2"));
128         doc.add(Field.Text( "text", "test1"));
129         doc.add(Field.Text( "text", "test2"));
130         doc.add(Field.UnIndexed("unindexed", "test1"));
131         doc.add(Field.UnIndexed("unindexed", "test2"));
132         doc.add(Field.UnStored( "unstored", "test1"));
133         doc.add(Field.UnStored( "unstored", "test2"));
134         return doc;
135     }
136
137     private void doAssert(Document doc, boolean fromIndex)
138     {
139         String JavaDoc[] keywordFieldValues = doc.getValues("keyword");
140         String JavaDoc[] textFieldValues = doc.getValues("text");
141         String JavaDoc[] unindexedFieldValues = doc.getValues("unindexed");
142         String JavaDoc[] unstoredFieldValues = doc.getValues("unstored");
143
144         assertTrue(keywordFieldValues.length == 2);
145         assertTrue(textFieldValues.length == 2);
146         assertTrue(unindexedFieldValues.length == 2);
147         // this test cannot work for documents retrieved from the index
148
// since unstored fields will obviously not be returned
149
if (! fromIndex)
150         {
151             assertTrue(unstoredFieldValues.length == 2);
152         }
153
154         assertTrue(keywordFieldValues[0].equals("test1"));
155         assertTrue(keywordFieldValues[1].equals("test2"));
156         assertTrue(textFieldValues[0].equals("test1"));
157         assertTrue(textFieldValues[1].equals("test2"));
158         assertTrue(unindexedFieldValues[0].equals("test1"));
159         assertTrue(unindexedFieldValues[1].equals("test2"));
160         // this test cannot work for documents retrieved from the index
161
// since unstored fields will obviously not be returned
162
if (! fromIndex)
163         {
164             assertTrue(unstoredFieldValues[0].equals("test1"));
165             assertTrue(unstoredFieldValues[1].equals("test2"));
166         }
167     }
168 }
169
Popular Tags