use of com.vaadin.generator.metadata.ComponentEventData 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.ComponentEventData in project flow by vaadin.
the class ComponentGeneratorTest method classContainsObjectProperty_componentContainsValueProperty_generatedClassImplementsHasValue.
@Test
public void classContainsObjectProperty_componentContainsValueProperty_generatedClassImplementsHasValue() {
ComponentObjectType objectType = new ComponentObjectType();
ComponentPropertyData property = new ComponentPropertyData();
property.setName("value");
property.setObjectType(Collections.singletonList(objectType));
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);
ComponentGeneratorTestUtils.assertClassImplementsInterface(generatedClass, "MyComponent", HasValue.class);
Assert.assertThat(generatedClass, CoreMatchers.containsString("@Override public ValueProperty getValue() { JsonObject _obj = (JsonObject) getElement().getPropertyRaw("));
Assert.assertThat(generatedClass, CoreMatchers.containsString("@Override public void setValue(ValueProperty property) { if (!Objects.equals(property, getValue()))"));
}
use of com.vaadin.generator.metadata.ComponentEventData in project flow by vaadin.
the class ComponentGeneratorTest method classContainsGetterAndRelatedChangeEvent_eventContainsPropertyAndGetter.
@Test
public void classContainsGetterAndRelatedChangeEvent_eventContainsPropertyAndGetter() {
ComponentPropertyData property = new ComponentPropertyData();
property.setName("someproperty");
property.setType(Collections.singleton(ComponentBasicType.STRING));
componentMetadata.setProperties(Collections.singletonList(property));
ComponentEventData event = new ComponentEventData();
event.setName("someproperty-changed");
componentMetadata.setEvents(Collections.singletonList(event));
String generatedClass = generator.generateClass(componentMetadata, "com.my.test", null);
generatedClass = ComponentGeneratorTestUtils.removeIndentation(generatedClass);
Assert.assertThat("Event should save the property value from the source component", generatedClass, containsString("someproperty = source.getSomeproperty();"));
Assert.assertThat("Event should have getter for the property", generatedClass, containsString("public String getSomeproperty() { return someproperty; }"));
}
use of com.vaadin.generator.metadata.ComponentEventData 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() + ";"));
}
use of com.vaadin.generator.metadata.ComponentEventData in project flow by vaadin.
the class ComponentGeneratorTest method componentContainsNumberValueProperty_generatedClassImplementsHasValueWithoutPrimitiveTypes.
@Test
public void componentContainsNumberValueProperty_generatedClassImplementsHasValueWithoutPrimitiveTypes() {
ComponentPropertyData property = new ComponentPropertyData();
property.setName("value");
property.setType(Collections.singleton(ComponentBasicType.NUMBER));
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);
ComponentGeneratorTestUtils.assertClassImplementsInterface(generatedClass, "MyComponent", HasValue.class);
Assert.assertTrue(generatedClass.contains("@Override public Double getValue()"));
Assert.assertTrue(generatedClass.contains("@Override public void setValue(Double value)"));
Assert.assertTrue(generatedClass.contains("public void setValue(Number value)"));
}
Aggregations