| 1 |
|
package org.apache.lucene.document; |
| 2 |
|
|
| 3 |
|
import junit.framework.TestCase; |
| 4 |
|
|
| 5 |
|
import org.apache.lucene.store.RAMDirectory; |
| 6 |
|
import org.apache.lucene.document.Document; |
| 7 |
|
import org.apache.lucene.document.Field; |
| 8 |
|
import org.apache.lucene.analysis.standard.StandardAnalyzer; |
| 9 |
|
import org.apache.lucene.index.IndexWriter; |
| 10 |
|
import org.apache.lucene.index.Term; |
| 11 |
|
import org.apache.lucene.search.Query; |
| 12 |
|
import org.apache.lucene.search.TermQuery; |
| 13 |
|
import org.apache.lucene.search.IndexSearcher; |
| 14 |
|
import org.apache.lucene.search.Searcher; |
| 15 |
|
import org.apache.lucene.search.Hits; |
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
@link |
| 35 |
|
|
| 36 |
|
@author |
| 37 |
|
@version |
| 38 |
|
|
|
|
|
| 98.1% |
Uncovered Elements: 2 (108) |
Complexity: 11 |
Complexity Density: 0.11 |
|
| 39 |
|
public class TestDocument extends TestCase |
| 40 |
|
{ |
| 41 |
|
|
| 42 |
|
String binaryVal = "this text will be stored as a byte array in the index"; |
| 43 |
|
String binaryVal2 = "this text will be also stored as a byte array in the index"; |
| 44 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (28) |
Complexity: 1 |
Complexity Density: 0.04 |
1
PASS
|
|
| 45 |
1
|
public void testBinaryField()... |
| 46 |
|
throws Exception |
| 47 |
|
{ |
| 48 |
1
|
Document doc = new Document(); |
| 49 |
1
|
Fieldable stringFld = new Field("string", binaryVal, Field.Store.YES, Field.Index.NO); |
| 50 |
1
|
Fieldable binaryFld = new Field("binary", binaryVal.getBytes(), Field.Store.YES); |
| 51 |
1
|
Fieldable binaryFld2 = new Field("binary", binaryVal2.getBytes(), Field.Store.YES); |
| 52 |
|
|
| 53 |
1
|
doc.add(stringFld); |
| 54 |
1
|
doc.add(binaryFld); |
| 55 |
|
|
| 56 |
1
|
assertEquals(2, doc.fields.size()); |
| 57 |
|
|
| 58 |
1
|
assertTrue(binaryFld.isBinary()); |
| 59 |
1
|
assertTrue(binaryFld.isStored()); |
| 60 |
1
|
assertFalse(binaryFld.isIndexed()); |
| 61 |
1
|
assertFalse(binaryFld.isTokenized()); |
| 62 |
|
|
| 63 |
1
|
String binaryTest = new String(doc.getBinaryValue("binary")); |
| 64 |
1
|
assertTrue(binaryTest.equals(binaryVal)); |
| 65 |
|
|
| 66 |
1
|
String stringTest = doc.get("string"); |
| 67 |
1
|
assertTrue(binaryTest.equals(stringTest)); |
| 68 |
|
|
| 69 |
1
|
doc.add(binaryFld2); |
| 70 |
|
|
| 71 |
1
|
assertEquals(3, doc.fields.size()); |
| 72 |
|
|
| 73 |
1
|
byte[][] binaryTests = doc.getBinaryValues("binary"); |
| 74 |
|
|
| 75 |
1
|
assertEquals(2, binaryTests.length); |
| 76 |
|
|
| 77 |
1
|
binaryTest = new String(binaryTests[0]); |
| 78 |
1
|
String binaryTest2 = new String(binaryTests[1]); |
| 79 |
|
|
| 80 |
1
|
assertFalse(binaryTest.equals(binaryTest2)); |
| 81 |
|
|
| 82 |
1
|
assertTrue(binaryTest.equals(binaryVal)); |
| 83 |
1
|
assertTrue(binaryTest2.equals(binaryVal2)); |
| 84 |
|
|
| 85 |
1
|
doc.removeField("string"); |
| 86 |
1
|
assertEquals(2, doc.fields.size()); |
| 87 |
|
|
| 88 |
1
|
doc.removeFields("binary"); |
| 89 |
1
|
assertEquals(0, doc.fields.size()); |
| 90 |
|
} |
| 91 |
|
|
| 92 |
|
|
| 93 |
|
@link |
| 94 |
|
|
| 95 |
|
|
| 96 |
|
@throws |
| 97 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (21) |
Complexity: 1 |
Complexity Density: 0.05 |
1
PASS
|
|
| 98 |
1
|
public void testRemoveForNewDocument() throws Exception... |
| 99 |
|
{ |
| 100 |
1
|
Document doc = makeDocumentWithFields(); |
| 101 |
1
|
assertEquals(8, doc.fields.size()); |
| 102 |
1
|
doc.removeFields("keyword"); |
| 103 |
1
|
assertEquals(6, doc.fields.size()); |
| 104 |
1
|
doc.removeFields("doesnotexists"); |
| 105 |
1
|
doc.removeFields("keyword"); |
| 106 |
1
|
assertEquals(6, doc.fields.size()); |
| 107 |
1
|
doc.removeField("text"); |
| 108 |
1
|
assertEquals(5, doc.fields.size()); |
| 109 |
1
|
doc.removeField("text"); |
| 110 |
1
|
assertEquals(4, doc.fields.size()); |
| 111 |
1
|
doc.removeField("text"); |
| 112 |
1
|
assertEquals(4, doc.fields.size()); |
| 113 |
1
|
doc.removeField("doesnotexists"); |
| 114 |
1
|
assertEquals(4, doc.fields.size()); |
| 115 |
1
|
doc.removeFields("unindexed"); |
| 116 |
1
|
assertEquals(2, doc.fields.size()); |
| 117 |
1
|
doc.removeFields("unstored"); |
| 118 |
1
|
assertEquals(0, doc.fields.size()); |
| 119 |
1
|
doc.removeFields("doesnotexists"); |
| 120 |
1
|
assertEquals(0, doc.fields.size()); |
| 121 |
|
} |
| 122 |
|
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 3 |
Complexity Density: 0.33 |
1
PASS
|
|
| 123 |
1
|
public void testConstructorExceptions()... |
| 124 |
|
{ |
| 125 |
1
|
new Field("name", "value", Field.Store.YES, Field.Index.NO); |
| 126 |
1
|
new Field("name", "value", Field.Store.NO, Field.Index.UN_TOKENIZED); |
| 127 |
1
|
try { |
| 128 |
1
|
new Field("name", "value", Field.Store.NO, Field.Index.NO); |
| 129 |
0
|
fail(); |
| 130 |
|
} catch(IllegalArgumentException e) { |
| 131 |
|
|
| 132 |
|
} |
| 133 |
1
|
new Field("name", "value", Field.Store.YES, Field.Index.NO, Field.TermVector.NO); |
| 134 |
1
|
try { |
| 135 |
1
|
new Field("name", "value", Field.Store.YES, Field.Index.NO, Field.TermVector.YES); |
| 136 |
0
|
fail(); |
| 137 |
|
} catch(IllegalArgumentException e) { |
| 138 |
|
|
| 139 |
|
} |
| 140 |
|
} |
| 141 |
|
|
| 142 |
|
|
| 143 |
|
@link |
| 144 |
|
|
| 145 |
|
|
| 146 |
|
@throws |
| 147 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
| 148 |
1
|
public void testGetValuesForNewDocument() throws Exception... |
| 149 |
|
{ |
| 150 |
1
|
doAssert(makeDocumentWithFields(), false); |
| 151 |
|
} |
| 152 |
|
|
| 153 |
|
|
| 154 |
|
@link |
| 155 |
|
|
| 156 |
|
|
| 157 |
|
@throws |
| 158 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1
PASS
|
|
| 159 |
1
|
public void testGetValuesForIndexedDocument() throws Exception... |
| 160 |
|
{ |
| 161 |
1
|
RAMDirectory dir = new RAMDirectory(); |
| 162 |
1
|
IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true); |
| 163 |
1
|
writer.addDocument(makeDocumentWithFields()); |
| 164 |
1
|
writer.close(); |
| 165 |
|
|
| 166 |
1
|
Searcher searcher = new IndexSearcher(dir); |
| 167 |
|
|
| 168 |
|
|
| 169 |
1
|
Query query = new TermQuery(new Term("keyword", "test1")); |
| 170 |
|
|
| 171 |
|
|
| 172 |
1
|
Hits hits = searcher.search(query); |
| 173 |
1
|
assertEquals(1, hits |