Java 类org.junit.ClassRule 实例源码

项目:spring4-understanding    文件:SpringMethodRule.java   
/**
 * Throw an {@link IllegalStateException} if the supplied {@code testClass}
 * does not declare a {@code public static final SpringClassRule} field
 * that is annotated with {@code @ClassRule}.
 */
private static SpringClassRule validateSpringClassRuleConfiguration(Class<?> testClass) {
    Field ruleField = null;

    for (Field field : testClass.getFields()) {
        if (ReflectionUtils.isPublicStaticFinal(field) && SpringClassRule.class.isAssignableFrom(field.getType())) {
            ruleField = field;
            break;
        }
    }

    if (ruleField == null) {
        throw new IllegalStateException(String.format(
                "Failed to find 'public static final SpringClassRule' field in test class [%s]. " +
                "Consult the javadoc for SpringClassRule for details.", testClass.getName()));
    }

    if (!ruleField.isAnnotationPresent(ClassRule.class)) {
        throw new IllegalStateException(String.format(
                "SpringClassRule field [%s] must be annotated with JUnit's @ClassRule annotation. " +
                "Consult the javadoc for SpringClassRule for details.", ruleField));
    }

    return (SpringClassRule) ReflectionUtils.getField(ruleField, null);
}
项目:kc-rice    文件:LoadTimeWeavableTestRunner.java   
/**
 * @return the {@code ClassRule}s that can transform the block that runs
 *         each method in the tested class.
 */
protected List<TestRule> classRules() {
    List<TestRule> result = getTestClass().getAnnotatedMethodValues(null, ClassRule.class, TestRule.class);

    result.addAll(getTestClass().getAnnotatedFieldValues(null, ClassRule.class, TestRule.class));

    return result;
}
项目:tool.lars    文件:ResourceFilteringTest.java   
/**
 * Create a RuleChain containing a RepositoryFixture for each of the repositories we are testing against
 *
 * @return a RuleChain
 */
@ClassRule
public static TestRule getClassRule() {
    // Put all the fixtures into one chain rule because we want to set up all the repositories at the start of the test, test with them and tear them down at the end
    RuleChain chain = RuleChain.emptyRuleChain();
    for (RepoData repoData : getRepoDataList()) {
        chain = chain.around(repoData.fixture);
    }
    return chain;
}
项目:camunda-bpm-process-test-coverage    文件:TestCoverageProcessEngineRule.java   
/**
 * Validates the annotation of the rule field in the test class.
 * 
 * @param description
 */
private void validateRuleAnnotations(Description description) {

    // If the first run is a @ClassRule run, check if @Rule is annotated
    if (firstRun && !description.isTest()) {

        /*
         * Get the fields of the test class and check if there is only one
         * coverage rule and if the coverage rule field is annotation with
         * both @ClassRule and @Rule.
         */

        int numberOfCoverageRules = 0;
        for (Field field : description.getTestClass().getFields()) {

            final Class<?> fieldType = field.getType();
            if (getClass().isAssignableFrom(fieldType)) {

                ++numberOfCoverageRules;

                final boolean isClassRule = field.isAnnotationPresent(ClassRule.class);
                final boolean isRule = field.isAnnotationPresent(Rule.class);
                if (isClassRule && !isRule) {

                    throw new RuntimeException(getClass().getCanonicalName()
                            + " can only be used as a @ClassRule if it is also a @Rule!");
                }
            }
        }

        // TODO if they really want to have multiple runs, let them?
        if (numberOfCoverageRules > 1) {
            throw new RuntimeException("Only one coverage rule can be used per test class!");
        }
    }
}
项目:hbase    文件:HBaseClassTestRuleChecker.java   
@Override
public void testStarted(Description description) throws Exception {
  for (Field field : description.getTestClass().getFields()) {
    if (Modifier.isStatic(field.getModifiers()) && field.getType() == HBaseClassTestRule.class &&
      field.isAnnotationPresent(ClassRule.class)) {
      HBaseClassTestRule timeout = (HBaseClassTestRule) field.get(null);
      assertEquals(
        "The HBaseClassTestRule ClassRule in " + description.getTestClass().getName() +
          " is for " + timeout.getClazz().getName(),
        description.getTestClass(), timeout.getClazz());
      return;
    }
  }
  fail("No HBaseClassTestRule ClassRule for " + description.getTestClass().getName());
}
项目:lcm    文件:ParentRunner.java   
/**
 * @return the {@code ClassRule}s that can transform the block that runs
 *         each method in the tested class.
 */
protected List<TestRule> classRules() {
    List<TestRule> result = fTestClass.getAnnotatedMethodValues(null, ClassRule.class, TestRule.class);

    result.addAll(fTestClass.getAnnotatedFieldValues(null, ClassRule.class, TestRule.class));

    return result;
}
项目:rice    文件:LoadTimeWeavableTestRunner.java   
/**
 * @return the {@code ClassRule}s that can transform the block that runs
 *         each method in the tested class.
 */
protected List<TestRule> classRules() {
    List<TestRule> result = getTestClass().getAnnotatedMethodValues(null, ClassRule.class, TestRule.class);

    result.addAll(getTestClass().getAnnotatedFieldValues(null, ClassRule.class, TestRule.class));

    return result;
}
项目:junit-hierarchicalcontextrunner    文件:ClassRuleStatementBuilder.java   
public Statement createStatement(final TestClass testClass, final Statement next,
                                 final Description description, final RunNotifier notifier) {
    final List<TestRule> classRules = new ArrayList<TestRule>();
    classRules.addAll(testClass.getAnnotatedMethodValues(null, ClassRule.class, TestRule.class));
    classRules.addAll(testClass.getAnnotatedFieldValues(null, ClassRule.class, TestRule.class));
    return classRules.isEmpty() ? next : new RunRules(next, classRules, description);
}
项目:junit-hierarchicalcontextrunner    文件:ClassRuleStatementBuilderTest.java   
@Test
public void givenTestClassWithoutRuleAnnotations_returnsNextStatement() throws Exception {
    when(testClass.getAnnotatedMethodValues(null, ClassRule.class, TestRule.class)).thenReturn(Collections.EMPTY_LIST);
    when(testClass.getAnnotatedFieldValues(null, ClassRule.class, TestRule.class)).thenReturn(Collections.EMPTY_LIST);

    Statement statement = builder.createStatement(testClass, next, description, notifier);
    assertThat(statement, is(equalTo(next)));
}
项目:junit-hierarchicalcontextrunner    文件:ClassRuleStatementBuilderTest.java   
@Test
public void givenTestClassWithRuleAnnotatedMethods_returnsRunRulesStatement() throws Exception {
    List<TestRule> methods = Arrays.asList(rule1, rule2);
    when(testClass.getAnnotatedMethodValues(null, ClassRule.class, TestRule.class)).thenReturn(methods);
    when(testClass.getAnnotatedFieldValues(null, ClassRule.class, TestRule.class)).thenReturn(Collections.EMPTY_LIST);

    Statement actual = builder.createStatement(testClass, next, description, notifier);
    assertThat(actual, is(instanceOf(RunRules.class)));
}
项目:junit-hierarchicalcontextrunner    文件:ClassRuleStatementBuilderTest.java   
@Test
public void givenTestClassWithRuleAnnotatedFields_returnsRunRulesStatement() throws Exception {
    List<TestRule> fields = Arrays.asList(rule1, rule2);
    when(testClass.getAnnotatedMethodValues(null, ClassRule.class, TestRule.class)).thenReturn(Collections.EMPTY_LIST);
    when(testClass.getAnnotatedFieldValues(null, ClassRule.class, TestRule.class)).thenReturn(fields);

    Statement actual = builder.createStatement(testClass, next, description, notifier);
    assertThat(actual, is(instanceOf(RunRules.class)));
}
项目:junit-hierarchicalcontextrunner    文件:ClassRuleStatementBuilderTest.java   
@Test
public void givenTestClassWithRuleAnnotatedMethodsAndFields_returnsRunRulesStatement() throws Exception {
    List<TestRule> methods = Arrays.asList(rule1);
    List<TestRule> fields = Arrays.asList(rule2);
    when(testClass.getAnnotatedMethodValues(null, ClassRule.class, TestRule.class)).thenReturn(methods);
    when(testClass.getAnnotatedFieldValues(null, ClassRule.class, TestRule.class)).thenReturn(fields);

    Statement actual = builder.createStatement(testClass, next, description, notifier);
    assertThat(actual, is(instanceOf(RunRules.class)));
}
项目:junit    文件:ParentRunner.java   
/**
 * @return the {@code ClassRule}s that can transform the block that runs
 *         each method in the tested class.
 */
protected List<TestRule> classRules() {
    List<TestRule> result = fTestClass.getAnnotatedMethodValues(null, ClassRule.class, TestRule.class);

    result.addAll(fTestClass.getAnnotatedFieldValues(null, ClassRule.class, TestRule.class));

    return result;
}
项目:org.openntf.domino    文件:ParentRunner.java   
/**
 * @return the {@code ClassRule}s that can transform the block that runs
 *         each method in the tested class.
 */
protected List<TestRule> classRules() {
    List<TestRule> result = fTestClass.getAnnotatedMethodValues(null, ClassRule.class, TestRule.class);

    result.addAll(fTestClass.getAnnotatedFieldValues(null, ClassRule.class, TestRule.class));

    return result;
}
项目:cloudera-framework    文件:TestRunner.java   
@Override
@SuppressWarnings("deprecation")
protected Statement methodBlock(FrameworkMethod method) {
  Object test;
  try {
    test = new ReflectiveCallable() {
      @Override
      protected Object runReflectiveCall() throws Throwable {
        return createTest();
      }
    }.run();
  } catch (Throwable e) {
    return new Fail(e);
  }
  boolean validated = true;
  for (CdhServer cdhServer : getTestClass().getAnnotatedFieldValues(test, ClassRule.class, CdhServer.class)) {
    validated = validated && cdhServer.isValid();
    if (validated) {
      for (CdhServer cdhServerDependency : cdhServer.getDependencies()) {
        validated = validated && cdhServerDependency.isValid();
      }
    }
  }
  Statement statement;
  if (validated) {
    statement = methodInvoker(method, test);
    statement = withLogging(method, test, statement);
    statement = possiblyExpectingExceptions(method, test, statement);
    statement = withPotentialTimeout(method, test, statement);
    statement = withBefores(method, test, statement);
    statement = withAfters(method, test, statement);
    statement = withServerRules(method, test, statement);
    statement = withRules(method, test, statement);
  } else {
    statement = new Statement() {
      @Override
      public void evaluate() throws Throwable {
        if (LOG.isWarnEnabled()) {
          LOG.warn("Skipping [" + method.getDeclaringClass().getCanonicalName() + "." + method.getName() + "], invalid classpath");
        }
      }
    };
  }
  return statement;
}
项目:sosiefier    文件:ParentRunner.java   
/**
 * @return the {@code ClassRule}s that can transform the block that runs
 *         each method in the tested class.
 */
protected List<TestRule> classRules() {
    List<TestRule> result = fTestClass.getAnnotatedMethodValues(null, ClassRule.class, TestRule.class);
    result.addAll(fTestClass.getAnnotatedFieldValues(null, ClassRule.class, TestRule.class));
    return result;
}
项目:sosiefier    文件:ClassRulesTest.java   
@ClassRule
public static OrderTestRule orderMethod() {
    return new OrderTestRule("orderMethod");
}
项目:sosiefier    文件:ClassRulesTest.java   
@ClassRule
public static Counter getCounter() {
    return counter;
}
项目:sosiefier    文件:ClassRulesTest.java   
@ClassRule
public static CustomCounter getCounter() {
    return counter;
}
项目:sosiefier    文件:ClassRulesTest.java   
@ClassRule
public static Dummy both() {
    countOfMethodCalls++;
    return new Dummy();
}
项目:sosiefier    文件:RuleFieldValidatorTest.java   
@ClassRule
protected static TestRule getTemporaryFolder() {
    return new TemporaryFolder();
}
项目:sosiefier    文件:RuleFieldValidatorTest.java   
@ClassRule
public TestRule getTemporaryFolder() {
    return new TemporaryFolder();
}
项目:lightblue-client    文件:BeforeAfterTestRule.java   
protected Statement prepareRules(TestClass extension, Statement base, Description description) {
    List<TestRule> rules = extension.getAnnotatedFieldValues(null, Rule.class, TestRule.class);
    rules.addAll(extension.getAnnotatedFieldValues(null, ClassRule.class, TestRule.class));
    return new RunRules(base, rules, description);
}
项目:lightblue-hibernate-ogm    文件:BeforeAfterTestRule.java   
protected Statement prepareRules(TestClass extension, Statement base, Description description) {
    List<TestRule> rules = extension.getAnnotatedFieldValues(null, Rule.class, TestRule.class);
    rules.addAll(extension.getAnnotatedFieldValues(null, ClassRule.class, TestRule.class));
    return new RunRules(base, rules, description);
}
项目:pitest    文件:JUnitCustomRunnerTestUnitFinderTest.java   
@ClassRule
public static TestRule rule() {
  return new ExternalResource() {
  };
}
项目:junit    文件:ClassRulesTest.java   
@ClassRule
public static OrderTestRule orderMethod() {
    return new OrderTestRule("orderMethod");
}
项目:junit    文件:ClassRulesTest.java   
@ClassRule
public static Counter getCounter() {
    return counter;
}
项目:junit    文件:ClassRulesTest.java   
@ClassRule
public static CustomCounter getCounter() {
    return counter;
}
项目:junit    文件:ClassRulesTest.java   
@ClassRule
public static Dummy both() {
    countOfMethodCalls++;
    return new Dummy();
}
项目:junit    文件:RuleFieldValidatorTest.java   
@ClassRule
protected static TestRule getTemporaryFolder() {
    return new TemporaryFolder();
}
项目:junit    文件:RuleFieldValidatorTest.java   
@ClassRule
public TestRule getTemporaryFolder() {
    return new TemporaryFolder();
}
项目:health-and-care-developer-network    文件:ClassRulesTest.java   
@ClassRule
public static OrderTestRule orderMethod() {
    return new OrderTestRule("orderMethod");
}
项目:health-and-care-developer-network    文件:ClassRulesTest.java   
@ClassRule
public static Counter getCounter() {
    return counter;
}
项目:health-and-care-developer-network    文件:ClassRulesTest.java   
@ClassRule
public static CustomCounter getCounter() {
    return counter;
}
项目:health-and-care-developer-network    文件:ClassRulesTest.java   
@ClassRule
public static Dummy both() {
    countOfMethodCalls++;
    return new Dummy();
}
项目:health-and-care-developer-network    文件:RuleFieldValidatorTest.java   
@ClassRule
protected static TestRule getTemporaryFolder() {
    return new TemporaryFolder();
}
项目:health-and-care-developer-network    文件:RuleFieldValidatorTest.java   
@ClassRule
public TestRule getTemporaryFolder() {
    return new TemporaryFolder();
}
项目:parameterized-suite    文件:ParentRunnerUtil.java   
/**
 * @param testClass 
 * @return the {@code ClassRule}s that can transform the block that runs
 *         each method in the tested class.
 *         
 * @see ParentRunner#classRules()
 */
public static List<TestRule> getClassRules(TestClass testClass) {
    List<TestRule> result = testClass.getAnnotatedMethodValues(null, ClassRule.class, TestRule.class);
    result.addAll(testClass.getAnnotatedFieldValues(null, ClassRule.class, TestRule.class));
    return result;
}
项目:j2objc    文件:ParentRunner.java   
/**
 * @return the {@code ClassRule}s that can transform the block that runs
 *         each method in the tested class.
 */
protected List<TestRule> classRules() {
    return fTestClass.getAnnotatedFieldValues(null, ClassRule.class, TestRule.class);
}
项目:logback-access-spring-boot-starter    文件:AbstractTestSpringConfigurationFileAutoDetectingTest.java   
/**
 * Creates a class test rule.
 *
 * @return a class test rule.
 */
@ClassRule
public static TestRule classRule() {
    return new ClassPathRule(AbstractTestSpringConfigurationFileAutoDetectingTest.class);
}