Search in sources :

Example 6 with JavaInputReader

use of com.devonfw.cobigen.javaplugin.inputreader.JavaInputReader in project cobigen by devonfw.

the class ReflectedJavaModelBuilderTest method testAnnotationExtractionOfSuperSuperTypes.

/**
 * Tests whether the annotation of super super types are available
 */
@Test
public void testAnnotationExtractionOfSuperSuperTypes() {
    JavaInputReader javaInputReader = new JavaInputReader();
    Map<String, Object> model = javaInputReader.createModel(TestClass.class);
    Map<String, Object> classField = JavaModelUtil.getMethodAccessibleField(model, "superSuperString");
    assertThat(classField).isNotNull();
    // test annotations for attribute, getter, setter, is-method
    assertThat(classField.get(ModelConstant.ANNOTATIONS)).isNotNull();
    // getter
    assertThat(JavaModelUtil.getAnnotations(classField)).containsKey("com_devonfw_cobigen_javaplugin_unittest_inputreader_testdata_MySuperSuperTypeGetterAnnotation");
    // Setter
    assertThat(JavaModelUtil.getAnnotations(classField)).containsKey("com_devonfw_cobigen_javaplugin_unittest_inputreader_testdata_MySuperSuperTypeSetterAnnotation");
    // is-method
    assertThat(JavaModelUtil.getAnnotations(classField)).containsKey("com_devonfw_cobigen_javaplugin_unittest_inputreader_testdata_MySuperSuperTypeIsAnnotation");
    // attribute
    assertThat(JavaModelUtil.getAnnotations(classField)).containsKey("com_devonfw_cobigen_javaplugin_unittest_inputreader_testdata_MySuperSuperTypeFieldAnnotation");
    @SuppressWarnings("unchecked") Map<String, Object> annotation = (Map<String, Object>) JavaModelUtil.getAnnotations(classField).get("com_devonfw_cobigen_javaplugin_unittest_inputreader_testdata_MySuperSuperTypeFieldAnnotation");
    assertThat(annotation).isNotNull();
    // currently all values are provided as Strings
    assertThat(annotation.get("b")).isEqualTo("0");
    assertThat(annotation.get("s")).isEqualTo("1");
    assertThat(annotation.get("i")).isEqualTo("2");
    assertThat(annotation.get("l")).isEqualTo("3");
    assertThat(annotation.get("f")).isEqualTo("4.0");
    assertThat(annotation.get("d")).isEqualTo("5.0");
    assertThat(annotation.get("c")).isEqualTo("c");
    assertThat(annotation.get("bool")).isEqualTo("true");
    assertThat(annotation.get("str")).isEqualTo("TestString");
}
Also used : JavaInputReader(com.devonfw.cobigen.javaplugin.inputreader.JavaInputReader) Map(java.util.Map) Test(org.junit.Test)

Example 7 with JavaInputReader

use of com.devonfw.cobigen.javaplugin.inputreader.JavaInputReader in project cobigen by devonfw.

the class ReflectedJavaModelBuilderTest method testCorrectlyExtractedAttributeTypes.

/**
 * Tests whether parametric attribute types will be extracted correctly to the model
 */
@Test
@SuppressWarnings("null")
public void testCorrectlyExtractedAttributeTypes() {
    JavaInputReader javaModelBuilder = new JavaInputReader();
    Map<String, Object> model = javaModelBuilder.createModel(getClass());
    Map<String, Object> pojoMap = JavaModelUtil.getRoot(model);
    assertThat(pojoMap).as(ModelConstant.MODEL_ROOT + " is not accessible in model").isNotNull();
    List<Map<String, Object>> attributes = JavaModelUtil.getFields(model);
    assertThat(attributes).as(ModelConstant.FIELDS + " is not accessible in model").isNotNull();
    Map<String, Object> parametricTestAttributeModel = null;
    for (Map<String, Object> attr : attributes) {
        if ("parametricTestAttribute".equals(attr.get(ModelConstant.NAME))) {
            parametricTestAttributeModel = attr;
            break;
        }
    }
    assertThat(parametricTestAttributeModel).as("There is no field with name 'parametricTestAttribute' in the model").isNotNull();
    // "List<String>" is not possible to retrieve using reflection due to type erasure
    assertThat(parametricTestAttributeModel.get(ModelConstant.TYPE)).isEqualTo("List<?>");
}
Also used : JavaInputReader(com.devonfw.cobigen.javaplugin.inputreader.JavaInputReader) Map(java.util.Map) Test(org.junit.Test)

Example 8 with JavaInputReader

use of com.devonfw.cobigen.javaplugin.inputreader.JavaInputReader in project cobigen by devonfw.

the class ReflectedJavaModelBuilderTest method testExtractionOfMethodAccessibleFields_local.

/**
 * Tests whether the input type's extracted fields are complete (including annotations and javaDoc)
 */
@Test
public void testExtractionOfMethodAccessibleFields_local() {
    JavaInputReader javaInputReader = new JavaInputReader();
    Map<String, Object> model = javaInputReader.createModel(TestClass.class);
    // test local field
    Map<String, Object> classField = JavaModelUtil.getMethodAccessibleField(model, "customList");
    assertThat(classField).isNotNull();
    assertThat(classField.get(ModelConstant.NAME)).isEqualTo("customList");
    assertThat(classField.get(ModelConstant.TYPE)).isEqualTo("List<?>");
    assertThat(classField.get(ModelConstant.CANONICAL_TYPE)).isEqualTo("java.util.List");
    // currently no javadoc provided
    // assertThat(classField.get(ModelConstant.JAVADOC)).isNotNull();
    // assertThat(JavaModelUtil.getJavaDocModel(classField).get("comment")).isEqualTo("Example JavaDoc");
    assertThat(classField.get("isId")).isEqualTo("false");
    // test annotations for attribute, getter, setter, is-method
    assertThat(classField.get(ModelConstant.ANNOTATIONS)).isNotNull();
    // getter
    assertThat(JavaModelUtil.getAnnotations(classField)).containsKey("com_devonfw_cobigen_javaplugin_unittest_inputreader_testdata_MyGetterAnnotation");
    // Setter
    assertThat(JavaModelUtil.getAnnotations(classField)).containsKey("com_devonfw_cobigen_javaplugin_unittest_inputreader_testdata_MySetterAnnotation");
    // is-method
    assertThat(JavaModelUtil.getAnnotations(classField)).containsKey("com_devonfw_cobigen_javaplugin_unittest_inputreader_testdata_MyIsAnnotation");
    // attribute
    assertThat(JavaModelUtil.getAnnotations(classField)).containsKey("com_devonfw_cobigen_javaplugin_unittest_inputreader_testdata_MyFieldAnnotation");
}
Also used : JavaInputReader(com.devonfw.cobigen.javaplugin.inputreader.JavaInputReader) Test(org.junit.Test)

Example 9 with JavaInputReader

use of com.devonfw.cobigen.javaplugin.inputreader.JavaInputReader in project cobigen by devonfw.

the class ReflectedJavaModelBuilderTest method testCorrectlyExtractingMethodsInInterfaceInheritance.

/**
 * Tests whether inherited methods will be resolved as well.
 *
 * @throws Exception test fails
 */
@Test
public void testCorrectlyExtractingMethodsInInterfaceInheritance() throws Exception {
    JavaInputReader javaModelBuilder = new JavaInputReader();
    Map<String, Object> model = javaModelBuilder.createModel(TestInterfaceInheritance.class);
    assertThat(JavaModelUtil.getMethods(model)).hasSize(2);
}
Also used : JavaInputReader(com.devonfw.cobigen.javaplugin.inputreader.JavaInputReader) Test(org.junit.Test)

Example 10 with JavaInputReader

use of com.devonfw.cobigen.javaplugin.inputreader.JavaInputReader in project cobigen by devonfw.

the class ReflectedJavaModelBuilderTest method testCorrectlyExtractedInheritedType.

/**
 * Tests whether the inherited type will be correctly extracted and put into the model
 */
@Test
public void testCorrectlyExtractedInheritedType() {
    JavaInputReader javaModelBuilder = new JavaInputReader();
    Map<String, Object> model = javaModelBuilder.createModel(TestClass.class);
    assertThat(JavaModelUtil.getExtendedType(model).get(ModelConstant.NAME)).isEqualTo("AbstractTestClass");
    assertThat(JavaModelUtil.getExtendedType(model).get(ModelConstant.CANONICAL_NAME)).isEqualTo("com.devonfw.cobigen.javaplugin.unittest.inputreader.testdata.AbstractTestClass");
    assertThat(JavaModelUtil.getExtendedType(model).get(ModelConstant.PACKAGE)).isEqualTo("com.devonfw.cobigen.javaplugin.unittest.inputreader.testdata");
}
Also used : JavaInputReader(com.devonfw.cobigen.javaplugin.inputreader.JavaInputReader) Test(org.junit.Test)

Aggregations

JavaInputReader (com.devonfw.cobigen.javaplugin.inputreader.JavaInputReader)30 Test (org.junit.Test)30 File (java.io.File)19 FileReader (java.io.FileReader)17 Map (java.util.Map)7 TestClass (com.devonfw.cobigen.javaplugin.unittest.inputreader.testdata.TestClass)3 Ignore (org.junit.Ignore)3 PackageFolder (com.devonfw.cobigen.javaplugin.inputreader.to.PackageFolder)2 JavaClass (com.thoughtworks.qdox.model.JavaClass)2 RootClass (com.devonfw.cobigen.javaplugin.unittest.inputreader.testdata.RootClass)1 TestClassWithAnnotations (com.devonfw.cobigen.javaplugin.unittest.inputreader.testdata.TestClassWithAnnotations)1 TestClassWithAnnotationsContainingObjectArrays (com.devonfw.cobigen.javaplugin.unittest.inputreader.testdata.TestClassWithAnnotationsContainingObjectArrays)1 TestClassWithRecursiveAnnotations (com.devonfw.cobigen.javaplugin.unittest.inputreader.testdata.TestClassWithRecursiveAnnotations)1