Search in sources :

Example 26 with ModelMap

use of com.vaadin.flow.internal.nodefeature.ModelMap in project flow by vaadin.

the class TemplateModelTest method setBeanIncludeProperties.

@Test
public void setBeanIncludeProperties() {
    TemplateWithInclude template = new TemplateWithInclude();
    template.getModel().setBean(new Bean(123));
    ModelMap modelMap = getModelMap(template, "bean");
    Set<String> mapKeys = getKeys(modelMap);
    Assert.assertTrue("Model should contain included 'doubleValue'", mapKeys.remove("doubleValue"));
    Assert.assertTrue("Model should contain included 'booleanObject'", mapKeys.remove("booleanObject"));
    Assert.assertTrue("model should be empty but contains: " + mapKeys, mapKeys.isEmpty());
}
Also used : ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) Bean(com.vaadin.flow.templatemodel.Bean) Test(org.junit.Test)

Example 27 with ModelMap

use of com.vaadin.flow.internal.nodefeature.ModelMap in project flow by vaadin.

the class TemplateModelTest method setBeanIncludeSubBeanProperties.

@Test
public void setBeanIncludeSubBeanProperties() {
    TemplateWithIncludeForSubBean template = new TemplateWithIncludeForSubBean();
    BeanContainingBeans beanContainer = new BeanContainingBeans();
    beanContainer.setBean1(new Bean(1));
    beanContainer.setBean2(new Bean(2));
    template.getModel().setBeanContainingBeans(beanContainer);
    Assert.assertNotNull(template.getModel().getBeanContainingBeans().getBean1());
    ModelMap bean1Map = getModelMap(template, "beanContainingBeans.bean1");
    Set<String> bean1Keys = getKeys(bean1Map);
    Assert.assertTrue(bean1Keys.contains("booleanObject"));
    Assert.assertEquals(1, bean1Keys.size());
}
Also used : BeanContainingBeans(com.vaadin.flow.templatemodel.BeanContainingBeans) ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) Bean(com.vaadin.flow.templatemodel.Bean) Test(org.junit.Test)

Example 28 with ModelMap

use of com.vaadin.flow.internal.nodefeature.ModelMap in project flow by vaadin.

the class TemplateModelTest method testBeanInModel.

@Test
public void testBeanInModel() {
    BeanModelTemplate template = new BeanModelTemplate();
    BeanModel model = template.getModel();
    AtomicInteger beanTriggered = new AtomicInteger();
    Bean bean = new BeanImpl(beanTriggered);
    bean.setString("foobar");
    StateNode stateNode = (StateNode) template.getElement().getNode().getFeature(ModelMap.class).getValue("bean");
    Assert.assertNull(stateNode);
    Assert.assertEquals(0, beanTriggered.get());
    model.setBean(bean);
    stateNode = (StateNode) template.getElement().getNode().getFeature(ModelMap.class).getValue("bean");
    // enough to verify that TemplateModelBeanUtil.importBeanIntoModel is
    // triggered, since TemplatemodelBeanUtilTests covers the bean import
    Assert.assertNotNull(stateNode);
    Assert.assertEquals(1, beanTriggered.get());
    ModelMap modelMap = ModelMap.get(stateNode);
    Assert.assertNotNull(modelMap);
    Assert.assertEquals("foobar", modelMap.getValue("string"));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) StateNode(com.vaadin.flow.internal.StateNode) Bean(com.vaadin.flow.templatemodel.Bean) Test(org.junit.Test)

Example 29 with ModelMap

use of com.vaadin.flow.internal.nodefeature.ModelMap in project flow by vaadin.

the class TemplateModelTest method includeExcludeWhenUsingSubclass.

@Test
public void includeExcludeWhenUsingSubclass() {
    TemplateWithExcludeAndIncludeSubclass template = new TemplateWithExcludeAndIncludeSubclass();
    template.getModel().setBean(new Bean(123));
    ModelMap modelMap = getModelMap(template, "bean");
    Assert.assertTrue(modelMap.hasValue("booleanObject"));
    Assert.assertEquals(1, modelMap.getKeys().count());
}
Also used : ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) Bean(com.vaadin.flow.templatemodel.Bean) Test(org.junit.Test)

Example 30 with ModelMap

use of com.vaadin.flow.internal.nodefeature.ModelMap in project flow by vaadin.

the class TemplateModelTest method setBeanExcludeProperties.

@Test
public void setBeanExcludeProperties() {
    TemplateWithExclude template = new TemplateWithExclude();
    template.getModel().setBean(new Bean(123));
    ModelMap modelMap = getModelMap(template, "bean");
    Set<String> mapKeys = getKeys(modelMap);
    Set<String> excluded = new HashSet<>();
    excluded.add("doubleValue");
    excluded.add("booleanObject");
    for (String excludedPropertyName : excluded) {
        Assert.assertFalse("Model should not contain excluded '" + excludedPropertyName + "'", mapKeys.contains(excludedPropertyName));
    }
    ReflectTools.getSetterMethods(Bean.class).map(method -> ReflectTools.getPropertyName(method)).forEach(propertyName -> {
        if (!excluded.contains(propertyName)) {
            Assert.assertTrue("Model should contain the property '" + propertyName + "'", mapKeys.remove(propertyName));
        }
    });
    Assert.assertTrue("model should be empty but contains: " + mapKeys, mapKeys.isEmpty());
}
Also used : Exclude(com.vaadin.flow.templatemodel.Exclude) Bean(com.vaadin.flow.templatemodel.Bean) ModelList(com.vaadin.flow.internal.nodefeature.ModelList) Supplier(java.util.function.Supplier) ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Include(com.vaadin.flow.templatemodel.Include) StateNode(com.vaadin.flow.internal.StateNode) TemplateModelTypeParser(com.vaadin.flow.template.angular.model.TemplateModelTypeParser) Set(java.util.Set) Matchers(org.hamcrest.Matchers) ReflectTools(com.vaadin.flow.internal.ReflectTools) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) InlineTemplate(com.vaadin.flow.template.angular.InlineTemplate) InvalidTemplateModelException(com.vaadin.flow.templatemodel.InvalidTemplateModelException) NodeChange(com.vaadin.flow.internal.change.NodeChange) List(java.util.List) Assert(org.junit.Assert) AngularTemplate(com.vaadin.flow.template.angular.AngularTemplate) TemplateModel(com.vaadin.flow.template.angular.model.TemplateModel) BeanContainingBeans(com.vaadin.flow.templatemodel.BeanContainingBeans) ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) Bean(com.vaadin.flow.templatemodel.Bean) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ModelMap (com.vaadin.flow.internal.nodefeature.ModelMap)41 Test (org.junit.Test)30 StateNode (com.vaadin.flow.internal.StateNode)18 Bean (com.vaadin.flow.templatemodel.Bean)18 ModelList (com.vaadin.flow.internal.nodefeature.ModelList)9 ModelType (com.vaadin.flow.templatemodel.ModelType)5 PropertyFilter (com.vaadin.flow.templatemodel.PropertyFilter)5 ArrayList (java.util.ArrayList)5 Collectors (java.util.stream.Collectors)5 Assert (org.junit.Assert)5 Arrays (java.util.Arrays)4 Date (java.util.Date)4 BeanContainingBeans (com.vaadin.flow.templatemodel.BeanContainingBeans)3 InvalidTemplateModelException (com.vaadin.flow.templatemodel.InvalidTemplateModelException)3 ReflectTools (com.vaadin.flow.internal.ReflectTools)2 NodeChange (com.vaadin.flow.internal.change.NodeChange)2 BasicModelType (com.vaadin.flow.templatemodel.BasicModelType)2 Serializable (java.io.Serializable)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ClassList (com.vaadin.flow.dom.ClassList)1