Search in sources :

Example 11 with ComponentPropertyData

use of com.vaadin.generator.metadata.ComponentPropertyData in project flow by vaadin.

the class ComponentGeneratorTest method classContainsObjectProperty_generatedClassContainsInnerClass.

@Test
public void classContainsObjectProperty_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));
    ComponentPropertyData property = new ComponentPropertyData();
    property.setName("something");
    property.setObjectType(Collections.singletonList(objectType));
    componentMetadata.setProperties(Collections.singletonList(property));
    String generatedClass = generator.generateClass(componentMetadata, "com.my.test", null);
    generatedClass = ComponentGeneratorTestUtils.removeIndentation(generatedClass);
    Assert.assertTrue("Generated class should contain the SomethingProperty nested class", generatedClass.contains("public static class SomethingProperty implements JsonSerializable"));
    Assert.assertTrue("Generated class should contain the getSomething method", generatedClass.contains("public SomethingProperty getSomething()"));
    Assert.assertTrue("Generated class should contain the setSomething method", generatedClass.contains("public void setSomething(SomethingProperty property)"));
}
Also used : ComponentObjectTypeInnerType(com.vaadin.generator.metadata.ComponentObjectType.ComponentObjectTypeInnerType) ComponentObjectType(com.vaadin.generator.metadata.ComponentObjectType) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ComponentPropertyData(com.vaadin.generator.metadata.ComponentPropertyData) Test(org.junit.Test)

Example 12 with ComponentPropertyData

use of com.vaadin.generator.metadata.ComponentPropertyData in project flow by vaadin.

the class ComponentGeneratorTest method componentContainsUnrecognizedPropertyTypes_methodsAreGeneratedAsProtected.

@Test
public void componentContainsUnrecognizedPropertyTypes_methodsAreGeneratedAsProtected() {
    ComponentPropertyData objectProperty = new ComponentPropertyData();
    objectProperty.setName("objectProperty");
    objectProperty.setType(Collections.singleton(ComponentBasicType.OBJECT));
    ComponentPropertyData arrayProperty = new ComponentPropertyData();
    arrayProperty.setName("arrayProperty");
    arrayProperty.setType(Collections.singleton(ComponentBasicType.ARRAY));
    ComponentPropertyData undefinedProperty = new ComponentPropertyData();
    undefinedProperty.setName("undefinedProperty");
    undefinedProperty.setType(Collections.singleton(ComponentBasicType.UNDEFINED));
    componentMetadata.setProperties(Arrays.asList(objectProperty, arrayProperty, undefinedProperty));
    ComponentFunctionParameterData objectParameter = new ComponentFunctionParameterData();
    objectParameter.setName("objectParam");
    objectParameter.setType(Collections.singleton(ComponentBasicType.OBJECT));
    ComponentFunctionData function = new ComponentFunctionData();
    function.setName("callSomething");
    function.setParameters(Collections.singletonList(objectParameter));
    componentMetadata.setMethods(Collections.singletonList(function));
    String generatedClass = generator.generateClass(componentMetadata, "com.my.test", null);
    generatedClass = ComponentGeneratorTestUtils.removeIndentation(generatedClass);
    Assert.assertThat(generatedClass, CoreMatchers.containsString("protected JsonObject getObjectPropertyJsonObject()"));
    Assert.assertThat(generatedClass, CoreMatchers.containsString("protected void setObjectProperty(JsonObject objectProperty)"));
    Assert.assertThat(generatedClass, CoreMatchers.containsString("protected JsonArray getArrayPropertyJsonArray()"));
    Assert.assertThat(generatedClass, CoreMatchers.containsString("protected void setArrayProperty(JsonArray arrayProperty)"));
    Assert.assertThat(generatedClass, CoreMatchers.containsString("protected JsonValue getUndefinedPropertyJsonValue()"));
    Assert.assertThat(generatedClass, CoreMatchers.containsString("protected void setUndefinedProperty(JsonValue undefinedProperty)"));
    Assert.assertThat(generatedClass, CoreMatchers.containsString("protected void callSomething(JsonObject objectParam)"));
}
Also used : ComponentFunctionParameterData(com.vaadin.generator.metadata.ComponentFunctionParameterData) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ComponentPropertyData(com.vaadin.generator.metadata.ComponentPropertyData) ComponentFunctionData(com.vaadin.generator.metadata.ComponentFunctionData) Test(org.junit.Test)

Example 13 with ComponentPropertyData

use of com.vaadin.generator.metadata.ComponentPropertyData 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\";"));
}
Also used : ComponentGenerator(com.vaadin.generator.ComponentGenerator) ComponentEventData(com.vaadin.generator.metadata.ComponentEventData) ComponentMetadata(com.vaadin.generator.metadata.ComponentMetadata) ComponentPropertyData(com.vaadin.generator.metadata.ComponentPropertyData) Test(org.junit.Test)

Example 14 with ComponentPropertyData

use of com.vaadin.generator.metadata.ComponentPropertyData in project flow by vaadin.

the class ComponentGeneratorTest method generateClassWithBooleanGetterAndNonFluentSetter_setterDoesNotSetEmptyForNullValue.

@Test
public void generateClassWithBooleanGetterAndNonFluentSetter_setterDoesNotSetEmptyForNullValue() {
    ComponentPropertyData propertyData = new ComponentPropertyData();
    propertyData.setName("required");
    propertyData.setType(Collections.singleton(ComponentBasicType.BOOLEAN));
    propertyData.setDescription("This is a required field.");
    componentMetadata.setProperties(Collections.singletonList(propertyData));
    String generatedClass = generator.generateClass(componentMetadata, "com.my.test", null);
    Assert.assertTrue("No setter found", generatedClass.contains("public void setRequired(boolean required)"));
    Assert.assertFalse("Setter checks for null value", generatedClass.contains(propertyData.getName() + " == null ? \"\" : " + propertyData.getName()));
}
Also used : CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ComponentPropertyData(com.vaadin.generator.metadata.ComponentPropertyData) Test(org.junit.Test)

Example 15 with ComponentPropertyData

use of com.vaadin.generator.metadata.ComponentPropertyData 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)

Aggregations

ComponentPropertyData (com.vaadin.generator.metadata.ComponentPropertyData)24 Test (org.junit.Test)23 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)21 ComponentEventData (com.vaadin.generator.metadata.ComponentEventData)10 ComponentGenerator (com.vaadin.generator.ComponentGenerator)3 ComponentMetadata (com.vaadin.generator.metadata.ComponentMetadata)3 ComponentFunctionData (com.vaadin.generator.metadata.ComponentFunctionData)2 ComponentObjectType (com.vaadin.generator.metadata.ComponentObjectType)2 ComponentFunctionParameterData (com.vaadin.generator.metadata.ComponentFunctionParameterData)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 Before (org.junit.Before)1