Search in sources :

Example 1 with JavaInputReader

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

the class JavaInputReaderTest method testGetInputObjectsRecursively_resultOrder.

/**
 * Test if the method input list returned by {@link JavaInputReader#getInputObjectsRecursively(Object, Charset)} has a
 * proper order to match the expected preview at any time
 */
@Test
public void testGetInputObjectsRecursively_resultOrder() {
    File javaSourceFile = new File(testFileRootPath + "packageFolder");
    PackageFolder pkg = new PackageFolder(javaSourceFile.toURI(), "notOfInterest");
    assertThat(pkg).isNotNull();
    JavaInputReader input = new JavaInputReader();
    List<Object> list = input.getInputObjectsRecursively(pkg, Charsets.UTF_8);
    assertThat(list).hasSize(3);
    List<String> simpleNames = Lists.newArrayList();
    for (Object o : list) {
        if (o instanceof JavaClass) {
            simpleNames.add(((JavaClass) o).getName());
        }
    }
    assertThat(simpleNames).containsExactlyInAnyOrder("RootClass", "SuperClass1", "SuperClass2");
}
Also used : JavaClass(com.thoughtworks.qdox.model.JavaClass) PackageFolder(com.devonfw.cobigen.javaplugin.inputreader.to.PackageFolder) JavaInputReader(com.devonfw.cobigen.javaplugin.inputreader.JavaInputReader) File(java.io.File) Test(org.junit.Test)

Example 2 with JavaInputReader

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

the class JavaInputReaderTest method provideParsingAndReflectionModelFeatures.

/**
 * Tests whether both features can be used when providing parsed and reflected inputs for one java class
 *
 * @throws Exception test fails
 */
@Test
public void provideParsingAndReflectionModelFeatures() throws Exception {
    File javaSourceFile = new File(testFileRootPath + "TestClass.java");
    Class<?> javaClass = TestClass.class;
    JavaInputReader javaInputReader = new JavaInputReader();
    Map<String, Object> model = javaInputReader.createModel(new Object[] { JavaParserUtil.getFirstJavaClass(new FileReader(javaSourceFile)), javaClass });
    assertThat(model).as("No model has been created!").isNotNull();
    // Check parser feature (resolving of parametric type variables)
    Map<String, Object> fieldAttributes = JavaModelUtil.getField(model, "customList");
    assertThat(fieldAttributes.get(ModelConstant.TYPE)).as("Parametric types are not be resolved correctly!").isEqualTo("List<String>");
}
Also used : TestClass(com.devonfw.cobigen.javaplugin.unittest.inputreader.testdata.TestClass) FileReader(java.io.FileReader) JavaInputReader(com.devonfw.cobigen.javaplugin.inputreader.JavaInputReader) File(java.io.File) Test(org.junit.Test)

Example 3 with JavaInputReader

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

the class JavaInputReaderTest method testExtractionOfFields.

/**
 * Test method for {@link JavaInputReader#createModel(Object)}. Checks whether the model includes all field
 * information (especially annotations) if two inputs (.java and .class) are used.
 *
 * @throws Exception test fails
 */
@Test
public void testExtractionOfFields() throws Exception {
    // create instance
    JavaInputReader reader = new JavaInputReader();
    // create test data
    File file = new File(testFileRootPath + "TestClass.java");
    JavaClass parsedJavaClass = JavaParserUtil.getFirstJavaClass(new FileReader(file));
    Class<?> reflectedJavaClass = TestClass.class;
    Object[] inputArray = new Object[2];
    inputArray[0] = parsedJavaClass;
    inputArray[1] = reflectedJavaClass;
    Map<String, Object> model = reader.createModel(inputArray);
    // validate
    // test field annotations
    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");
    // test local field of method accessible fields
    Map<String, Object> classFieldAccessible = JavaModelUtil.getMethodAccessibleField(model, "customList");
    assertThat(classFieldAccessible).isNotNull();
    assertThat(classFieldAccessible.get(ModelConstant.NAME)).isEqualTo("customList");
    assertThat(classFieldAccessible.get(ModelConstant.TYPE)).isEqualTo("List<String>");
    assertThat(classFieldAccessible.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(classFieldAccessible.get("isId")).isEqualTo("false");
    // test annotations for attribute, getter, setter, is-method
    assertThat(classFieldAccessible.get(ModelConstant.ANNOTATIONS)).isNotNull();
    // getter
    assertThat(JavaModelUtil.getAnnotations(classFieldAccessible)).containsKey("com_devonfw_cobigen_javaplugin_unittest_inputreader_testdata_MyGetterAnnotation");
    // Setter
    assertThat(JavaModelUtil.getAnnotations(classFieldAccessible)).containsKey("com_devonfw_cobigen_javaplugin_unittest_inputreader_testdata_MySetterAnnotation");
    // is-method
    assertThat(JavaModelUtil.getAnnotations(classFieldAccessible)).containsKey("com_devonfw_cobigen_javaplugin_unittest_inputreader_testdata_MyIsAnnotation");
    // attribute
    assertThat(JavaModelUtil.getAnnotations(classFieldAccessible)).containsKey("com_devonfw_cobigen_javaplugin_unittest_inputreader_testdata_MyFieldAnnotation");
    // test inherited field of method accessible fields
    Map<String, Object> inheritedField = JavaModelUtil.getMethodAccessibleField(model, "id");
    assertThat(inheritedField).isNotNull();
    assertThat(inheritedField.get(ModelConstant.NAME)).isEqualTo("id");
    // TODO qDox library returns full qualified names for the superclass' fields
    /*
     * assertThat(inheritedField.get(ModelConstant.TYPE)).isEqualTo("Long");
     */
    assertThat(inheritedField.get(ModelConstant.CANONICAL_TYPE)).isEqualTo("java.lang.Long");
    // is deprecated, so its not necessary to test here
    // assertThat(inheritedField.get("isId")).isEqualTo("false");
    // currently no javadoc provided
    // assertThat(inheritedField.get(ModelConstant.JAVADOC)).isNotNull();
    // assertThat(JavaModelUtil.getJavaDocModel(inheritedField).get("comment")).isEqualTo("Example
    // JavaDoc");
    // test annotations for attribute, getter, setter, is-method
    assertThat(inheritedField.get(ModelConstant.ANNOTATIONS)).isNotNull();
    // getter
    assertThat(JavaModelUtil.getAnnotations(inheritedField)).containsKey("com_devonfw_cobigen_javaplugin_unittest_inputreader_testdata_MySuperTypeGetterAnnotation");
    // Setter
    assertThat(JavaModelUtil.getAnnotations(inheritedField)).containsKey("com_devonfw_cobigen_javaplugin_unittest_inputreader_testdata_MySuperTypeSetterAnnotation");
    // is-method
    assertThat(JavaModelUtil.getAnnotations(inheritedField)).containsKey("com_devonfw_cobigen_javaplugin_unittest_inputreader_testdata_MySuperTypeIsAnnotation");
    // attribute
    assertThat(JavaModelUtil.getAnnotations(inheritedField)).containsKey("com_devonfw_cobigen_javaplugin_unittest_inputreader_testdata_MySuperTypeFieldAnnotation");
}
Also used : JavaClass(com.thoughtworks.qdox.model.JavaClass) FileReader(java.io.FileReader) TestClass(com.devonfw.cobigen.javaplugin.unittest.inputreader.testdata.TestClass) JavaInputReader(com.devonfw.cobigen.javaplugin.inputreader.JavaInputReader) File(java.io.File) Test(org.junit.Test)

Example 4 with JavaInputReader

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

the class JavaInputReaderTest method provideParsingAndReflectionModelFeatures_withRecursiveAnnotations.

/**
 * Tests whether both features can be used when providing parsed and reflected inputs for one java class, whereas the
 * models are equal and contain recursive annotations
 *
 * @throws Exception test fails
 */
@Test
public void provideParsingAndReflectionModelFeatures_withRecursiveAnnotations() throws Exception {
    File javaSourceFile = new File(testFileRootPath + "TestClassWithRecursiveAnnotations.java");
    Class<?> javaClass = TestClassWithRecursiveAnnotations.class;
    JavaInputReader javaInputReader = new JavaInputReader();
    Map<String, Object> model = javaInputReader.createModel(new Object[] { JavaParserUtil.getFirstJavaClass(new FileReader(javaSourceFile)), javaClass });
    assertThat(model).as("No model has been created!").isNotNull();
}
Also used : TestClassWithRecursiveAnnotations(com.devonfw.cobigen.javaplugin.unittest.inputreader.testdata.TestClassWithRecursiveAnnotations) FileReader(java.io.FileReader) JavaInputReader(com.devonfw.cobigen.javaplugin.inputreader.JavaInputReader) File(java.io.File) Test(org.junit.Test)

Example 5 with JavaInputReader

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

the class ReflectedJavaModelBuilderTest method testExtractionOfFields.

/**
 * Tests whether the input type's fields are extracted correctly (including annotations and javaDoc)
 */
@Test
public void testExtractionOfFields() {
    JavaInputReader javaInputReader = new JavaInputReader();
    Map<String, Object> model = javaInputReader.createModel(TestClass.class);
    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<?>");
    assertThat(classField.get(ModelConstant.CANONICAL_TYPE)).isEqualTo("java.util.List");
    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");
    // 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");
    // Setter
    assertThat(JavaModelUtil.getAnnotations(classField)).containsKey("com_devonfw_cobigen_javaplugin_unittest_inputreader_testdata_MySetterAnnotation");
}
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