pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / benchmark / src / test / org / apache / lucene / benchmark / byTask / feeds / DocMakerTest.java
1 package org.apache.lucene.benchmark.byTask.feeds;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.IOException;
21 import java.util.Properties;
22
23 import org.apache.lucene.analysis.WhitespaceAnalyzer;
24 import org.apache.lucene.benchmark.BenchmarkTestCase;
25 import org.apache.lucene.benchmark.byTask.PerfRunData;
26 import org.apache.lucene.benchmark.byTask.tasks.AddDocTask;
27 import org.apache.lucene.benchmark.byTask.tasks.CloseIndexTask;
28 import org.apache.lucene.benchmark.byTask.tasks.CreateIndexTask;
29 import org.apache.lucene.benchmark.byTask.tasks.TaskSequence;
30 import org.apache.lucene.benchmark.byTask.utils.Config;
31 import org.apache.lucene.document.Document;
32 import org.apache.lucene.index.IndexReader;
33 import org.apache.lucene.index.Term;
34 import org.apache.lucene.search.IndexSearcher;
35 import org.apache.lucene.search.TermQuery;
36 import org.apache.lucene.search.TopDocs;
37
38 /** Tests the functionality of {@link DocMaker}. */
39 public class DocMakerTest extends BenchmarkTestCase {
40
41   static final class OneDocSource extends ContentSource {
42
43     private boolean finish = false;
44     
45     @Override
46     public void close() throws IOException {
47     }
48
49     @Override
50     public DocData getNextDocData(DocData docData) throws NoMoreDataException,
51         IOException {
52       if (finish) {
53         throw new NoMoreDataException();
54       }
55       
56       docData.setBody("body");
57       docData.setDate("date");
58       docData.setTitle("title");
59       Properties props = new Properties();
60       props.setProperty("key", "value");
61       docData.setProps(props);
62       finish = true;
63       
64       return docData;
65     }
66     
67   }
68
69   private void doTestIndexProperties(boolean setIndexProps,
70       boolean indexPropsVal, int numExpectedResults) throws Exception {
71     Properties props = new Properties();
72     
73     // Indexing configuration.
74     props.setProperty("analyzer", WhitespaceAnalyzer.class.getName());
75     props.setProperty("content.source", OneDocSource.class.getName());
76     props.setProperty("directory", "RAMDirectory");
77     if (setIndexProps) {
78       props.setProperty("doc.index.props", Boolean.toString(indexPropsVal));
79     }
80     
81     // Create PerfRunData
82     Config config = new Config(props);
83     PerfRunData runData = new PerfRunData(config);
84
85     TaskSequence tasks = new TaskSequence(runData, getName(), null, false);
86     tasks.addTask(new CreateIndexTask(runData));
87     tasks.addTask(new AddDocTask(runData));
88     tasks.addTask(new CloseIndexTask(runData));
89     tasks.doLogic();
90     tasks.close();
91     
92     IndexReader r = IndexReader.open(runData.getDirectory(), true);
93     IndexSearcher searcher = new IndexSearcher(r);
94     TopDocs td = searcher.search(new TermQuery(new Term("key", "value")), 10);
95     assertEquals(numExpectedResults, td.totalHits);
96     searcher.close();
97     r.close();
98   }
99   
100   private Document createTestNormsDocument(boolean setNormsProp,
101       boolean normsPropVal, boolean setBodyNormsProp, boolean bodyNormsVal)
102       throws Exception {
103     Properties props = new Properties();
104     
105     // Indexing configuration.
106     props.setProperty("analyzer", WhitespaceAnalyzer.class.getName());
107     props.setProperty("content.source", OneDocSource.class.getName());
108     props.setProperty("directory", "RAMDirectory");
109     if (setNormsProp) {
110       props.setProperty("doc.tokenized.norms", Boolean.toString(normsPropVal));
111     }
112     if (setBodyNormsProp) {
113       props.setProperty("doc.body.tokenized.norms", Boolean.toString(bodyNormsVal));
114     }
115     
116     // Create PerfRunData
117     Config config = new Config(props);
118     
119     DocMaker dm = new DocMaker();
120     dm.setConfig(config);
121     return dm.makeDocument();
122   }
123   
124   /* Tests doc.index.props property. */
125   public void testIndexProperties() throws Exception {
126     // default is to not index properties.
127     doTestIndexProperties(false, false, 0);
128     
129     // set doc.index.props to false.
130     doTestIndexProperties(true, false, 0);
131     
132     // set doc.index.props to true.
133     doTestIndexProperties(true, true, 1);
134   }
135   
136   /* Tests doc.tokenized.norms and doc.body.tokenized.norms properties. */
137   public void testNorms() throws Exception {
138     
139     Document doc;
140     
141     // Don't set anything, use the defaults
142     doc = createTestNormsDocument(false, false, false, false);
143     assertTrue(doc.getFieldable(DocMaker.TITLE_FIELD).getOmitNorms());
144     assertFalse(doc.getFieldable(DocMaker.BODY_FIELD).getOmitNorms());
145     
146     // Set norms to false
147     doc = createTestNormsDocument(true, false, false, false);
148     assertTrue(doc.getFieldable(DocMaker.TITLE_FIELD).getOmitNorms());
149     assertFalse(doc.getFieldable(DocMaker.BODY_FIELD).getOmitNorms());
150     
151     // Set norms to true
152     doc = createTestNormsDocument(true, true, false, false);
153     assertFalse(doc.getFieldable(DocMaker.TITLE_FIELD).getOmitNorms());
154     assertFalse(doc.getFieldable(DocMaker.BODY_FIELD).getOmitNorms());
155     
156     // Set body norms to false
157     doc = createTestNormsDocument(false, false, true, false);
158     assertTrue(doc.getFieldable(DocMaker.TITLE_FIELD).getOmitNorms());
159     assertTrue(doc.getFieldable(DocMaker.BODY_FIELD).getOmitNorms());
160     
161     // Set body norms to true
162     doc = createTestNormsDocument(false, false, true, true);
163     assertTrue(doc.getFieldable(DocMaker.TITLE_FIELD).getOmitNorms());
164     assertFalse(doc.getFieldable(DocMaker.BODY_FIELD).getOmitNorms());
165   }
166   
167 }