use of com.vaadin.generator.metadata.ComponentEventData in project flow by vaadin.
the class ComponentGeneratorTest method generateClassWithPropertyChangeEvent_propertyChangeListenerUsedInsteadOfDomEvent.
@Test
public void generateClassWithPropertyChangeEvent_propertyChangeListenerUsedInsteadOfDomEvent() {
ComponentEventData eventData = new ComponentEventData();
eventData.setName("something-changed");
eventData.setDescription("Property change event.");
componentMetadata.setEvents(Collections.singletonList(eventData));
String generatedClass = generator.generateClass(componentMetadata, "com.my.test", null);
generatedClass = ComponentGeneratorTestUtils.removeIndentation(generatedClass);
Assert.assertThat("Custom event class was not found.", generatedClass, containsString("public static class SomethingChangeEvent<R extends MyComponent<R>> extends ComponentEvent<R> {"));
Assert.assertFalse("DomEvent should not be used for property changes", generatedClass.contains("@DomEvent"));
// Using matcher as the formatter may cut the method.
Pattern pattern = Pattern.compile("addSomethingChangeListener\\((\\w?)(\\s*?)ComponentEventListener<SomethingChangeEvent<R>> listener\\)");
Matcher matcher = pattern.matcher(generatedClass);
Assert.assertTrue("Couldn't find correct listener for event.", matcher.find());
Assert.assertThat("Event should be propagated to a property change listener", StringUtils.deleteWhitespace(generatedClass), containsString("getElement().addPropertyChangeListener(\"something\","));
Assert.assertFalse("DomEvent imported even without dom-events", generatedClass.contains("import " + DomEvent.class.getName() + ";"));
Assert.assertThat("Missing ComponentEvent import", generatedClass, containsString("import " + ComponentEvent.class.getName() + ";"));
Assert.assertThat("Missing ComponentEventListener import", generatedClass, containsString("import " + ComponentEventListener.class.getName() + ";"));
Assert.assertFalse("EventData imported even without events", generatedClass.contains("import " + EventData.class.getName() + ";"));
}
use of com.vaadin.generator.metadata.ComponentEventData 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.ComponentEventData in project flow by vaadin.
the class ComponentGeneratorTest method componentContainsValueProperty_generatedSetValuePreventsSettingTheSameValue.
@Test
public void componentContainsValueProperty_generatedSetValuePreventsSettingTheSameValue() {
ComponentPropertyData property = new ComponentPropertyData();
property.setName("value");
property.setType(Collections.singleton(ComponentBasicType.STRING));
componentMetadata.setProperties(Collections.singletonList(property));
ComponentEventData event = new ComponentEventData();
event.setName("value-changed");
componentMetadata.setEvents(Collections.singletonList(event));
String generatedClass = generator.generateClass(componentMetadata, "com.my.test", null);
generatedClass = ComponentGeneratorTestUtils.removeIndentation(generatedClass);
Assert.assertTrue(generatedClass.contains("@Override public void setValue(String value) {" + " Objects.requireNonNull(value, \"value cannot be null\");" + " if (!Objects.equals(value, getValue())) {"));
Assert.assertTrue(generatedClass.contains("@Override public String getEmptyValue() { return \"\"; }"));
}
use of com.vaadin.generator.metadata.ComponentEventData in project flow by vaadin.
the class PropertyNameRemapRegistryTest method hasValueWithRemapping_remapFromOtherToValue.
@Test
public void hasValueWithRemapping_remapFromOtherToValue() {
ComponentGenerator generator = new ComponentGenerator();
ComponentMetadata componentMetadata = new ComponentMetadata();
componentMetadata.setName("test-name");
componentMetadata.setTag(TEST_COMPONENT_TAG);
componentMetadata.setBaseUrl("");
ComponentPropertyData propertyData = new ComponentPropertyData();
propertyData.setName("map-to-value");
propertyData.setType(Collections.singleton(ComponentBasicType.STRING));
componentMetadata.setProperties(Arrays.asList(propertyData));
ComponentEventData eventData = new ComponentEventData();
eventData.setName("map-to-value-changed");
componentMetadata.setEvents(Arrays.asList(eventData));
String generated = generator.generateClass(componentMetadata, "com.my.test", null);
ComponentGeneratorTestUtils.assertClassImplementsInterface(generated, "TestTag", HasValue.class);
Assert.assertFalse("Remapped value change event should not be generated", generated.contains("ValueChangeEvent"));
Assert.assertTrue("HasValue getClientValuePropertyName overridden", generated.contains("String getClientValuePropertyName()"));
Assert.assertTrue("HasValue getClientValuePropertyName overridden", generated.contains("return \"map-to-value\";"));
}
use of com.vaadin.generator.metadata.ComponentEventData 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);
}
}
Aggregations