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.

FieldGroupTest.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright 2000-2014 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.tests.server.component.fieldgroup;
  17. import java.util.Arrays;
  18. import java.util.Collection;
  19. import java.util.HashSet;
  20. import java.util.Map;
  21. import java.util.Map.Entry;
  22. import java.util.Set;
  23. import org.junit.Assert;
  24. import org.junit.Test;
  25. import com.vaadin.data.Item;
  26. import com.vaadin.data.Property;
  27. import com.vaadin.data.fieldgroup.FieldGroup;
  28. import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
  29. import com.vaadin.data.util.AbstractProperty;
  30. import com.vaadin.legacy.data.Validator.InvalidValueException;
  31. import com.vaadin.legacy.ui.LegacyField;
  32. import com.vaadin.legacy.ui.LegacyTextField;
  33. /**
  34. *
  35. * Tests for {@link FieldGroup}.
  36. *
  37. * @author Vaadin Ltd
  38. */
  39. public class FieldGroupTest {
  40. @Test
  41. public void setReadOnly_readOnlyAndNoDataSource_fieldIsReadOnly() {
  42. FieldGroup fieldGroup = new FieldGroup();
  43. LegacyTextField field = new LegacyTextField();
  44. fieldGroup.bind(field, "property");
  45. fieldGroup.setReadOnly(true);
  46. Assert.assertTrue("Field is not read only", field.isReadOnly());
  47. }
  48. @Test
  49. public void setReadOnly_writableAndNoDataSource_fieldIsWritable() {
  50. FieldGroup fieldGroup = new FieldGroup();
  51. LegacyTextField field = new LegacyTextField();
  52. fieldGroup.bind(field, "property");
  53. fieldGroup.setReadOnly(false);
  54. Assert.assertFalse("Field is not writable", field.isReadOnly());
  55. }
  56. @Test
  57. public void commit_validationFailed_allValidationFailuresAvailable()
  58. throws CommitException {
  59. FieldGroup fieldGroup = new FieldGroup();
  60. fieldGroup.setItemDataSource(new TestItem());
  61. LegacyTextField field1 = new LegacyTextField();
  62. field1.setRequired(true);
  63. fieldGroup.bind(field1, "prop1");
  64. LegacyTextField field2 = new LegacyTextField();
  65. field2.setRequired(true);
  66. fieldGroup.bind(field2, "prop2");
  67. Set<LegacyTextField> set = new HashSet<>(Arrays.asList(field1,
  68. field2));
  69. try {
  70. fieldGroup.commit();
  71. Assert.fail("No commit exception is thrown");
  72. } catch (CommitException exception) {
  73. Map<LegacyField<?>, ? extends InvalidValueException> invalidFields = exception
  74. .getInvalidFields();
  75. for (Entry<LegacyField<?>, ? extends InvalidValueException> entry : invalidFields
  76. .entrySet()) {
  77. set.remove(entry.getKey());
  78. }
  79. Assert.assertEquals(
  80. "Some fields are not found in the invalid fields map", 0,
  81. set.size());
  82. Assert.assertEquals(
  83. "Invalid value exception should be thrown for each field",
  84. 2, invalidFields.size());
  85. }
  86. }
  87. private static class TestItem implements Item {
  88. @Override
  89. public Property<String> getItemProperty(Object id) {
  90. return new StringProperty();
  91. }
  92. @Override
  93. public Collection<?> getItemPropertyIds() {
  94. return Arrays.asList("prop1", "prop2");
  95. }
  96. @Override
  97. public boolean addItemProperty(Object id, Property property)
  98. throws UnsupportedOperationException {
  99. return false;
  100. }
  101. @Override
  102. public boolean removeItemProperty(Object id)
  103. throws UnsupportedOperationException {
  104. return false;
  105. }
  106. }
  107. private static class StringProperty extends AbstractProperty<String> {
  108. @Override
  109. public String getValue() {
  110. return null;
  111. }
  112. @Override
  113. public void setValue(String newValue)
  114. throws Property.ReadOnlyException {
  115. }
  116. @Override
  117. public Class<? extends String> getType() {
  118. return String.class;
  119. }
  120. }
  121. }