You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by rm...@apache.org on 2010/07/13 16:21:48 UTC
svn commit: r963720 [5/5] - in /lucene/dev/trunk/lucene:
contrib/queries/src/test/org/apache/lucene/search/
contrib/queries/src/test/org/apache/lucene/search/regex/
contrib/queries/src/test/org/apache/lucene/search/similar/
src/java/org/apache/lucene/i...
Modified: lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/spans/TestBasics.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/spans/TestBasics.java?rev=963720&r1=963719&r2=963720&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/spans/TestBasics.java (original)
+++ lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/spans/TestBasics.java Tue Jul 13 14:21:46 2010
@@ -23,8 +23,9 @@ import org.apache.lucene.analysis.MockAn
import org.apache.lucene.analysis.MockTokenizer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
-import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
@@ -34,6 +35,7 @@ import org.apache.lucene.search.PhraseQu
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryUtils;
import org.apache.lucene.search.TermQuery;
+import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.English;
import org.apache.lucene.util.LuceneTestCase;
@@ -52,25 +54,35 @@ import org.apache.lucene.util.LuceneTest
*/
public class TestBasics extends LuceneTestCase {
private IndexSearcher searcher;
+ private IndexReader reader;
+ private Directory directory;
@Override
protected void setUp() throws Exception {
super.setUp();
- RAMDirectory directory = new RAMDirectory();
- IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(
- TEST_VERSION_CURRENT, new MockAnalyzer(MockTokenizer.SIMPLE, true)));
+ directory = new RAMDirectory();
+ RandomIndexWriter writer = new RandomIndexWriter(newRandom(), directory,
+ new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(MockTokenizer.SIMPLE, true)));
//writer.infoStream = System.out;
for (int i = 0; i < 1000; i++) {
Document doc = new Document();
doc.add(new Field("field", English.intToEnglish(i), Field.Store.YES, Field.Index.ANALYZED));
writer.addDocument(doc);
}
-
+ reader = writer.getReader();
+ searcher = new IndexSearcher(reader);
writer.close();
+ }
- searcher = new IndexSearcher(directory, true);
+ @Override
+ protected void tearDown() throws Exception {
+ searcher.close();
+ reader.close();
+ directory.close();
+ super.tearDown();
}
-
+
+
public void testTerm() throws Exception {
Query query = new TermQuery(new Term("field", "seventy"));
checkHits(query, new int[]
Modified: lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/spans/TestFieldMaskingSpanQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/spans/TestFieldMaskingSpanQuery.java?rev=963720&r1=963719&r2=963720&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/spans/TestFieldMaskingSpanQuery.java (original)
+++ lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/spans/TestFieldMaskingSpanQuery.java Tue Jul 13 14:21:46 2010
@@ -24,13 +24,14 @@ import org.apache.lucene.analysis.MockAn
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.RandomIndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.CheckHits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryUtils;
+import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.LuceneTestCase;
@@ -49,12 +50,15 @@ public class TestFieldMaskingSpanQuery e
}
protected IndexSearcher searcher;
+ protected Directory directory;
+ protected IndexReader reader;
@Override
protected void setUp() throws Exception {
super.setUp();
- RAMDirectory directory = new RAMDirectory();
- IndexWriter writer= new IndexWriter(directory, new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer()));
+ directory = new RAMDirectory();
+ RandomIndexWriter writer= new RandomIndexWriter(newRandom(), directory,
+ new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer()));
writer.addDocument(doc(new Field[] { field("id", "0")
,
@@ -109,14 +113,16 @@ public class TestFieldMaskingSpanQuery e
field("gender", "male"),
field("first", "bubba"),
field("last", "jones") }));
-
+ reader = writer.getReader();
writer.close();
- searcher = new IndexSearcher(directory, true);
+ searcher = new IndexSearcher(reader);
}
@Override
protected void tearDown() throws Exception {
searcher.close();
+ reader.close();
+ directory.close();
super.tearDown();
}
Modified: lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/spans/TestNearSpansOrdered.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/spans/TestNearSpansOrdered.java?rev=963720&r1=963719&r2=963720&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/spans/TestNearSpansOrdered.java (original)
+++ lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/spans/TestNearSpansOrdered.java Tue Jul 13 14:21:46 2010
@@ -20,8 +20,9 @@ package org.apache.lucene.search.spans;
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
-import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.CheckHits;
@@ -29,11 +30,14 @@ import org.apache.lucene.search.Explanat
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Weight;
import org.apache.lucene.search.Scorer;
+import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.LuceneTestCase;
public class TestNearSpansOrdered extends LuceneTestCase {
protected IndexSearcher searcher;
+ protected Directory directory;
+ protected IndexReader reader;
public static final String FIELD = "field";
public static final QueryParser qp =
@@ -42,21 +46,25 @@ public class TestNearSpansOrdered extend
@Override
protected void tearDown() throws Exception {
searcher.close();
+ reader.close();
+ directory.close();
super.tearDown();
}
@Override
protected void setUp() throws Exception {
super.setUp();
- RAMDirectory directory = new RAMDirectory();
- IndexWriter writer= new IndexWriter(directory, new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer()));
+ directory = new RAMDirectory();
+ RandomIndexWriter writer= new RandomIndexWriter(newRandom(), directory,
+ new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer()));
for (int i = 0; i < docFields.length; i++) {
Document doc = new Document();
doc.add(new Field(FIELD, docFields[i], Field.Store.NO, Field.Index.ANALYZED));
writer.addDocument(doc);
}
+ reader = writer.getReader();
writer.close();
- searcher = new IndexSearcher(directory, true);
+ searcher = new IndexSearcher(reader);
}
protected String[] docFields = {
Modified: lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/spans/TestSpans.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/spans/TestSpans.java?rev=963720&r1=963719&r2=963720&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/spans/TestSpans.java (original)
+++ lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/spans/TestSpans.java Tue Jul 13 14:21:46 2010
@@ -33,32 +33,44 @@ import org.apache.lucene.analysis.MockAn
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.util.LuceneTestCase;
import java.io.IOException;
-import java.util.Collections;
public class TestSpans extends LuceneTestCase {
private IndexSearcher searcher;
+ private IndexReader reader;
+ private Directory directory;
public static final String field = "field";
@Override
protected void setUp() throws Exception {
super.setUp();
- RAMDirectory directory = new RAMDirectory();
- IndexWriter writer= new IndexWriter(directory, new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer()));
+ directory = new RAMDirectory();
+ RandomIndexWriter writer= new RandomIndexWriter(newRandom(), directory,
+ new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer()));
for (int i = 0; i < docFields.length; i++) {
Document doc = new Document();
doc.add(new Field(field, docFields[i], Field.Store.YES, Field.Index.ANALYZED));
writer.addDocument(doc);
}
+ reader = writer.getReader();
writer.close();
- searcher = new IndexSearcher(directory, true);
+ searcher = new IndexSearcher(reader);
}
-
+
+ @Override
+ protected void tearDown() throws Exception {
+ searcher.close();
+ reader.close();
+ directory.close();
+ super.tearDown();
+ }
+
private String[] docFields = {
"w1 w2 w3 w4 w5",
"w1 w3 w2 w3",
Modified: lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/spans/TestSpansAdvanced.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/spans/TestSpansAdvanced.java?rev=963720&r1=963719&r2=963720&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/spans/TestSpansAdvanced.java (original)
+++ lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/spans/TestSpansAdvanced.java Tue Jul 13 14:21:46 2010
@@ -18,6 +18,7 @@ package org.apache.lucene.search.spans;
*/
import java.io.IOException;
+import java.util.Random;
import org.apache.lucene.util.LuceneTestCase;
@@ -26,8 +27,9 @@ import org.apache.lucene.analysis.MockTo
import org.apache.lucene.analysis.MockTokenizer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
-import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
@@ -36,136 +38,145 @@ import org.apache.lucene.store.RAMDirect
/*******************************************************************************
* Tests the span query bug in Lucene. It demonstrates that SpanTermQuerys don't
* work correctly in a BooleanQuery.
- *
+ *
*/
public class TestSpansAdvanced extends LuceneTestCase {
-
- // location to the index
- protected Directory mDirectory;
-
- protected IndexSearcher searcher;
-
- // field names in the index
- private final static String FIELD_ID = "ID";
- protected final static String FIELD_TEXT = "TEXT";
-
- /**
- * Initializes the tests by adding 4 identical documents to the index.
- */
- @Override
- protected void setUp() throws Exception {
- super.setUp();
-
- // create test index
- mDirectory = new RAMDirectory();
- final IndexWriter writer = new IndexWriter(mDirectory,
- new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(MockTokenizer.SIMPLE, true, MockTokenFilter.ENGLISH_STOPSET, true)));
- addDocument(writer, "1", "I think it should work.");
- addDocument(writer, "2", "I think it should work.");
- addDocument(writer, "3", "I think it should work.");
- addDocument(writer, "4", "I think it should work.");
- writer.close();
- searcher = new IndexSearcher(mDirectory, true);
- }
-
- @Override
- protected void tearDown() throws Exception {
- searcher.close();
- mDirectory.close();
- mDirectory = null;
- super.tearDown();
- }
-
- /**
- * Adds the document to the index.
- *
- * @param writer the Lucene index writer
- * @param id the unique id of the document
- * @param text the text of the document
- * @throws IOException
- */
- protected void addDocument(final IndexWriter writer, final String id, final String text) throws IOException {
-
- final Document document = new Document();
- document.add(new Field(FIELD_ID, id, Field.Store.YES, Field.Index.NOT_ANALYZED));
- document.add(new Field(FIELD_TEXT, text, Field.Store.YES, Field.Index.ANALYZED));
- writer.addDocument(document);
- }
-
- /**
- * Tests two span queries.
- *
- * @throws IOException
- */
- public void testBooleanQueryWithSpanQueries() throws IOException {
-
- doTestBooleanQueryWithSpanQueries(searcher,0.3884282f);
+
+ // location to the index
+ protected Directory mDirectory;
+ protected IndexReader reader;
+ protected IndexSearcher searcher;
+ protected Random random;
+
+ // field names in the index
+ private final static String FIELD_ID = "ID";
+ protected final static String FIELD_TEXT = "TEXT";
+
+ /**
+ * Initializes the tests by adding 4 identical documents to the index.
+ */
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ random = newRandom();
+ // create test index
+ mDirectory = new RAMDirectory();
+ final RandomIndexWriter writer = new RandomIndexWriter(random,
+ mDirectory, new IndexWriterConfig(TEST_VERSION_CURRENT,
+ new MockAnalyzer(MockTokenizer.SIMPLE, true,
+ MockTokenFilter.ENGLISH_STOPSET, true)));
+ addDocument(writer, "1", "I think it should work.");
+ addDocument(writer, "2", "I think it should work.");
+ addDocument(writer, "3", "I think it should work.");
+ addDocument(writer, "4", "I think it should work.");
+ reader = writer.getReader();
+ writer.close();
+ searcher = new IndexSearcher(reader);
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ searcher.close();
+ reader.close();
+ mDirectory.close();
+ mDirectory = null;
+ super.tearDown();
+ }
+
+ /**
+ * Adds the document to the index.
+ *
+ * @param writer the Lucene index writer
+ * @param id the unique id of the document
+ * @param text the text of the document
+ * @throws IOException
+ */
+ protected void addDocument(final RandomIndexWriter writer, final String id,
+ final String text) throws IOException {
+
+ final Document document = new Document();
+ document.add(new Field(FIELD_ID, id, Field.Store.YES,
+ Field.Index.NOT_ANALYZED));
+ document.add(new Field(FIELD_TEXT, text, Field.Store.YES,
+ Field.Index.ANALYZED));
+ writer.addDocument(document);
+ }
+
+ /**
+ * Tests two span queries.
+ *
+ * @throws IOException
+ */
+ public void testBooleanQueryWithSpanQueries() throws IOException {
+
+ doTestBooleanQueryWithSpanQueries(searcher, 0.3884282f);
+ }
+
+ /**
+ * Tests two span queries.
+ *
+ * @throws IOException
+ */
+ protected void doTestBooleanQueryWithSpanQueries(IndexSearcher s,
+ final float expectedScore) throws IOException {
+
+ final Query spanQuery = new SpanTermQuery(new Term(FIELD_TEXT, "work"));
+ final BooleanQuery query = new BooleanQuery();
+ query.add(spanQuery, BooleanClause.Occur.MUST);
+ query.add(spanQuery, BooleanClause.Occur.MUST);
+ final String[] expectedIds = new String[] {"1", "2", "3", "4"};
+ final float[] expectedScores = new float[] {expectedScore, expectedScore,
+ expectedScore, expectedScore};
+ assertHits(s, query, "two span queries", expectedIds, expectedScores);
+ }
+
+ /**
+ * Checks to see if the hits are what we expected.
+ *
+ * @param query the query to execute
+ * @param description the description of the search
+ * @param expectedIds the expected document ids of the hits
+ * @param expectedScores the expected scores of the hits
+ *
+ * @throws IOException
+ */
+ protected static void assertHits(Searcher s, Query query,
+ final String description, final String[] expectedIds,
+ final float[] expectedScores) throws IOException {
+ QueryUtils.check(query, s);
+
+ final float tolerance = 1e-5f;
+
+ // Hits hits = searcher.search(query);
+ // hits normalizes and throws things off if one score is greater than 1.0
+ TopDocs topdocs = s.search(query, null, 10000);
+
+ /*****
+ * // display the hits System.out.println(hits.length() +
+ * " hits for search: \"" + description + '\"'); for (int i = 0; i <
+ * hits.length(); i++) { System.out.println(" " + FIELD_ID + ':' +
+ * hits.doc(i).get(FIELD_ID) + " (score:" + hits.score(i) + ')'); }
+ *****/
+
+ // did we get the hits we expected
+ assertEquals(expectedIds.length, topdocs.totalHits);
+ for (int i = 0; i < topdocs.totalHits; i++) {
+ // System.out.println(i + " exp: " + expectedIds[i]);
+ // System.out.println(i + " field: " + hits.doc(i).get(FIELD_ID));
+
+ int id = topdocs.scoreDocs[i].doc;
+ float score = topdocs.scoreDocs[i].score;
+ Document doc = s.doc(id);
+ assertEquals(expectedIds[i], doc.get(FIELD_ID));
+ boolean scoreEq = Math.abs(expectedScores[i] - score) < tolerance;
+ if (!scoreEq) {
+ System.out.println(i + " warning, expected score: " + expectedScores[i]
+ + ", actual " + score);
+ System.out.println(s.explain(query, id));
+ }
+ assertEquals(expectedScores[i], score, tolerance);
+ assertEquals(s.explain(query, id).getValue(), score, tolerance);
}
-
- /**
- * Tests two span queries.
- *
- * @throws IOException
- */
- protected void doTestBooleanQueryWithSpanQueries(IndexSearcher s, final float expectedScore) throws IOException {
-
- final Query spanQuery = new SpanTermQuery(new Term(FIELD_TEXT, "work"));
- final BooleanQuery query = new BooleanQuery();
- query.add(spanQuery, BooleanClause.Occur.MUST);
- query.add(spanQuery, BooleanClause.Occur.MUST);
- final String[] expectedIds = new String[] { "1", "2", "3", "4" };
- final float[] expectedScores = new float[] { expectedScore, expectedScore, expectedScore, expectedScore };
- assertHits(s, query, "two span queries", expectedIds, expectedScores);
- }
-
-
- /**
- * Checks to see if the hits are what we expected.
- *
- * @param query the query to execute
- * @param description the description of the search
- * @param expectedIds the expected document ids of the hits
- * @param expectedScores the expected scores of the hits
- *
- * @throws IOException
- */
- protected static void assertHits(Searcher s, Query query, final String description, final String[] expectedIds,
- final float[] expectedScores) throws IOException {
- QueryUtils.check(query,s);
-
- final float tolerance = 1e-5f;
-
- // Hits hits = searcher.search(query);
- // hits normalizes and throws things off if one score is greater than 1.0
- TopDocs topdocs = s.search(query,null,10000);
-
- /*****
- // display the hits
- System.out.println(hits.length() + " hits for search: \"" + description + '\"');
- for (int i = 0; i < hits.length(); i++) {
- System.out.println(" " + FIELD_ID + ':' + hits.doc(i).get(FIELD_ID) + " (score:" + hits.score(i) + ')');
- }
- *****/
-
- // did we get the hits we expected
- assertEquals(expectedIds.length, topdocs.totalHits);
- for (int i = 0; i < topdocs.totalHits; i++) {
- //System.out.println(i + " exp: " + expectedIds[i]);
- //System.out.println(i + " field: " + hits.doc(i).get(FIELD_ID));
-
- int id = topdocs.scoreDocs[i].doc;
- float score = topdocs.scoreDocs[i].score;
- Document doc = s.doc(id);
- assertEquals(expectedIds[i], doc.get(FIELD_ID));
- boolean scoreEq = Math.abs(expectedScores[i] - score) < tolerance;
- if (!scoreEq) {
- System.out.println(i + " warning, expected score: " + expectedScores[i] + ", actual " + score);
- System.out.println(s.explain(query,id));
- }
- assertEquals(expectedScores[i], score, tolerance);
- assertEquals(s.explain(query,id).getValue(), score, tolerance);
- }
- }
-
-
+ }
+
}
\ No newline at end of file
Modified: lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/spans/TestSpansAdvanced2.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/spans/TestSpansAdvanced2.java?rev=963720&r1=963719&r2=963720&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/spans/TestSpansAdvanced2.java (original)
+++ lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/spans/TestSpansAdvanced2.java Tue Jul 13 14:21:46 2010
@@ -23,8 +23,8 @@ import org.apache.lucene.analysis.MockAn
import org.apache.lucene.analysis.MockTokenFilter;
import org.apache.lucene.analysis.MockTokenizer;
import org.apache.lucene.index.IndexReader;
-import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.search.*;
@@ -32,84 +32,97 @@ import org.apache.lucene.search.*;
/*******************************************************************************
* Some expanded tests to make sure my patch doesn't break other SpanTermQuery
* functionality.
- *
+ *
*/
public class TestSpansAdvanced2 extends TestSpansAdvanced {
- IndexSearcher searcher2;
- /**
- * Initializes the tests by adding documents to the index.
- */
- @Override
- protected void setUp() throws Exception {
- super.setUp();
-
- // create test index
- final IndexWriter writer = new IndexWriter(mDirectory,
- new IndexWriterConfig(TEST_VERSION_CURRENT,
- new MockAnalyzer(MockTokenizer.SIMPLE, true, MockTokenFilter.ENGLISH_STOPSET, true)).setOpenMode(
- OpenMode.APPEND));
- addDocument(writer, "A", "Should we, could we, would we?");
- addDocument(writer, "B", "It should. Should it?");
- addDocument(writer, "C", "It shouldn't.");
- addDocument(writer, "D", "Should we, should we, should we.");
- writer.close();
-
- // re-open the searcher since we added more docs
- searcher2 = new IndexSearcher(mDirectory, true);
- }
-
- /**
- * Verifies that the index has the correct number of documents.
- *
- * @throws Exception
- */
- public void testVerifyIndex() throws Exception {
- final IndexReader reader = IndexReader.open(mDirectory, true);
- assertEquals(8, reader.numDocs());
- reader.close();
- }
-
- /**
- * Tests a single span query that matches multiple documents.
- *
- * @throws IOException
- */
- public void testSingleSpanQuery() throws IOException {
-
- final Query spanQuery = new SpanTermQuery(new Term(FIELD_TEXT, "should"));
- final String[] expectedIds = new String[] { "B", "D", "1", "2", "3", "4", "A" };
- final float[] expectedScores = new float[] { 0.625f, 0.45927936f, 0.35355338f, 0.35355338f, 0.35355338f,
- 0.35355338f, 0.26516503f, };
- assertHits(searcher2, spanQuery, "single span query", expectedIds, expectedScores);
- }
-
- /**
- * Tests a single span query that matches multiple documents.
- *
- * @throws IOException
- */
- public void testMultipleDifferentSpanQueries() throws IOException {
-
- final Query spanQuery1 = new SpanTermQuery(new Term(FIELD_TEXT, "should"));
- final Query spanQuery2 = new SpanTermQuery(new Term(FIELD_TEXT, "we"));
- final BooleanQuery query = new BooleanQuery();
- query.add(spanQuery1, BooleanClause.Occur.MUST);
- query.add(spanQuery2, BooleanClause.Occur.MUST);
- final String[] expectedIds = new String[] { "D", "A" };
- // these values were pre LUCENE-413
- // final float[] expectedScores = new float[] { 0.93163157f, 0.20698164f };
- final float[] expectedScores = new float[] { 1.0191123f, 0.93163157f };
- assertHits(searcher2, query, "multiple different span queries", expectedIds, expectedScores);
- }
-
- /**
- * Tests two span queries.
- *
- * @throws IOException
- */
- @Override
- public void testBooleanQueryWithSpanQueries() throws IOException {
-
- doTestBooleanQueryWithSpanQueries(searcher2, 0.73500174f);
- }
+ IndexSearcher searcher2;
+ IndexReader reader2;
+
+ /**
+ * Initializes the tests by adding documents to the index.
+ */
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ // create test index
+ final RandomIndexWriter writer = new RandomIndexWriter(random, mDirectory,
+ new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(
+ MockTokenizer.SIMPLE, true, MockTokenFilter.ENGLISH_STOPSET, true))
+ .setOpenMode(OpenMode.APPEND));
+ addDocument(writer, "A", "Should we, could we, would we?");
+ addDocument(writer, "B", "It should. Should it?");
+ addDocument(writer, "C", "It shouldn't.");
+ addDocument(writer, "D", "Should we, should we, should we.");
+ reader2 = writer.getReader();
+ writer.close();
+
+ // re-open the searcher since we added more docs
+ searcher2 = new IndexSearcher(reader2);
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ searcher2.close();
+ reader2.close();
+ super.tearDown();
+ }
+
+ /**
+ * Verifies that the index has the correct number of documents.
+ *
+ * @throws Exception
+ */
+ public void testVerifyIndex() throws Exception {
+ final IndexReader reader = IndexReader.open(mDirectory, true);
+ assertEquals(8, reader.numDocs());
+ reader.close();
+ }
+
+ /**
+ * Tests a single span query that matches multiple documents.
+ *
+ * @throws IOException
+ */
+ public void testSingleSpanQuery() throws IOException {
+
+ final Query spanQuery = new SpanTermQuery(new Term(FIELD_TEXT, "should"));
+ final String[] expectedIds = new String[] {"B", "D", "1", "2", "3", "4",
+ "A"};
+ final float[] expectedScores = new float[] {0.625f, 0.45927936f,
+ 0.35355338f, 0.35355338f, 0.35355338f, 0.35355338f, 0.26516503f,};
+ assertHits(searcher2, spanQuery, "single span query", expectedIds,
+ expectedScores);
+ }
+
+ /**
+ * Tests a single span query that matches multiple documents.
+ *
+ * @throws IOException
+ */
+ public void testMultipleDifferentSpanQueries() throws IOException {
+
+ final Query spanQuery1 = new SpanTermQuery(new Term(FIELD_TEXT, "should"));
+ final Query spanQuery2 = new SpanTermQuery(new Term(FIELD_TEXT, "we"));
+ final BooleanQuery query = new BooleanQuery();
+ query.add(spanQuery1, BooleanClause.Occur.MUST);
+ query.add(spanQuery2, BooleanClause.Occur.MUST);
+ final String[] expectedIds = new String[] {"D", "A"};
+ // these values were pre LUCENE-413
+ // final float[] expectedScores = new float[] { 0.93163157f, 0.20698164f };
+ final float[] expectedScores = new float[] {1.0191123f, 0.93163157f};
+ assertHits(searcher2, query, "multiple different span queries",
+ expectedIds, expectedScores);
+ }
+
+ /**
+ * Tests two span queries.
+ *
+ * @throws IOException
+ */
+ @Override
+ public void testBooleanQueryWithSpanQueries() throws IOException {
+
+ doTestBooleanQueryWithSpanQueries(searcher2, 0.73500174f);
+ }
}
Modified: lucene/dev/trunk/lucene/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java?rev=963720&r1=963719&r2=963720&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java (original)
+++ lucene/dev/trunk/lucene/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java Tue Jul 13 14:21:46 2010
@@ -33,6 +33,7 @@ import java.io.File;
import java.io.PrintStream;
import java.io.IOException;
import java.util.Arrays;
+import java.util.Hashtable;
import java.util.Iterator;
import java.util.Random;
import java.util.ArrayList;
@@ -330,6 +331,30 @@ public class LuceneTestCaseJ4 {
return new Random(seed);
}
+ private static Hashtable<Class<?>,Long> staticSeeds = new Hashtable<Class<?>,Long>();
+
+ /**
+ * Returns a {@link Random} instance for generating random numbers from a beforeclass
+ * annotated method.
+ * The random seed is logged during test execution and printed to System.out on any failure
+ * for reproducing the test using {@link #newStaticRandom(Class, long)} with the recorded seed
+ * .
+ */
+ public static Random newStaticRandom(Class<?> clazz) {
+ return newStaticRandom(clazz, seedRnd.nextLong());
+ }
+
+ /**
+ * Returns a {@link Random} instance for generating random numbers from a beforeclass
+ * annotated method.
+ * If an error occurs in the test that is not reproducible, you can use this method to
+ * initialize the number generator with the seed that was printed out during the failing test.
+ */
+ public static Random newStaticRandom(Class<?> clazz, long seed) {
+ staticSeeds.put(clazz, Long.valueOf(seed));
+ return new Random(seed);
+ }
+
public String getName() {
return this.name;
}
@@ -348,6 +373,11 @@ public class LuceneTestCaseJ4 {
// We get here from InterceptTestCaseEvents on the 'failed' event....
public void reportAdditionalFailureInfo() {
+ Long staticSeed = staticSeeds.get(getClass());
+ if (staticSeed != null) {
+ System.out.println("NOTE: random static seed of testclass '" + getName() + "' was: " + staticSeed);
+ }
+
if (seed != null) {
System.out.println("NOTE: random seed of testcase '" + getName() + "' was: " + seed);
}