All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.eBus.test.OrderedRunner Maven / Gradle / Ivy

There is a newer version: 7.4.0
Show newest version
//
// Copyright 2013 Charles W. Rapp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package net.sf.eBus.test;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;

/**
 * This class extends
 * {@code org.junit.runners.BlockJUnit4ClassRunner} and overrides
 * the {@code computeTestMethods()} method to guarantee that
 * test methods are run in a user-defined order using the
 * {@link TestIndex} annotation. While JUnit discourages test
 * method dependencies because increases test maintenance
 * difficultly, breaking a complex test down to a series of
 * simple, dependent test methods is necessary at times.
 * 

* An example using this class runner: *


 * @RunWith(OrderedRunner.class)
 * public final class MyTest
 * {
 *     @Test
 *     @TestIndex(index=0)
 *     public void test0()
 *     {
 *         ...
 *     }
 *
 *     @Test
 *     @TestIndex(index=1)
 *     public void test1
 *     {
 *         ...
 *     }
 * }
*

* Note: this is not meant to replace * {@code org.junit.runner.manipulation.Sortable} interface since * that interface should not be used to cope with test method * dependencies (as per the JUnit documentation). * * @author Charles Rapp */ public class OrderedRunner extends BlockJUnit4ClassRunner { //--------------------------------------------------------------- // Member data. // //----------------------------------------------------------- // Constants. // /** * The FrameworkMethod comparator singleton. Used to sort * the default test method list by test index values. */ private static final Comparator FM_COMPARATOR = new FMComparator(); //--------------------------------------------------------------- // Member methods. // //----------------------------------------------------------- // Constructors. // /** * Creates a new OrderedRunner instance for the given test * class. * @param clazz the Java class containing the test methods. * @throws InitializationError * if this constructor fails. */ public OrderedRunner(final Class clazz) throws InitializationError { super (clazz); } // end of OrderedRunner(Class) // // end of Constructors. //----------------------------------------------------------- //----------------------------------------------------------- // BlockJUnit4ClassRunner Override Methods. // /** * Returns the test methods list sorted according to their * {@link TestIndex test index} values. * @return the sorted test method list. */ @Override protected List computeTestMethods() { final List retval = new ArrayList<>(super.computeTestMethods()); Collections.sort(retval, FM_COMPARATOR); return (retval); } // end of computeTestMethod() // // end of BlockJUnit4ClassRunner Override Methods. //----------------------------------------------------------- //--------------------------------------------------------------- // Inner classes. // /** * Used to compare two test methods based on their * {@link TestIndex @TestIndex} annotation values. */ private static final class FMComparator implements Comparator, Serializable { //----------------------------------------------------------- // Member data. // //------------------------------------------------------- // Constants. // private static final long serialVersionUID = 0x050200L; //----------------------------------------------------------- // Member methods. // //------------------------------------------------------- // Constructors. // /** * Default constructor. */ public FMComparator() {} // // end of Constructors. //------------------------------------------------------- //------------------------------------------------------- // Comparator Interface Implementation. // /** * Returns a value <, equal to, or > zero if * {@code t0} test index is <, equal to, or > * {@code t1} test index. * @param t0 the first test method. * @param t1 the second test method. * @return an integer value <, equal to, or > zero. */ @Override public int compare(final FrameworkMethod t0, final FrameworkMethod t1) { final TestIndex i0 = t0.getAnnotation(TestIndex.class); final TestIndex i1 = t1.getAnnotation(TestIndex.class); int retval = -1; if (i0 != null && i1 != null) { retval = (i0.index() - i1.index()); } return (retval); } // end of compare(FrameworkMethd, FrameworkMethod) // // end of Comparator Interface Implementation. //------------------------------------------------------- } // end of class FMComparator } // end of class OrderedRunner





© 2015 - 2024 Weber Informatics LLC | Privacy Policy