Search in sources :

Example 26 with JavaInputReader

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

the class ParsedJavaModelBuilderTest method testExtractionOfMethodAccessibleFields_local.

/**
 * Tests whether the input type's extracted fields are complete (including annotations and javaDoc)
 *
 * @throws Exception test fails
 * @author fkreis (08.05.2015)
 */
@Test
public void testExtractionOfMethodAccessibleFields_local() throws Exception {
    File file = new File(testFileRootPath + "TestClass.java");
    JavaInputReader javaModelBuilder = new JavaInputReader();
    try (FileReader fileReader = new FileReader(file)) {
        Map<String, Object> model = javaModelBuilder.createModel(JavaParserUtil.getFirstJavaClass(fileReader));
        // 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<String>");
        assertThat(classField.get(ModelConstant.CANONICAL_TYPE)).isEqualTo("java.util.List<java.lang.String>");
        // 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 : FileReader(java.io.FileReader) JavaInputReader(com.devonfw.cobigen.javaplugin.inputreader.JavaInputReader) File(java.io.File) Test(org.junit.Test)

Example 27 with JavaInputReader

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

the class ParsedJavaModelBuilderTest method testCorrectExtractionOfInheritedFields_input_packageFolder.

/**
 * Tests the correct extraction of 'methodAccessibleFields' for {@link PackageFolder} as input.
 */
@Test
@Ignore("see com.devonfw.cobigen.javaplugin.inputreader.ParsedJavaModelBuilder.extractMethodAccessibleFields#L115")
public void testCorrectExtractionOfInheritedFields_input_packageFolder() {
    File packageFolderFile = new File(testFileRootPath + "packageFolder");
    PackageFolder packageFolder = new PackageFolder(packageFolderFile.toURI(), RootClass.class.getPackage().getName());
    JavaInputReader javaInputReader = new JavaInputReader();
    List<Object> objects = javaInputReader.getInputObjects(packageFolder, Charsets.UTF_8);
    assertThat(objects).as("The package folder does not contain any java sources!").isNotNull();
    assertThat(objects).hasSize(2);
    boolean found = false;
    for (Object o : objects) {
        Map<String, Object> model = javaInputReader.createModel(o);
        assertThat(model).as("No model has been created!").isNotNull();
        if (RootClass.class.getSimpleName().equals(JavaModelUtil.getName(model))) {
            List<Map<String, Object>> methodAccessibleFields = JavaModelUtil.getMethodAccessibleFields(model);
            assertThat(methodAccessibleFields).isNotNull();
            assertThat(methodAccessibleFields).hasSize(3);
            Map<String, Object> field = JavaModelUtil.getMethodAccessibleField(model, "value");
            assertThat(field).as("Field 'value' not found!").isNotNull();
            assertThat(field.get(ModelConstant.NAME)).isEqualTo("value");
            assertThat(field.get(ModelConstant.TYPE)).isEqualTo("String");
            assertThat(field.get(ModelConstant.CANONICAL_TYPE)).isEqualTo("java.lang.String");
            field = JavaModelUtil.getMethodAccessibleField(model, "setterVisibleByte");
            assertThat(field).as("Field 'setterVisibleByte' not found!").isNotNull();
            assertThat(field.get(ModelConstant.NAME)).isEqualTo("setterVisibleByte");
            assertThat(field.get(ModelConstant.TYPE)).isEqualTo("byte");
            assertThat(field.get(ModelConstant.CANONICAL_TYPE)).isEqualTo("byte");
            field = JavaModelUtil.getMethodAccessibleField(model, "genericAccessible");
            assertThat(field).as("Field 'genericAccessible' not found!").isNotNull();
            assertThat(field.get(ModelConstant.NAME)).isEqualTo("genericAccessible");
            // TODO: Known Issue, this is not possible as the SuperClass2 is not in the same folder and
            // thus not parsed. Thus, due to type erasure the parametric type will be lost.
            // assertThat(field.get(ModelConstant.TYPE)).isEqualTo("List<RootClass>");
            // assertThat(field.get(ModelConstant.CANONICAL_TYPE)).isEqualTo("java.util.List<RootClass>");
            assertThat(field.get(ModelConstant.TYPE)).isEqualTo("List");
            assertThat(field.get(ModelConstant.CANONICAL_TYPE)).isEqualTo("java.util.List");
            found = true;
        }
    }
    assertTrue("Class " + RootClass.class.getName() + "could not be found as child of the package folder.", found);
}
Also used : RootClass(com.devonfw.cobigen.javaplugin.unittest.inputreader.testdata.RootClass) PackageFolder(com.devonfw.cobigen.javaplugin.inputreader.to.PackageFolder) JavaInputReader(com.devonfw.cobigen.javaplugin.inputreader.JavaInputReader) File(java.io.File) Map(java.util.Map) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 28 with JavaInputReader

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

the class ParsedJavaModelBuilderTest method testExtractionOfFields.

/**
 * Tests whether the input type's fields are extracted correctly (including annotations and javaDoc)
 *
 * @throws Exception test fails
 */
@Test
public void testExtractionOfFields() throws Exception {
    File file = new File(testFileRootPath + "TestClass.java");
    JavaInputReader javaModelBuilder = new JavaInputReader();
    try (FileReader fileReader = new FileReader(file)) {
        Map<String, Object> model = javaModelBuilder.createModel(JavaParserUtil.getFirstJavaClass(fileReader));
        Map<String, Object> classField = JavaModelUtil.getField(model, "customList");
        assertThat(classField).isNotNull();
        assertThat(classField.get(ModelConstant.NAME)).isEqualTo("customList");
        assertThat(classField.get(ModelConstant.TYPE)).isEqualTo("List<String>");
        assertThat(classField.get(ModelConstant.CANONICAL_TYPE)).isEqualTo("java.util.List<java.lang.String>");
        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 : FileReader(java.io.FileReader) JavaInputReader(com.devonfw.cobigen.javaplugin.inputreader.JavaInputReader) File(java.io.File) Test(org.junit.Test)

Example 29 with JavaInputReader

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

the class ParsedJavaModelBuilderTest method testCorrectlyExtractedGenericAttributeTypes.

/**
 * Tests whether parametric attribute types will be extracted correctly to the model
 *
 * @throws Exception test fails
 */
@Test
public void testCorrectlyExtractedGenericAttributeTypes() throws Exception {
    File file = new File(testFileRootPath + "TestClass.java");
    JavaInputReader javaModelBuilder = new JavaInputReader();
    try (FileReader fr = new FileReader(file)) {
        Map<String, Object> model = javaModelBuilder.createModel(JavaParserUtil.getFirstJavaClass(fr));
        Map<String, Object> customList = JavaModelUtil.getField(model, "customList");
        // "List<String>" is not possible to retrieve using reflection due to type erasure
        assertThat(customList.get(ModelConstant.TYPE)).isEqualTo("List<String>");
        assertThat(customList.get(ModelConstant.CANONICAL_TYPE)).isEqualTo("java.util.List<java.lang.String>");
    }
}
Also used : FileReader(java.io.FileReader) JavaInputReader(com.devonfw.cobigen.javaplugin.inputreader.JavaInputReader) File(java.io.File) Test(org.junit.Test)

Example 30 with JavaInputReader

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

the class ParsedJavaModelBuilderTest method testCorrectlyResolvedFieldTypes.

/**
 * Tests whether the type and the canonical type of a field will be extracted correctly
 *
 * @throws Exception test fails
 */
@Test
public void testCorrectlyResolvedFieldTypes() throws Exception {
    File file = new File(testFileRootPath + "Pojo.java");
    JavaInputReader javaModelBuilder = new JavaInputReader();
    try (FileReader fileReader = new FileReader(file)) {
        Map<String, Object> model = javaModelBuilder.createModel(JavaParserUtil.getFirstJavaClass(fileReader));
        Map<String, Object> customTypeField = JavaModelUtil.getField(model, "customTypeField");
        // "List<String>" is not possible to retrieve using reflection due to type erasure
        assertThat(customTypeField.get(ModelConstant.TYPE)).isEqualTo("AnyOtherType");
        assertThat(customTypeField.get(ModelConstant.CANONICAL_TYPE)).isEqualTo("com.devonfw.cobigen.javaplugin.unittest.inputreader.AnyOtherType");
    }
}
Also used : FileReader(java.io.FileReader) JavaInputReader(com.devonfw.cobigen.javaplugin.inputreader.JavaInputReader) File(java.io.File) 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