LUCENE-2642: Fix small problem with one incorrectly written contrib test and add a extra exception, if somebody uses a static declared test method

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@996647 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2010-09-13 19:11:21 +00:00
parent a505f18a0c
commit 9a3133c786
2 changed files with 40 additions and 26 deletions

View File

@ -17,6 +17,8 @@ package org.apache.lucene.misc;
* limitations under the License.
*/
import java.util.Random;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.util.BytesRef;
@ -26,47 +28,53 @@ import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.analysis.MockTokenizer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.junit.AfterClass;
import org.junit.BeforeClass;
public class TestHighFreqTerms extends LuceneTestCase {
private static IndexWriter writer =null;
private static Directory dir = null;
private static IndexReader reader =null;
private static Random random = null;
public void setUp() throws Exception {
super.setUp();
dir= newDirectory();
writer = new IndexWriter(dir, newIndexWriterConfig(
@BeforeClass
public static void setUpClass() throws Exception {
random = newStaticRandom(TestHighFreqTerms.class);
dir = newDirectory(random);
writer = new IndexWriter(dir, newIndexWriterConfig(random,
TEST_VERSION_CURRENT, new MockAnalyzer(MockTokenizer.WHITESPACE, false))
.setMaxBufferedDocs(2));
indexDocs(writer);
reader = IndexReader.open(dir, true);
}
public void tearDown() throws Exception{
@AfterClass
public static void tearDownClass() throws Exception{
reader.close();
dir.close();
super.tearDown();
dir = null;
reader = null;
}
/******************** Tests for getHighFreqTerms **********************************/
// test without specifying field (i.e. if we pass in field=null it should examine all fields)
// the term "diff" in the field "different_field" occurs 20 times and is the highest df term
public static void testFirstTermHighestDocFreqAllFields () throws Exception{
public void testFirstTermHighestDocFreqAllFields () throws Exception{
int numTerms = 12;
String field =null;
TermStats[] terms = HighFreqTerms.getHighFreqTerms(reader, numTerms, field);
assertEquals("Term with highest docfreq is first", 20,terms[0].docFreq );
}
public static void testFirstTermHighestDocFreq () throws Exception{
public void testFirstTermHighestDocFreq () throws Exception{
int numTerms = 12;
String field="FIELD_1";
TermStats[] terms = HighFreqTerms.getHighFreqTerms(reader, numTerms, field);
assertEquals("Term with highest docfreq is first", 10,terms[0].docFreq );
}
public static void testOrderedByDocFreqDescending () throws Exception{
public void testOrderedByDocFreqDescending () throws Exception{
int numTerms = 12;
String field="FIELD_1";
TermStats[] terms = HighFreqTerms.getHighFreqTerms(reader, numTerms, field);
@ -77,14 +85,14 @@ public class TestHighFreqTerms extends LuceneTestCase {
}
}
public static void testNumTerms () throws Exception{
public void testNumTerms () throws Exception{
int numTerms = 12;
String field = null;
TermStats[] terms = HighFreqTerms.getHighFreqTerms(reader, numTerms, field);
assertEquals("length of terms array equals numTerms :" + numTerms, numTerms, terms.length);
}
public static void testGetHighFreqTerms () throws Exception{
public void testGetHighFreqTerms () throws Exception{
int numTerms=12;
String field="FIELD_1";
TermStats[] terms = HighFreqTerms.getHighFreqTerms(reader, numTerms, field);
@ -108,7 +116,7 @@ public class TestHighFreqTerms extends LuceneTestCase {
/********************Test sortByTotalTermFreq**********************************/
public static void testFirstTermHighestTotalTermFreq () throws Exception{
public void testFirstTermHighestTotalTermFreq () throws Exception{
int numTerms = 20;
String field = null;
TermStats[] terms = HighFreqTerms.getHighFreqTerms(reader, numTerms, field);
@ -116,7 +124,7 @@ public class TestHighFreqTerms extends LuceneTestCase {
assertEquals("Term with highest totalTermFreq is first",200, termsWithTotalTermFreq[0].totalTermFreq);
}
public static void testFirstTermHighestTotalTermFreqDifferentField () throws Exception{
public void testFirstTermHighestTotalTermFreqDifferentField () throws Exception{
int numTerms = 20;
String field = "different_field";
TermStats[] terms = HighFreqTerms.getHighFreqTerms(reader, numTerms, field);
@ -124,7 +132,7 @@ public class TestHighFreqTerms extends LuceneTestCase {
assertEquals("Term with highest totalTermFreq is first"+ termsWithTotalTermFreq[0].getTermText(),150, termsWithTotalTermFreq[0].totalTermFreq);
}
public static void testOrderedByTermFreqDescending () throws Exception{
public void testOrderedByTermFreqDescending () throws Exception{
int numTerms = 12;
String field = "FIELD_1";
TermStats[] terms = HighFreqTerms.getHighFreqTerms(reader, numTerms, field);
@ -138,7 +146,7 @@ public class TestHighFreqTerms extends LuceneTestCase {
}
}
public static void testGetTermFreqOrdered () throws Exception{
public void testGetTermFreqOrdered () throws Exception{
int numTerms = 12;
String field = "FIELD_1";
TermStats[] terms = HighFreqTerms.getHighFreqTerms(reader, numTerms, field);
@ -168,7 +176,7 @@ public class TestHighFreqTerms extends LuceneTestCase {
/********************Tests for getTotalTermFreq**********************************/
public static void testGetTotalTermFreq() throws Exception{
public void testGetTotalTermFreq() throws Exception{
String term ="highTF";
BytesRef termtext = new BytesRef (term);
String field = "FIELD_1";
@ -178,7 +186,7 @@ public class TestHighFreqTerms extends LuceneTestCase {
}
public static void testGetTotalTermFreqBadTerm() throws Exception{
public void testGetTotalTermFreqBadTerm() throws Exception{
String term ="foobar";
BytesRef termtext = new BytesRef (term);
String field = "FIELD_1";
@ -189,7 +197,7 @@ public class TestHighFreqTerms extends LuceneTestCase {
}
/********************Testing Utils**********************************/
private void indexDocs(IndexWriter writer) throws Exception {
private static void indexDocs(IndexWriter writer) throws Exception {
/**
* Generate 10 documents where term n has a docFreq of n and a totalTermFreq of n*2 (squared).
@ -198,9 +206,9 @@ public class TestHighFreqTerms extends LuceneTestCase {
Document doc = new Document();
String content = getContent(i);
doc.add(newField("FIELD_1", content, Field.Store.YES,Field.Index.ANALYZED, Field.TermVector.NO));
doc.add(newField(random, "FIELD_1", content, Field.Store.YES,Field.Index.ANALYZED, Field.TermVector.NO));
//add a different field
doc.add(newField("different_field", "diff", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
doc.add(newField(random, "different_field", "diff", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
writer.addDocument(doc);
}
@ -208,7 +216,7 @@ public class TestHighFreqTerms extends LuceneTestCase {
//highest freq terms for a specific field.
for (int i = 1; i <= 10; i++) {
Document doc = new Document();
doc.add(newField("different_field", "diff", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
doc.add(newField(random, "different_field", "diff", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
writer.addDocument(doc);
}
// add some docs where tf < df so we can see if sorting works
@ -219,7 +227,7 @@ public class TestHighFreqTerms extends LuceneTestCase {
for (int i = 0; i < highTF; i++) {
content += "highTF ";
}
doc.add(newField("FIELD_1", content, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
doc.add(newField(random, "FIELD_1", content, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
writer.addDocument(doc);
// highTF medium df =5
int medium_df = 5;
@ -230,7 +238,7 @@ public class TestHighFreqTerms extends LuceneTestCase {
for (int j = 0; j < tf; j++) {
newcontent += "highTFmedDF ";
}
newdoc.add(newField("FIELD_1", newcontent, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
newdoc.add(newField(random, "FIELD_1", newcontent, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
writer.addDocument(newdoc);
}
// add a doc with high tf in field different_field
@ -240,7 +248,7 @@ public class TestHighFreqTerms extends LuceneTestCase {
for (int i = 0; i < targetTF; i++) {
content += "TF150 ";
}
doc.add(newField("different_field", content, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
doc.add(newField(random, "different_field", content, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
writer.addDocument(doc);
writer.close();

View File

@ -783,13 +783,19 @@ public abstract class LuceneTestCase extends Assert {
if (testMethods != null)
return testMethods;
testMethods = getTestClass().getAnnotatedMethods(Test.class);
for (Method m : getTestClass().getJavaClass().getMethods())
for (Method m : getTestClass().getJavaClass().getMethods()) {
final int mod = m.getModifiers();
if (m.getName().startsWith("test") &&
m.getAnnotation(Test.class) == null &&
(m.getModifiers() & (Modifier.STATIC|Modifier.ABSTRACT)) == 0 &&
!Modifier.isAbstract(mod) &&
m.getParameterTypes().length == 0 &&
m.getReturnType() == Void.TYPE)
{
if (Modifier.isStatic(mod))
throw new RuntimeException("Test methods must not be static.");
testMethods.add(new FrameworkMethod(m));
}
}
return testMethods;
}