選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

TransactionalPropertyWrapperTest.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.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.data.fieldgroup.FieldGroup;
  22. import com.vaadin.v7.ui.LegacyTextField;
  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 LegacyTextField nameField = new LegacyTextField("Name");
  55. private final LegacyTextField ageField = new LegacyTextField("Age");
  56. private final LegacyTextField unboundField = new LegacyTextField(
  57. "No FieldGroup");
  58. private final TestingProperty<String> unboundProp = new TestingProperty<String>(
  59. "Hello World");
  60. private final PropertysetItem item = new PropertysetItem();
  61. @Test
  62. public void fieldGroupBindAndUnbind() {
  63. item.addItemProperty("name",
  64. new TestingProperty<String>("Just some text"));
  65. item.addItemProperty("age", new TestingProperty<String>("42"));
  66. final FieldGroup binder = new FieldGroup(item);
  67. binder.setBuffered(false);
  68. for (int i = 0; i < 2; ++i) {
  69. binder.bind(nameField, "name");
  70. binder.bind(ageField, "age");
  71. unboundField.setPropertyDataSource(unboundProp);
  72. assertTrue("No listeners in Properties", fieldsHaveListeners(true));
  73. binder.unbind(nameField);
  74. binder.unbind(ageField);
  75. unboundField.setPropertyDataSource(null);
  76. assertTrue("Listeners in Properties after unbinding",
  77. fieldsHaveListeners(false));
  78. }
  79. }
  80. /**
  81. * Check that all listeners have same hasListeners() response
  82. *
  83. * @param expected
  84. * expected response
  85. * @return true if all are the same as expected. false if not
  86. */
  87. private boolean fieldsHaveListeners(boolean expected) {
  88. for (Object id : item.getItemPropertyIds()) {
  89. TestingProperty<?> itemProperty = (TestingProperty<?>) item
  90. .getItemProperty(id);
  91. if (itemProperty.hasListeners() != expected) {
  92. return false;
  93. }
  94. }
  95. return unboundProp.hasListeners() == expected;
  96. }
  97. }