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);
}
}
};
}
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);
}
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;
}
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;
}
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();
}
};
}
Aggregations