Java Code Examples for org.junit.runners.model.FrameworkMethod#isStatic()

The following examples show how to use org.junit.runners.model.FrameworkMethod#isStatic() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ParameterTest.java    From openCypher with Apache License 2.0 6 votes vote down vote up
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 );
}
 
Example 2
Source File: PtlBlockJUnit4ClassRunnerWithParameters.java    From hifive-pitalium with Apache License 2.0 6 votes vote down vote up
/**
 * {@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"));
		}
	}
}
 
Example 3
Source File: PtlBlockJUnit4ClassRunnerWithParameters.java    From hifive-pitalium with Apache License 2.0 6 votes vote down vote up
/**
 * {@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"));
		}
	}
}
 
Example 4
Source File: BrowserParameterizedRunner.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 5
Source File: XtextParametrizedRunner.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
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;
}
 
Example 6
Source File: CustomParameterizedRunner.java    From registry with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 7
Source File: CustomParameterizedRunner.java    From registry with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 8
Source File: PtlBlockJUnit4ClassRunnerWithParameters.java    From hifive-pitalium with Apache License 2.0 5 votes vote down vote up
/**
 * {@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"));
		}
	}
}
 
Example 9
Source File: WildflyTestRunner.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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()));
}
 
Example 10
Source File: OldVersionTestRunner.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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());
}
 
Example 11
Source File: TransformersTestParameterized.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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());
}
 
Example 12
Source File: EndToEndRunner.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Marks validation errors if given method is not static, or does not return an {@link
 * EndToEndEnvironment}
 */
private void validateEnvironmentMethod(
    FrameworkMethod environmentMethod, List<Throwable> errors) {
  if (!environmentMethod.isStatic()) {
    errors.add(
        new AnnotationFormatError(
            "Methods marked by @Environment or @EnvironmentFor must be static"));
  }
  if (!EndToEndEnvironment.class.isAssignableFrom(environmentMethod.getReturnType())) {
    errors.add(
        new AnnotationFormatError(
            "Methods marked by @Environment or @EnvironmentFor must return an EndToEndEnvironment"));
  }
}
 
Example 13
Source File: ParameterStatement.java    From neodymium-library with MIT License 4 votes vote down vote up
@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;
}
 
Example 14
Source File: AsyncTestRunner.java    From j2cl with Apache License 2.0 4 votes vote down vote up
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())));
  }
}