Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

BinderCustomPropertySetTest.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.data.Binder.BindingBuilder;
  24. import com.vaadin.server.Setter;
  25. import com.vaadin.ui.TextField;
  26. public class BinderCustomPropertySetTest {
  27. public static class MapPropertyDefinition
  28. implements BinderPropertyDefinition<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. @Override
  54. public BindingBuilder<Map<String, String>, String> beforeBind(
  55. BindingBuilder<Map<String, String>, String> originalBuilder) {
  56. return originalBuilder;
  57. }
  58. @Override
  59. public String getName() {
  60. return name;
  61. }
  62. @Override
  63. public BinderPropertySet<Map<String, String>> getPropertySet() {
  64. return propertySet;
  65. }
  66. }
  67. public static class MapPropertySet
  68. implements BinderPropertySet<Map<String, String>> {
  69. @Override
  70. public Stream<BinderPropertyDefinition<Map<String, String>, ?>> getProperties() {
  71. return Stream.of("one", "two", "three").map(this::createProperty);
  72. }
  73. @Override
  74. public Optional<BinderPropertyDefinition<Map<String, String>, ?>> getProperty(
  75. String name) {
  76. return Optional.of(createProperty(name));
  77. }
  78. private BinderPropertyDefinition<Map<String, String>, ?> createProperty(
  79. String name) {
  80. return new MapPropertyDefinition(this, name);
  81. }
  82. }
  83. public static class InstanceFields {
  84. private TextField one;
  85. private TextField another;
  86. }
  87. @Test
  88. public void testBindByString() {
  89. TextField field = new TextField();
  90. Map<String, String> map = new HashMap<>();
  91. Binder<Map<String, String>> binder = Binder
  92. .withPropertySet(new MapPropertySet());
  93. binder.bind(field, "key");
  94. binder.setBean(map);
  95. field.setValue("value");
  96. Assert.assertEquals(
  97. "Field value should propagate to the corresponding key in the map",
  98. "value", map.get("key"));
  99. }
  100. @Test
  101. public void testBindInstanceFields() {
  102. Map<String, String> map = new HashMap<>();
  103. Binder<Map<String, String>> binder = Binder
  104. .withPropertySet(new MapPropertySet());
  105. InstanceFields instanceFields = new InstanceFields();
  106. binder.bindInstanceFields(instanceFields);
  107. Assert.assertNotNull(
  108. "Field corresponding to supported property name should be bound",
  109. instanceFields.one);
  110. Assert.assertNull(
  111. "Field corresponding to unsupported property name should be ignored",
  112. instanceFields.another);
  113. binder.setBean(map);
  114. instanceFields.one.setValue("value");
  115. Assert.assertEquals(
  116. "Field value should propagate to the corresponding key in the map",
  117. "value", map.get("one"));
  118. }
  119. }