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

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