issue #66 : add Inherited annotation to support base (abstract) classes as rules

pull/76/head
Mahmoud Ben Hassine 8 years ago
parent aaf2898a3d
commit 2e72286120

@ -23,10 +23,7 @@
*/
package org.easyrules.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.*;
/**
* Annotation to mark a method as a rule action.
@ -35,6 +32,8 @@ import java.lang.annotation.Target;
*
* @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
*/
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Action {

@ -23,10 +23,7 @@
*/
package org.easyrules.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.*;
/**
* Annotation to mark a method as a rule condition.
@ -34,6 +31,8 @@ import java.lang.annotation.Target;
*
* @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
*/
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Condition {

@ -23,10 +23,7 @@
*/
package org.easyrules.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.*;
/**
* Annotation to mark the method to execute to get rule priority.
@ -34,6 +31,8 @@ import java.lang.annotation.Target;
*
* @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
*/
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Priority {

@ -23,16 +23,15 @@
*/
package org.easyrules.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.*;
/**
* Annotation to mark a class as a rule.
*
* @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
*/
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Rule {

@ -35,6 +35,7 @@ import org.junit.runners.Suite;
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({
AnnotationInheritanceTest.class,
BasicRuleTest.class,
RulePriorityThresholdTest.class,
SkipOnFirstAppliedRuleTest.class,

@ -0,0 +1,64 @@
/**
* The MIT License
*
* Copyright (c) 2017, Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.easyrules.core;
import org.easyrules.annotation.Action;
import org.easyrules.annotation.Condition;
import org.easyrules.api.RulesEngine;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class AnnotationInheritanceTest {
@Test
public void annotationsShouldBeInherited() throws Exception {
MyChildRule myChildRule = new MyChildRule();
RulesEngine rulesEngine = RulesEngineBuilder.aNewRulesEngine().build();
rulesEngine.registerRule(myChildRule);
rulesEngine.fireRules();
assertThat(myChildRule.isExecuted()).isTrue();
}
@org.easyrules.annotation.Rule
class MyBaseRule {
protected boolean executed;
@Condition
public boolean when() {
return true;
}
@Action
public void then() {
executed = true;
}
public boolean isExecuted() {
return executed;
}
}
class MyChildRule extends MyBaseRule {
}
}
Loading…
Cancel
Save