1 package org.apache.lucene.document;
3 import org.apache.lucene.util.LuceneTestCase;
5 import org.apache.lucene.index.IndexReader;
6 import org.apache.lucene.index.RandomIndexWriter;
7 import org.apache.lucene.store.Directory;
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
17 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 * Tests {@link Document} class.
29 public class TestBinaryDocument extends LuceneTestCase {
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";
34 public void testBinaryFieldInIndex()
37 Fieldable binaryFldStored = new Field("binaryStored", binaryValStored.getBytes());
38 Fieldable stringFldStored = new Field("stringStored", binaryValStored, Field.Store.YES, Field.Index.NO, Field.TermVector.NO);
41 // binary fields with store off are not allowed
42 new Field("fail", binaryValStored.getBytes(), Field.Store.NO);
45 catch (IllegalArgumentException iae) {
48 Document doc = new Document();
50 doc.add(binaryFldStored);
52 doc.add(stringFldStored);
54 /** test for field count */
55 assertEquals(2, doc.fields.size());
57 /** add the doc to a ram index */
58 Directory dir = newDirectory();
59 RandomIndexWriter writer = new RandomIndexWriter(random, dir);
60 writer.addDocument(doc);
62 /** open a reader and fetch the document */
63 IndexReader reader = writer.getReader();
64 Document docFromReader = reader.document(0);
65 assertTrue(docFromReader != null);
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));
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));
78 reader = IndexReader.open(dir, false);
79 /** delete the document from index */
80 reader.deleteDocument(0);
81 assertEquals(0, reader.numDocs());
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));
91 Document doc = new Document();
93 doc.add(binaryFldCompressed);
94 doc.add(stringFldCompressed);
96 /** add the doc to a ram index */
97 Directory dir = newDirectory();
98 RandomIndexWriter writer = new RandomIndexWriter(random, dir);
99 writer.addDocument(doc);
101 /** open a reader and fetch the document */
102 IndexReader reader = writer.getReader();
103 Document docFromReader = reader.document(0);
104 assertTrue(docFromReader != null);
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));