类org.junit.rules.TestRule源码实例Demo

下面列出了怎么用org.junit.rules.TestRule的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: HiveRunner   文件: StandaloneHiveRunner.java
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;
        }
    };
}
 
源代码2 项目: camunda-bpm-platform   文件: JerseySpecifics.java
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();
      }
    });
}
 
源代码3 项目: camunda-bpm-platform   文件: ResteasySpecifics.java
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();
      }
    });
}
 
源代码4 项目: rice   文件: LoadTimeWeavableTestRunner.java
/**
 * @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;
}
 
源代码5 项目: camunda-bpm-platform   文件: ResteasySpecifics.java
public TestRule createTestRule() {
  return new ExternalResource() {

    ResteasyServerBootstrap bootstrap = new ResteasyServerBootstrap(jaxRsApplication);

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

    protected void after() {
      bootstrap.stop();
    }
  };
}
 
源代码6 项目: quickperf   文件: QuickPerfSpringRunner.java
@Override
protected List<TestRule> classRules() {
    if (SystemProperties.TEST_CODE_EXECUTING_IN_NEW_JVM.evaluate()) {
        return QUICK_PERF_SPRING_RUNNER_FOR_SPECIFIC_JVM.classRules();
    }
    if(quickPerfFeaturesAreDisabled) {
        return springRunner.classRules();
    }
    return super.classRules();
}
 
源代码7 项目: logging-log4j2   文件: RuleChainFactory.java
/**
 * Creates a {@link RuleChain} where the rules are evaluated in the order you pass in.
 * 
 * @param testRules
 *            test rules to evaluate
 * @return a new rule chain.
 */
public static RuleChain create(final TestRule... testRules) {
    if (testRules == null || testRules.length == 0) {
        return RuleChain.emptyRuleChain();
    }
    RuleChain ruleChain = RuleChain.outerRule(testRules[0]);
    for (int i = 1; i < testRules.length; i++) {
        ruleChain = ruleChain.around(testRules[i]);
    }
    return ruleChain;
}
 
源代码8 项目: HiveRunner   文件: ThrowOnTimeout.java
public static TestRule create(final HiveRunnerConfig config, final Object target) {
    return new TestRule() {
        @Override
        public Statement apply(Statement base, Description description) {
            return new ThrowOnTimeout(base, config, target);
        }
    };
}
 
源代码9 项目: camunda-bpm-platform   文件: JerseySpecifics.java
public TestRule createTestRule() {
  return new ExternalResource() {

    JerseyServerBootstrap bootstrap = new JerseyServerBootstrap(jaxRsApplication);

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

    protected void after() {
      bootstrap.stop();
    }
  };
}
 
源代码10 项目: rice   文件: LoadTimeWeavableTestRunner.java
private Statement withMethodRules(FrameworkMethod method, List<TestRule> testRules,
        Object target, Statement result) {
    for (org.junit.rules.MethodRule each : getMethodRules(target)) {
        if (!testRules.contains(each)) {
            result = each.apply(result, method, target);
        }
    }
    return result;
}
 
源代码11 项目: quickperf   文件: QuickPerfSpringRunner.java
@Override
protected List<TestRule> getTestRules(Object target) {
    if (SystemProperties.TEST_CODE_EXECUTING_IN_NEW_JVM.evaluate()) {
        return QUICK_PERF_SPRING_RUNNER_FOR_SPECIFIC_JVM.getTestRules(target);
    }
    if(quickPerfFeaturesAreDisabled) {
        return springRunner.getTestRules(target);
    }
    return super.getTestRules(target);
}
 
源代码12 项目: flink   文件: FlinkStandaloneHiveRunner.java
@Override
protected List<TestRule> classRules() {
	final TemporaryFolder temporaryFolder = new TemporaryFolder();
	try {
		hmsPort = HiveTestUtils.getFreePort();
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
	HiveServerContext context = new FlinkStandaloneHiveServerContext(temporaryFolder, config, hmsPort);
	List<TestRule> rules = super.classRules();
	ExternalResource hms = new ExternalResource() {
		@Override
		protected void before() throws Throwable {
			LOGGER.info("Setting up {} in {}", getName(), temporaryFolder.getRoot().getAbsolutePath());
			hmsWatcher = startHMS(context, hmsPort);
		}

		@Override
		protected void after() {
			if (hmsWatcher != null) {
				hmsWatcher.cancel(true);
			}
		}
	};
	ExternalResource hiveShell = new ExternalResource() {
		@Override
		protected void before() throws Throwable {
			container = createHiveServerContainer(getTestClass().getJavaClass(), context);
		}

		@Override
		protected void after() {
			tearDown();
		}
	};
	rules.add(hiveShell);
	rules.add(hms);
	rules.add(temporaryFolder);
	return rules;
}
 
源代码13 项目: htmlunit   文件: BrowserVersionClassRunner.java
private Statement withMethodRules(final FrameworkMethod method, final Object target, Statement result) {
    final List<TestRule> testRules = getTestRules(target);
    for (final org.junit.rules.MethodRule each : getMethodRules(target)) {
        if (!testRules.contains(each)) {
            result = each.apply(result, method, target);
        }
    }
    return result;
}
 
源代码14 项目: rice   文件: LoadTimeWeavableTestRunner.java
private Statement withRules(FrameworkMethod method, Object target,
        Statement statement) {
    List<TestRule> testRules = getTestRules(target);
    Statement result = statement;
    result = withMethodRules(method, testRules, target, result);
    result = withTestRules(method, testRules, result);

    return result;
}
 
源代码15 项目: Selenium-Foundation   文件: JUnitBase.java
/**
 * Get the test rule of the specified type that's attached to the rule chain.
 * 
 * @param <T> test rule type
 * @param testRuleType test rule type
 * @return {@link ScreenshotCapture} test rule
 */
public <T extends TestRule> T getLinkedRule(final Class<T> testRuleType) {
    Optional<T> optional = RuleChainWalker.getAttachedRule(ruleChain, testRuleType);
    if (optional.isPresent()) {
        return optional.get();
    }
    throw new IllegalStateException(testRuleType.getSimpleName() + " test rule wasn't found on the rule chain");
}
 
源代码16 项目: netbeans   文件: TestCatalogModel.java
/**
 * A JUnit {@link TestRule} that stops tests from interfering with one 
 * another. JUnit will automatically set up/clean up the catalog when this
 * rule is used. <br/>
 * Usage:<br/>
 * {@code @Rule public final TestRule catalogMaintainer = TestCatalogModel.maintainer()}
 * @return the TestRule
 */
public static TestRule maintainer() {
    return new ExternalResource() {

        @Override
        protected void after() {
            getDefault().clearDocumentPool();
        }
    
    };
}
 
private <R extends TestRule> Statement apply(Statement base, Description description,
		Delegate<R> delegate) {
	final R rule = delegate.createRule();
	Statement statement = new Statement() {

		@Override
		public void evaluate() throws Throwable {
			ArtifactoryServerConnection.this.serverFactory = (artifactory) -> delegate
					.getArtifactoryServer(rule, artifactory);
			base.evaluate();
		}

	};
	return rule.apply(statement, description);
}
 
源代码18 项目: fake-sftp-server-rule   文件: Executor.java
static void executeTestThatThrowsExceptionWithRule(
    Statement test,
    TestRule rule
) {
    ignoreException(
        executeTestWithRuleRaw(test, rule),
        Throwable.class
    );
}
 
源代码19 项目: fake-sftp-server-rule   文件: Executor.java
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();
}
 
源代码20 项目: video-recorder-java   文件: TestUtils.java
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()));
    }
}
 
源代码21 项目: camunda-bpm-platform   文件: WinkSpecifics.java
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();
}
 
源代码22 项目: camunda-bpm-platform   文件: JerseySpecifics.java
public TestRule createTestRule() {
  return new ExternalResource() {

    JerseyServerBootstrap bootstrap = new JerseyServerBootstrap(jaxRsApplication);

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

    protected void after() {
      bootstrap.stop();
    }
  };
}
 
源代码23 项目: tomee   文件: EJBContainerRunner.java
@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;
}
 
源代码24 项目: camunda-bpm-platform   文件: JerseySpecifics.java
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();
}
 
/**
 * Creates a test rule.
 *
 * @return a test rule.
 */
@Rule
public TestRule rule() {
    return RuleChain
            .outerRule(new LogbackAccessEventQueuingAppenderRule())
            .around(new LogbackAccessEventQueuingListenerRule());
}
 
/**
 * Creates a test rule.
 *
 * @return a test rule.
 */
@Rule
public TestRule rule() {
    return RuleChain
            .outerRule(new LogbackAccessEventQueuingAppenderRule())
            .around(new LogbackAccessEventQueuingListenerRule());
}
 
/**
 * Creates a test rule.
 *
 * @return a test rule.
 */
@Rule
public TestRule rule() {
    return RuleChain
            .outerRule(new LogbackAccessEventQueuingAppenderRule())
            .around(new LogbackAccessEventQueuingListenerRule());
}
 
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;
}
 
/**
 * Creates a test rule.
 *
 * @return a test rule.
 */
@Rule
public TestRule rule() {
    return RuleChain
            .outerRule(new LogbackAccessEventQueuingAppenderRule())
            .around(new LogbackAccessEventQueuingListenerRule());
}
 
源代码30 项目: junit-servers   文件: JunitServerRunner.java
@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;
}