001: package org.apache.lucene.document;
002:
003: import org.apache.lucene.util.LuceneTestCase;
004:
005: import org.apache.lucene.analysis.standard.StandardAnalyzer;
006: import org.apache.lucene.index.IndexReader;
007: import org.apache.lucene.index.IndexWriter;
008: import org.apache.lucene.store.RAMDirectory;
009:
010: /**
011: * Licensed to the Apache Software Foundation (ASF) under one or more
012: * contributor license agreements. See the NOTICE file distributed with
013: * this work for additional information regarding copyright ownership.
014: * The ASF licenses this file to You under the Apache License, Version 2.0
015: * (the "License"); you may not use this file except in compliance with
016: * the License. You may obtain a copy of the License at
017: *
018: * http://www.apache.org/licenses/LICENSE-2.0
019: *
020: * Unless required by applicable law or agreed to in writing, software
021: * distributed under the License is distributed on an "AS IS" BASIS,
022: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
023: * See the License for the specific language governing permissions and
024: * limitations under the License.
025: */
026:
027: /**
028: * Tests {@link Document} class.
029: *
030: *
031: * @version $Id: TestBinaryDocument.java 598296 2007-11-26 14:52:01Z mikemccand $
032: */
033: public class TestBinaryDocument extends LuceneTestCase {
034:
035: String binaryValStored = "this text will be stored as a byte array in the index";
036: String binaryValCompressed = "this text will be also stored and compressed as a byte array in the index";
037:
038: public void testBinaryFieldInIndex() throws Exception {
039: Fieldable binaryFldStored = new Field("binaryStored",
040: binaryValStored.getBytes(), Field.Store.YES);
041: Fieldable binaryFldCompressed = new Field("binaryCompressed",
042: binaryValCompressed.getBytes(), Field.Store.COMPRESS);
043: Fieldable stringFldStored = new Field("stringStored",
044: binaryValStored, Field.Store.YES, Field.Index.NO,
045: Field.TermVector.NO);
046: Fieldable stringFldCompressed = new Field("stringCompressed",
047: binaryValCompressed, Field.Store.COMPRESS,
048: Field.Index.NO, Field.TermVector.NO);
049:
050: try {
051: // binary fields with store off are not allowed
052: new Field("fail", binaryValCompressed.getBytes(),
053: Field.Store.NO);
054: fail();
055: } catch (IllegalArgumentException iae) {
056: ;
057: }
058:
059: Document doc = new Document();
060:
061: doc.add(binaryFldStored);
062: doc.add(binaryFldCompressed);
063:
064: doc.add(stringFldStored);
065: doc.add(stringFldCompressed);
066:
067: /** test for field count */
068: assertEquals(4, doc.fields.size());
069:
070: /** add the doc to a ram index */
071: RAMDirectory dir = new RAMDirectory();
072: IndexWriter writer = new IndexWriter(dir,
073: new StandardAnalyzer(), true);
074: writer.addDocument(doc);
075: writer.close();
076:
077: /** open a reader and fetch the document */
078: IndexReader reader = IndexReader.open(dir);
079: Document docFromReader = reader.document(0);
080: assertTrue(docFromReader != null);
081:
082: /** fetch the binary stored field and compare it's content with the original one */
083: String binaryFldStoredTest = new String(docFromReader
084: .getBinaryValue("binaryStored"));
085: assertTrue(binaryFldStoredTest.equals(binaryValStored));
086:
087: /** fetch the binary compressed field and compare it's content with the original one */
088: String binaryFldCompressedTest = new String(docFromReader
089: .getBinaryValue("binaryCompressed"));
090: assertTrue(binaryFldCompressedTest.equals(binaryValCompressed));
091:
092: /** fetch the string field and compare it's content with the original one */
093: String stringFldStoredTest = docFromReader.get("stringStored");
094: assertTrue(stringFldStoredTest.equals(binaryValStored));
095:
096: /** fetch the compressed string field and compare it's content with the original one */
097: String stringFldCompressedTest = docFromReader
098: .get("stringCompressed");
099: assertTrue(stringFldCompressedTest.equals(binaryValCompressed));
100:
101: /** delete the document from index */
102: reader.deleteDocument(0);
103: assertEquals(0, reader.numDocs());
104:
105: reader.close();
106:
107: }
108:
109: }
|