Search in sources :

Example 1 with Key

use of org.kie.api.definition.type.Key in project drools by kiegroup.

the class AnnotationsTest method annotationTest.

@Test
public void annotationTest() {
    String drl = "package org.drools.compiler.test;\n " + "" + "import org.kie.api.definition.type.Position; \n " + "import " + AnnotationsTest.class.getCanonicalName() + "; \n" + "import " + AnnotationsTest.Annot.class.getCanonicalName() + "; \n" + "" + "declare AnnotatedBean \n" + " @Deprecated \n" + "" + " @Annot( intProp=7 " + "         ,typeProp=String.class " + "         ,strProp=\"hello world\" " + "         ,enumProp=AnnPropEnum.THREE " + "         ,dblArrProp={1.0,2.0} " + "         ,typeArrProp={String.class, AnnotationsTest.class} " + "         ,strArrProp={\"x1\",\"x2\"} " + "         ,enumArrProp={AnnPropEnum.ONE, AnnPropEnum.THREE} " + "         ) \n " + " \n " + " @role(event) \n " + " " + " age : int \n" + " name : String      @key    @Position(0)    @Deprecated \n" + " end \n " + " " + " \n\n" + " " + "declare SecondBean \n " + " @NonexistingAnnotation" + "  \n" + " field : String @Annot \n" + "end \n";
    KieBase kbase = loadKnowledgeBaseFromString(drl);
    Class clazz = kbase.getFactType("org.drools.compiler.test", "AnnotatedBean").getFactClass();
    assertNotNull(clazz);
    try {
        Field fld = clazz.getDeclaredField("name");
        assertEquals(3, fld.getAnnotations().length);
        assertNotNull(fld.getAnnotation(Deprecated.class));
        assertNotNull(fld.getAnnotation(Position.class));
        assertNotNull(fld.getAnnotation(Key.class));
        Position pos = fld.getAnnotation(Position.class);
        assertEquals(0, pos.value());
    } catch (NoSuchFieldException nsfe) {
        fail("field name has not been generated correctly : " + nsfe.getMessage());
    }
    Annotation[] anns = clazz.getAnnotations();
    assertEquals(3, anns.length);
    assertNotNull(clazz.getAnnotation(Deprecated.class));
    assertNotNull(clazz.getAnnotation(Annot.class));
    assertNotNull(clazz.getAnnotation(Role.class));
    Annot ann = (Annot) clazz.getAnnotation(Annot.class);
    assertEquals(7, ann.intProp());
    assertEquals(String.class, ann.typeProp());
    assertEquals("hello world", ann.strProp());
    assertEquals(AnnPropEnum.THREE, ann.enumProp());
    assertArrayEquals(new double[] { 1.0, 2.0 }, ann.dblArrProp(), 1e-16);
    assertArrayEquals(new Class[] { String.class, AnnotationsTest.class }, ann.typeArrProp());
    assertArrayEquals(new String[] { "x1", "x2" }, ann.strArrProp());
    assertArrayEquals(new AnnPropEnum[] { AnnPropEnum.ONE, AnnPropEnum.THREE }, ann.enumArrProp());
    Class clazz2 = kbase.getFactType("org.drools.compiler.test", "SecondBean").getFactClass();
    assertNotNull(clazz2);
    Annotation[] anns2 = clazz2.getAnnotations();
    assertEquals(0, anns2.length);
    Annot ann2 = null;
    try {
        Field fld2 = clazz2.getDeclaredField("field");
        assertEquals(1, fld2.getAnnotations().length);
        assertNotNull(fld2.getAnnotation(Annot.class));
        ann2 = fld2.getAnnotation(Annot.class);
    } catch (NoSuchFieldException nsfe) {
        fail("field name has not been generated correctly : " + nsfe.getMessage());
    }
    assertNotNull(ann2);
    assertEquals(0, ann2.intProp());
    assertEquals("foo", ann2.strProp());
    assertEquals(AnnPropEnum.ONE, ann2.enumProp());
    assertArrayEquals(new double[] { 0.4, 0.5 }, ann2.dblArrProp(), 1e-16);
    assertArrayEquals(new String[] { "a", "b", "c" }, ann2.strArrProp());
    assertArrayEquals(new AnnPropEnum[] { AnnPropEnum.TWO, AnnPropEnum.THREE }, ann2.enumArrProp());
}
Also used : Role(org.kie.api.definition.type.Role) Field(java.lang.reflect.Field) Position(org.kie.api.definition.type.Position) KieBase(org.kie.api.KieBase) Key(org.kie.api.definition.type.Key) Annotation(java.lang.annotation.Annotation) Test(org.junit.Test)

Aggregations

Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Test (org.junit.Test)1 KieBase (org.kie.api.KieBase)1 Key (org.kie.api.definition.type.Key)1 Position (org.kie.api.definition.type.Position)1 Role (org.kie.api.definition.type.Role)1