use of com.vaadin.generator.metadata.ComponentPropertyBaseData in project flow by vaadin.
the class ComponentGeneratorTest method classContainsEventWithObjectParameter_generatedClassContainsInnerClass.
@Test
public void classContainsEventWithObjectParameter_generatedClassContainsInnerClass() {
// note: the tests for the nested class are covered by the
// NestedClassGeneratorTest
ComponentObjectTypeInnerType stringObjectType = new ComponentObjectTypeInnerType();
stringObjectType.setName("internalString");
stringObjectType.setType(Collections.singletonList(ComponentBasicType.STRING));
ComponentObjectType objectType = new ComponentObjectType();
objectType.setInnerTypes(Collections.singletonList(stringObjectType));
ComponentPropertyBaseData eventData = new ComponentPropertyBaseData();
eventData.setName("details");
eventData.setObjectType(Collections.singletonList(objectType));
ComponentEventData event = new ComponentEventData();
event.setName("something-happened");
event.setProperties(Collections.singletonList(eventData));
componentMetadata.setEvents(Collections.singletonList(event));
String generatedClass = generator.generateClass(componentMetadata, "com.my.test", null);
generatedClass = ComponentGeneratorTestUtils.removeIndentation(generatedClass);
Assert.assertTrue("Generated class should contain the SomethingHappenedDetails nested class", generatedClass.contains("public static class SomethingHappenedDetails implements JsonSerializable"));
Assert.assertTrue("Generated class should contain the addSomethingChangHappenedListener method", generatedClass.contains("public Registration addSomethingHappenedListener( ComponentEventListener<SomethingHappenedEvent<R>> listener)"));
int indexOfEventDeclaration = generatedClass.indexOf("public static class SomethingHappenedEvent<R extends MyComponent<R>> extends ComponentEvent<R> {");
int endIndexOfEventDeclaration = generatedClass.indexOf("} }", indexOfEventDeclaration);
String eventDeclaration = generatedClass.substring(indexOfEventDeclaration, endIndexOfEventDeclaration + 3);
Assert.assertTrue("Generated event should contain the getDetails method", eventDeclaration.contains("public SomethingHappenedDetails getDetails() { return new SomethingHappenedDetails().readJson(details); } }"));
}
use of com.vaadin.generator.metadata.ComponentPropertyBaseData in project flow by vaadin.
the class ComponentGenerator method addEventDataParameters.
private void addEventDataParameters(JavaClassSource javaClass, ComponentEventData event, String eventClassName, JavaClassSource eventClass, MethodSource<JavaClassSource> eventConstructor) {
for (ComponentPropertyBaseData property : event.getProperties()) {
// Add new parameter to constructor
final String propertyName = property.getName();
String normalizedProperty = ComponentGeneratorUtils.formatStringToValidJavaIdentifier(propertyName);
Class<?> propertyJavaType;
if (containsObjectType(property)) {
JavaClassSource nestedClass = generateNestedPojo(javaClass, property.getObjectType().get(0), eventClassName + "-" + propertyName, String.format("Class that encapsulates the data received on the '%s' property of @{link %s} events, from the @{link %s} component.", propertyName, eventClass.getName(), javaClass.getName()));
propertyJavaType = JsonObject.class;
eventClass.addField().setType(propertyJavaType).setPrivate().setFinal(true).setName(normalizedProperty);
eventClass.addMethod().setName(ComponentGeneratorUtils.generateMethodNameForProperty("get", propertyName)).setPublic().setReturnType(nestedClass).setBody(String.format("return new %s().readJson(%s);", nestedClass.getName(), normalizedProperty));
} else {
if (!property.getType().isEmpty()) {
// for varying types, using the first type declared in
// the
// JSDoc
// it is anyway very rare to have varying property type
propertyJavaType = ComponentGeneratorUtils.toJavaType(property.getType().iterator().next());
} else {
// object property
propertyJavaType = JsonObject.class;
}
// Create private field
eventClass.addProperty(propertyJavaType, normalizedProperty).setAccessible(true).setMutable(false);
}
ParameterSource<JavaClassSource> parameter = ComponentGeneratorUtils.addMethodParameter(javaClass, eventConstructor, propertyJavaType, normalizedProperty);
parameter.addAnnotation(EventData.class).setStringValue(String.format("event.%s", propertyName));
// Set value to private field
eventConstructor.setBody(String.format("%s%nthis.%s = %s;", eventConstructor.getBody(), normalizedProperty, normalizedProperty));
// Add the EventData as a import
javaClass.addImport(EventData.class);
}
}
use of com.vaadin.generator.metadata.ComponentPropertyBaseData in project flow by vaadin.
the class ComponentGeneratorTest method generateClassWithEventWithEventDataContainingDotNotation_classTypedComponentEventWithEventData.
@Test
public void generateClassWithEventWithEventDataContainingDotNotation_classTypedComponentEventWithEventData() {
ComponentEventData eventData = new ComponentEventData();
eventData.setName("change");
eventData.setDescription("Component change event.");
componentMetadata.setEvents(Collections.singletonList(eventData));
ComponentPropertyBaseData property = new ComponentPropertyBaseData();
property.setName("details.property");
property.setType(Collections.singleton(ComponentBasicType.NUMBER));
eventData.setProperties(Collections.singletonList(property));
String generatedClass = generator.generateClass(componentMetadata, "com.my.test", null);
// Using matcher as the formatter may cut the method.
Pattern pattern = Pattern.compile("public ChangeEvent\\(R source, boolean fromClient,(\\w?)(\\s*?)@EventData\\(\"event\\.details\\.property\"\\) double detailsProperty\\)");
Matcher matcher = pattern.matcher(generatedClass);
Assert.assertTrue("Couldn't find constructor with EventData.", matcher.find());
Assert.assertTrue("Couldn't find variable reference", generatedClass.contains("private final double detailsProperty;"));
Assert.assertTrue("Couldn't find getter for event data", generatedClass.contains("public double getDetailsProperty() {"));
Assert.assertFalse("Found setter even though one shouldn't exist", generatedClass.contains("public void setDetailsProperty("));
Assert.assertTrue("Missing EventData import", generatedClass.contains("import " + EventData.class.getName() + ";"));
}
use of com.vaadin.generator.metadata.ComponentPropertyBaseData in project flow by vaadin.
the class ComponentGeneratorTest method generateClassWithEventWithEventData_classTypedComponentEventWithEventData.
@Test
public void generateClassWithEventWithEventData_classTypedComponentEventWithEventData() {
ComponentEventData eventData = new ComponentEventData();
eventData.setName("change");
eventData.setDescription("Component change event.");
componentMetadata.setEvents(Collections.singletonList(eventData));
ComponentPropertyBaseData property = new ComponentPropertyBaseData();
property.setName("button");
property.setType(Collections.singleton(ComponentBasicType.NUMBER));
eventData.setProperties(Collections.singletonList(property));
String generatedClass = generator.generateClass(componentMetadata, "com.my.test", null);
// Using matcher as the formatter may cut the method.
Pattern pattern = Pattern.compile("public ChangeEvent\\(R source, boolean fromClient,(\\w?)(\\s*?)@EventData\\(\"event\\.button\"\\) double button\\)");
Matcher matcher = pattern.matcher(generatedClass);
Assert.assertTrue("Couldn't find constructor with EventData.", matcher.find());
Assert.assertTrue("Couldn't find variable reference", generatedClass.contains("private final double button;"));
Assert.assertTrue("Couldn't find getter for event data", generatedClass.contains("public double getButton() {"));
Assert.assertFalse("Found setter even though one shouldn't exist", generatedClass.contains("public void setButton("));
Assert.assertTrue("Missing EventData import", generatedClass.contains("import " + EventData.class.getName() + ";"));
}
Aggregations