Search in sources :

Example 11 with ComponentEventData

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() + ";"));
}
Also used : Pattern(java.util.regex.Pattern) ComponentEventData(com.vaadin.generator.metadata.ComponentEventData) Matcher(java.util.regex.Matcher) ComponentPropertyBaseData(com.vaadin.generator.metadata.ComponentPropertyBaseData) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 12 with ComponentEventData

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()))"));
}
Also used : ComponentEventData(com.vaadin.generator.metadata.ComponentEventData) ComponentObjectType(com.vaadin.generator.metadata.ComponentObjectType) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ComponentPropertyData(com.vaadin.generator.metadata.ComponentPropertyData) Test(org.junit.Test)

Example 13 with ComponentEventData

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; }"));
}
Also used : ComponentEventData(com.vaadin.generator.metadata.ComponentEventData) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ComponentPropertyData(com.vaadin.generator.metadata.ComponentPropertyData) Test(org.junit.Test)

Example 14 with ComponentEventData

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() + ";"));
}
Also used : Pattern(java.util.regex.Pattern) ComponentEventData(com.vaadin.generator.metadata.ComponentEventData) Matcher(java.util.regex.Matcher) ComponentPropertyBaseData(com.vaadin.generator.metadata.ComponentPropertyBaseData) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 15 with ComponentEventData

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)"));
}
Also used : ComponentEventData(com.vaadin.generator.metadata.ComponentEventData) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ComponentPropertyData(com.vaadin.generator.metadata.ComponentPropertyData) Test(org.junit.Test)

Aggregations

ComponentEventData (com.vaadin.generator.metadata.ComponentEventData)17 Test (org.junit.Test)14 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)12 ComponentPropertyData (com.vaadin.generator.metadata.ComponentPropertyData)10 ComponentPropertyBaseData (com.vaadin.generator.metadata.ComponentPropertyBaseData)4 Matcher (java.util.regex.Matcher)4 Pattern (java.util.regex.Pattern)4 ComponentGenerator (com.vaadin.generator.ComponentGenerator)3 ComponentMetadata (com.vaadin.generator.metadata.ComponentMetadata)3 ComponentObjectType (com.vaadin.generator.metadata.ComponentObjectType)2 EventData (com.vaadin.flow.component.EventData)1 ComponentObjectTypeInnerType (com.vaadin.generator.metadata.ComponentObjectType.ComponentObjectTypeInnerType)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Map (java.util.Map)1 JavaClassSource (org.jboss.forge.roaster.model.source.JavaClassSource)1 Before (org.junit.Before)1