123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- /*
- * Copyright 2000-2014 Vaadin Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
- package com.vaadin.tests.server.component.fieldgroup;
-
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.HashSet;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Set;
-
- import org.junit.Assert;
- import org.junit.Test;
-
- import com.vaadin.data.Item;
- import com.vaadin.data.Property;
- import com.vaadin.data.fieldgroup.FieldGroup;
- import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
- import com.vaadin.data.util.AbstractProperty;
- import com.vaadin.legacy.data.Validator.InvalidValueException;
- import com.vaadin.legacy.ui.LegacyField;
- import com.vaadin.legacy.ui.LegacyTextField;
-
- /**
- *
- * Tests for {@link FieldGroup}.
- *
- * @author Vaadin Ltd
- */
- public class FieldGroupTest {
-
- @Test
- public void setReadOnly_readOnlyAndNoDataSource_fieldIsReadOnly() {
- FieldGroup fieldGroup = new FieldGroup();
-
- LegacyTextField field = new LegacyTextField();
- fieldGroup.bind(field, "property");
-
- fieldGroup.setReadOnly(true);
-
- Assert.assertTrue("Field is not read only", field.isReadOnly());
- }
-
- @Test
- public void setReadOnly_writableAndNoDataSource_fieldIsWritable() {
- FieldGroup fieldGroup = new FieldGroup();
-
- LegacyTextField field = new LegacyTextField();
- fieldGroup.bind(field, "property");
-
- fieldGroup.setReadOnly(false);
-
- Assert.assertFalse("Field is not writable", field.isReadOnly());
- }
-
- @Test
- public void commit_validationFailed_allValidationFailuresAvailable()
- throws CommitException {
- FieldGroup fieldGroup = new FieldGroup();
-
- fieldGroup.setItemDataSource(new TestItem());
-
- LegacyTextField field1 = new LegacyTextField();
- field1.setRequired(true);
- fieldGroup.bind(field1, "prop1");
-
- LegacyTextField field2 = new LegacyTextField();
- field2.setRequired(true);
- fieldGroup.bind(field2, "prop2");
-
- Set<LegacyTextField> set = new HashSet<>(Arrays.asList(field1,
- field2));
-
- try {
- fieldGroup.commit();
- Assert.fail("No commit exception is thrown");
- } catch (CommitException exception) {
- Map<LegacyField<?>, ? extends InvalidValueException> invalidFields = exception
- .getInvalidFields();
- for (Entry<LegacyField<?>, ? extends InvalidValueException> entry : invalidFields
- .entrySet()) {
- set.remove(entry.getKey());
- }
- Assert.assertEquals(
- "Some fields are not found in the invalid fields map", 0,
- set.size());
- Assert.assertEquals(
- "Invalid value exception should be thrown for each field",
- 2, invalidFields.size());
- }
- }
-
- private static class TestItem implements Item {
-
- @Override
- public Property<String> getItemProperty(Object id) {
- return new StringProperty();
- }
-
- @Override
- public Collection<?> getItemPropertyIds() {
- return Arrays.asList("prop1", "prop2");
- }
-
- @Override
- public boolean addItemProperty(Object id, Property property)
- throws UnsupportedOperationException {
- return false;
- }
-
- @Override
- public boolean removeItemProperty(Object id)
- throws UnsupportedOperationException {
- return false;
- }
-
- }
-
- private static class StringProperty extends AbstractProperty<String> {
-
- @Override
- public String getValue() {
- return null;
- }
-
- @Override
- public void setValue(String newValue)
- throws Property.ReadOnlyException {
- }
-
- @Override
- public Class<? extends String> getType() {
- return String.class;
- }
- }
-
- }
|