下面列出了org.junit.runners.Suite#org.junit.runners.model.RunnerBuilder 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public CustomRunner(Class<?> klass, RunnerBuilder builder) throws InitializationError {
super(
klass,
new RunnerBuilder() {
@Override public Runner runnerForClass(Class<?> testClass) throws Throwable {
Boolean oldValue = IS_FAST_TEST_SUITE_ACTIVE.get();
try {
IS_FAST_TEST_SUITE_ACTIVE.set(true);
Runner r = builder.runnerForClass(testClass);
return r;
} finally {
IS_FAST_TEST_SUITE_ACTIVE.set(oldValue);
}
}
}
);
}
/**
* Initialize.
*
* <p>Each class in the {@code customRunnerBuilderClasses} list must be a concrete public class
* and must have a public no-argument constructor.
*
* @param suiteBuilder the top level {@link RunnerBuilder} to use to build nested classes.
* @param runnerParams {@link AndroidRunnerParams} that stores common runner parameters
* @param scanningPath true if being used to build {@link Runner} from classes found while
* scanning the path; requires extra checks to avoid unnecessary errors.
* @param customRunnerBuilderClasses custom {@link RunnerBuilder} classes
* @throws IllegalStateException if any of the custom {@link RunnerBuilder} classes cannot be
* instantiated.
*/
AndroidRunnerBuilder(
RunnerBuilder suiteBuilder,
AndroidRunnerParams runnerParams,
boolean scanningPath,
List<Class<? extends RunnerBuilder>> customRunnerBuilderClasses) {
super(true);
androidJUnit3Builder = new AndroidJUnit3Builder(runnerParams, scanningPath);
androidJUnit4Builder = new AndroidJUnit4Builder(runnerParams, scanningPath);
androidSuiteBuilder = new AndroidSuiteBuilder(runnerParams);
androidAnnotatedBuilder =
new AndroidAnnotatedBuilder(suiteBuilder == null ? this : suiteBuilder, runnerParams);
ignoredBuilder = new IgnoredBuilder();
customRunnerBuilders = instantiateRunnerBuilders(customRunnerBuilderClasses);
}
/** Test that a custom runner builder is loaded */
@Test
public void fromBundle_notRunnerBuilder() {
Bundle b = new Bundle();
String className = CustomTestFilter.class.getName();
b.putString(RunnerArgs.ARGUMENT_RUNNER_BUILDER, className);
try {
new RunnerArgs.Builder().fromBundle(InstrumentationRegistry.getInstrumentation(), b).build();
fail("Did not detect invalid runner builder");
} catch (IllegalArgumentException e) {
assertEquals(
"Unexpected exception",
className + " does not extend " + RunnerBuilder.class.getName(),
e.getMessage());
}
}
public StructureBasicSuite(final Class<?> klass,
final RunnerBuilder builder)
throws InitializationError,
ConfigurationException {
super(klass, builder, allTests, null, true,
TraversalEngine.Type.STANDARD);
RegisterUtil.registerBackends();
}
public ProcessBasicSuite(final Class<?> klass,
final RunnerBuilder builder)
throws InitializationError {
super(klass, builder, allTests, testsToEnforce, true,
TraversalEngine.Type.STANDARD);
RegisterUtil.registerBackends();
}
public ProcessBasicSuite(final Class<?> klass,
final RunnerBuilder builder,
final Class<?>[] testsToExecute)
throws InitializationError {
super(klass, builder, testsToExecute, testsToEnforce, true,
TraversalEngine.Type.STANDARD);
RegisterUtil.registerBackends();
}
public CustomSuite(final Class<?> klass, final RunnerBuilder builder) throws InitializationError {
super(klass, builder,
new Class<?>[]{
CustomTest.class,
},
null,
false,
TraversalEngine.Type.STANDARD);
}
public TitanStrategySuite(final Class<?> klass, final RunnerBuilder builder) throws InitializationError {
super(klass, builder,
new Class<?>[]{
TitanGraphStepStrategyTest.class
}, new Class<?>[]{
TitanGraphStepStrategyTest.class
},
false,
TraversalEngine.Type.STANDARD);
}
/**
* Constructs a Gremlin Test Suite implementation.
*
* @param klass Required for JUnit Suite construction
* @param builder Required for JUnit Suite construction
* @param testsToExecute The list of tests to execute
* @param testsToEnforce The list of tests to "enforce" such that a check is made to ensure that in this list,
* there exists an implementation in the testsToExecute (use {@code null} for no
* enforcement).
* @param gremlinFlavorSuite Ignore validation of {@link Graph.OptIn} annotations which is typically reserved for structure tests
* @param traversalEngineType The {@link TraversalEngine.Type} to enforce on this suite
*/
public AbstractGremlinSuite(final Class<?> klass, final RunnerBuilder builder, final Class<?>[] testsToExecute,
final Class<?>[] testsToEnforce, final boolean gremlinFlavorSuite,
final TraversalEngine.Type traversalEngineType) throws InitializationError {
super(builder, klass, enforce(testsToExecute, testsToEnforce));
this.gremlinFlavorSuite = gremlinFlavorSuite;
// figures out what the implementer assigned as the GraphProvider class and make it available to tests.
// the klass is the Suite that implements this suite (e.g. GroovyTinkerGraphProcessStandardTest).
// this class should be annotated with GraphProviderClass. Failure to do so will toss an InitializationError
final Pair<Class<? extends GraphProvider>, Class<? extends Graph>> pair = getGraphProviderClass(klass);
// the GraphProvider.Descriptor is only needed right now if the test if for a computer engine - an
// exception is thrown if it isn't present.
final Optional<GraphProvider.Descriptor> graphProviderDescriptor = getGraphProviderDescriptor(traversalEngineType, pair.getValue0());
// validate public acknowledgement of the test suite and filter out tests ignored by the implementation
validateOptInToSuite(pair.getValue1());
validateOptInAndOutAnnotations(pair.getValue0());
validateOptInAndOutAnnotations(pair.getValue1());
registerOptOuts(pair.getValue0(), graphProviderDescriptor, traversalEngineType);
registerOptOuts(pair.getValue1(), graphProviderDescriptor, traversalEngineType);
try {
final GraphProvider graphProvider = pair.getValue0().newInstance();
GraphManager.setGraphProvider(graphProvider);
GraphManager.setTraversalEngineType(traversalEngineType);
} catch (Exception ex) {
throw new InitializationError(ex);
}
}
public NativeNeo4jSuite(final Class<?> klass, final RunnerBuilder builder) throws InitializationError {
super(klass, builder,
new Class<?>[]{
NativeNeo4jStructureCheck.class,
NativeNeo4jIndexCheck.class,
NativeNeo4jCypherCheck.class,
}, new Class<?>[]{
NativeNeo4jStructureCheck.class,
NativeNeo4jIndexCheck.class,
NativeNeo4jCypherCheck.class
},
false,
TraversalEngine.Type.STANDARD);
}
/**
* Builds the {@link Request} based on provided data.
*
* @throws java.lang.IllegalArgumentException if provided set of data is not valid
*/
public Request build() {
includedPackages.removeAll(excludedPackages);
includedClasses.removeAll(excludedClasses);
validate(includedClasses);
boolean scanningPath = includedClasses.isEmpty();
// If scanning then suite methods are not supported.
boolean ignoreSuiteMethods = this.ignoreSuiteMethods || scanningPath;
AndroidRunnerParams runnerParams =
new AndroidRunnerParams(instr, argsBundle, perTestTimeout, ignoreSuiteMethods);
RunnerBuilder runnerBuilder = getRunnerBuilder(runnerParams, scanningPath);
TestLoader loader = TestLoader.testLoader(classLoader, runnerBuilder, scanningPath);
Collection<String> classNames;
if (scanningPath) {
// no class restrictions have been specified. Load all classes.
classNames = getClassNamesFromClassPath();
} else {
classNames = includedClasses;
}
List<Runner> runners = loader.getRunnersFor(classNames, scanningPath);
Suite suite = ExtendedSuite.createSuite(runners);
Request request = Request.runner(suite);
return new LenientFilterRequest(request, filter);
}
/**
* Get the {@link RunnerBuilder} to use to create the {@link Runner} instances.
*
* @param runnerParams {@link AndroidRunnerParams} that stores common runner parameters
* @param scanningPath true if being used to build {@link Runner} from classes found while
* scanning the path; requires extra checks to avoid unnecessary errors.
* @return a {@link RunnerBuilder}.
*/
private RunnerBuilder getRunnerBuilder(AndroidRunnerParams runnerParams, boolean scanningPath) {
RunnerBuilder builder;
if (skipExecution) {
// If all that is needed is the list of tests then replace the Runner which will
// run the test with one that will simply fire events for each of the tests.
builder = new AndroidLogOnlyBuilder(runnerParams, scanningPath, customRunnerBuilderClasses);
} else {
builder = new AndroidRunnerBuilder(runnerParams, scanningPath, customRunnerBuilderClasses);
}
return builder;
}
/**
* @param runnerParams {@link AndroidRunnerParams} that stores common runner parameters
* @param scanningPath true if being used to build {@link Runner} from classes found while
* scanning the path; requires extra checks to avoid unnecessary errors.
*/
AndroidLogOnlyBuilder(
AndroidRunnerParams runnerParams,
boolean scanningPath,
List<Class<? extends RunnerBuilder>> customRunnerBuilderClasses) {
this.runnerParams = checkNotNull(runnerParams, "runnerParams cannot be null!");
this.scanningPath = scanningPath;
// Create a builder for creating the executable Runner instances to wrap. Pass in this
// builder as the suiteBuilder so that this will be called to create Runners for nested
// classes, e.g. in Suite or Enclosed.
builder =
new AndroidRunnerBuilder(this, runnerParams, scanningPath, customRunnerBuilderClasses);
}
static TestLoader testLoader(
ClassLoader classLoader, RunnerBuilder runnerBuilder, boolean scanningPath) {
// If scanning then wrap the supplied RunnerBuilder with one that will ignore abstract
// classes.
if (scanningPath) {
runnerBuilder = new ScanningRunnerBuilder(runnerBuilder);
}
if (null == classLoader) {
classLoader = TestLoader.class.getClassLoader();
}
return new TestLoader(classLoader, runnerBuilder);
}
/**
* @param runnerParams {@link AndroidRunnerParams} that stores common runner parameters
* @param scanningPath true if being used to build {@link Runner} from classes found while
* scanning the path; requires extra checks to avoid unnecessary errors.
* @param customRunnerBuilderClasses custom {@link RunnerBuilder} classes
*/
AndroidRunnerBuilder(
AndroidRunnerParams runnerParams,
boolean scanningPath,
List<Class<? extends RunnerBuilder>> customRunnerBuilderClasses) {
this(null, runnerParams, scanningPath, customRunnerBuilderClasses);
}
@Before
public void initBuilder() {
MockitoAnnotations.initMocks(this);
androidLogOnlyBuilder =
new AndroidLogOnlyBuilder(
mockAndroidRunnerParams,
scanningPath,
Collections.<Class<? extends RunnerBuilder>>emptyList());
}
@Before
public void setUp() throws Exception {
AndroidRunnerParams runnerParams = new AndroidRunnerParams(null, null, -1, false);
AndroidRunnerBuilder runnerBuilder =
new AndroidRunnerBuilder(
runnerParams, scanningPath, Collections.<Class<? extends RunnerBuilder>>emptyList());
loader = TestLoader.testLoader(null, runnerBuilder, scanningPath);
}
/** Test that a custom runner builder is loaded */
@Test
public void fromBundle_customRunnerBuilder() {
Bundle b = new Bundle();
b.putString(RunnerArgs.ARGUMENT_RUNNER_BUILDER, CustomRunnerBuilder.class.getName());
RunnerArgs args =
new RunnerArgs.Builder()
.fromBundle(InstrumentationRegistry.getInstrumentation(), b)
.build();
assertEquals(
"Mismatch in number of runner builders loaded", 1, args.runnerBuilderClasses.size());
Class<? extends RunnerBuilder> runnerBuilderClass = args.runnerBuilderClasses.get(0);
assertTrue(
"RunnerBuilder not of correct type", runnerBuilderClass == CustomRunnerBuilder.class);
}
public static Runner createRunner(final Class<?> clazz) {
final RunnerBuilder builder = createRunnerBuilder();
try {
return builder.runnerForClass(clazz);
} catch (final Throwable ex) {
LOG.log(Level.SEVERE, "Error while creating runner for " + clazz, ex);
throw translateCheckedException(ex);
}
}
public SmokeTestSuite(Class<?> clazz, RunnerBuilder builder) throws InitializationError {
super(clazz);
this.runners = builder.runners(clazz, getTestClasses());
}
public TestSuite(Class<?> klass, RunnerBuilder builder) throws InitializationError {
super(klass, builder);
}
@Override
public Runner getSuite(RunnerBuilder builder, java.lang.Class<?>[] classes) throws InitializationError {
Runner suite = super.getSuite(builder, classes);
return fClasses ? parallelize(suite) : suite;
}
@Override
protected Runner getRunner(RunnerBuilder builder, Class<?> testClass) throws Throwable {
Runner runner = super.getRunner(builder, testClass);
return fMethods ? parallelize(runner) : runner;
}
public TestsInPkgSuite(Class<?> klass, RunnerBuilder builder) throws InitializationError {
super(builder, klass, getPkgTestClasses(klass));
}
public BitsyGraphStructureTestSuite(final Class<?> klass, final RunnerBuilder builder) throws InitializationError {
super(klass, builder, allTests, null, false, TraversalEngine.Type.STANDARD);
}
/** Only called reflectively. Do not use programmatically. */
public CustomSuite(Class<?> klass, RunnerBuilder builder) throws Throwable {
super(builder, klass, getClasses(klass));
}
public VerboseSuite(Class<?> klass, RunnerBuilder builder) throws InitializationError {
super(klass, builder);
}
public StructureStandardSuite(final Class<?> klass, final RunnerBuilder builder) throws InitializationError {
super(klass, builder, allTests, null, false, TraversalEngine.Type.STANDARD);
}
public StructureBasicSuite(final Class<?> klass, final RunnerBuilder builder) throws InitializationError {
super(klass, builder, allTests, null, false, TraversalEngine.Type.STANDARD);
}
/** Required constructor: called by JUnit reflectively. */
public WycheproofRunner(Class<?> runnerClass, RunnerBuilder builder) throws InitializationError {
super(runnerClass, builder);
addFilter(new ExcludeTestFilter(runnerClass));
TestUtil.printJavaInformation();
}