pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / src / test-framework / org / apache / lucene / util / LuceneTestCaseRunner.java
1 package org.apache.lucene.util;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
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;
27
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;
40
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;
48
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;
53   static {
54     runnerSeed = "random".equals(TEST_SEED) ? LuceneTestCase.seedRand.nextLong() : ThreeLongs.fromString(TEST_SEED).l3;
55   }
56   
57   @Override
58   protected List<FrameworkMethod> computeTestMethods() {
59     if (testMethods != null)
60       return testMethods;
61     
62     Random r = new Random(runnerSeed);
63     
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());
71       }
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))
79       {
80         if (Modifier.isStatic(mod))
81           throw new RuntimeException("Test methods must not be static.");
82         testMethods.add(new FrameworkMethod(m));
83       }
84     }
85     
86     if (testMethods.isEmpty()) {
87       throw new RuntimeException("No runnable methods!");
88     }
89     
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 + "'");
95         testMethods.clear();
96       } else {
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--);
103           }
104         }
105       }
106       /* dodge a possible "no-runnable methods" exception by adding a fake ignored test */
107       if (testMethods.isEmpty()) {
108         try {
109           testMethods.add(new FrameworkMethod(LuceneTestCase.class.getMethod("alwaysIgnoredTestMethod")));
110         } catch (Exception e) { throw new RuntimeException(e); }
111       }
112     }
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());
118       }
119     });
120     Collections.shuffle(testMethods, r);
121     return testMethods;
122   }
123   
124   @Override
125   protected void runChild(FrameworkMethod arg0, RunNotifier arg1) {
126     if (VERBOSE) {
127       System.out.println("\nNOTE: running test " + arg0.getName());
128     }
129     
130     // only print iteration info if the user requested more than one iterations
131     final boolean verbose = VERBOSE && TEST_ITER > 1;
132     
133     final int currentIter[] = new int[1];
134     arg1.addListener(new RunListener() {
135       @Override
136       public void testFailure(Failure failure) throws Exception {
137         if (verbose) {
138           System.out.println("\nNOTE: iteration " + currentIter[0] + " failed! ");
139         }
140       }
141     });
142     for (int i = 0; i < TEST_ITER; i++) {
143       currentIter[0] = i;
144       if (verbose) {
145         System.out.println("\nNOTE: running iter=" + (1+i) + " of " + TEST_ITER);
146       }
147       super.runChild(arg0, arg1);
148       if (LuceneTestCase.testsFailed) {
149         if (i >= TEST_ITER_MIN - 1) { // XXX is this still off-by-one?
150           break;
151         }
152       }
153     }
154   }
155   
156   public LuceneTestCaseRunner(Class<?> clazz) throws InitializationError {
157     super(clazz);
158     // evil we cannot init our random here, because super() calls computeTestMethods!!!!;
159     Filter f = new Filter() {
160       
161       @Override
162       public String describe() { return "filters according to TEST_METHOD"; }
163       
164       @Override
165       public boolean shouldRun(Description d) {
166         return TEST_METHOD == null || d.getMethodName().equals(TEST_METHOD);
167       }
168     };
169     
170     try {
171       f.apply(this);
172     } catch (NoTestsRemainException e) {
173       throw new RuntimeException(e);
174     }
175   }
176 }