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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.Map;
  19. import java.util.Optional;
  20. import java.util.stream.Stream;
  21. import org.junit.Assert;
  22. import org.junit.Test;
  23. import com.vaadin.server.Setter;
  24. import com.vaadin.ui.TextField;
  25. public class BinderCustomPropertySetTest {
  26. public static class MapPropertyDefinition
  27. implements BinderPropertyDefinition<Map<String, String>, String> {
  28. private MapPropertySet propertySet;
  29. private String name;
  30. public MapPropertyDefinition(MapPropertySet propertySet, String name) {
  31. this.propertySet = propertySet;
  32. this.name = name;
  33. }
  34. @Override
  35. public ValueProvider<Map<String, String>, String> getGetter() {
  36. return map -> map.get(name);
  37. }
  38. @Override
  39. public Optional<Setter<Map<String, String>, String>> getSetter() {
  40. return Optional.of((map, value) -> {
  41. if (value == null) {
  42. map.remove(name);
  43. } else {
  44. map.put(name, value);
  45. }
  46. });
  47. }
  48. @Override
  49. public Class<String> getType() {
  50. return String.class;
  51. }
  52. @Override
  53. public String getName() {
  54. return name;
  55. }
  56. @Override
  57. public BinderPropertySet<Map<String, String>> getPropertySet() {
  58. return propertySet;
  59. }
  60. }
  61. public static class MapPropertySet
  62. implements BinderPropertySet<Map<String, String>> {
  63. @Override
  64. public Stream<BinderPropertyDefinition<Map<String, String>, ?>> getProperties() {
  65. return Stream.of("one", "two", "three").map(this::createProperty);
  66. }
  67. @Override
  68. public Optional<BinderPropertyDefinition<Map<String, String>, ?>> getProperty(
  69. String name) {
  70. return Optional.of(createProperty(name));
  71. }
  72. private BinderPropertyDefinition<Map<String, String>, ?> createProperty(
  73. String name) {
  74. return new MapPropertyDefinition(this, name);
  75. }
  76. }
  77. public static class InstanceFields {
  78. private TextField one;
  79. private TextField another;
  80. }
  81. @Test
  82. public void testBindByString() {
  83. TextField field = new TextField();
  84. Map<String, String> map = new HashMap<>();
  85. Binder<Map<String, String>> binder = Binder
  86. .withPropertySet(new MapPropertySet());
  87. binder.bind(field, "key");
  88. binder.setBean(map);
  89. field.setValue("value");
  90. Assert.assertEquals(
  91. "Field value should propagate to the corresponding key in the map",
  92. "value", map.get("key"));
  93. }
  94. @Test
  95. public void testBindInstanceFields() {
  96. Map<String, String> map = new HashMap<>();
  97. Binder<Map<String, String>> binder = Binder
  98. .withPropertySet(new MapPropertySet());
  99. InstanceFields instanceFields = new InstanceFields();
  100. binder.bindInstanceFields(instanceFields);
  101. Assert.assertNotNull(
  102. "Field corresponding to supported property name should be bound",
  103. instanceFields.one);
  104. Assert.assertNull(
  105. "Field corresponding to unsupported property name should be ignored",
  106. instanceFields.another);
  107. binder.setBean(map);
  108. instanceFields.one.setValue("value");
  109. Assert.assertEquals(
  110. "Field value should propagate to the corresponding key in the map",
  111. "value", map.get("one"));
  112. }
  113. }