You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BinderCustomPropertySetTest.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.data;
  17. import java.util.HashMap;
  18. import java.util.Locale;
  19. import java.util.Map;
  20. import java.util.Optional;
  21. import java.util.stream.Stream;
  22. import org.junit.Assert;
  23. import org.junit.Test;
  24. import com.vaadin.server.Setter;
  25. import com.vaadin.ui.TextField;
  26. public class BinderCustomPropertySetTest {
  27. public static class MapPropertyDefinition
  28. implements PropertyDefinition<Map<String, String>, String> {
  29. private MapPropertySet propertySet;
  30. private String name;
  31. public MapPropertyDefinition(MapPropertySet propertySet, String name) {
  32. this.propertySet = propertySet;
  33. this.name = name;
  34. }
  35. @Override
  36. public ValueProvider<Map<String, String>, String> getGetter() {
  37. return map -> map.get(name);
  38. }
  39. @Override
  40. public Optional<Setter<Map<String, String>, String>> getSetter() {
  41. return Optional.of((map, value) -> {
  42. if (value == null) {
  43. map.remove(name);
  44. } else {
  45. map.put(name, value);
  46. }
  47. });
  48. }
  49. @Override
  50. public Class<String> getType() {
  51. return String.class;
  52. }
  53. public Class<?> getPropertyHolderType(){
  54. return Map.class;
  55. }
  56. @Override
  57. public String getName() {
  58. return name;
  59. }
  60. @Override
  61. public PropertySet<Map<String, String>> getPropertySet() {
  62. return propertySet;
  63. }
  64. @Override
  65. public String getCaption() {
  66. return name.toUpperCase(Locale.ENGLISH);
  67. }
  68. }
  69. public static class MapPropertySet
  70. implements PropertySet<Map<String, String>> {
  71. @Override
  72. public Stream<PropertyDefinition<Map<String, String>, ?>> getProperties() {
  73. return Stream.of("one", "two", "three").map(this::createProperty);
  74. }
  75. @Override
  76. public Optional<PropertyDefinition<Map<String, String>, ?>> getProperty(
  77. String name) {
  78. return Optional.of(createProperty(name));
  79. }
  80. private PropertyDefinition<Map<String, String>, ?> createProperty(
  81. String name) {
  82. return new MapPropertyDefinition(this, name);
  83. }
  84. }
  85. public static class InstanceFields {
  86. private TextField one;
  87. private TextField another;
  88. }
  89. @Test
  90. public void testBindByString() {
  91. TextField field = new TextField();
  92. Map<String, String> map = new HashMap<>();
  93. Binder<Map<String, String>> binder = Binder
  94. .withPropertySet(new MapPropertySet());
  95. binder.bind(field, "key");
  96. binder.setBean(map);
  97. field.setValue("value");
  98. Assert.assertEquals(
  99. "Field value should propagate to the corresponding key in the map",
  100. "value", map.get("key"));
  101. }
  102. @Test
  103. public void testBindInstanceFields() {
  104. Map<String, String> map = new HashMap<>();
  105. Binder<Map<String, String>> binder = Binder
  106. .withPropertySet(new MapPropertySet());
  107. InstanceFields instanceFields = new InstanceFields();
  108. binder.bindInstanceFields(instanceFields);
  109. Assert.assertNotNull(
  110. "Field corresponding to supported property name should be bound",
  111. instanceFields.one);
  112. Assert.assertNull(
  113. "Field corresponding to unsupported property name should be ignored",
  114. instanceFields.another);
  115. binder.setBean(map);
  116. instanceFields.one.setValue("value");
  117. Assert.assertEquals(
  118. "Field value should propagate to the corresponding key in the map",
  119. "value", map.get("one"));
  120. }
  121. }