~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/backwards/src/test/org/apache/lucene/index/TestIndexWriterConfig.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.lucene.index;
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.lang.reflect.Field;
22
 
import java.lang.reflect.Method;
23
 
import java.lang.reflect.Modifier;
24
 
import java.util.HashSet;
25
 
import java.util.Set;
26
 
 
27
 
import org.apache.lucene.analysis.MockAnalyzer;
28
 
import org.apache.lucene.analysis.WhitespaceAnalyzer;
29
 
import org.apache.lucene.index.DocumentsWriter.IndexingChain;
30
 
import org.apache.lucene.index.IndexWriter.IndexReaderWarmer;
31
 
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
32
 
import org.apache.lucene.search.DefaultSimilarity;
33
 
import org.apache.lucene.search.Similarity;
34
 
import org.apache.lucene.store.Directory;
35
 
import org.apache.lucene.util.LuceneTestCase;
36
 
import org.junit.Test;
37
 
 
38
 
public class TestIndexWriterConfig extends LuceneTestCase {
39
 
 
40
 
  private static final class MySimilarity extends DefaultSimilarity {
41
 
    // Does not implement anything - used only for type checking on IndexWriterConfig.
42
 
  }
43
 
  
44
 
  private static final class MyIndexingChain extends IndexingChain {
45
 
    // Does not implement anything - used only for type checking on IndexWriterConfig.
46
 
 
47
 
    @Override
48
 
    DocConsumer getChain(DocumentsWriter documentsWriter) {
49
 
      return null;
50
 
    }
51
 
    
52
 
  }
53
 
 
54
 
  private static final class MyWarmer extends IndexReaderWarmer {
55
 
    // Does not implement anything - used only for type checking on IndexWriterConfig.
56
 
 
57
 
    @Override
58
 
    public void warm(IndexReader reader) throws IOException {
59
 
    }
60
 
    
61
 
  }
62
 
  
63
 
  @Test
64
 
  public void testDefaults() throws Exception {
65
 
    IndexWriterConfig conf = new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random));
66
 
    assertEquals(MockAnalyzer.class, conf.getAnalyzer().getClass());
67
 
    assertNull(conf.getIndexCommit());
68
 
    assertEquals(KeepOnlyLastCommitDeletionPolicy.class, conf.getIndexDeletionPolicy().getClass());
69
 
    assertEquals(ConcurrentMergeScheduler.class, conf.getMergeScheduler().getClass());
70
 
    assertEquals(OpenMode.CREATE_OR_APPEND, conf.getOpenMode());
71
 
    assertTrue(Similarity.getDefault() == conf.getSimilarity());
72
 
    assertEquals(IndexWriterConfig.DEFAULT_TERM_INDEX_INTERVAL, conf.getTermIndexInterval());
73
 
    assertEquals(IndexWriterConfig.getDefaultWriteLockTimeout(), conf.getWriteLockTimeout());
74
 
    assertEquals(IndexWriterConfig.WRITE_LOCK_TIMEOUT, IndexWriterConfig.getDefaultWriteLockTimeout());
75
 
    assertEquals(IndexWriterConfig.DEFAULT_MAX_BUFFERED_DELETE_TERMS, conf.getMaxBufferedDeleteTerms());
76
 
    assertEquals(IndexWriterConfig.DEFAULT_RAM_BUFFER_SIZE_MB, conf.getRAMBufferSizeMB(), 0.0);
77
 
    assertEquals(IndexWriterConfig.DEFAULT_MAX_BUFFERED_DOCS, conf.getMaxBufferedDocs());
78
 
    assertEquals(IndexWriterConfig.DEFAULT_READER_POOLING, conf.getReaderPooling());
79
 
    assertTrue(DocumentsWriter.defaultIndexingChain == conf.getIndexingChain());
80
 
    assertNull(conf.getMergedSegmentWarmer());
81
 
    assertEquals(IndexWriterConfig.DEFAULT_MAX_THREAD_STATES, conf.getMaxThreadStates());
82
 
    assertEquals(IndexWriterConfig.DEFAULT_READER_TERMS_INDEX_DIVISOR, conf.getReaderTermsIndexDivisor());
83
 
    assertEquals(TieredMergePolicy.class, conf.getMergePolicy().getClass());
84
 
    
85
 
    // Sanity check - validate that all getters are covered.
86
 
    Set<String> getters = new HashSet<String>();
87
 
    getters.add("getAnalyzer");
88
 
    getters.add("getIndexCommit");
89
 
    getters.add("getIndexDeletionPolicy");
90
 
    getters.add("getMergeScheduler");
91
 
    getters.add("getOpenMode");
92
 
    getters.add("getSimilarity");
93
 
    getters.add("getTermIndexInterval");
94
 
    getters.add("getWriteLockTimeout");
95
 
    getters.add("getDefaultWriteLockTimeout");
96
 
    getters.add("getMaxBufferedDeleteTerms");
97
 
    getters.add("getRAMBufferSizeMB");
98
 
    getters.add("getMaxBufferedDocs");
99
 
    getters.add("getIndexingChain");
100
 
    getters.add("getMergedSegmentWarmer");
101
 
    getters.add("getMergePolicy");
102
 
    getters.add("getMaxThreadStates");
103
 
    getters.add("getReaderPooling");
104
 
    getters.add("getReaderTermsIndexDivisor");
105
 
    for (Method m : IndexWriterConfig.class.getDeclaredMethods()) {
106
 
      if (m.getDeclaringClass() == IndexWriterConfig.class && m.getName().startsWith("get")) {
107
 
        assertTrue("method " + m.getName() + " is not tested for defaults", getters.contains(m.getName()));
108
 
      }
109
 
    }
110
 
  }
111
 
 
112
 
  @Test
113
 
  public void testSettersChaining() throws Exception {
114
 
    // Ensures that every setter returns IndexWriterConfig to enable easy
115
 
    // chaining.
116
 
    for (Method m : IndexWriterConfig.class.getDeclaredMethods()) {
117
 
      if (m.getDeclaringClass() == IndexWriterConfig.class
118
 
          && m.getName().startsWith("set")
119
 
          && !Modifier.isStatic(m.getModifiers())) {
120
 
        assertEquals("method " + m.getName() + " does not return IndexWriterConfig", 
121
 
            IndexWriterConfig.class, m.getReturnType());
122
 
      }
123
 
    }
124
 
  }
125
 
  
126
 
  @Test
127
 
  public void testConstants() throws Exception {
128
 
    // Tests that the values of the constants does not change
129
 
    assertEquals(1000, IndexWriterConfig.WRITE_LOCK_TIMEOUT);
130
 
    assertEquals(128, IndexWriterConfig.DEFAULT_TERM_INDEX_INTERVAL);
131
 
    assertEquals(-1, IndexWriterConfig.DISABLE_AUTO_FLUSH);
132
 
    assertEquals(IndexWriterConfig.DISABLE_AUTO_FLUSH, IndexWriterConfig.DEFAULT_MAX_BUFFERED_DELETE_TERMS);
133
 
    assertEquals(IndexWriterConfig.DISABLE_AUTO_FLUSH, IndexWriterConfig.DEFAULT_MAX_BUFFERED_DOCS);
134
 
    assertEquals(16.0, IndexWriterConfig.DEFAULT_RAM_BUFFER_SIZE_MB, 0.0);
135
 
    assertEquals(false, IndexWriterConfig.DEFAULT_READER_POOLING);
136
 
    assertEquals(8, IndexWriterConfig.DEFAULT_MAX_THREAD_STATES);
137
 
    assertEquals(IndexReader.DEFAULT_TERMS_INDEX_DIVISOR, IndexWriterConfig.DEFAULT_READER_TERMS_INDEX_DIVISOR);
138
 
  }
139
 
  
140
 
  @Test
141
 
  public void testToString() throws Exception {
142
 
    String str = new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).toString();
143
 
    for (Field f : IndexWriterConfig.class.getDeclaredFields()) {
144
 
      int modifiers = f.getModifiers();
145
 
      if (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)) {
146
 
        // Skip static final fields, they are only constants
147
 
        continue;
148
 
      } else if ("indexingChain".equals(f.getName())) {
149
 
        // indexingChain is a package-private setting and thus is not output by
150
 
        // toString.
151
 
        continue;
152
 
      }
153
 
      assertTrue(f.getName() + " not found in toString", str.indexOf(f.getName()) != -1);
154
 
    }
155
 
  }
156
 
  
157
 
  @Test
158
 
  public void testClone() throws Exception {
159
 
    IndexWriterConfig conf = new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random));
160
 
    IndexWriterConfig clone = (IndexWriterConfig) conf.clone();
161
 
    
162
 
    // Clone is shallow since not all parameters are cloneable.
163
 
    assertTrue(conf.getIndexDeletionPolicy() == clone.getIndexDeletionPolicy());
164
 
    
165
 
    conf.setMergeScheduler(new SerialMergeScheduler());
166
 
    assertEquals(ConcurrentMergeScheduler.class, clone.getMergeScheduler().getClass());
167
 
  }
168
 
 
169
 
  @Test
170
 
  public void testInvalidValues() throws Exception {
171
 
    IndexWriterConfig conf = new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random));
172
 
    
173
 
    // Test IndexDeletionPolicy
174
 
    assertEquals(KeepOnlyLastCommitDeletionPolicy.class, conf.getIndexDeletionPolicy().getClass());
175
 
    conf.setIndexDeletionPolicy(new SnapshotDeletionPolicy(null));
176
 
    assertEquals(SnapshotDeletionPolicy.class, conf.getIndexDeletionPolicy().getClass());
177
 
    conf.setIndexDeletionPolicy(null);
178
 
    assertEquals(KeepOnlyLastCommitDeletionPolicy.class, conf.getIndexDeletionPolicy().getClass());
179
 
    
180
 
    // Test MergeScheduler
181
 
    assertEquals(ConcurrentMergeScheduler.class, conf.getMergeScheduler().getClass());
182
 
    conf.setMergeScheduler(new SerialMergeScheduler());
183
 
    assertEquals(SerialMergeScheduler.class, conf.getMergeScheduler().getClass());
184
 
    conf.setMergeScheduler(null);
185
 
    assertEquals(ConcurrentMergeScheduler.class, conf.getMergeScheduler().getClass());
186
 
 
187
 
    // Test Similarity
188
 
    assertTrue(Similarity.getDefault() == conf.getSimilarity());
189
 
    conf.setSimilarity(new MySimilarity());
190
 
    assertEquals(MySimilarity.class, conf.getSimilarity().getClass());
191
 
    conf.setSimilarity(null);
192
 
    assertTrue(Similarity.getDefault() == conf.getSimilarity());
193
 
 
194
 
    // Test IndexingChain
195
 
    assertTrue(DocumentsWriter.defaultIndexingChain == conf.getIndexingChain());
196
 
    conf.setIndexingChain(new MyIndexingChain());
197
 
    assertEquals(MyIndexingChain.class, conf.getIndexingChain().getClass());
198
 
    conf.setIndexingChain(null);
199
 
    assertTrue(DocumentsWriter.defaultIndexingChain == conf.getIndexingChain());
200
 
    
201
 
    try {
202
 
      conf.setMaxBufferedDeleteTerms(0);
203
 
      fail("should not have succeeded to set maxBufferedDeleteTerms to 0");
204
 
    } catch (IllegalArgumentException e) {
205
 
      // this is expected
206
 
    }
207
 
 
208
 
    try {
209
 
      conf.setMaxBufferedDocs(1);
210
 
      fail("should not have succeeded to set maxBufferedDocs to 1");
211
 
    } catch (IllegalArgumentException e) {
212
 
      // this is expected
213
 
    }
214
 
 
215
 
    try {
216
 
      // Disable both MAX_BUF_DOCS and RAM_SIZE_MB
217
 
      conf.setMaxBufferedDocs(4);
218
 
      conf.setRAMBufferSizeMB(IndexWriterConfig.DISABLE_AUTO_FLUSH);
219
 
      conf.setMaxBufferedDocs(IndexWriterConfig.DISABLE_AUTO_FLUSH);
220
 
      fail("should not have succeeded to disable maxBufferedDocs when ramBufferSizeMB is disabled as well");
221
 
    } catch (IllegalArgumentException e) {
222
 
      // this is expected
223
 
    }
224
 
 
225
 
    conf.setRAMBufferSizeMB(IndexWriterConfig.DEFAULT_RAM_BUFFER_SIZE_MB);
226
 
    conf.setMaxBufferedDocs(IndexWriterConfig.DEFAULT_MAX_BUFFERED_DOCS);
227
 
    try {
228
 
      conf.setRAMBufferSizeMB(IndexWriterConfig.DISABLE_AUTO_FLUSH);
229
 
      fail("should not have succeeded to disable ramBufferSizeMB when maxBufferedDocs is disabled as well");
230
 
    } catch (IllegalArgumentException e) {
231
 
      // this is expected
232
 
    }
233
 
 
234
 
    // Test setReaderTermsIndexDivisor
235
 
    try {
236
 
      conf.setReaderTermsIndexDivisor(0);
237
 
      fail("should not have succeeded to set termsIndexDivisor to 0");
238
 
    } catch (IllegalArgumentException e) {
239
 
      // this is expected
240
 
    }
241
 
    
242
 
    // Setting to -1 is ok
243
 
    conf.setReaderTermsIndexDivisor(-1);
244
 
    try {
245
 
      conf.setReaderTermsIndexDivisor(-2);
246
 
      fail("should not have succeeded to set termsIndexDivisor to < -1");
247
 
    } catch (IllegalArgumentException e) {
248
 
      // this is expected
249
 
    }
250
 
    
251
 
    assertEquals(IndexWriterConfig.DEFAULT_MAX_THREAD_STATES, conf.getMaxThreadStates());
252
 
    conf.setMaxThreadStates(5);
253
 
    assertEquals(5, conf.getMaxThreadStates());
254
 
    conf.setMaxThreadStates(0);
255
 
    assertEquals(IndexWriterConfig.DEFAULT_MAX_THREAD_STATES, conf.getMaxThreadStates());
256
 
    
257
 
    // Test MergePolicy
258
 
    assertEquals(TieredMergePolicy.class, conf.getMergePolicy().getClass());
259
 
    conf.setMergePolicy(new LogDocMergePolicy());
260
 
    assertEquals(LogDocMergePolicy.class, conf.getMergePolicy().getClass());
261
 
    conf.setMergePolicy(null);
262
 
    assertEquals(LogByteSizeMergePolicy.class, conf.getMergePolicy().getClass());
263
 
  }
264
 
 
265
 
  /**
266
 
   * @deprecated should be removed once all the deprecated setters are removed
267
 
   *             from IndexWriter.
268
 
   */
269
 
  @Test @Deprecated
270
 
  public void testIndexWriterSetters() throws Exception {
271
 
    // This test intentionally tests deprecated methods. The purpose is to pass
272
 
    // whatever the user set on IW to IWC, so that if the user calls
273
 
    // iw.getConfig().getXYZ(), he'll get the same value he passed to
274
 
    // iw.setXYZ().
275
 
    IndexWriterConfig conf = new IndexWriterConfig(TEST_VERSION_CURRENT, new WhitespaceAnalyzer(TEST_VERSION_CURRENT));
276
 
    Directory dir = newDirectory();
277
 
    IndexWriter writer = new IndexWriter(dir, conf);
278
 
 
279
 
    writer.setSimilarity(new MySimilarity());
280
 
    assertEquals(MySimilarity.class, writer.getConfig().getSimilarity().getClass());
281
 
 
282
 
    writer.setMaxBufferedDeleteTerms(4);
283
 
    assertEquals(4, writer.getConfig().getMaxBufferedDeleteTerms());
284
 
 
285
 
    writer.setMaxBufferedDocs(10);
286
 
    assertEquals(10, writer.getConfig().getMaxBufferedDocs());
287
 
    
288
 
    writer.setMergeScheduler(new SerialMergeScheduler());
289
 
    assertEquals(SerialMergeScheduler.class, writer.getConfig().getMergeScheduler().getClass());
290
 
    
291
 
    writer.setRAMBufferSizeMB(1.5);
292
 
    assertEquals(1.5, writer.getConfig().getRAMBufferSizeMB(), 0.0);
293
 
    
294
 
    writer.setTermIndexInterval(40);
295
 
    assertEquals(40, writer.getConfig().getTermIndexInterval());
296
 
    
297
 
    writer.setWriteLockTimeout(100);
298
 
    assertEquals(100, writer.getConfig().getWriteLockTimeout());
299
 
    
300
 
    writer.setMergedSegmentWarmer(new MyWarmer());
301
 
    assertEquals(MyWarmer.class, writer.getConfig().getMergedSegmentWarmer().getClass());
302
 
    
303
 
    writer.setMergePolicy(new LogDocMergePolicy());
304
 
    assertEquals(LogDocMergePolicy.class, writer.getConfig().getMergePolicy().getClass());
305
 
    writer.close();
306
 
    dir.close();
307
 
  }
308
 
 
309
 
}