use of org.junit.runners.model.Statement in project java-faker by DiUS.
the class RepeatRule method apply.
@Override
public Statement apply(Statement statement, Description description) {
Statement result = statement;
Repeat repeat = description.getAnnotation(Repeat.class);
if (repeat != null) {
int times = repeat.times();
result = new RepeatStatement(times, statement);
}
return result;
}
use of org.junit.runners.model.Statement in project pact-jvm by DiUS.
the class InteractionRunner method interactionBlock.
protected Statement interactionBlock(final Interaction interaction) {
//1. prepare object
//2. get Target
//3. run Rule`s
//4. run Before`s
//5. run OnStateChange`s
//6. run test
//7. run After`s
final Object test;
try {
test = new ReflectiveCallable() {
@Override
protected Object runReflectiveCall() throws Throwable {
return createTest();
}
}.run();
} catch (Throwable e) {
return new Fail(e);
}
final Target target = testClass.getAnnotatedFieldValues(test, TestTarget.class, Target.class).get(0);
if (target instanceof TestClassAwareTarget) {
((TestClassAwareTarget) target).setTestClass(testClass, test);
}
Statement statement = new Statement() {
@Override
public void evaluate() throws Throwable {
target.testInteraction(pact.getConsumer().getName(), interaction);
}
};
statement = withStateChanges(interaction, test, statement);
statement = withBefores(interaction, test, statement);
statement = withRules(interaction, test, statement);
statement = withAfters(interaction, test, statement);
return statement;
}
use of org.junit.runners.model.Statement in project pact-jvm by DiUS.
the class MessagePactProviderRule method apply.
/* (non-Javadoc)
* @see org.junit.rules.ExternalResource#apply(org.junit.runners.model.Statement, org.junit.runner.Description)
*/
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
PactVerifications pactVerifications = description.getAnnotation(PactVerifications.class);
if (pactVerifications != null) {
evaluatePactVerifications(pactVerifications, base, description);
return;
}
PactVerification pactDef = description.getAnnotation(PactVerification.class);
// no pactVerification? execute the test normally
if (pactDef == null) {
base.evaluate();
return;
}
Message providedMessage = null;
Map<String, Message> pacts;
if (StringUtils.isNoneEmpty(pactDef.fragment())) {
Optional<Method> possiblePactMethod = findPactMethod(pactDef);
if (!possiblePactMethod.isPresent()) {
base.evaluate();
return;
}
pacts = new HashMap<>();
Method method = possiblePactMethod.get();
Pact pact = method.getAnnotation(Pact.class);
MessagePactBuilder builder = MessagePactBuilder.consumer(pact.consumer()).hasPactWith(provider);
messagePact = (MessagePact) method.invoke(testClassInstance, builder);
for (Message message : messagePact.getMessages()) {
pacts.put(message.getProviderState(), message);
}
} else {
pacts = parsePacts();
}
if (pactDef.value().length == 2 && !pactDef.value()[1].trim().isEmpty()) {
providedMessage = pacts.get(pactDef.value()[1].trim());
} else if (!pacts.isEmpty()) {
providedMessage = pacts.values().iterator().next();
}
if (providedMessage == null) {
base.evaluate();
return;
}
setMessage(providedMessage.contentsAsBytes(), description);
try {
base.evaluate();
messagePact.write(PactConsumerConfig$.MODULE$.pactRootDir());
} catch (Throwable t) {
throw t;
}
}
};
}
use of org.junit.runners.model.Statement in project pact-jvm by DiUS.
the class PactRule method apply.
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
PactVerification pactDef = description.getAnnotation(PactVerification.class);
//no pactVerification? execute the test normally
if (pactDef == null) {
base.evaluate();
return;
}
PactFragment fragment = getPacts().get(pactDef.value());
if (fragment == null) {
throw new UnsupportedOperationException("Fragment not found: " + pactDef.value());
}
VerificationResult result = fragment.runConsumer(config, new TestRun() {
@Override
public void run(MockProviderConfig config) throws Throwable {
base.evaluate();
}
});
if (!result.equals(PACT_VERIFIED)) {
if (result instanceof PactError) {
throw new RuntimeException(((PactError) result).error());
}
if (result instanceof UserCodeFailed) {
throw new RuntimeException(((UserCodeFailed<RuntimeException>) result).error());
}
if (result instanceof PactMismatch) {
PactMismatch mismatch = (PactMismatch) result;
throw new PactMismatchException(mismatch);
}
}
}
};
}
use of org.junit.runners.model.Statement in project tomee by apache.
the class TomEEEmbeddedSingleRunner method rules.
@Override
protected List<MethodRule> rules(final Object test) {
final List<MethodRule> rules = super.rules(test);
rules.add(new MethodRule() {
@Override
public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
start(test);
getRunner().composerInject(target);
base.evaluate();
}
};
}
});
return rules;
}
Aggregations