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.

TransactionalPropertyWrapperTest.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.v7.data.util;
  17. import static org.junit.Assert.assertTrue;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import org.junit.Test;
  21. import com.vaadin.v7.data.fieldgroup.FieldGroup;
  22. import com.vaadin.v7.ui.TextField;
  23. /**
  24. * Test verifying that TransactionalPropertyWrapper removes it's listener from
  25. * wrapped Property
  26. *
  27. * @since 7.1.15
  28. * @author Vaadin Ltd
  29. */
  30. public class TransactionalPropertyWrapperTest {
  31. @SuppressWarnings("serial")
  32. public class TestingProperty<T extends Object>
  33. extends ObjectProperty<Object> {
  34. private List<ValueChangeListener> listeners = new ArrayList<ValueChangeListener>();
  35. public TestingProperty(Object value) {
  36. super(value);
  37. }
  38. @Override
  39. public void addValueChangeListener(ValueChangeListener listener) {
  40. super.addValueChangeListener(listener);
  41. listeners.add(listener);
  42. }
  43. @Override
  44. public void removeValueChangeListener(ValueChangeListener listener) {
  45. super.removeValueChangeListener(listener);
  46. if (listeners.contains(listener)) {
  47. listeners.remove(listener);
  48. }
  49. }
  50. public boolean hasListeners() {
  51. return !listeners.isEmpty();
  52. }
  53. }
  54. private final TextField nameField = new TextField("Name");
  55. private final TextField ageField = new TextField("Age");
  56. private final TextField unboundField = new TextField("No FieldGroup");
  57. private final TestingProperty<String> unboundProp = new TestingProperty<String>(
  58. "Hello World");
  59. private final PropertysetItem item = new PropertysetItem();
  60. @Test
  61. public void fieldGroupBindAndUnbind() {
  62. item.addItemProperty("name",
  63. new TestingProperty<String>("Just some text"));
  64. item.addItemProperty("age", new TestingProperty<String>("42"));
  65. final FieldGroup binder = new FieldGroup(item);
  66. binder.setBuffered(false);
  67. for (int i = 0; i < 2; ++i) {
  68. binder.bind(nameField, "name");
  69. binder.bind(ageField, "age");
  70. unboundField.setPropertyDataSource(unboundProp);
  71. assertTrue("No listeners in Properties", fieldsHaveListeners(true));
  72. binder.unbind(nameField);
  73. binder.unbind(ageField);
  74. unboundField.setPropertyDataSource(null);
  75. assertTrue("Listeners in Properties after unbinding",
  76. fieldsHaveListeners(false));
  77. }
  78. }
  79. /**
  80. * Check that all listeners have same hasListeners() response
  81. *
  82. * @param expected
  83. * expected response
  84. * @return true if all are the same as expected. false if not
  85. */
  86. private boolean fieldsHaveListeners(boolean expected) {
  87. for (Object id : item.getItemPropertyIds()) {
  88. TestingProperty<?> itemProperty = (TestingProperty<?>) item
  89. .getItemProperty(id);
  90. if (itemProperty.hasListeners() != expected) {
  91. return false;
  92. }
  93. }
  94. return unboundProp.hasListeners() == expected;
  95. }
  96. }