Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package org.robolectric;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import org.junit.Assert;
import org.junit.runner.Runner;
import org.junit.runners.Parameterized;
import org.junit.runners.Suite;
import org.junit.runners.model.FrameworkField;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.TestClass;
import org.robolectric.internal.SandboxTestRunner;
import org.robolectric.util.ReflectionHelpers;
/**
* A Parameterized test runner for Robolectric. Copied from the {@link Parameterized} class, then
* modified the custom test runner to extend the {@link RobolectricTestRunner}. The {@link
* org.robolectric.RobolectricTestRunner#getHelperTestRunner(Class)} is overridden in order to
* create instances of the test class with the appropriate parameters. Merged in the ability to name
* your tests through the {@link Parameters#name()} property. Merged in support for {@link
* Parameter} annotation alternative to providing a constructor.
*
*
This class takes care of the fact that the test runner and the test class are actually loaded
* from different class loaders and therefore parameter objects created by one cannot be assigned to
* instances of the other.
*
*
See also {@link RobolectricTestParameterInjector} for a more modern alternative.
*/
public final class ParameterizedRobolectricTestRunner extends Suite {
/**
* Annotation for a method which provides parameters to be injected into the test class
* constructor by Parameterized
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Parameters {
/**
* Optional pattern to derive the test's name from the parameters. Use numbers in braces to
* refer to the parameters or the additional data as follows:
*
*
* {index} - the current parameter index
* {0} - the first parameter value
* {1} - the second parameter value
* etc...
*
*
*
Default value is "{index}" for compatibility with previous JUnit versions.
*
* @return {@link MessageFormat} pattern string, except the index placeholder.
* @see MessageFormat
*/
String name() default "{index}";
}
/**
* Annotation for fields of the test class which will be initialized by the method annotated by
* Parameters
* By using directly this annotation, the test class constructor isn't needed.
* Index range must start at 0. Default value is 0.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Parameter {
/**
* Method that returns the index of the parameter in the array returned by the method annotated
* by Parameters.
* Index range must start at 0. Default value is 0.
*
* @return the index of the parameter.
*/
int value() default 0;
}
private static class TestClassRunnerForParameters extends RobolectricTestRunner {
private final int parametersIndex;
private final String name;
TestClassRunnerForParameters(Class> type, int parametersIndex, String name)
throws InitializationError {
super(type);
this.parametersIndex = parametersIndex;
this.name = name;
}
private Object createTestInstance(Class bootstrappedClass) throws Exception {
Constructor>[] constructors = bootstrappedClass.getConstructors();
Assert.assertEquals(1, constructors.length);
if (!fieldsAreAnnotated()) {
return constructors[0].newInstance(computeParams(bootstrappedClass.getClassLoader()));
} else {
Object instance = constructors[0].newInstance();
injectParametersIntoFields(instance, bootstrappedClass.getClassLoader());
return instance;
}
}
private Object[] computeParams(ClassLoader classLoader) throws Exception {
// Robolectric uses a different class loader when running the tests, so the parameters objects
// created by the test runner are not compatible with the parameters required by the test.
// Instead, we compute the parameters within the test's class loader.
try {
List