add --shared
[pylucene.git] / lucene-java-3.4.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.SimpleAnalyzer;
24 import org.apache.lucene.analysis.WhitespaceAnalyzer;
25 import org.apache.lucene.benchmark.BenchmarkTestCase;
26 import org.apache.lucene.benchmark.byTask.PerfRunData;
27 import org.apache.lucene.benchmark.byTask.tasks.AddDocTask;
28 import org.apache.lucene.benchmark.byTask.tasks.CloseIndexTask;
29 import org.apache.lucene.benchmark.byTask.tasks.CreateIndexTask;
30 import org.apache.lucene.benchmark.byTask.tasks.TaskSequence;
31 import org.apache.lucene.benchmark.byTask.utils.Config;
32 import org.apache.lucene.document.Document;
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     
91     IndexSearcher searcher = new IndexSearcher(runData.getDirectory(), true);
92     TopDocs td = searcher.search(new TermQuery(new Term("key", "value")), 10);
93     assertEquals(numExpectedResults, td.totalHits);
94     searcher.close();
95   }
96   
97   private Document createTestNormsDocument(boolean setNormsProp,
98       boolean normsPropVal, boolean setBodyNormsProp, boolean bodyNormsVal)
99       throws Exception {
100     Properties props = new Properties();
101     
102     // Indexing configuration.
103     props.setProperty("analyzer", WhitespaceAnalyzer.class.getName());
104     props.setProperty("content.source", OneDocSource.class.getName());
105     props.setProperty("directory", "RAMDirectory");
106     if (setNormsProp) {
107       props.setProperty("doc.tokenized.norms", Boolean.toString(normsPropVal));
108     }
109     if (setBodyNormsProp) {
110       props.setProperty("doc.body.tokenized.norms", Boolean.toString(bodyNormsVal));
111     }
112     
113     // Create PerfRunData
114     Config config = new Config(props);
115     
116     DocMaker dm = new DocMaker();
117     dm.setConfig(config);
118     return dm.makeDocument();
119   }
120   
121   /* Tests doc.index.props property. */
122   public void testIndexProperties() throws Exception {
123     // default is to not index properties.
124     doTestIndexProperties(false, false, 0);
125     
126     // set doc.index.props to false.
127     doTestIndexProperties(true, false, 0);
128     
129     // set doc.index.props to true.
130     doTestIndexProperties(true, true, 1);
131   }
132   
133   /* Tests doc.tokenized.norms and doc.body.tokenized.norms properties. */
134   public void testNorms() throws Exception {
135     
136     Document doc;
137     
138     // Don't set anything, use the defaults
139     doc = createTestNormsDocument(false, false, false, false);
140     assertTrue(doc.getField(DocMaker.TITLE_FIELD).getOmitNorms());
141     assertFalse(doc.getField(DocMaker.BODY_FIELD).getOmitNorms());
142     
143     // Set norms to false
144     doc = createTestNormsDocument(true, false, false, false);
145     assertTrue(doc.getField(DocMaker.TITLE_FIELD).getOmitNorms());
146     assertFalse(doc.getField(DocMaker.BODY_FIELD).getOmitNorms());
147     
148     // Set norms to true
149     doc = createTestNormsDocument(true, true, false, false);
150     assertFalse(doc.getField(DocMaker.TITLE_FIELD).getOmitNorms());
151     assertFalse(doc.getField(DocMaker.BODY_FIELD).getOmitNorms());
152     
153     // Set body norms to false
154     doc = createTestNormsDocument(false, false, true, false);
155     assertTrue(doc.getField(DocMaker.TITLE_FIELD).getOmitNorms());
156     assertTrue(doc.getField(DocMaker.BODY_FIELD).getOmitNorms());
157     
158     // Set body norms to true
159     doc = createTestNormsDocument(false, false, true, true);
160     assertTrue(doc.getField(DocMaker.TITLE_FIELD).getOmitNorms());
161     assertFalse(doc.getField(DocMaker.BODY_FIELD).getOmitNorms());
162   }
163   
164 }