Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
48   154   14   8
14   84   0.29   6
6     2.33  
1    
 
  TestRAMDirectory       Line # 43 48 14 95.6% 0.9558824
 
  (3)
 
1    package org.apache.lucene.index.store;
2   
3    /**
4    * Copyright 2005 The Apache Software Foundation
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10    * http://www.apache.org/licenses/LICENSE-2.0
11    *
12    * Unless required by applicable law or agreed to in writing, software
13    * distributed under the License is distributed on an "AS IS" BASIS,
14    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    * See the License for the specific language governing permissions and
16    * limitations under the License.
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    * JUnit testcase to test RAMDirectory. RAMDirectory itself is used in many testcases,
37    * but not one of them uses an different constructor other than the default constructor.
38    *
39    * @author Bernhard Messer
40    *
41    * @version $Id: RAMDirectory.java 150537 2004-09-28 22:45:26 +0200 (Di, 28 Sep 2004) cutting $
42    */
 
43    public class TestRAMDirectory extends TestCase {
44   
45    private File indexDir = null;
46   
47    // add enough document so that the index will be larger than RAMDirectory.READ_BUFFER_SIZE
48    private final int docsToAdd = 500;
49   
50    // setup the index
 
51  3 toggle 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    // add some documents
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   
 
70  1 toggle public void testRAMDirectory () throws IOException {
71   
72  1 Directory dir = FSDirectory.getDirectory(indexDir, false);
73  1 RAMDirectory ramDir = new RAMDirectory(dir);
74   
75    // close the underlaying directory and delete the index
76  1 dir.close();
77   
78    // open reader to test document count
79  1 IndexReader reader = IndexReader.open(ramDir);
80  1 assertEquals(docsToAdd, reader.numDocs());
81   
82    // open search zo check if all doc's are there
83  1 IndexSearcher searcher = new IndexSearcher(reader);
84   
85    // search for all documents
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    // cleanup
92  1 reader.close();
93  1 searcher.close();
94    }
95   
 
96  1 toggle public void testRAMDirectoryFile () throws IOException {
97   
98  1 RAMDirectory ramDir = new RAMDirectory(indexDir);
99   
100    // open reader to test document count
101  1 IndexReader reader = IndexReader.open(ramDir);
102  1 assertEquals(docsToAdd, reader.numDocs());
103   
104    // open search zo check if all doc's are there
105  1 IndexSearcher searcher = new IndexSearcher(reader);
106   
107    // search for all documents
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    // cleanup
114  1 reader.close();
115  1 searcher.close();
116    }
117   
 
118  1 toggle public void testRAMDirectoryString () throws IOException {
119   
120  1 RAMDirectory ramDir = new RAMDirectory(indexDir.getCanonicalPath());
121   
122    // open reader to test document count
123  1 IndexReader reader = IndexReader.open(ramDir);
124  1 assertEquals(docsToAdd, reader.numDocs());
125   
126    // open search zo check if all doc's are there
127  1 IndexSearcher searcher = new IndexSearcher(reader);
128   
129    // search for all documents
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    // cleanup
136  1 reader.close();
137  1 searcher.close();
138    }
139   
 
140  3 toggle public void tearDown() {
141    // cleanup
142  3 if (indexDir != null && indexDir.exists()) {
143  3 rmDir (indexDir);
144    }
145    }
146   
 
147  3 toggle 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    }