Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
19   78   2   19
2   40   0.11   1
1     2  
1    
 
  TestDemo       Line # 40 19 2 100% 1.0
 
  (1)
 
1    package org.apache.lucene;
2   
3    /**
4    * Copyright 2004 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 junit.framework.TestCase;
20    import org.apache.lucene.analysis.Analyzer;
21    import org.apache.lucene.analysis.standard.StandardAnalyzer;
22    import org.apache.lucene.document.Document;
23    import org.apache.lucene.document.Field;
24    import org.apache.lucene.index.IndexWriter;
25    import org.apache.lucene.queryParser.ParseException;
26    import org.apache.lucene.queryParser.QueryParser;
27    import org.apache.lucene.search.Hits;
28    import org.apache.lucene.search.IndexSearcher;
29    import org.apache.lucene.search.Query;
30    import org.apache.lucene.store.Directory;
31    import org.apache.lucene.store.RAMDirectory;
32   
33    import java.io.IOException;
34   
35    /**
36    * A very simple demo used in the API documentation (src/java/overview.html).
37    *
38    * @author Daniel Naber
39    */
 
40    public class TestDemo extends TestCase {
41   
 
42  1 toggle public void testDemo() throws IOException, ParseException {
43   
44  1 Analyzer analyzer = new StandardAnalyzer();
45   
46    // Store the index in memory:
47  1 Directory directory = new RAMDirectory();
48    // To store an index on disk, use this instead (note that the
49    // parameter true will overwrite the index in that directory
50    // if one exists):
51    //Directory directory = FSDirectory.getDirectory("/tmp/testindex", true);
52  1 IndexWriter iwriter = new IndexWriter(directory, analyzer, true);
53  1 iwriter.setMaxFieldLength(25000);
54  1 Document doc = new Document();
55  1 String text = "This is the text to be indexed.";
56  1 doc.add(new Field("fieldname", text, Field.Store.YES,
57    Field.Index.TOKENIZED));
58  1 iwriter.addDocument(doc);
59  1 iwriter.close();
60   
61    // Now search the index:
62  1 IndexSearcher isearcher = new IndexSearcher(directory);
63    // Parse a simple query that searches for "text":
64  1 QueryParser parser = new QueryParser("fieldname", analyzer);
65  1 Query query = parser.parse("text");
66  1 Hits hits = isearcher.search(query);
67  1 assertEquals(1, hits.length());
68    // Iterate through the results:
69  2 for (int i = 0; i < hits.length(); i++) {
70  1 Document hitDoc = hits.doc(i);
71  1 assertEquals("This is the text to be indexed.", hitDoc.get("fieldname"));
72    }
73  1 isearcher.close();
74  1 directory.close();
75   
76    }
77   
78    }