use of org.kie.api.definition.type.Annotation in project drools by kiegroup.
the class TypeDeclarationTest method testTypeDeclarationMetadata.
@Test
public void testTypeDeclarationMetadata() {
String str = "";
str += "package org.drools.compiler.test; \n" + "import org.drools.compiler.compiler.TypeDeclarationTest.KlassAnnotation; \n" + "import org.drools.compiler.compiler.TypeDeclarationTest.FieldAnnotation; \n" + "import org.drools.compiler.Person\n" + "\n" + "declare Bean \n" + "@role(event) \n" + "@expires( 1s ) \n" + "@KlassAnnotation( \"klass\" )" + "" + " name : String @key @FieldAnnotation( prop = \"fld\" )\n" + "end \n" + "declare Person @role(event) end";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
System.err.println(kbuilder.getErrors());
assertFalse(kbuilder.hasErrors());
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
kBase.addPackages(kbuilder.getKnowledgePackages());
FactType bean = kBase.getFactType("org.drools.compiler.test", "Bean");
FactType pers = kBase.getFactType("org.drools", "Person");
assertEquals("org.drools.compiler.test.Bean", bean.getName());
assertEquals("Bean", bean.getSimpleName());
assertEquals("org.drools.compiler.test", bean.getPackageName());
assertEquals(3, bean.getClassAnnotations().size());
Annotation ann = bean.getClassAnnotations().get(0);
if (!ann.getName().equals("org.drools.compiler.compiler.TypeDeclarationTest$KlassAnnotation")) {
ann = bean.getClassAnnotations().get(1);
}
if (!ann.getName().equals("org.drools.compiler.compiler.TypeDeclarationTest$KlassAnnotation")) {
ann = bean.getClassAnnotations().get(2);
}
assertEquals("org.drools.compiler.compiler.TypeDeclarationTest$KlassAnnotation", ann.getName());
assertEquals("klass", ann.getPropertyValue("value"));
assertEquals(String.class, ann.getPropertyType("value"));
assertEquals(2, bean.getMetaData().size());
assertEquals("event", bean.getMetaData().get("role"));
FactField field = bean.getField("name");
assertNotNull(field);
assertEquals(2, field.getFieldAnnotations().size());
Annotation fnn = field.getFieldAnnotations().get(0);
if (!fnn.getName().equals("org.drools.compiler.compiler.TypeDeclarationTest$FieldAnnotation")) {
fnn = field.getFieldAnnotations().get(1);
}
assertEquals("org.drools.compiler.compiler.TypeDeclarationTest$FieldAnnotation", fnn.getName());
assertEquals("fld", fnn.getPropertyValue("prop"));
assertEquals(String.class, fnn.getPropertyType("prop"));
assertEquals(1, field.getMetaData().size());
assertTrue(field.getMetaData().containsKey("key"));
}
Aggregations