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");
}
}
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);
}
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");
}
}
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>");
}
}
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");
}
}
Aggregations