Skip to content

Commit

Permalink
issue j-easy#96 : add priority attribute to Rule annotation
Browse files Browse the repository at this point in the history
If a priority method is defined, it will take precedence over the
priority attribute of the annotation.
  • Loading branch information
fmbenhassine committed Apr 8, 2018
1 parent abad973 commit ca4b78d
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,10 @@
*/
String description() default org.jeasy.rules.api.Rule.DEFAULT_DESCRIPTION;

/**
* The rule priority.
* @return The rule priority
*/
int priority() default org.jeasy.rules.api.Rule.DEFAULT_PRIORITY;

}
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ private int compareTo(final Rule otherRule) throws Exception {
private int getRulePriority() throws Exception {
int priority = Rule.DEFAULT_PRIORITY;

org.jeasy.rules.annotation.Rule rule = getRuleAnnotation();
if (rule.priority() != Rule.DEFAULT_PRIORITY) {
priority = rule.priority();
}

Method[] methods = getMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(Priority.class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.jeasy.rules.annotation.Action;
import org.jeasy.rules.annotation.AnnotatedRuleWithMetaRuleAnnotation;
import org.jeasy.rules.annotation.Condition;
import org.jeasy.rules.annotation.Priority;
import org.jeasy.rules.api.Rule;
import org.junit.Test;

Expand Down Expand Up @@ -155,6 +156,80 @@ public void invokeToString() {
assertEquals(rule.toString(), proxy1.toString());
}

@Test
public void testPriorityFromAnnotation() {

@org.jeasy.rules.annotation.Rule(priority = 1)
class MyRule {
@Condition
public boolean when() { return true; }

@Action
public void then() { }
}

Object rule = new MyRule();
Rule proxy = RuleProxy.asRule(rule);
assertEquals(1, proxy.getPriority());
}

@Test
public void testPriorityFromMethod() {

@org.jeasy.rules.annotation.Rule
class MyRule {
@Condition
public boolean when() { return true; }

@Action
public void then() { }

@Priority
public int getPriority() { return 2; }
}

Object rule = new MyRule();
Rule proxy = RuleProxy.asRule(rule);
assertEquals(2, proxy.getPriority());
}

@Test
public void testPriorityPrecedence() {

@org.jeasy.rules.annotation.Rule(priority = 1)
class MyRule {
@Condition
public boolean when() { return true; }

@Action
public void then() { }

@Priority
public int getPriority() { return 2; }
}

Object rule = new MyRule();
Rule proxy = RuleProxy.asRule(rule);
assertEquals(2, proxy.getPriority());
}

@Test
public void testDefaultPriority() {

@org.jeasy.rules.annotation.Rule
class MyRule {
@Condition
public boolean when() { return true; }

@Action
public void then() { }
}

Object rule = new MyRule();
Rule proxy = RuleProxy.asRule(rule);
assertEquals(Rule.DEFAULT_PRIORITY, proxy.getPriority());
}

@org.jeasy.rules.annotation.Rule
class DummyRule {
@Condition
Expand All @@ -168,7 +243,6 @@ public String toString() {
return "I am a Dummy rule";
}


}
}

}

0 comments on commit ca4b78d

Please sign in to comment.