Search in sources :

Example 96 with Statement

use of org.junit.runners.model.Statement in project neo4j by neo4j.

the class TestData method apply.

@Override
public Statement apply(final Statement base, final Description description) {
    final Title title = description.getAnnotation(Title.class);
    final Documented doc = description.getAnnotation(Documented.class);
    GraphDescription.Graph g = description.getAnnotation(GraphDescription.Graph.class);
    if (g == null) {
        g = description.getTestClass().getAnnotation(GraphDescription.Graph.class);
    }
    final GraphDescription graph = GraphDescription.create(g);
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            product.set(create(graph, title == null ? null : title.value(), doc == null ? null : doc.value(), description.getMethodName()));
            try {
                try {
                    base.evaluate();
                } catch (Throwable err) {
                    try {
                        destroy(get(false), false);
                    } catch (Throwable sub) {
                        List<Throwable> failures = new ArrayList<Throwable>();
                        if (err instanceof MultipleFailureException) {
                            failures.addAll(((MultipleFailureException) err).getFailures());
                        } else {
                            failures.add(err);
                        }
                        failures.add(sub);
                        throw new MultipleFailureException(failures);
                    }
                    throw err;
                }
                destroy(get(false), false);
            } finally {
                product.set(null);
            }
        }
    };
}
Also used : Documented(org.neo4j.kernel.impl.annotations.Documented) MultipleFailureException(org.junit.runners.model.MultipleFailureException) Statement(org.junit.runners.model.Statement) ArrayList(java.util.ArrayList)

Example 97 with Statement

use of org.junit.runners.model.Statement in project neo4j by neo4j.

the class JUnitRuleTest method shouldRuleWorkWithExsitingDirectory.

@Test
public void shouldRuleWorkWithExsitingDirectory() {
    // given
    GraphDatabaseService db = new TestGraphDatabaseFactory().newEmbeddedDatabaseBuilder(testDirectory.directory()).newGraphDatabase();
    try {
        db.execute("CREATE ()");
    } finally {
        db.shutdown();
    }
    // When a rule with an pre-populated graph db directory is used
    final Neo4jRule ruleWithDirectory = new Neo4jRule(testDirectory.directory()).copyFrom(testDirectory.directory());
    ruleWithDirectory.apply(new Statement() {

        @Override
        public void evaluate() throws Throwable {
            // Then the database is not empty
            Result result = ruleWithDirectory.getGraphDatabaseService().execute("match (n) return count(n) as " + "count");
            List<Object> column = Iterators.asList(result.columnAs("count"));
            assertEquals(1, column.size());
            assertEquals(1, column.get(0));
        }
    }, null);
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Neo4jRule(org.neo4j.harness.junit.Neo4jRule) Statement(org.junit.runners.model.Statement) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) List(java.util.List) Result(org.neo4j.graphdb.Result) Test(org.junit.Test)

Example 98 with Statement

use of org.junit.runners.model.Statement in project spring-framework by spring-projects.

the class SpringJUnit4ClassRunner method withPotentialTimeout.

/**
	 * Perform the same logic as
	 * {@link BlockJUnit4ClassRunner#withPotentialTimeout(FrameworkMethod, Object, Statement)}
	 * but with additional support for Spring's {@code @Timed} annotation.
	 * <p>Supports both Spring's {@link org.springframework.test.annotation.Timed @Timed}
	 * and JUnit's {@link Test#timeout() @Test(timeout=...)} annotations, but not both
	 * simultaneously.
	 * @return either a {@link SpringFailOnTimeout}, a {@link FailOnTimeout},
	 * or the supplied {@link Statement} as appropriate
	 * @see #getSpringTimeout(FrameworkMethod)
	 * @see #getJUnitTimeout(FrameworkMethod)
	 */
@Override
@SuppressWarnings("deprecation")
protected Statement withPotentialTimeout(FrameworkMethod frameworkMethod, Object testInstance, Statement next) {
    Statement statement = null;
    long springTimeout = getSpringTimeout(frameworkMethod);
    long junitTimeout = getJUnitTimeout(frameworkMethod);
    if (springTimeout > 0 && junitTimeout > 0) {
        String msg = String.format("Test method [%s] has been configured with Spring's @Timed(millis=%s) and " + "JUnit's @Test(timeout=%s) annotations, but only one declaration of a 'timeout' is " + "permitted per test method.", frameworkMethod.getMethod(), springTimeout, junitTimeout);
        logger.error(msg);
        throw new IllegalStateException(msg);
    } else if (springTimeout > 0) {
        statement = new SpringFailOnTimeout(next, springTimeout);
    } else if (junitTimeout > 0) {
        statement = FailOnTimeout.builder().withTimeout(junitTimeout, TimeUnit.MILLISECONDS).build(next);
    } else {
        statement = next;
    }
    return statement;
}
Also used : Statement(org.junit.runners.model.Statement) SpringFailOnTimeout(org.springframework.test.context.junit4.statements.SpringFailOnTimeout)

Example 99 with Statement

use of org.junit.runners.model.Statement in project spring-framework by spring-projects.

the class SpringMethodRule method apply.

/**
	 * Apply <em>instance-level</em> and <em>method-level</em> features of
	 * the <em>Spring TestContext Framework</em> to the supplied {@code base}
	 * statement.
	 * <p>Specifically, this method invokes the
	 * {@link TestContextManager#prepareTestInstance prepareTestInstance()},
	 * {@link TestContextManager#beforeTestMethod beforeTestMethod()}, and
	 * {@link TestContextManager#afterTestMethod afterTestMethod()} methods
	 * on the {@code TestContextManager}, potentially with Spring timeouts
	 * and repetitions.
	 * <p>In addition, this method checks whether the test is enabled in
	 * the current execution environment. This prevents methods with a
	 * non-matching {@code @IfProfileValue} annotation from running altogether,
	 * even skipping the execution of {@code prepareTestInstance()} methods
	 * in {@code TestExecutionListeners}.
	 * @param base the base {@code Statement} that this rule should be applied to
	 * @param frameworkMethod the method which is about to be invoked on the test instance
	 * @param testInstance the current test instance
	 * @return a statement that wraps the supplied {@code base} with instance-level
	 * and method-level features of the Spring TestContext Framework
	 * @see #withBeforeTestMethodCallbacks
	 * @see #withAfterTestMethodCallbacks
	 * @see #withPotentialRepeat
	 * @see #withPotentialTimeout
	 * @see #withTestInstancePreparation
	 * @see #withProfileValueCheck
	 */
@Override
public Statement apply(Statement base, FrameworkMethod frameworkMethod, Object testInstance) {
    Method testMethod = frameworkMethod.getMethod();
    if (logger.isDebugEnabled()) {
        logger.debug("Applying SpringMethodRule to test method [" + testMethod + "]");
    }
    Class<?> testClass = testInstance.getClass();
    validateSpringClassRuleConfiguration(testClass);
    TestContextManager testContextManager = SpringClassRule.getTestContextManager(testClass);
    Statement statement = base;
    statement = withBeforeTestMethodCallbacks(statement, testMethod, testInstance, testContextManager);
    statement = withAfterTestMethodCallbacks(statement, testMethod, testInstance, testContextManager);
    statement = withTestInstancePreparation(statement, testInstance, testContextManager);
    statement = withPotentialRepeat(statement, testMethod, testInstance);
    statement = withPotentialTimeout(statement, testMethod, testInstance);
    statement = withProfileValueCheck(statement, testMethod, testInstance);
    return statement;
}
Also used : Statement(org.junit.runners.model.Statement) FrameworkMethod(org.junit.runners.model.FrameworkMethod) Method(java.lang.reflect.Method) TestContextManager(org.springframework.test.context.TestContextManager)

Example 100 with Statement

use of org.junit.runners.model.Statement in project keywhiz by square.

the class MigrationsRule method apply.

@Override
public Statement apply(final Statement base, Description description) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            File yamlFile = new File(Resources.getResource("keywhiz-test.yaml").getFile());
            Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
            ObjectMapper objectMapper = KeywhizService.customizeObjectMapper(Jackson.newObjectMapper());
            KeywhizConfig config = new ConfigurationFactory<>(KeywhizConfig.class, validator, objectMapper, "dw").build(yamlFile);
            DataSource dataSource = config.getDataSourceFactory().build(new MetricRegistry(), "db-migrations");
            Flyway flyway = new Flyway();
            flyway.setDataSource(dataSource);
            flyway.setLocations(config.getMigrationsDir());
            flyway.clean();
            flyway.migrate();
            DSLContext dslContext = DSLContexts.databaseAgnostic(dataSource);
            DbSeedCommand.doImport(dslContext);
            base.evaluate();
        }
    };
}
Also used : Flyway(org.flywaydb.core.Flyway) Statement(org.junit.runners.model.Statement) MetricRegistry(com.codahale.metrics.MetricRegistry) DSLContext(org.jooq.DSLContext) File(java.io.File) Validator(javax.validation.Validator) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DataSource(javax.sql.DataSource)

Aggregations

Statement (org.junit.runners.model.Statement)129 Test (org.junit.Test)22 FrameworkMethod (org.junit.runners.model.FrameworkMethod)15 Method (java.lang.reflect.Method)10 AssumptionViolatedException (org.junit.internal.AssumptionViolatedException)10 Fail (org.junit.internal.runners.statements.Fail)9 TestRule (org.junit.rules.TestRule)7 Description (org.junit.runner.Description)7 MethodRule (org.junit.rules.MethodRule)6 MultipleFailureException (org.junit.runners.model.MultipleFailureException)6 ExecutorService (java.util.concurrent.ExecutorService)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 ReflectiveCallable (org.junit.internal.runners.model.ReflectiveCallable)5 ArrayList (java.util.ArrayList)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 EmptyStatement (com.hazelcast.util.EmptyStatement)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 TestExecutorService (org.apache.beam.fn.harness.test.TestExecutors.TestExecutorService)3 CompositeConfiguration (org.apache.logging.log4j.core.config.composite.CompositeConfiguration)3 LoggerContextRule (org.apache.logging.log4j.junit.LoggerContextRule)3