Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
97   224   11   13.86
4   145   0.11   7
7     1.57  
1    
 
  TestDocument       Line # 39 97 11 98.1% 0.9814815
 
  (5)
 
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    /**
18    * Copyright 2004 The Apache Software Foundation
19    *
20    * Licensed under the Apache License, Version 2.0 (the "License");
21    * you may not use this file except in compliance with the License.
22    * You may obtain a copy of the License at
23    *
24    * http://www.apache.org/licenses/LICENSE-2.0
25    *
26    * Unless required by applicable law or agreed to in writing, software
27    * distributed under the License is distributed on an "AS IS" BASIS,
28    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29    * See the License for the specific language governing permissions and
30    * limitations under the License.
31    */
32   
33    /**
34    * Tests {@link Document} class.
35    *
36    * @author Otis Gospodnetic
37    * @version $Id: TestDocument.java 413201 2006-06-10 01:23:22Z gsingers $
38    */
 
39    public class TestDocument extends TestCase
40    {
41   
42    String binaryVal = "this text will be stored as a byte array in the index";
43    String binaryVal2 = "this text will be also stored as a byte array in the index";
44   
 
45  1 toggle public void testBinaryField()
46    throws Exception
47    {
48  1 Document doc = new Document();
49  1 Fieldable stringFld = new Field("string", binaryVal, Field.Store.YES, Field.Index.NO);
50  1 Fieldable binaryFld = new Field("binary", binaryVal.getBytes(), Field.Store.YES);
51  1 Fieldable binaryFld2 = new Field("binary", binaryVal2.getBytes(), Field.Store.YES);
52   
53  1 doc.add(stringFld);
54  1 doc.add(binaryFld);
55   
56  1 assertEquals(2, doc.fields.size());
57   
58  1 assertTrue(binaryFld.isBinary());
59  1 assertTrue(binaryFld.isStored());
60  1 assertFalse(binaryFld.isIndexed());
61  1 assertFalse(binaryFld.isTokenized());
62   
63  1 String binaryTest = new String(doc.getBinaryValue("binary"));
64  1 assertTrue(binaryTest.equals(binaryVal));
65   
66  1 String stringTest = doc.get("string");
67  1 assertTrue(binaryTest.equals(stringTest));
68   
69  1 doc.add(binaryFld2);
70   
71  1 assertEquals(3, doc.fields.size());
72   
73  1 byte[][] binaryTests = doc.getBinaryValues("binary");
74   
75  1 assertEquals(2, binaryTests.length);
76   
77  1 binaryTest = new String(binaryTests[0]);
78  1 String binaryTest2 = new String(binaryTests[1]);
79   
80  1 assertFalse(binaryTest.equals(binaryTest2));
81   
82  1 assertTrue(binaryTest.equals(binaryVal));
83  1 assertTrue(binaryTest2.equals(binaryVal2));
84   
85  1 doc.removeField("string");
86  1 assertEquals(2, doc.fields.size());
87   
88  1 doc.removeFields("binary");
89  1 assertEquals(0, doc.fields.size());
90    }
91   
92    /**
93    * Tests {@link Document#removeField(String)} method for a brand new Document
94    * that has not been indexed yet.
95    *
96    * @throws Exception on error
97    */
 
98  1 toggle public void testRemoveForNewDocument() throws Exception
99    {
100  1 Document doc = makeDocumentWithFields();
101  1 assertEquals(8, doc.fields.size());
102  1 doc.removeFields("keyword");
103  1 assertEquals(6, doc.fields.size());
104  1 doc.removeFields("doesnotexists"); // removing non-existing fields is siltenlty ignored
105  1 doc.removeFields("keyword"); // removing a field more than once
106  1 assertEquals(6, doc.fields.size());
107  1 doc.removeField("text");
108  1 assertEquals(5, doc.fields.size());
109  1 doc.removeField("text");
110  1 assertEquals(4, doc.fields.size());
111  1 doc.removeField("text");
112  1 assertEquals(4, doc.fields.size());
113  1 doc.removeField("doesnotexists"); // removing non-existing fields is siltenlty ignored
114  1 assertEquals(4, doc.fields.size());
115  1 doc.removeFields("unindexed");
116  1 assertEquals(2, doc.fields.size());
117  1 doc.removeFields("unstored");
118  1 assertEquals(0, doc.fields.size());
119  1 doc.removeFields("doesnotexists"); // removing non-existing fields is siltenlty ignored
120  1 assertEquals(0, doc.fields.size());
121    }
122   
 
123  1 toggle public void testConstructorExceptions()
124    {
125  1 new Field("name", "value", Field.Store.YES, Field.Index.NO); // okay
126  1 new Field("name", "value", Field.Store.NO, Field.Index.UN_TOKENIZED); // okay
127  1 try {
128  1 new Field("name", "value", Field.Store.NO, Field.Index.NO);
129  0 fail();
130    } catch(IllegalArgumentException e) {
131    // expected exception
132    }
133  1 new Field("name", "value", Field.Store.YES, Field.Index.NO, Field.TermVector.NO); // okay
134  1 try {
135  1 new Field("name", "value", Field.Store.YES, Field.Index.NO, Field.TermVector.YES);
136  0 fail();
137    } catch(IllegalArgumentException e) {
138    // expected exception
139    }
140    }
141   
142    /**
143    * Tests {@link Document#getValues(String)} method for a brand new Document
144    * that has not been indexed yet.
145    *
146    * @throws Exception on error
147    */
 
148  1 toggle public void testGetValuesForNewDocument() throws Exception
149    {
150  1 doAssert(makeDocumentWithFields(), false);
151    }
152   
153    /**
154    * Tests {@link Document#getValues(String)} method for a Document retrieved from
155    * an index.
156    *
157    * @throws Exception on error
158    */
 
159  1 toggle public void testGetValuesForIndexedDocument() throws Exception
160    {
161  1 RAMDirectory dir = new RAMDirectory();
162  1 IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true);
163  1 writer.addDocument(makeDocumentWithFields());
164  1 writer.close();
165   
166  1 Searcher searcher = new IndexSearcher(dir);
167   
168    // search for something that does exists
169  1 Query query = new TermQuery(new Term("keyword", "test1"));
170   
171    // ensure that queries return expected results without DateFilter first
172  1 Hits hits = searcher.search(query);
173  1 assertEquals(1, hits