下面列出了org.junit.runners.model.FrameworkMethod#isPublic() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Runner( Class<?> klass ) throws Throwable
{
super( klass, emptyList() );
List<FrameworkMethod> parameters = getTestClass().getAnnotatedMethods( Test.class );
List<Throwable> errors = null;
for ( FrameworkMethod parameter : parameters )
{
if ( !(parameter.isPublic() && parameter.isStatic()) )
{
if ( errors == null )
{
errors = new ArrayList<>();
}
errors.add( new IllegalStateException(
"@Test method " + parameter.getName() + " should be public and static" ) );
}
}
if ( errors != null )
{
throw new InitializationError( errors );
}
this.runners = createRunners( parameters );
}
/**
* {@link ParameterizedBeforeClass}が設定されているフィールドをチェックします。
*
* @param errors エラーのリスト。チェックした結果エラーがあった場合、このリストに追加される
*/
protected void validateParameterizedBeforeClass(List<Throwable> errors) {
for (FrameworkMethod method : getTestClass().getAnnotatedMethods(ParameterizedBeforeClass.class)) {
if (!method.isStatic()) {
errors.add(new Exception("Method " + method.getName() + "() should be static"));
}
if (!method.isPublic()) {
errors.add(new Exception("Method " + method.getName() + "() should be public"));
}
// このメソッドはコンストラクタのsuper内部で実行されるため、パラメーター数の取得ができない
// if (method.getMethod().getParameterTypes().length != parameters.length) {
// errors.add(new Exception(
// "Method " + method.getName() + "() should have " + parameters.length + " parameters"));
// }
if (method.getType() != Void.TYPE) {
errors.add(new Exception("Method " + method.getName() + "() should be void"));
}
}
}
/**
* {@link ParameterizedAfterClass}が設定されているフィールドをチェックします。
*
* @param errors エラーのリスト。チェックした結果エラーがあった場合、このリストに追加される
*/
protected void validateParameterizedAfterClass(List<Throwable> errors) {
for (FrameworkMethod method : getTestClass().getAnnotatedMethods(ParameterizedAfterClass.class)) {
if (!method.isStatic()) {
errors.add(new Exception("Method " + method.getName() + "() should be static"));
}
if (!method.isPublic()) {
errors.add(new Exception("Method " + method.getName() + "() should be public"));
}
// このメソッドはコンストラクタのsuper内部で実行されるため、パラメーター数の取得ができない
// if (method.getMethod().getParameterTypes().length != parameters.length) {
// errors.add(new Exception(
// "Method " + method.getName() + "() should have " + parameters.length + " parameters"));
// }
if (method.getType() != Void.TYPE) {
errors.add(new Exception("Method " + method.getName() + "() should be void"));
}
}
}
private FrameworkMethod getParametersMethod() throws Exception {
final List<FrameworkMethod> methods = getTestClass().getAnnotatedMethods(
Parameters.class);
for (final FrameworkMethod each : methods) {
if (each.isStatic() && each.isPublic()) {
return each;
}
}
throw new Exception("No public static parameters method on class "
+ getTestClass().getName());
}
private FrameworkMethod getAnnotatedPublicStaticMethod(Class<? extends Annotation> anno) {
List<FrameworkMethod> methods = getTestClass().getAnnotatedMethods(anno);
for (FrameworkMethod each : methods) {
if (each.isStatic() && each.isPublic()) {
return each;
}
}
return null;
}
private FrameworkMethod getParametersMethod() throws Exception {
List<FrameworkMethod> methods = testClass
.getAnnotatedMethods(Parameters.class);
for (FrameworkMethod each : methods) {
if (each.isStatic() && each.isPublic()) {
return each;
}
}
throw new Exception("No public static parameters method on class "
+ testClass.getName());
}
private FrameworkMethod getParametersMethod() throws Exception {
List<FrameworkMethod> methods = testClass
.getAnnotatedMethods(Parameters.class);
for (FrameworkMethod each : methods) {
if (each.isStatic() && each.isPublic()) {
return each;
}
}
throw new Exception("No public static parameters method on class "
+ testClass.getName());
}
/**
* {@link ParameterizedClassRule}が設定されているフィールドまたはメソッドをチェックします。
*
* @param errors エラーのリスト。チェックした結果エラーがあった場合、このリストに追加される
*/
protected void validateParameterizedClassRules(List<Throwable> errors) {
for (FrameworkMethod method : getTestClass().getAnnotatedMethods(ParameterizedClassRule.class)) {
if (!method.isStatic()) {
errors.add(new Exception("Method " + method.getName() + "() should be static"));
}
if (!method.isPublic()) {
errors.add(new Exception("Method " + method.getName() + "() should be public"));
}
if (method.getMethod().getParameterTypes().length != 0) {
errors.add(new Exception("Method " + method.getName() + "() should have no parameters"));
}
if (!ParameterizedTestRule.class.isAssignableFrom(method.getType())) {
errors.add(new Exception("Method " + method.getName()
+ "() must return an implementation of ParameterizedTestRule"));
}
}
for (FrameworkField field : getTestClass().getAnnotatedFields(ParameterizedClassRule.class)) {
if (!field.isStatic()) {
errors.add(new Exception("Field " + field.getName() + " should be static"));
}
if (!field.isPublic()) {
errors.add(new Exception("Field " + field.getName() + " should be public"));
}
if (!ParameterizedTestRule.class.isAssignableFrom(field.getType())) {
errors.add(new Exception("Field " + field.getName() + " must implement ParameterizedTestRule"));
}
}
}
private FrameworkMethod findParametersMethod(final TestClass testClass) {
final List<FrameworkMethod> methods = testClass.getAnnotatedMethods(Parameters.class);
if (methods.size() > 1) {
throw new RuntimeException(String.format("More than one method was annotated with @%s: %s",
Parameters.class.getSimpleName(), methods));
} else if (methods.isEmpty()) {
return null;
}
final FrameworkMethod method = methods.get(0);
if (method.isPublic() && method.isStatic() && Collection.class.isAssignableFrom(method.getReturnType())) {
return method;
}
throw new RuntimeException(String.format("Method %s must be public, static and have a return type assignable to Collection<Object>.",
method.getName()));
}
private FrameworkMethod getParametersMethod() throws Exception {
List<FrameworkMethod> methods = getTestClass().getAnnotatedMethods(
OldVersionParameter.class);
for (FrameworkMethod each : methods) {
if (each.isStatic() && each.isPublic()) {
return each;
}
}
throw new Exception("No public static parameters method on class "
+ getTestClass().getName());
}
private FrameworkMethod getParametersMethod() throws Exception {
List<FrameworkMethod> methods = getTestClass().getAnnotatedMethods(
TransformersParameter.class);
for (FrameworkMethod each : methods) {
if (each.isStatic() && each.isPublic()) {
return each;
}
}
throw new Exception("No public static parameters method on class "
+ getTestClass().getName());
}
@SuppressWarnings("unchecked")
@Override
public List<Object> createIterationData(TestClass testClass, FrameworkMethod method) throws Exception
{
List<FrameworkMethod> parametersMethods = testClass.getAnnotatedMethods(Parameters.class);
Iterable<Object> parameter = null;
for (FrameworkMethod parametersMethod : parametersMethods)
{
if (parametersMethod.isPublic() && parametersMethod.isStatic())
{
// take the first public static method. invoke it and use the result as parameter
try
{
Object parametersResult = parametersMethod.invokeExplosively(null);
if (parametersResult instanceof Iterable)
{
parameter = (Iterable<Object>) parametersResult;
}
else if (parametersResult instanceof Object[])
{
parameter = Arrays.asList((Object[]) parametersResult);
}
else
{
String msg = MessageFormat.format("{0}.{1}() must return an Iterable of arrays.",
testClass.getJavaClass().getName(), parametersMethod.getName());
throw new Exception(msg);
}
break;
}
catch (Throwable e)
{
throw new RuntimeException(e);
}
}
}
if (!parametersMethods.isEmpty() && parameter == null)
{
throw new Exception("No public static parameters method on class " + testClass.getJavaClass().getCanonicalName());
}
List<FrameworkField> parameterFrameworkFields = testClass.getAnnotatedFields(Parameter.class);
LOGGER.debug("Found " + parameterFrameworkFields.size() + " parameter fields");
List<Object> iterations = new LinkedList<>();
if (parameter != null)
{
int parameterSetCounter = 0;
for (Object para : parameter)
{
Object[] p;
if (para instanceof Object[])
{
p = (Object[]) para;
}
else
{
p = new Object[]
{
para
};
}
iterations.add(new ParameterStatementData(parameterSetCounter, p, parameterFrameworkFields));
parameterSetCounter++;
}
}
return iterations;
}
private void validateTestMethod(
boolean isStatic, List<Throwable> errors, FrameworkMethod testMethod) {
Class<?> returnType = testMethod.getReturnType();
// taken from FrameworkMethod.validatePublicVoid
if (testMethod.isStatic() != isStatic) {
String state = isStatic ? "should" : "should not";
errors.add(makeError("Method %s() " + state + " be static", testMethod));
}
if (!testMethod.isPublic()) {
errors.add(makeError("Method %s() should be public", testMethod));
}
if (returnType == Void.TYPE) {
return;
}
if (returnType == ListenableFuture.class) {
return;
}
try {
getPromiseType(returnType);
} catch (InvalidTypeException e) {
errors.add(makeError(e.getMessage()));
return;
}
Test testAnnotation = testMethod.getAnnotation(Test.class);
// Make sure we have a value greater than zero for test timeout
if (getTimeout(testMethod) <= 0) {
errors.add(
makeError(ErrorMessage.ASYNC_HAS_NO_TIMEOUT.format(testMethod.getMethod().getName())));
}
if (testAnnotation != null && !testAnnotation.expected().equals(Test.None.class)) {
errors.add(
makeError(
ErrorMessage.ASYNC_HAS_EXPECTED_EXCEPTION.format(testMethod.getMethod().getName())));
}
}