use of org.kie.workbench.common.services.datamodeller.core.JavaClass in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriverTest method createNestedClassUpdateTestResult.
private DataObject createNestedClassUpdateTestResult() {
DataObject dataObject = createDataObject("org.kie.workbench.common.services.datamodeller.driver.package5", "NestedClassUpdateTestResult", null);
JavaClass nestedClass = new JavaClassImpl("", "UpdatedNestedClass");
nestedClass.addInterface("java.io.Serializable");
nestedClass.addInterface("ParametrizedInterface<Double>");
Annotation generatedAnnotation = createAnnotation(Generated.class);
generatedAnnotation.setValue("value", Arrays.asList("foo.bar.Generator"));
nestedClass.addAnnotation(generatedAnnotation);
MethodImpl method = new MethodImpl("updatedMethod", Collections.EMPTY_LIST, "", new TypeImpl(void.class.getName()), Visibility.PUBLIC);
generatedAnnotation = createAnnotation(Generated.class);
generatedAnnotation.setValue("value", Arrays.asList("foo.bar.Generator"));
method.addAnnotation(generatedAnnotation);
nestedClass.addMethod(method);
dataObject.addNestedClass(nestedClass);
return dataObject;
}
use of org.kie.workbench.common.services.datamodeller.core.JavaClass in project kie-wb-common by kiegroup.
the class JavaClassImpl method addNestedClass.
@Override
public JavaClass addNestedClass(JavaClass javaClass) {
if (javaClass == null) {
return null;
}
Iterator<JavaClass> iterator = nestedClasses.listIterator();
while (iterator.hasNext()) {
JavaClass nestedClass = iterator.next();
if (nestedClass.getName().equals(javaClass.getName())) {
iterator.remove();
break;
}
}
nestedClasses.add(javaClass);
return javaClass;
}
use of org.kie.workbench.common.services.datamodeller.core.JavaClass in project kie-wb-common by kiegroup.
the class DataModelerAssert method assertEqualsNestedClasses.
public static void assertEqualsNestedClasses(List<JavaClass> methods1, List<JavaClass> methods2) {
if (methods1 != null) {
assertNotNull(methods2);
assertEquals(methods1.size(), methods2.size());
Map<String, JavaClass> methodMap = new HashMap<>();
for (JavaClass method : methods2) {
methodMap.put(method.getName(), method);
}
for (JavaClass method : methods1) {
assertEqualsJavaClass(method, methodMap.get(method.getName()));
}
} else {
assertNull(methods2);
}
}
use of org.kie.workbench.common.services.datamodeller.core.JavaClass in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriverTest method nestedClassUpdateTest.
@Test
public void nestedClassUpdateTest() {
try {
// First read the NestedClassUpdateTest
Path nestedClassUpdateTestFilePath = rootPath.resolve("package5").resolve("NestedClassUpdateTest.java");
String source = ioService.readAllString(nestedClassUpdateTestFilePath);
JavaClassSource nestedClassUpdateTestJavaClassSource = (JavaClassSource) Roaster.parse(source);
ClassLoader classLoader = getClass().getClassLoader();
ClassTypeResolver classTypeResolver = DriverUtils.createClassTypeResolver(nestedClassUpdateTestJavaClassSource, classLoader);
final SourceFilter pojo1Filter = javaType -> false;
final NestedClassFilter nestedClassFilter = javaType -> javaType.isClass() && javaType.getAnnotation(Generated.class) != null;
final MethodFilter methodFilter = method -> !method.isConstructor() && method.getAnnotation(Generated.class) != null;
FilterHolder filterHolder = mock(FilterHolder.class);
when(filterHolder.getSourceFilters()).thenReturn(Collections.singleton(pojo1Filter));
when(filterHolder.getNestedClassFilters()).thenReturn(Collections.singleton(nestedClassFilter));
when(filterHolder.getMethodFilters()).thenReturn(Collections.singleton(methodFilter));
JavaRoasterModelDriver javaRoasterModelDriver = new JavaRoasterModelDriver(ioService, rootPath, classLoader, filterHolder);
ModelDriverResult result = javaRoasterModelDriver.loadDataObject(source, nestedClassUpdateTestFilePath);
// 1) read the NestedClassUpdateTest
DataObject nestedClassUpdateTest = result.getDataModel().getDataObject("org.kie.workbench.common.services.datamodeller.driver.package5.NestedClassUpdateTest");
// 2) modify the NestedClassUpdateTest according to the expected result
JavaClass nestedClass = nestedClassUpdateTest.getNestedClasses().stream().filter(t -> t.getName().equals("NestedClass")).findFirst().get();
assertNotNull(nestedClass);
nestedClass.setName("UpdatedNestedClass");
String removedInterface = nestedClass.removeInterface("ParametrizedInterface<Integer>");
assertNotNull(removedInterface);
nestedClass.addInterface("ParametrizedInterface<Double>");
Method method = nestedClass.getMethod("method", Collections.EMPTY_LIST);
assertNotNull(method);
method.setName("updatedMethod");
nestedClassUpdateTest.setName("NestedClassUpdateTestResult");
// 3) compare the modified data object with the expected data object.
Path expectedFilePath = rootPath.resolve("package5").resolve("NestedClassUpdateTestResult.java");
String expectedSource = ioService.readAllString(expectedFilePath);
JavaClassSource expectedJavaClassSource = (JavaClassSource) Roaster.parse(expectedSource);
ModelDriverResult expectedResult = javaRoasterModelDriver.loadDataObject(expectedSource, expectedFilePath);
DataObject nestedClassUpdateTestResult = expectedResult.getDataModel().getDataObject("org.kie.workbench.common.services.datamodeller.driver.package5.NestedClassUpdateTestResult");
// First check, the modified data object in memory should be the same as the readed from the model.
DataModelerAssert.assertEqualsDataObject(nestedClassUpdateTestResult, nestedClassUpdateTest);
// Second check, update the JavaClassSource corresponding to the NestedClassUpdateTest
javaRoasterModelDriver.updateSource(nestedClassUpdateTestJavaClassSource, nestedClassUpdateTest, new UpdateInfo(), classTypeResolver);
ModelDriverResult updatedResult = javaRoasterModelDriver.loadDataObject(nestedClassUpdateTestJavaClassSource.toString(), nestedClassUpdateTestFilePath);
// and now compare the updatedResult with the expected value.
DataModelerAssert.assertEqualsDataObject(nestedClassUpdateTestResult, updatedResult.getDataModel().getDataObject("org.kie.workbench.common.services.datamodeller.driver.package5.NestedClassUpdateTestResult"));
logger.debug(nestedClassUpdateTestJavaClassSource.toString());
} catch (Exception e) {
e.printStackTrace();
fail("Test failed: " + e.getMessage());
}
}
use of org.kie.workbench.common.services.datamodeller.core.JavaClass in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriverTest method createNestedClassUpdateTest.
private DataObject createNestedClassUpdateTest() {
DataObject dataObject = createDataObject("org.kie.workbench.common.services.datamodeller.driver.package5", "NestedClassUpdateTest", null);
JavaClass nestedClass = new JavaClassImpl("", "NestedClass");
nestedClass.addInterface("java.io.Serializable");
nestedClass.addInterface("ParametrizedInterface<Integer>");
Annotation generatedAnnotation = createAnnotation(Generated.class);
generatedAnnotation.setValue("value", Arrays.asList("foo.bar.Generator"));
nestedClass.addAnnotation(generatedAnnotation);
MethodImpl method = new MethodImpl("method", Collections.EMPTY_LIST, "", new TypeImpl(void.class.getName()), Visibility.PUBLIC);
generatedAnnotation = createAnnotation(Generated.class);
generatedAnnotation.setValue("value", Arrays.asList("foo.bar.Generator"));
method.addAnnotation(generatedAnnotation);
nestedClass.addMethod(method);
dataObject.addNestedClass(nestedClass);
return dataObject;
}
Aggregations