Search in sources :

Example 21 with JavaInputReader

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

the class JavaInputReaderTest method provideParsingAndReflectionModelFeatures_oneModelEmpty.

/**
 * Tests whether both features can be used when providing parsed and reflected inputs for one java class, whereas one
 * model does not provide any fields and/or methods
 *
 * @throws Exception test fails
 */
@Test
public void provideParsingAndReflectionModelFeatures_oneModelEmpty() throws Exception {
    File javaSourceFile = new File(testFileRootPath + "TestClass_empty.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();
}
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 22 with JavaInputReader

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

the class JavaInputReaderTest method provideParsingAndReflectionModelFeatures_withAnnotations.

/**
 * Tests whether both features can be used when providing parsed and reflected inputs for one java class, whereas the
 * models are equal and contain boolean values within annotations
 *
 * @throws Exception test fails
 */
@Test
public void provideParsingAndReflectionModelFeatures_withAnnotations() throws Exception {
    File javaSourceFile = new File(testFileRootPath + "TestClassWithAnnotations.java");
    Class<?> javaClass = TestClassWithAnnotations.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 : TestClassWithAnnotations(com.devonfw.cobigen.javaplugin.unittest.inputreader.testdata.TestClassWithAnnotations) FileReader(java.io.FileReader) JavaInputReader(com.devonfw.cobigen.javaplugin.inputreader.JavaInputReader) File(java.io.File) Test(org.junit.Test)

Example 23 with JavaInputReader

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

the class ReflectedJavaModelBuilderTest method testCorrectlyExtractedImplementedTypes.

/**
 * Tests whether super types (extended Type and implemented Types) will be extracted correctly to the model
 */
@Test
public void testCorrectlyExtractedImplementedTypes() {
    JavaInputReader javaModelBuilder = new JavaInputReader();
    Map<String, Object> model = javaModelBuilder.createModel(TestClass.class);
    // check whether implemented Types (interfaces) meet expectations
    List<Map<String, Object>> interfaces = JavaModelUtil.getImplementedTypes(model);
    // interface1
    assertThat(interfaces.get(0).get(ModelConstant.NAME)).isEqualTo("TestInterface1");
    assertThat(interfaces.get(0).get(ModelConstant.CANONICAL_NAME)).isEqualTo("com.devonfw.cobigen.javaplugin.unittest.inputreader.testdata.TestInterface1");
    assertThat(interfaces.get(0).get(ModelConstant.PACKAGE)).isEqualTo("com.devonfw.cobigen.javaplugin.unittest.inputreader.testdata");
    // interface2
    assertThat(interfaces.get(1).get(ModelConstant.NAME)).isEqualTo("TestInterface2");
    assertThat(interfaces.get(1).get(ModelConstant.CANONICAL_NAME)).isEqualTo("com.devonfw.cobigen.javaplugin.unittest.inputreader.testdata.TestInterface2");
    assertThat(interfaces.get(1).get(ModelConstant.PACKAGE)).isEqualTo("com.devonfw.cobigen.javaplugin.unittest.inputreader.testdata");
}
Also used : JavaInputReader(com.devonfw.cobigen.javaplugin.inputreader.JavaInputReader) Map(java.util.Map) Test(org.junit.Test)

Example 24 with JavaInputReader

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

the class ReflectedJavaModelBuilderTest method testExtractionOfMethodAccessibleFields.

/**
 * Tests the inclusion of all fields accessible by setter and getter methods into the model. This also includes
 * inherited accessible fields.
 */
@Test
public void testExtractionOfMethodAccessibleFields() {
    JavaInputReader javaInputReader = new JavaInputReader();
    Map<String, Object> model = javaInputReader.createModel(RootClass.class);
    assertThat(JavaModelUtil.getMethodAccessibleFields(model)).isNotNull();
    assertThat(JavaModelUtil.getMethodAccessibleFields(model)).hasSize(3);
    Map<String, Object> setterVisibleByteField = JavaModelUtil.getMethodAccessibleField(model, "setterVisibleByte");
    assertThat(setterVisibleByteField).isNotNull();
    assertThat(setterVisibleByteField.get(ModelConstant.NAME)).isEqualTo("setterVisibleByte");
    assertThat(setterVisibleByteField.get(ModelConstant.TYPE)).isEqualTo("byte");
    assertThat(setterVisibleByteField.get(ModelConstant.CANONICAL_TYPE)).isEqualTo("byte");
    Map<String, Object> valueField = JavaModelUtil.getMethodAccessibleField(model, "value");
    assertThat(valueField).isNotNull();
    assertThat(valueField.get(ModelConstant.NAME)).isEqualTo("value");
    assertThat(valueField.get(ModelConstant.TYPE)).isEqualTo("String");
    assertThat(valueField.get(ModelConstant.CANONICAL_TYPE)).isEqualTo("java.lang.String");
}
Also used : JavaInputReader(com.devonfw.cobigen.javaplugin.inputreader.JavaInputReader) Test(org.junit.Test)

Example 25 with JavaInputReader

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

the class ReflectedJavaModelBuilderTest method testAnnotationExtraction.

/**
 * Tests whether the input type's annotations are extracted complete
 */
@Test
public void testAnnotationExtraction() {
    JavaInputReader javaInputReader = new JavaInputReader();
    Map<String, Object> model = javaInputReader.createModel(TestClass.class);
    Map<String, Object> classField = JavaModelUtil.getMethodAccessibleField(model, "customList");
    assertThat(classField).isNotNull();
    @SuppressWarnings("unchecked") Map<String, Object> annotation = (Map<String, Object>) JavaModelUtil.getAnnotations(classField).get("com_devonfw_cobigen_javaplugin_unittest_inputreader_testdata_MyFieldAnnotation");
    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)

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