diff options
Diffstat (limited to 'server/src/test/java/com/vaadin')
-rw-r--r-- | server/src/test/java/com/vaadin/data/BinderTest.java | 68 | ||||
-rw-r--r-- | server/src/test/java/com/vaadin/data/ResultTest.java | 105 | ||||
-rw-r--r-- | server/src/test/java/com/vaadin/data/ValidatorTest.java | 93 |
3 files changed, 266 insertions, 0 deletions
diff --git a/server/src/test/java/com/vaadin/data/BinderTest.java b/server/src/test/java/com/vaadin/data/BinderTest.java index 2e284ca292..c3193603ef 100644 --- a/server/src/test/java/com/vaadin/data/BinderTest.java +++ b/server/src/test/java/com/vaadin/data/BinderTest.java @@ -4,10 +4,18 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertSame; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import com.vaadin.data.Binder.Binding; +import com.vaadin.server.AbstractErrorMessage; +import com.vaadin.server.ErrorMessage; +import com.vaadin.server.UserError; import com.vaadin.tests.data.bean.Person; import com.vaadin.ui.AbstractField; @@ -189,6 +197,66 @@ public class BinderTest { Assert.assertEquals("", nameField.getValue()); } + @Test + public void validate_notBound_noErrors() { + Binder<Person> binder = new Binder<>(); + + List<ValidationError<?>> errors = binder.validate(); + + Assert.assertTrue(errors.isEmpty()); + } + + @Test + public void bound_validatorsAreOK_noErrors() { + Binder<Person> binder = new Binder<>(); + Binding<Person, String> binding = binder.forField(nameField); + binding.withValidator(Validator.alwaysPass()).bind(Person::getFirstName, + Person::setFirstName); + + nameField.setComponentError(new UserError("")); + List<ValidationError<?>> errors = binder.validate(); + + Assert.assertTrue(errors.isEmpty()); + Assert.assertNull(nameField.getComponentError()); + } + + @SuppressWarnings("serial") + @Test + public void bound_validatorsFail_errors() { + Binder<Person> binder = new Binder<>(); + Binding<Person, String> binding = binder.forField(nameField); + binding.withValidator(Validator.alwaysPass()); + String msg1 = "foo"; + String msg2 = "bar"; + binding.withValidator(new Validator<String>() { + @Override + public Result<String> apply(String value) { + return new SimpleResult<>(null, msg1); + } + }); + binding.withValidator(value -> false, msg2); + binding.bind(Person::getFirstName, Person::setFirstName); + + List<ValidationError<?>> errors = binder.validate(); + + Assert.assertEquals(2, errors.size()); + + Set<String> errorMessages = errors.stream() + .map(ValidationError::getMessage).collect(Collectors.toSet()); + Assert.assertTrue(errorMessages.contains(msg1)); + Assert.assertTrue(errorMessages.contains(msg2)); + + Set<?> fields = errors.stream().map(ValidationError::getField) + .collect(Collectors.toSet()); + Assert.assertEquals(1, fields.size()); + Assert.assertTrue(fields.contains(nameField)); + + ErrorMessage componentError = nameField.getComponentError(); + Assert.assertNotNull(componentError); + Assert.assertEquals("foo", + ((AbstractErrorMessage) componentError).getMessage()); + } + private void bindName() { binder.bind(nameField, Person::getFirstName, Person::setFirstName); binder.bind(p); diff --git a/server/src/test/java/com/vaadin/data/ResultTest.java b/server/src/test/java/com/vaadin/data/ResultTest.java new file mode 100644 index 0000000000..c01daa0123 --- /dev/null +++ b/server/src/test/java/com/vaadin/data/ResultTest.java @@ -0,0 +1,105 @@ +/* + * 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.data; + +import java.util.function.Function; + +import org.junit.Assert; +import org.junit.Test; + +/** + * @author Vaadin Ltd + * + */ +public class ResultTest { + + @Test + public void testOk() { + String value = "foo"; + Result<String> ok = Result.ok(value); + Assert.assertFalse(ok.isError()); + Assert.assertFalse(ok.getMessage().isPresent()); + ok.ifOk(v -> Assert.assertEquals(value, v)); + ok.ifError(msg -> Assert.fail()); + } + + @Test + public void testError() { + String message = "foo"; + Result<String> error = Result.error(message); + Assert.assertTrue(error.isError()); + Assert.assertTrue(error.getMessage().isPresent()); + error.ifOk(v -> Assert.fail()); + error.ifError(msg -> Assert.assertEquals(message, msg)); + Assert.assertEquals(message, error.getMessage().get()); + } + + @Test + public void of_noException() { + Result<String> result = Result.of(() -> "", exception -> null); + Assert.assertTrue(result instanceof SimpleResult); + Assert.assertFalse(((SimpleResult<?>) result).isError()); + } + + @Test + public void of_exception() { + String message = "foo"; + Result<String> result = Result.<String> of(() -> { + throw new RuntimeException(); + }, exception -> message); + Assert.assertTrue(result instanceof SimpleResult); + Assert.assertTrue(((SimpleResult<?>) result).isError()); + Assert.assertEquals(message, result.getMessage().get()); + } + + @SuppressWarnings("serial") + @Test + public void map_norError_mapperIsApplied() { + Result<String> result = new SimpleResult<String>("foo", null) { + + @Override + public <S> Result<S> flatMap(Function<String, Result<S>> mapper) { + return mapper.apply("foo"); + } + }; + Result<String> mapResult = result.map(value -> { + Assert.assertEquals("foo", value); + return "bar"; + }); + Assert.assertTrue(mapResult instanceof SimpleResult); + Assert.assertFalse(((SimpleResult<?>) mapResult).isError()); + mapResult.ifOk(v -> Assert.assertEquals("bar", v)); + } + + @SuppressWarnings("serial") + @Test + public void map_error_mapperIsApplied() { + Result<String> result = new SimpleResult<String>("foo", null) { + + @Override + public <S> Result<S> flatMap(Function<String, Result<S>> mapper) { + return new SimpleResult<S>(null, "bar"); + } + }; + Result<String> mapResult = result.map(value -> { + Assert.assertEquals("foo", value); + return "somevalue"; + }); + Assert.assertTrue(mapResult instanceof SimpleResult); + Assert.assertTrue(((SimpleResult<?>) mapResult).isError()); + mapResult.ifError(msg -> Assert.assertEquals("bar", msg)); + } +} diff --git a/server/src/test/java/com/vaadin/data/ValidatorTest.java b/server/src/test/java/com/vaadin/data/ValidatorTest.java new file mode 100644 index 0000000000..1a432c063c --- /dev/null +++ b/server/src/test/java/com/vaadin/data/ValidatorTest.java @@ -0,0 +1,93 @@ +/* + * 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.data; + +import java.util.Objects; + +import org.junit.Assert; +import org.junit.Test; + +/** + * @author Vaadin Ltd + * + */ +public class ValidatorTest { + + @Test + public void alwaysPass() { + Validator<String> alwaysPass = Validator.alwaysPass(); + Result<String> result = alwaysPass.apply("foo"); + Assert.assertTrue(result instanceof SimpleResult); + SimpleResult<String> implRes = (SimpleResult<String>) result; + Assert.assertFalse(implRes.getMessage().isPresent()); + } + + @Test + public void chain_alwaysPassAndError() { + Validator<String> alwaysPass = Validator.alwaysPass(); + Validator<String> chain = alwaysPass + .chain(value -> Result.error("foo")); + Result<String> result = chain.apply("bar"); + Assert.assertTrue(result.isError()); + Assert.assertEquals("foo", result.getMessage().get()); + } + + @SuppressWarnings("serial") + @Test + public void chain_mixture() { + Validator<String> first = new Validator<String>() { + + @Override + public Result<String> apply(String value) { + if (value == null) { + return Result.error("Cannot be null"); + } + return Result.ok(value); + } + }; + Validator<String> second = new Validator<String>() { + + @Override + public Result<String> apply(String value) { + if (value != null && value.isEmpty()) { + return Result.error("Cannot be empty"); + } + return Result.ok(value); + } + }; + + Validator<String> chain = first.chain(second); + Result<String> result = chain.apply("bar"); + Assert.assertFalse(result.isError()); + + result = chain.apply(null); + Assert.assertTrue(result.isError()); + + result = chain.apply(""); + Assert.assertTrue(result.isError()); + } + + @Test + public void from() { + Validator<String> validator = Validator.from(Objects::nonNull, + "Cannot be null"); + Result<String> result = validator.apply(null); + Assert.assertTrue(result.isError()); + + result = validator.apply(""); + Assert.assertFalse(result.isError()); + } +} |