| 1 |
|
package org.apache.lucene.index.store; |
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
import java.io.File; |
| 20 |
|
import java.io.IOException; |
| 21 |
|
|
| 22 |
|
import junit.framework.TestCase; |
| 23 |
|
|
| 24 |
|
import org.apache.lucene.analysis.WhitespaceAnalyzer; |
| 25 |
|
import org.apache.lucene.document.Document; |
| 26 |
|
import org.apache.lucene.document.Field; |
| 27 |
|
import org.apache.lucene.index.IndexReader; |
| 28 |
|
import org.apache.lucene.index.IndexWriter; |
| 29 |
|
import org.apache.lucene.search.IndexSearcher; |
| 30 |
|
import org.apache.lucene.store.Directory; |
| 31 |
|
import org.apache.lucene.store.FSDirectory; |
| 32 |
|
import org.apache.lucene.store.RAMDirectory; |
| 33 |
|
import org.apache.lucene.util.English; |
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
@author |
| 40 |
|
|
| 41 |
|
@version |
| 42 |
|
|
|
|
|
| 95.6% |
Uncovered Elements: 3 (68) |
Complexity: 14 |
Complexity Density: 0.29 |
|
| 43 |
|
public class TestRAMDirectory extends TestCase { |
| 44 |
|
|
| 45 |
|
private File indexDir = null; |
| 46 |
|
|
| 47 |
|
|
| 48 |
|
private final int docsToAdd = 500; |
| 49 |
|
|
| 50 |
|
|
|
|
|
| 88.2% |
Uncovered Elements: 2 (17) |
Complexity: 3 |
Complexity Density: 0.23 |
|
| 51 |
3
|
public void setUp () throws IOException {... |
| 52 |
3
|
String tempDir = System.getProperty("java.io.tmpdir"); |
| 53 |
3
|
if (tempDir == null) |
| 54 |
0
|
throw new IOException("java.io.tmpdir undefined, cannot run test"); |
| 55 |
3
|
indexDir = new File(tempDir, "RAMDirIndex"); |
| 56 |
|
|
| 57 |
3
|
IndexWriter writer = new IndexWriter(indexDir, new WhitespaceAnalyzer(), true); |
| 58 |
|
|
| 59 |
3
|
Document doc = null; |
| 60 |
1503
|
for (int i = 0; i < docsToAdd; i++) { |
| 61 |
1500
|
doc = new Document(); |
| 62 |
1500
|
doc.add(new Field("content", English.intToEnglish(i).trim(), Field.Store.YES, Field.Index.UN_TOKENIZED)); |
| 63 |
1500
|
writer.addDocument(doc); |
| 64 |
|
} |
| 65 |
3
|
assertEquals(docsToAdd, writer.docCount()); |
| 66 |
3
|
writer.optimize(); |
| 67 |
3
|
writer.close(); |
| 68 |
|
} |
| 69 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 2 |
Complexity Density: 0.18 |
1
PASS
|
|
| 70 |
1
|
public void testRAMDirectory () throws IOException {... |
| 71 |
|
|
| 72 |
1
|
Directory dir = FSDirectory.getDirectory(indexDir, false); |
| 73 |
1
|
RAMDirectory ramDir = new RAMDirectory(dir); |
| 74 |
|
|
| 75 |
|
|
| 76 |
1
|
dir.close(); |
| 77 |
|
|
| 78 |
|
|
| 79 |
1
|
IndexReader reader = IndexReader.open(ramDir); |
| 80 |
1
|
assertEquals(docsToAdd, reader.numDocs()); |
| 81 |
|
|
| 82 |
|
|
| 83 |
1
|
IndexSearcher searcher = new IndexSearcher(reader); |
| 84 |
|
|
| 85 |
|
|
| 86 |
501
|
for (int i = 0; i < docsToAdd; i++) { |
| 87 |
500
|
Document doc = searcher.doc(i); |
| 88 |
500
|
assertTrue(doc.getField("content") != null); |
| 89 |
|
} |
| 90 |
|
|
| 91 |
|
|
| 92 |
1
|
reader.close(); |
| 93 |
1
|
searcher.close(); |
| 94 |
|
} |
| 95 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
1
PASS
|
|
| 96 |
1
|
public void testRAMDirectoryFile () throws IOException {... |
| 97 |
|
|
| 98 |
1
|
RAMDirectory ramDir = new RAMDirectory(indexDir); |
| 99 |
|
|
| 100 |
|
|
| 101 |
1
|
IndexReader reader = IndexReader.open(ramDir); |
| 102 |
1
|
assertEquals(docsToAdd, reader.numDocs()); |
| 103 |
|
|
| 104 |
|
|
| 105 |
1
|
IndexSearcher searcher = new IndexSearcher(reader); |
| 106 |
|
|
| 107 |
|
|
| 108 |
501
|
for (int i = 0; i < docsToAdd; i++) { |
| 109 |
500
|
Document doc = searcher.doc(i); |
| 110 |
500
|
assertTrue(doc.getField("content") != null); |
| 111 |
|
} |
| 112 |
|
|
| 113 |
|
|
| 114 |
1
|
reader.close(); |
| 115 |
1
|
searcher.close(); |
| 116 |
|
} |
| 117 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
1
PASS
|
|
| 118 |
1
|
public void testRAMDirectoryString () throws IOException {... |
| 119 |
|
|
| 120 |
1
|
RAMDirectory ramDir = new RAMDirectory(indexDir.getCanonicalPath()); |
| 121 |
|
|
| 122 |
|
|
| 123 |
1
|
IndexReader reader = IndexReader.open(ramDir); |
| 124 |
1
|
assertEquals(docsToAdd, reader.numDocs()); |
| 125 |
|
|
| 126 |
|
|
| 127 |
1
|
IndexSearcher searcher = new IndexSearcher(reader); |
| 128 |
|
|
| 129 |
|
|
| 130 |
501
|
for (int i = 0; i < docsToAdd; i++) { |
| 131 |
500
|
Document doc = searcher.doc(i); |
| 132 |
500
|
assertTrue(doc.getField("content") != null); |
| 133 |
|
} |
| 134 |
|
|
| 135 |
|
|
| 136 |
1
|
reader.close(); |
| 137 |
1
|
searcher.close(); |
| 138 |
|
} |
| 139 |
|
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 3 |
Complexity Density: 1.5 |
|
| 140 |
3
|
public void tearDown() {... |
| 141 |
|
|
| 142 |
3
|
if (indexDir != null && indexDir.exists()) { |
| 143 |
3
|
rmDir (indexDir); |
| 144 |
|
} |
| 145 |
|
} |
| 146 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 147 |
3
|
private void rmDir(File dir) {... |
| 148 |
3
|
File[] files = dir.listFiles(); |
| 149 |
12
|
for (int i = 0; i < files.length; i++) { |
| 150 |
9
|
files[i].delete(); |
| 151 |
|
} |
| 152 |
3
|
dir.delete(); |
| 153 |
|
} |
| 154 |
|
} |