pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / src / test / org / apache / lucene / document / TestBinaryDocument.java
1 package org.apache.lucene.document;
2
3 import org.apache.lucene.util.LuceneTestCase;
4
5 import org.apache.lucene.index.IndexReader;
6 import org.apache.lucene.index.RandomIndexWriter;
7 import org.apache.lucene.store.Directory;
8
9 /**
10  * Licensed to the Apache Software Foundation (ASF) under one or more
11  * contributor license agreements.  See the NOTICE file distributed with
12  * this work for additional information regarding copyright ownership.
13  * The ASF licenses this file to You under the Apache License, Version 2.0
14  * (the "License"); you may not use this file except in compliance with
15  * the License.  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 public class TestBinaryDocument extends LuceneTestCase {
30
31   String binaryValStored = "this text will be stored as a byte array in the index";
32   String binaryValCompressed = "this text will be also stored and compressed as a byte array in the index";
33   
34   public void testBinaryFieldInIndex()
35     throws Exception
36   {
37     Fieldable binaryFldStored = new Field("binaryStored", binaryValStored.getBytes());
38     Fieldable stringFldStored = new Field("stringStored", binaryValStored, Field.Store.YES, Field.Index.NO, Field.TermVector.NO);
39
40     try {
41       // binary fields with store off are not allowed
42       new Field("fail", binaryValStored.getBytes(), Field.Store.NO);
43       fail();
44     }
45     catch (IllegalArgumentException iae) {
46     }
47     
48     Document doc = new Document();
49     
50     doc.add(binaryFldStored);
51     
52     doc.add(stringFldStored);
53
54     /** test for field count */
55     assertEquals(2, doc.fields.size());
56     
57     /** add the doc to a ram index */
58     Directory dir = newDirectory();
59     RandomIndexWriter writer = new RandomIndexWriter(random, dir);
60     writer.addDocument(doc);
61     
62     /** open a reader and fetch the document */ 
63     IndexReader reader = writer.getReader();
64     Document docFromReader = reader.document(0);
65     assertTrue(docFromReader != null);
66     
67     /** fetch the binary stored field and compare it's content with the original one */
68     String binaryFldStoredTest = new String(docFromReader.getBinaryValue("binaryStored"));
69     assertTrue(binaryFldStoredTest.equals(binaryValStored));
70     
71     /** fetch the string field and compare it's content with the original one */
72     String stringFldStoredTest = docFromReader.get("stringStored");
73     assertTrue(stringFldStoredTest.equals(binaryValStored));
74     
75     writer.close();    
76     reader.close();
77     
78     reader = IndexReader.open(dir, false);
79     /** delete the document from index */
80     reader.deleteDocument(0);
81     assertEquals(0, reader.numDocs());
82     
83     reader.close();
84     dir.close();
85   }
86   
87   public void testCompressionTools() throws Exception {
88     Fieldable binaryFldCompressed = new Field("binaryCompressed", CompressionTools.compress(binaryValCompressed.getBytes()));
89     Fieldable stringFldCompressed = new Field("stringCompressed", CompressionTools.compressString(binaryValCompressed));
90     
91     Document doc = new Document();
92     
93     doc.add(binaryFldCompressed);
94     doc.add(stringFldCompressed);
95     
96     /** add the doc to a ram index */
97     Directory dir = newDirectory();
98     RandomIndexWriter writer = new RandomIndexWriter(random, dir);
99     writer.addDocument(doc);
100     
101     /** open a reader and fetch the document */ 
102     IndexReader reader = writer.getReader();
103     Document docFromReader = reader.document(0);
104     assertTrue(docFromReader != null);
105     
106     /** fetch the binary compressed field and compare it's content with the original one */
107     String binaryFldCompressedTest = new String(CompressionTools.decompress(docFromReader.getBinaryValue("binaryCompressed")));
108     assertTrue(binaryFldCompressedTest.equals(binaryValCompressed));
109     assertTrue(CompressionTools.decompressString(docFromReader.getBinaryValue("stringCompressed")).equals(binaryValCompressed));
110
111     writer.close();
112     reader.close();
113     dir.close();
114   }
115 }