From 2e72286120627a9999b7138e5a122ba95dfa09d0 Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Wed, 10 May 2017 00:40:07 +0200 Subject: [PATCH] issue #66 : add Inherited annotation to support base (abstract) classes as rules --- .../java/org/easyrules/annotation/Action.java | 7 +- .../org/easyrules/annotation/Condition.java | 7 +- .../org/easyrules/annotation/Priority.java | 7 +- .../java/org/easyrules/annotation/Rule.java | 7 +- .../org/easyrules/EasyRulesTestSuite.java | 1 + .../core/AnnotationInheritanceTest.java | 64 +++++++++++++++++++ 6 files changed, 77 insertions(+), 16 deletions(-) create mode 100644 easyrules-core/src/test/java/org/easyrules/core/AnnotationInheritanceTest.java diff --git a/easyrules-core/src/main/java/org/easyrules/annotation/Action.java b/easyrules-core/src/main/java/org/easyrules/annotation/Action.java index 01af94f..a7e406a 100644 --- a/easyrules-core/src/main/java/org/easyrules/annotation/Action.java +++ b/easyrules-core/src/main/java/org/easyrules/annotation/Action.java @@ -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 { diff --git a/easyrules-core/src/main/java/org/easyrules/annotation/Condition.java b/easyrules-core/src/main/java/org/easyrules/annotation/Condition.java index 302b310..fdafc57 100644 --- a/easyrules-core/src/main/java/org/easyrules/annotation/Condition.java +++ b/easyrules-core/src/main/java/org/easyrules/annotation/Condition.java @@ -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 { diff --git a/easyrules-core/src/main/java/org/easyrules/annotation/Priority.java b/easyrules-core/src/main/java/org/easyrules/annotation/Priority.java index b0ddd71..ca58789 100644 --- a/easyrules-core/src/main/java/org/easyrules/annotation/Priority.java +++ b/easyrules-core/src/main/java/org/easyrules/annotation/Priority.java @@ -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 { diff --git a/easyrules-core/src/main/java/org/easyrules/annotation/Rule.java b/easyrules-core/src/main/java/org/easyrules/annotation/Rule.java index b580083..c8abc05 100644 --- a/easyrules-core/src/main/java/org/easyrules/annotation/Rule.java +++ b/easyrules-core/src/main/java/org/easyrules/annotation/Rule.java @@ -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 { diff --git a/easyrules-core/src/test/java/org/easyrules/EasyRulesTestSuite.java b/easyrules-core/src/test/java/org/easyrules/EasyRulesTestSuite.java index 663e184..fb0cc73 100644 --- a/easyrules-core/src/test/java/org/easyrules/EasyRulesTestSuite.java +++ b/easyrules-core/src/test/java/org/easyrules/EasyRulesTestSuite.java @@ -35,6 +35,7 @@ import org.junit.runners.Suite; */ @RunWith(Suite.class) @Suite.SuiteClasses({ + AnnotationInheritanceTest.class, BasicRuleTest.class, RulePriorityThresholdTest.class, SkipOnFirstAppliedRuleTest.class, diff --git a/easyrules-core/src/test/java/org/easyrules/core/AnnotationInheritanceTest.java b/easyrules-core/src/test/java/org/easyrules/core/AnnotationInheritanceTest.java new file mode 100644 index 0000000..b1bc5e9 --- /dev/null +++ b/easyrules-core/src/test/java/org/easyrules/core/AnnotationInheritanceTest.java @@ -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 { + + } +}