@Override public ConditionMethod visit(Condition annotation, Method method) throws RuleDefinitionException { return new ConditionMethod(annotation.name(), annotation.hint(), method); } });
/** * Executes this {@link ConditionMethod}. If the #method does not succeed a * {@link ConditionFailure} is returned. Otherwise null is returned. * * @param context * The current executing {@link ExecutionContext} * @return A {@link ConditionFailure} if condition fails, null otherwise * @throws RuleExecutionException * If execution of the condition method fails with an exception. * @see ExecutionContext * @see ConditionFailure */ public ConditionFailure execute(ExecutionContext context) throws RuleExecutionException { try { boolean valid = (boolean) ReflectionUtils.invokeMethod(getMethod(), context.getInstance()); if (!valid) { // Store information about the failed condition for later usage return new ConditionFailure(getName(), getHint()); } return null; } catch (Exception e) { throw new RuleExecutionException("Invocation of condition method failed.", context, e); } }
/** * Default Constructor. * * @param name * The name of the condition * @param hint * A hint why the condition failed * @param method * The actual backing implementation of the condition method * @throws RuleDefinitionException * If the {@link ConditionMethod} is invalid. A condition method must be public, * with zero arguments and boolean/Boolean return type. */ public ConditionMethod(String name, String hint, Method method) throws RuleDefinitionException { this.method = checkNotNull(method); this.name = StringUtils.defaultIfEmpty(name, this.method.getName()); this.hint = hint; validate(); }
@Test public void shouldProduceConditionFailure() throws Exception { // prepare Mocks when(dummy.successCondiction()).thenReturn(true); ConditionMethod condition = new ConditionMethod("TestFailCondition", "failure", RuleDummy.failConditionMethod()); ConditionFailure failure = condition.execute(context); assertThat(failure.getConditionName(), equalTo(condition.getName())); assertThat(failure.getHint(), equalTo(condition.getHint())); }
@Test public void shouldProduceNull() throws Exception { // prepare Mocks when(dummy.successCondiction()).thenReturn(true); ConditionMethod condition = new ConditionMethod("TestSuccessCondition", "success", RuleDummy.successConditionMethod()); ConditionFailure failure = condition.execute(context); assertThat(failure, nullValue()); }
ConditionFailure failure = conditionMethod.execute(ctx); if (failure != null) { if (null == conditionFailures) {
@Test(expectedExceptions = { RuleExecutionException.class }) public void shouldthrowException() throws Exception { // prepare Mocks when(dummy.invalidCondition()).thenThrow(new RuntimeException()); ConditionMethod condition = new ConditionMethod("TestInvalidCondition", "invalid", RuleDummy.invalidConditionMethod()); ConditionFailure failure = condition.execute(context); System.out.println(failure); } }
@Test public void testWithRuleAnnotation() throws Exception { RuleDefinition definition = Rules.define(ValidAndAnnotated.class); assertThat(definition.getName(), is("AnnotatedRule")); assertThat(definition.getDescription(), is("Description")); assertThat(definition.getFireCondition().getTagTypes(), containsInAnyOrder("T1", "T2")); // Test tag injections TagInjection tagInjection = new TagInjection("T1", ValidAndAnnotated.class.getDeclaredField("t1AsTag"), TagValue.InjectionStrategy.BY_TAG); TagInjection tagInjection1 = new TagInjection("T2", ValidAndAnnotated.class.getDeclaredField("t2TagValue"), TagValue.InjectionStrategy.BY_VALUE); assertThat(definition.getTagInjections(), is(notNullValue())); assertThat(definition.getTagInjections(), containsInAnyOrder(tagInjection, tagInjection1)); // Test session variables SessionVariableInjection s1 = new SessionVariableInjection("baseline", false, ValidAndAnnotated.class.getDeclaredField("baseline")); SessionVariableInjection s2 = new SessionVariableInjection("baseline2", true, ValidAndAnnotated.class.getDeclaredField("baseline2")); assertThat(definition.getSessionVariableInjections(), containsInAnyOrder(s1, s2)); // Test action method assertThat(definition.getActionMethod(), is(new ActionMethod(ValidAndAnnotated.class.getDeclaredMethod("action"), "T2", Action.Quantity.SINGLE))); // Test condition method ConditionMethod conditionMethod = new ConditionMethod("myCondition", "No way out", ValidAndAnnotated.class.getDeclaredMethod("condition")); assertThat(definition.getConditionMethods(), containsInAnyOrder(conditionMethod)); }