1 package org.apache.lucene.util;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.lang.reflect.Method;
21 import java.lang.reflect.Modifier;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.Comparator;
25 import java.util.List;
26 import java.util.Random;
28 import org.apache.lucene.util.LuceneTestCase.Nightly;
29 import org.junit.Ignore;
30 import org.junit.Test;
31 import org.junit.runner.Description;
32 import org.junit.runner.manipulation.Filter;
33 import org.junit.runner.manipulation.NoTestsRemainException;
34 import org.junit.runner.notification.Failure;
35 import org.junit.runner.notification.RunListener;
36 import org.junit.runner.notification.RunNotifier;
37 import org.junit.runners.BlockJUnit4ClassRunner;
38 import org.junit.runners.model.FrameworkMethod;
39 import org.junit.runners.model.InitializationError;
41 // please don't reorganize these into a wildcard!
42 import static org.apache.lucene.util.LuceneTestCase.TEST_ITER;
43 import static org.apache.lucene.util.LuceneTestCase.TEST_ITER_MIN;
44 import static org.apache.lucene.util.LuceneTestCase.TEST_METHOD;
45 import static org.apache.lucene.util.LuceneTestCase.TEST_SEED;
46 import static org.apache.lucene.util.LuceneTestCase.TEST_NIGHTLY;
47 import static org.apache.lucene.util.LuceneTestCase.VERBOSE;
49 /** optionally filters the tests to be run by TEST_METHOD */
50 public class LuceneTestCaseRunner extends BlockJUnit4ClassRunner {
51 private List<FrameworkMethod> testMethods;
52 static final long runnerSeed;
54 runnerSeed = "random".equals(TEST_SEED) ? LuceneTestCase.seedRand.nextLong() : ThreeLongs.fromString(TEST_SEED).l3;
58 protected List<FrameworkMethod> computeTestMethods() {
59 if (testMethods != null)
62 Random r = new Random(runnerSeed);
64 LuceneTestCase.testClassesRun.add(getTestClass().getJavaClass().getSimpleName());
65 testMethods = new ArrayList<FrameworkMethod>();
66 for (Method m : getTestClass().getJavaClass().getMethods()) {
67 // check if the current test's class has methods annotated with @Ignore
68 final Ignore ignored = m.getAnnotation(Ignore.class);
69 if (ignored != null && !m.getName().equals("alwaysIgnoredTestMethod")) {
70 System.err.println("NOTE: Ignoring test method '" + m.getName() + "': " + ignored.value());
72 // add methods starting with "test"
73 final int mod = m.getModifiers();
74 if (m.getAnnotation(Test.class) != null ||
75 (m.getName().startsWith("test") &&
76 !Modifier.isAbstract(mod) &&
77 m.getParameterTypes().length == 0 &&
78 m.getReturnType() == Void.TYPE))
80 if (Modifier.isStatic(mod))
81 throw new RuntimeException("Test methods must not be static.");
82 testMethods.add(new FrameworkMethod(m));
86 if (testMethods.isEmpty()) {
87 throw new RuntimeException("No runnable methods!");
90 if (TEST_NIGHTLY == false) {
91 if (getTestClass().getJavaClass().isAnnotationPresent(Nightly.class)) {
92 /* the test class is annotated with nightly, remove all methods */
93 String className = getTestClass().getJavaClass().getSimpleName();
94 System.err.println("NOTE: Ignoring nightly-only test class '" + className + "'");
97 /* remove all nightly-only methods */
98 for (int i = 0; i < testMethods.size(); i++) {
99 final FrameworkMethod m = testMethods.get(i);
100 if (m.getAnnotation(Nightly.class) != null) {
101 System.err.println("NOTE: Ignoring nightly-only test method '" + m.getName() + "'");
102 testMethods.remove(i--);
106 /* dodge a possible "no-runnable methods" exception by adding a fake ignored test */
107 if (testMethods.isEmpty()) {
109 testMethods.add(new FrameworkMethod(LuceneTestCase.class.getMethod("alwaysIgnoredTestMethod")));
110 } catch (Exception e) { throw new RuntimeException(e); }
113 // sort the test methods first before shuffling them, so that the shuffle is consistent
114 // across different implementations that might order the methods different originally.
115 Collections.sort(testMethods, new Comparator<FrameworkMethod>() {
116 public int compare(FrameworkMethod f1, FrameworkMethod f2) {
117 return f1.getName().compareTo(f2.getName());
120 Collections.shuffle(testMethods, r);
125 protected void runChild(FrameworkMethod arg0, RunNotifier arg1) {
127 System.out.println("\nNOTE: running test " + arg0.getName());
130 // only print iteration info if the user requested more than one iterations
131 final boolean verbose = VERBOSE && TEST_ITER > 1;
133 final int currentIter[] = new int[1];
134 arg1.addListener(new RunListener() {
136 public void testFailure(Failure failure) throws Exception {
138 System.out.println("\nNOTE: iteration " + currentIter[0] + " failed! ");
142 for (int i = 0; i < TEST_ITER; i++) {
145 System.out.println("\nNOTE: running iter=" + (1+i) + " of " + TEST_ITER);
147 super.runChild(arg0, arg1);
148 if (LuceneTestCase.testsFailed) {
149 if (i >= TEST_ITER_MIN - 1) { // XXX is this still off-by-one?
156 public LuceneTestCaseRunner(Class<?> clazz) throws InitializationError {
158 // evil we cannot init our random here, because super() calls computeTestMethods!!!!;
159 Filter f = new Filter() {
162 public String describe() { return "filters according to TEST_METHOD"; }
165 public boolean shouldRun(Description d) {
166 return TEST_METHOD == null || d.getMethodName().equals(TEST_METHOD);
172 } catch (NoTestsRemainException e) {
173 throw new RuntimeException(e);