Advertisement
mbpaster

OrderedTestRunner

Mar 7th, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.77 KB | None | 0 0
  1. /*
  2.  * Copyright (C) <2014> <Michele Bonazza>
  3.  *
  4.  *
  5.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  6.  * of this software and associated documentation files (the "Software"), to deal
  7.  * in the Software without restriction, including without limitation the rights
  8.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9.  * copies of the Software, and to permit persons to whom the Software is
  10.  * furnished to do so, subject to the following conditions:
  11.  *
  12.  * The above copyright notice and this permission notice shall be included in
  13.  * all copies or substantial portions of the Software.
  14.  *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21.  * SOFTWARE.
  22.  */
  23.  
  24. /**
  25.  * A test runner that runs tests according to their position in the source file
  26.  * of the test class.
  27.  *
  28.  * @author Michele Bonazza
  29.  */
  30. public class OrderedTestRunner extends BlockJUnit4ClassRunner {
  31.  
  32.     /**
  33.      * Creates a new runner
  34.      *
  35.      * @param clazz
  36.      *            the class being tested
  37.      * @throws InitializationError
  38.      *             if something goes wrong
  39.      */
  40.     public OrderedTestRunner(Class<?> clazz) throws InitializationError {
  41.         super(clazz);
  42.     }
  43.  
  44.     /*
  45.      * (non-Javadoc)
  46.      *
  47.      * @see org.junit.runners.BlockJUnit4ClassRunner#computeTestMethods()
  48.      */
  49.     @Override
  50.     protected List<FrameworkMethod> computeTestMethods() {
  51.         // get all methods to be tested
  52.         List<FrameworkMethod> toSort = super.computeTestMethods();
  53.  
  54.         if (toSort.isEmpty())
  55.             return toSort;
  56.  
  57.         // a map containing <line_number, method>
  58.         final Map<Integer, FrameworkMethod> testMethods = new TreeMap<>();
  59.  
  60.         // check that all methods here are declared in the same class, we don't
  61.         // deal with test methods from superclasses that haven't been overridden
  62.         Class<?> clazz = getDeclaringClass(toSort);
  63.         if (clazz == null) {
  64.             // fail explicitly
  65.             System.err
  66.                     .println("OrderedTestRunner can only run test classes that"
  67.                             + " don't have test methods inherited from superclasses");
  68.             return Collections.emptyList();
  69.         }
  70.  
  71.         // use Javassist to figure out line numbers for methods
  72.         ClassPool pool = ClassPool.getDefault();
  73.         try {
  74.             CtClass cc = pool.get(clazz.getName());
  75.             // all methods in toSort are declared in the same class, we checked
  76.             for (FrameworkMethod m : toSort) {
  77.                 String methodName = m.getName();
  78.                 CtMethod method = cc.getDeclaredMethod(methodName);
  79.                 testMethods.put(method.getMethodInfo().getLineNumber(0), m);
  80.             }
  81.         } catch (NotFoundException e) {
  82.             e.printStackTrace();
  83.         }
  84.  
  85.         return new ArrayList<>(testMethods.values());
  86.     }
  87.  
  88.     private Class<?> getDeclaringClass(List<FrameworkMethod> methods) {
  89.         // methods can't be empty, it's been checked
  90.         Class<?> clazz = methods.get(0).getMethod().getDeclaringClass();
  91.  
  92.         for (int i = 1; i < methods.size(); i++) {
  93.             if (!methods.get(i).getMethod().getDeclaringClass().equals(clazz)) {
  94.                 // they must be all in the same class
  95.                 return null;
  96.             }
  97.         }
  98.  
  99.         return clazz;
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement