@Override protected Statement applyRuleToLastStatement(final Method method, final Object testInstance, Field field, final LastRuleTestExecutorStatement lastStatement) throws IllegalAccessException { final Object fieldValue = field.get(testInstance); final Statement statement; if (fieldValue instanceof MethodRule) { // the MethodRule is known by junit 4.9 -> delegate to super-class statement = super.applyRuleToLastStatement(method, testInstance, field, lastStatement); } else if (fieldValue instanceof TestRule){ TestRule rule = (TestRule) fieldValue; statement = rule.apply(lastStatement, description); } else { throw new IllegalStateException("Can only handle MethodRule and TestRule"); } return statement; }
@Override public Statement apply(final Statement result, final FrameworkMethod method, final Object target) { Statement lastResult = result; for (final MethodRule rule : getRules()) { lastResult = rule.apply(lastResult, method, target); } return lastResult; }
private Statement withRules( FrameworkMethod method, Object target, Statement statement ) { Statement result = statement; for( MethodRule each : rules( target ) ) result = each.apply( result, method, target ); return result; }
/** * @param test the test class * @return the MethodRules that can transform the block * that runs each method in the tested class. */ protected List<MethodRule> rules( Object test ) { List<MethodRule> results = new ArrayList<MethodRule>(); for( FrameworkField each : ruleFields() ) results.add( createRule( test, each ) ); return results; }
private MethodRule createRule( Object test, FrameworkField each ) { try { return (MethodRule) each.get( test ); } catch( IllegalAccessException e ) { throw new RuntimeException( "How did getFields return a field we couldn't access?" ); } catch( IllegalArgumentException ex ) { throw new RuntimeException( "How did getFields return a field we couldn't access?" ); } }
@Rule public MethodRule getTemporaryFolder() { return new MethodRule() { public Statement apply(Statement base, FrameworkMethod method, Object target) { return null; } }; }
@Override protected List<MethodRule> rules(Object test) { final LinkedList<MethodRule> methodRules = new LinkedList<MethodRule>( super.rules(test)); methodRules.add(new FlipBitRule()); return methodRules; }
/** * method taken as is from BlockJUnit4ClassRunner 4.7 in order to preserve * its functionality over following versions */ private Statement withRules(FrameworkMethod method, Object target, Statement statement) { Statement result = statement; for (MethodRule each : rules(target)) { result = each.apply(result, method, target); } return result; }
/** * method taken as is from BlockJUnit4ClassRunner 4.7 in order to preserve * its functionality over following versions */ private MethodRule createRule(Object test, FrameworkField each) { try { return (MethodRule) each.get(test); } catch (IllegalAccessException e) { throw new RuntimeException( "How did getFields return a field we couldn't access?"); } }
/** * {@inheritDoc} Before evaluation of the base statement, the test instance * will initialized. */ @Override public Statement apply(final Statement base, final FrameworkMethod method, final Object target) { Statement appliedStatement = base; for (MethodRule rule : methodRuleChain) { appliedStatement = rule.apply(appliedStatement, method, target); } return statement(appliedStatement, target); }
/** * Encloses the added rule. * * @param rule * - outer method rule * @return {@link NeedleRule} */ public NeedleRule withOuter(final MethodRule rule) { if (rule instanceof InjectionProvider) { addInjectionProvider((InjectionProvider<?>) rule); } methodRuleChain.add(0, rule); return this; }
@Override protected NeedleRule build(final InjectionConfiguration injectionConfiguration, final InjectionProvider<?>... injectionProvider) { final NeedleRule needleRule = new NeedleRule(injectionConfiguration, injectionProvider); for (final MethodRule rule : methodRuleChain) { needleRule.withOuter(rule); } return needleRule; }
/** * Internal JUnit method - overridden to add a {@link MoxieRule} to all tests. */ @Override protected List<MethodRule> rules(Object test) { ArrayList<MethodRule> result = new ArrayList<MethodRule>(super.rules(test)); result.add(new MoxieRule()); return result; }
public boolean contains(MethodRule methodRule) { for (TestRuleInTestHierarchy t : testRulePositionInTestHierarchies) { if (t.getTestRule().equals(methodRule)) return true; } return false; }
public List<MethodRule> getMethodRulesDefinedForThisHierarchyLevel(Object hierarchyContext) { List<MethodRule> result = new ArrayList<MethodRule>(); for (MethodRuleInTestHierarchy testRulePosition : methodRulePositionInTestHierarchies) if (hierarchyOfTestsFromLowestToHighest.indexOf(testRulePosition.getObjectRepresentingHierarchyLevel()) >= hierarchyOfTestsFromLowestToHighest.indexOf(hierarchyContext)) result.add(testRulePosition.getMethodRule()); return result; }
/** * Simply copied from BlockJUnit4ClassRunner, since its not accessible. */ private Statement withRules(FrameworkMethod method, Object target, Statement statement) { Statement result = statement; for (MethodRule each : getTestClass().getAnnotatedFieldValues(target, Rule.class, MethodRule.class)) result = each.apply(result, method, target); return result; }
@Override protected List<MethodRule> rules(final Object target) { return registerRules(super.rules(target), executor, JpaUnitContext.getInstance(getTestClass().getJavaClass())); }
private List<MethodRule> getRules() { return registerRules(new ArrayList<>(), executor, ctx); }
public static List<MethodRule> registerRules(final List<MethodRule> rules, final DecoratorExecutor executor, final ExecutionContext ctx) { rules.add((base, method, target) -> new TestMethodStatement(ctx, executor, base, method, target)); rules.add((base, method, target) -> new TestClassStatement(ctx, executor, base, target)); return rules; }
protected Statement applyRuleToLastStatement(final Method method, final Object testInstance, Field field, final LastRuleTestExecutorStatement lastStatement) throws IllegalAccessException { MethodRule rule = (MethodRule) field.get(testInstance); Statement statement = rule.apply(lastStatement, new FrameworkMethod(method), testInstance); return statement; }
public AbstractNeedleRuleBuilder<NeedleRuleBuilder, NeedleRule> withOuter(final MethodRule rule) { methodRuleChain.add(0, rule); return this; }
@Test public void shouldGetFieldType() { assertEquals(MethodRule.class, reflector.getFieldType("testTimeout")); assertEquals(FieldReflector.class, reflector.getFieldType("reflector")); }
@Before public void setUp() { filter = new TypeMemberFilter(MethodRule.class); }
public void addMethodRules(List<MethodRule> annotatedFieldValues, Object instance) { for (MethodRule methodRule : annotatedFieldValues) methodRulePositionInTestHierarchies.add(new MethodRuleInTestHierarchy(methodRule, instance)); }
public MethodRuleInTestHierarchy(MethodRule methodRule, Object instance) { this.methodRule = methodRule; this.instance = instance; }
public MethodRule getMethodRule() { return methodRule; }