Index: src/test/org/apache/lucene/search/TestRegexpRandom2.java =================================================================== --- src/test/org/apache/lucene/search/TestRegexpRandom2.java (revision 963233) +++ src/test/org/apache/lucene/search/TestRegexpRandom2.java (working copy) @@ -25,10 +25,12 @@ import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexReader; -import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.Term; import org.apache.lucene.index.TermsEnum; -import org.apache.lucene.store.RAMDirectory; +import org.apache.lucene.index.RandomIndexWriter; +import org.apache.lucene.store.Directory; +import org.apache.lucene.store.MockRAMDirectory; import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.UnicodeUtil; @@ -44,33 +46,36 @@ */ public class TestRegexpRandom2 extends LuceneTestCase { private IndexSearcher searcher; + private IndexReader reader; + private Directory dir; private Random random; @Override protected void setUp() throws Exception { super.setUp(); random = newRandom(); - RAMDirectory dir = new RAMDirectory(); - IndexWriter writer = new IndexWriter(dir, new MockAnalyzer(MockTokenizer.KEYWORD, false), - IndexWriter.MaxFieldLength.UNLIMITED); + dir = new MockRAMDirectory(); + RandomIndexWriter writer = new RandomIndexWriter(random, dir, new IndexWriterConfig(TEST_VERSION_CURRENT, + new MockAnalyzer(MockTokenizer.KEYWORD, false))); Document doc = new Document(); Field field = new Field("field", "", Field.Store.NO, Field.Index.ANALYZED); doc.add(field); - + for (int i = 0; i < 2000*_TestUtil.getRandomMultiplier(); i++) { field.setValue(_TestUtil.randomUnicodeString(random)); writer.addDocument(doc); } - - writer.optimize(); + reader = writer.getReader(); + searcher = new IndexSearcher(reader); writer.close(); - searcher = new IndexSearcher(dir); } @Override protected void tearDown() throws Exception { + reader.close(); searcher.close(); + dir.close(); super.tearDown(); } Index: src/test/org/apache/lucene/index/RandomIndexWriter.java =================================================================== --- src/test/org/apache/lucene/index/RandomIndexWriter.java (revision 0) +++ src/test/org/apache/lucene/index/RandomIndexWriter.java (revision 0) @@ -0,0 +1,74 @@ +package org.apache.lucene.index; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.util.Random; +import java.io.Closeable; +import java.io.IOException; + +import org.apache.lucene.util._TestUtil; +import org.apache.lucene.store.Directory; +import org.apache.lucene.document.Document; + +/** Silly class that randomizes the indexing experience. EG + * it may swap in a different merge policy/scheduler; may + * commit periodically; may or may not optimize in the end, + * may flush by doc count instead of RAM, etc. + */ + +public class RandomIndexWriter implements Closeable { + + public IndexWriter w; + private final Random r; + int docCount; + int flushAt; + + public RandomIndexWriter(Random r, Directory dir, IndexWriterConfig c) throws IOException { + this.r = r; + if (r.nextBoolean()) { + c.setMergePolicy(new LogDocMergePolicy()); + } + if (r.nextBoolean()) { + c.setMergeScheduler(new SerialMergeScheduler()); + } + if (r.nextBoolean()) { + c.setMaxBufferedDocs(_TestUtil.nextInt(r, 20, 1000)); + } + w = new IndexWriter(dir, c); + flushAt = _TestUtil.nextInt(r, 10, 1000); + } + + public void addDocument(Document doc) throws IOException { + w.addDocument(doc); + if (docCount++ == flushAt) { + w.commit(); + flushAt += _TestUtil.nextInt(r, 10, 1000); + } + } + + public IndexReader getReader() throws IOException { + return w.getReader(); + } + + public void close() throws IOException { + if (r.nextInt(4) == 2) { + w.optimize(); + } + w.close(); + } +} Property changes on: src/test/org/apache/lucene/index/RandomIndexWriter.java ___________________________________________________________________ Added: svn:eol-style + native