Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
32   101   2   32
0   50   0.06   1
1     2  
1    
 
  TestBinaryDocument       Line # 32 32 2 97% 0.969697
 
  (1)
 
1    package org.apache.lucene.document;
2   
3    import junit.framework.TestCase;
4   
5    import org.apache.lucene.analysis.standard.StandardAnalyzer;
6    import org.apache.lucene.index.IndexReader;
7    import org.apache.lucene.index.IndexWriter;
8    import org.apache.lucene.store.RAMDirectory;
9   
10    /**
11    * Copyright 2004 The Apache Software Foundation
12    *
13    * Licensed under the Apache License, Version 2.0 (the "License");
14    * you may not use this file except in compliance with the License.
15    * You may obtain a copy of the License at
16    *
17    * http://www.apache.org/licenses/LICENSE-2.0
18    *
19    * Unless required by applicable law or agreed to in writing, software
20    * distributed under the License is distributed on an "AS IS" BASIS,
21    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22    * See the License for the specific language governing permissions and
23    * limitations under the License.
24    */
25   
26    /**
27    * Tests {@link Document} class.
28    *
29    * @author Bernhard Messer
30    * @version $Id: TestBinaryDocument.java 413201 2006-06-10 01:23:22Z gsingers $
31    */
 
32    public class TestBinaryDocument extends TestCase
33    {
34   
35    String binaryValStored = "this text will be stored as a byte array in the index";
36    String binaryValCompressed = "this text will be also stored and compressed as a byte array in the index";
37   
 
38  1 toggle public void testBinaryFieldInIndex()
39    throws Exception
40    {
41  1 Fieldable binaryFldStored = new Field("binaryStored", binaryValStored.getBytes(), Field.Store.YES);
42  1 Fieldable binaryFldCompressed = new Field("binaryCompressed", binaryValCompressed.getBytes(), Field.Store.COMPRESS);
43  1 Fieldable stringFldStored = new Field("stringStored", binaryValStored, Field.Store.YES, Field.Index.NO, Field.TermVector.NO);
44  1 Fieldable stringFldCompressed = new Field("stringCompressed", binaryValCompressed, Field.Store.COMPRESS, Field.Index.NO, Field.TermVector.NO);
45   
46  1 try {
47    // binary fields with store off are not allowed
48  1 new Field("fail", binaryValCompressed.getBytes(), Field.Store.NO);
49  0 fail();
50    }
51    catch (IllegalArgumentException iae) {
52  1 ;
53    }
54   
55  1 Document doc = new Document();
56   
57  1 doc.add(binaryFldStored);
58  1 doc.add(binaryFldCompressed);
59   
60  1 doc.add(stringFldStored);
61  1 doc.add(stringFldCompressed);
62   
63    /** test for field count */
64  1 assertEquals(4, doc.fields.size());
65   
66    /** add the doc to a ram index */
67  1 RAMDirectory dir = new RAMDirectory();
68  1 IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true);
69  1 writer.addDocument(doc);
70  1 writer.close();
71   
72    /** open a reader and fetch the document */
73  1 IndexReader reader = IndexReader.open(dir);
74  1 Document docFromReader = reader.document(0);
75  1 assertTrue(docFromReader != null);
76   
77    /** fetch the binary stored field and compare it's content with the original one */
78  1 String binaryFldStoredTest = new String(docFromReader.getBinaryValue("binaryStored"));
79  1 assertTrue(binaryFldStoredTest.equals(binaryValStored));
80   
81    /** fetch the binary compressed field and compare it's content with the original one */
82  1 String binaryFldCompressedTest = new String(docFromReader.getBinaryValue("binaryCompressed"));
83  1 assertTrue(binaryFldCompressedTest.equals(binaryValCompressed));
84   
85    /** fetch the string field and compare it's content with the original one */
86  1 String stringFldStoredTest = new String(docFromReader.get("stringStored"));
87  1 assertTrue(stringFldStoredTest.equals(binaryValStored));
88   
89    /** fetch the compressed string field and compare it's content with the original one */
90  1 String stringFldCompressedTest = new String(docFromReader.get("stringCompressed"));
91  1 assertTrue(stringFldCompressedTest.equals(binaryValCompressed));
92   
93    /** delete the document from index */
94  1 reader.deleteDocument(0);
95  1 assertEquals(0, reader.numDocs());
96   
97  1 reader.close();
98   
99    }
100   
101    }