1 # ====================================================================
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13 # ====================================================================
15 from unittest import TestCase, main
19 class TestBinaryDocument(TestCase):
21 binaryValStored = "this text will be stored as a byte array in the index"
22 binaryValCompressed = "this text will be also stored and compressed as a byte array in the index"
24 def testBinaryFieldInIndex(self):
26 bytes = JArray('byte')(self.binaryValStored)
27 binaryFldStored = Field("binaryStored", bytes,
29 stringFldStored = Field("stringStored", self.binaryValStored,
30 Field.Store.YES, Field.Index.NO,
34 # binary fields with store off are not allowed
35 Field("fail", bytes, Field.Store.NO)
38 self.assertEqual(e.getJavaException().getClass().getName(),
39 'java.lang.IllegalArgumentException')
42 doc.add(binaryFldStored)
43 doc.add(stringFldStored)
45 # test for field count
46 self.assertEqual(2, doc.fields.size())
48 # add the doc to a ram index
50 writer = IndexWriter(dir, StandardAnalyzer(Version.LUCENE_CURRENT),
51 True, IndexWriter.MaxFieldLength.LIMITED)
52 writer.addDocument(doc)
55 # open a reader and fetch the document
56 reader = IndexReader.open(dir, False)
57 docFromReader = reader.document(0)
58 self.assert_(docFromReader is not None)
60 # fetch the binary stored field and compare it's content with the
62 bytes = docFromReader.getBinaryValue("binaryStored")
63 binaryFldStoredTest = bytes.string_
64 self.assertEqual(binaryFldStoredTest, self.binaryValStored)
66 # fetch the string field and compare it's content with the original
68 stringFldStoredTest = docFromReader.get("stringStored")
69 self.assertEqual(stringFldStoredTest, self.binaryValStored)
71 # delete the document from index
72 reader.deleteDocument(0)
73 self.assertEqual(0, reader.numDocs())
78 def testCompressionTools(self):
80 bytes = JArray('byte')(self.binaryValCompressed)
81 binaryFldCompressed = Field("binaryCompressed", CompressionTools.compress(bytes), Field.Store.YES)
82 stringFldCompressed = Field("stringCompressed", CompressionTools.compressString(self.binaryValCompressed), Field.Store.YES)
85 doc.add(binaryFldCompressed)
86 doc.add(stringFldCompressed)
88 # add the doc to a ram index
90 writer = IndexWriter(dir, StandardAnalyzer(Version.LUCENE_CURRENT),
91 True, IndexWriter.MaxFieldLength.LIMITED)
92 writer.addDocument(doc)
95 # open a reader and fetch the document
96 reader = IndexReader.open(dir, False)
97 docFromReader = reader.document(0)
98 self.assert_(docFromReader is not None)
100 # fetch the binary compressed field and compare it's content with
102 bytes = CompressionTools.decompress(docFromReader.getBinaryValue("binaryCompressed"))
103 binaryFldCompressedTest = bytes.string_
104 self.assertEqual(binaryFldCompressedTest, self.binaryValCompressed)
105 self.assertEqual(CompressionTools.decompressString(docFromReader.getBinaryValue("stringCompressed")), self.binaryValCompressed)
111 if __name__ == '__main__':
114 if '-loop' in sys.argv:
115 sys.argv.remove('-loop')