From db067550e89f7480d865a8ff478666fe364ad480 Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Sun, 14 Mar 2010 16:31:07 +0000 Subject: [PATCH] LUCENE-2037: Added a check for now, that tests in LuceneTestCaseJ4, that all JUnit4 tests have @Test added to all methods with name.startsWith("test"). This helps us during converting tests to JUnit4, as i found lots of tests that simply forgot to add @Test and were never ran. git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@922887 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/lucene/util/LuceneTestCaseJ4.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java b/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java index f6ce1dfd1f2..fdb3e9e0461 100644 --- a/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java +++ b/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java @@ -25,6 +25,7 @@ import org.apache.lucene.util.FieldCacheSanityChecker.Insanity; import org.junit.After; import org.junit.Before; import org.junit.Rule; +import org.junit.Test; import org.junit.rules.TestWatchman; import org.junit.runners.model.FrameworkMethod; @@ -36,7 +37,10 @@ import java.util.Iterator; import java.util.Random; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.WeakHashMap; import java.util.Collections; +import java.lang.reflect.Method; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; @@ -113,6 +117,11 @@ public class LuceneTestCaseJ4 { } private List uncaughtExceptions = Collections.synchronizedList(new ArrayList()); + // checks if class correctly annotated + private static final Object PLACEHOLDER = new Object(); + private static final Map,Object> checkedClasses = + Collections.synchronizedMap(new WeakHashMap,Object>()); + // This is how we get control when errors occur. // Think of this as start/end/success/failed // events. @@ -127,7 +136,18 @@ public class LuceneTestCaseJ4 { @Override public void starting(FrameworkMethod method) { + // set current method name for logging LuceneTestCaseJ4.this.name = method.getName(); + // check if the current test's class annotated all test* methods with @Test + final Class clazz = LuceneTestCaseJ4.this.getClass(); + if (!checkedClasses.containsKey(clazz)) { + checkedClasses.put(clazz, PLACEHOLDER); + for (Method m : clazz.getMethods()) { + if (m.getName().startsWith("test") && m.getAnnotation(Test.class) == null) { + fail("In class '" + clazz.getName() + "' the method '" + m.getName() + "' is not annotated with @Test."); + } + } + } super.starting(method); }