org.junit.rules.TestRule Java Examples

The following examples show how to use org.junit.rules.TestRule. 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: StandaloneHiveRunner.java    From HiveRunner with Apache License 2.0 6 votes vote down vote up
private TestRule getHiveRunnerConfigRule(Object target) {
    return new TestRule() {
        @Override
        public Statement apply(Statement base, Description description) {
            Set<Field> fields = ReflectionUtils.getAllFields(target.getClass(),
                    Predicates.and(
                            withAnnotation(HiveRunnerSetup.class),
                            withType(HiveRunnerConfig.class)));

            Preconditions.checkState(fields.size() <= 1,
                    "Exact one field of type HiveRunnerConfig should to be annotated with @HiveRunnerSetup");

            /*
             Override the config with test case config. Taking care to not replace the config instance since it
              has been passes around and referenced by some of the other test rules.
              */
            if (!fields.isEmpty()) {
                config.override(ReflectionUtils
                        .getFieldValue(target, fields.iterator().next().getName(), HiveRunnerConfig.class));
            }

            return base;
        }
    };
}
 
Example #2
Source File: JerseySpecifics.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
public TestRule createTestRule() {
  final TemporaryFolder tempFolder = new TemporaryFolder();

  return RuleChain
    .outerRule(tempFolder)
    .around(new ExternalResource() {

      TomcatServerBootstrap bootstrap = new JerseyTomcatServerBootstrap(webXmlResource);

      protected void before() throws Throwable {
        bootstrap.setWorkingDir(tempFolder.getRoot().getAbsolutePath());
        bootstrap.start();
      }

      protected void after() {
        bootstrap.stop();
      }
    });
}
 
Example #3
Source File: ResteasySpecifics.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
public TestRule createTestRule() {
  final TemporaryFolder tempFolder = new TemporaryFolder();

  return RuleChain
    .outerRule(tempFolder)
    .around(new ExternalResource() {

      ResteasyTomcatServerBootstrap bootstrap = new ResteasyTomcatServerBootstrap(webXmlResource);

      protected void before() throws Throwable {
        bootstrap.setWorkingDir(tempFolder.getRoot().getAbsolutePath());
        bootstrap.start();
      }

      protected void after() {
        bootstrap.stop();
      }
    });
}
 
Example #4
Source File: EJBContainerRunner.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
protected List<TestRule> getTestRules(final Object target) {
    final List<TestRule> rules = new ArrayList<TestRule>();
    rules.add(new InjectRule(target, startingStatement));
    rules.add(new TransactionRule());
    rules.addAll(getTestClass().getAnnotatedFieldValues(target, Rule.class, TestRule.class));
    return rules;
}
 
Example #5
Source File: JerseySpecifics.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public TestRule getTestRule(Class<?> testClass) {
  TestRuleFactory ruleFactory = DEFAULT_RULE_FACTORY;

  if (TEST_RULE_FACTORIES.containsKey(testClass)) {
    ruleFactory = TEST_RULE_FACTORIES.get(testClass);
  }

  return ruleFactory.createTestRule();
}
 
Example #6
Source File: ResteasySpecifics.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public TestRule getTestRule(Class<?> testClass) {
  TestRuleFactory ruleFactory = DEFAULT_RULE_FACTORY;

  if (TEST_RULE_FACTORIES.containsKey(testClass)) {
    ruleFactory = TEST_RULE_FACTORIES.get(testClass);
  }

  return ruleFactory.createTestRule();
}
 
Example #7
Source File: ProcessEngineRuleRunner.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Override
protected List<TestRule> getTestRules(Object target) {
  List<TestRule> testRules = super.getTestRules(target);

  testRules.stream()
    .filter(t -> t instanceof ProcessEngineRule)
    .map(t -> (ProcessEngineRule)t)
    .forEach(processEngineRules::add);

  return testRules;
}
 
Example #8
Source File: EndToEndRunner.java    From buck with Apache License 2.0 5 votes vote down vote up
private Statement withRules(EndToEndTestDescriptor child, Object target, Statement statement) {
  // We do not support MethodRules like the JUnit runner does as it has been functionally
  // replaced by TestRules (https://junit.org/junit4/javadoc/4.12/org/junit/rules/MethodRule.html)
  List<TestRule> testRules =
      getTestClass().getAnnotatedMethodValues(target, Rule.class, TestRule.class);
  testRules.addAll(getTestClass().getAnnotatedFieldValues(target, Rule.class, TestRule.class));
  return testRules.isEmpty()
      ? statement
      : new RunRules(statement, testRules, describeChild(child));
}
 
Example #9
Source File: RuleContext.java    From spectrum with MIT License 5 votes vote down vote up
/**
 * Find the test rules within the test mixin.
 * @param target the test case instance
 * @return a list of TestRules that should be applied when executing this
 *         test
 */
private List<TestRule> getTestRules(final Object target) {
  return Stream.concat(
      testClass.getAnnotatedMethodValues(target, Rule.class, TestRule.class).stream(),
      testClass.getAnnotatedFieldValues(target, Rule.class, TestRule.class).stream())
      .collect(Collectors.toList());
}
 
Example #10
Source File: RegisterUserAccount.java    From jenkins-build-monitor-plugin with MIT License 5 votes vote down vote up
@Override
public TestRule applyTo(final JenkinsInstance jenkins) {
    return new TestWatcher() {
        @Override
        protected void starting(Description description) {
            jenkins.client().registerAccount(user.getName(), user.password());
        }
    };
}
 
Example #11
Source File: AbstractMainConfigurationFileAutoDetectingTest.java    From logback-access-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a test rule.
 *
 * @return a test rule.
 */
@Rule
public TestRule rule() {
    return RuleChain
            .outerRule(new LogbackAccessEventQueuingAppenderRule())
            .around(new LogbackAccessEventQueuingListenerRule());
}
 
Example #12
Source File: AbstractMainSpringConfigurationFileAutoDetectingTest.java    From logback-access-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a test rule.
 *
 * @return a test rule.
 */
@Rule
public TestRule rule() {
    return RuleChain
            .outerRule(new LogbackAccessEventQueuingAppenderRule())
            .around(new LogbackAccessEventQueuingListenerRule());
}
 
Example #13
Source File: AbstractLogbackAccessFilteringTest.java    From logback-access-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a test rule.
 *
 * @return a test rule.
 */
@Rule
public TestRule rule() {
    return RuleChain
            .outerRule(new LogbackAccessEventQueuingAppenderRule())
            .around(new LogbackAccessEventQueuingListenerRule());
}
 
Example #14
Source File: CXFSpecifics.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public TestRule createTestRule() {
  return new ExternalResource() {

    CXFServerBootstrap bootstrap = new CXFServerBootstrap(jaxRsApplication);

    protected void before() throws Throwable {
      bootstrap.start();
    }

    protected void after() {
      bootstrap.stop();
    }
  };
}
 
Example #15
Source File: JunitServerRunner.java    From junit-servers with MIT License 5 votes vote down vote up
@Override
protected List<TestRule> getTestRules(Object target) {
	AnnotationsHandlerRule rule = new AnnotationsHandlerRule(target, server, configuration);

	log.debug("Injecting {} to test rules", rule);
	List<TestRule> testRules = super.getTestRules(target);
	testRules.add(rule);
	return testRules;
}
 
Example #16
Source File: ConditionallyIgnoreClassRule.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
public ConditionallyIgnoreClassRule(IgnoreCondition condition, TestRule... chained) {
    Assert.assertNotNull(condition);
    this.condition = condition;
    List<TestRule> chainedList = Arrays.asList(chained);
    Collections.reverse(chainedList);
    this.chained = Collections.unmodifiableList(chainedList);
}
 
Example #17
Source File: ConditionallyIgnoreClassRule.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
@Override
public Statement apply(Statement statement, Description description) {
    Statement next = statement;
    for (TestRule testRule : chained) {
        next = testRule.apply(next, description);
    }
    return createStatement(next);
}
 
Example #18
Source File: ResteasySpecifics.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public TestRule getTestRule(Class<?> testClass) {
  TestRuleFactory ruleFactory = DEFAULT_RULE_FACTORY;

  if (TEST_RULE_FACTORIES.containsKey(testClass)) {
    ruleFactory = TEST_RULE_FACTORIES.get(testClass);
  }

  return ruleFactory.createTestRule();
}
 
Example #19
Source File: AbstractLogbackAccessEventsTest.java    From logback-access-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a test rule.
 *
 * @return a test rule.
 */
@Rule
public TestRule rule() {
    return RuleChain
            .outerRule(new LogbackAccessEventQueuingAppenderRule())
            .around(new LogbackAccessEventQueuingListenerRule());
}
 
Example #20
Source File: JenkinsInstance.java    From jenkins-build-monitor-plugin with MIT License 5 votes vote down vote up
private <ATR extends ApplicativeTestRule<JenkinsInstance>> List<TestRule> instantiated(List<ATR> rules) {
    List<TestRule> instantiatedRules = new ArrayList<>(rules.size());

    for (ATR testRule : rules) {
        instantiatedRules.add(testRule.applyTo(this));
    }

    return instantiatedRules;
}
 
Example #21
Source File: AbstractServerPortUnusingTest.java    From logback-access-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a test rule.
 *
 * @return a test rule.
 */
@Rule
public TestRule rule() {
    return RuleChain
            .outerRule(new LogbackAccessEventQueuingAppenderRule())
            .around(new LogbackAccessEventQueuingListenerRule());
}
 
Example #22
Source File: AbstractSecurityAttributesTest.java    From logback-access-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a test rule.
 *
 * @return a test rule.
 */
@Rule
public TestRule rule() {
    return RuleChain
            .outerRule(new LogbackAccessEventQueuingAppenderRule())
            .around(new LogbackAccessEventQueuingListenerRule());
}
 
Example #23
Source File: AbstractForwardHeadersUsingTest.java    From logback-access-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a test rule.
 *
 * @return a test rule.
 */
@Rule
public TestRule rule() {
    return RuleChain
            .outerRule(new LogbackAccessEventQueuingAppenderRule())
            .around(new LogbackAccessEventQueuingListenerRule());
}
 
Example #24
Source File: JerseySpecifics.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public TestRule getTestRule(Class<?> testClass) {
  TestRuleFactory ruleFactory = DEFAULT_RULE_FACTORY;

  if (TEST_RULE_FACTORIES.containsKey(testClass)) {
    ruleFactory = TEST_RULE_FACTORIES.get(testClass);
  }

  return ruleFactory.createTestRule();
}
 
Example #25
Source File: LoadTimeWeavableTestRunner.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * @param target the test case instance
 * @return a list of TestRules that should be applied when executing this
 *         test
 */
protected List<TestRule> getTestRules(Object target) {
    List<TestRule> result = getTestClass().getAnnotatedMethodValues(target,
            Rule.class, TestRule.class);

    result.addAll(getTestClass().getAnnotatedFieldValues(target,
            Rule.class, TestRule.class));

    return result;
}
 
Example #26
Source File: JerseySpecifics.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public TestRule createTestRule() {
  return new ExternalResource() {

    JerseyServerBootstrap bootstrap = new JerseyServerBootstrap(jaxRsApplication);

    protected void before() throws Throwable {
      bootstrap.start();
    }

    protected void after() {
      bootstrap.stop();
    }
  };
}
 
Example #27
Source File: WinkSpecifics.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public TestRule getTestRule(Class<?> testClass) {
  TestRuleFactory ruleFactory = DEFAULT_RULE_FACTORY;

  if (TEST_RULE_FACTORIES.containsKey(testClass)) {
    ruleFactory = TEST_RULE_FACTORIES.get(testClass);
  }

  return ruleFactory.createTestRule();
}
 
Example #28
Source File: TestUtils.java    From video-recorder-java with MIT License 5 votes vote down vote up
public static void runRule(TestRule rule, Object target, String methodName) {
    Class<?> clazz = target.getClass();
    Method method = TestUtils.getMethod(clazz, methodName);
    Description description = Description.createTestDescription(clazz, method.getName(), method.getDeclaredAnnotations());
    try {
        InvokeMethod invokeMethod = new InvokeMethod(new FrameworkMethod(method), target);
        rule.apply(invokeMethod, description).evaluate();
    } catch (Throwable throwable) {
        logger.warning(Arrays.toString(throwable.getStackTrace()));
    }
}
 
Example #29
Source File: Executor.java    From fake-sftp-server-rule with MIT License 5 votes vote down vote up
private static Statement executeTestWithRuleRaw(
    Statement test,
    TestRule rule
) {
    org.junit.runners.model.Statement statement
        = new org.junit.runners.model.Statement() {
            @Override
            public void evaluate() throws Throwable {
                test.evaluate();
            }
        };
    return () -> rule.apply(statement, DUMMY_DESCRIPTION).evaluate();
}
 
Example #30
Source File: Executor.java    From fake-sftp-server-rule with MIT License 5 votes vote down vote up
static void executeTestThatThrowsExceptionWithRule(
    Statement test,
    TestRule rule
) {
    ignoreException(
        executeTestWithRuleRaw(test, rule),
        Throwable.class
    );
}