Add support for custom @Rule annotations
parent
b684602e5a
commit
b64a807051
@ -0,0 +1,14 @@
|
|||||||
|
package org.easyrules.annotation;
|
||||||
|
|
||||||
|
@MetaRule
|
||||||
|
public class AnnotatedRuleWithMetaRuleAnnotation {
|
||||||
|
|
||||||
|
@Condition
|
||||||
|
public boolean when() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Action
|
||||||
|
public void then() throws Exception {
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package org.easyrules.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.TYPE)
|
||||||
|
@Rule
|
||||||
|
public @interface MetaRule {
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package org.easyrules.core;
|
||||||
|
|
||||||
|
import org.easyrules.annotation.AnnotatedRuleWithMetaRuleAnnotation;
|
||||||
|
import org.easyrules.api.Rule;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
|
public class RuleProxyTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void proxyingHappensEvenWhenRuleIsAnnotatedWithMetaRuleAnnotation() {
|
||||||
|
AnnotatedRuleWithMetaRuleAnnotation rule1 = new AnnotatedRuleWithMetaRuleAnnotation();
|
||||||
|
|
||||||
|
Rule rule = RuleProxy.asRule(rule1);
|
||||||
|
|
||||||
|
assertNotNull(rule.getDescription());
|
||||||
|
assertNotNull(rule.getName());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,72 @@
|
|||||||
|
package org.easyrules.util;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
public class UtilsTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void findAnnotationWithClassWhereAnnotationIsPresent() {
|
||||||
|
Annotation foo = Utils.findAnnotation(Foo.class, AnnotationIsPresent.class);
|
||||||
|
|
||||||
|
assertCorrectAnnotationIsFound(Foo.class, foo);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void findAnnotationWithClassWhereAnnotationIsPresentViaMetaAnnotation() {
|
||||||
|
Annotation foo = Utils.findAnnotation(Foo.class, AnnotationIsPresentViaMetaAnnotation.class);
|
||||||
|
|
||||||
|
assertCorrectAnnotationIsFound(Foo.class, foo);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void findAnnotationWithClassWhereAnnotationIsNotPresent() {
|
||||||
|
Annotation foo = Utils.findAnnotation(Foo.class, Object.class);
|
||||||
|
|
||||||
|
assertNull(foo);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isAnnotationPresentWithClassWhereAnnotationIsPresent() {
|
||||||
|
assertTrue(Utils.isAnnotationPresent(Foo.class, AnnotationIsPresent.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isAnnotationPresentWithClassWhereAnnotationIsPresentViaMetaAnnotation() {
|
||||||
|
assertTrue(Utils.isAnnotationPresent(Foo.class, AnnotationIsPresentViaMetaAnnotation.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isAnnotationPresentWithClassWhereAnnotationIsNotPresent() {
|
||||||
|
assertFalse(Utils.isAnnotationPresent(Foo.class, Object.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void assertCorrectAnnotationIsFound(
|
||||||
|
Class expectedAnnotationType, Annotation actualAnnotation) {
|
||||||
|
|
||||||
|
assertNotNull(actualAnnotation);
|
||||||
|
assertEquals(expectedAnnotationType, actualAnnotation.annotationType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.TYPE)
|
||||||
|
private @interface Foo {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.TYPE)
|
||||||
|
@Foo
|
||||||
|
private @interface MetaFoo {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Foo
|
||||||
|
private static final class AnnotationIsPresent {
|
||||||
|
}
|
||||||
|
|
||||||
|
@MetaFoo
|
||||||
|
private static final class AnnotationIsPresentViaMetaAnnotation {
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue