From: Denis Anisimov Date: Tue, 1 Nov 2016 09:38:17 +0000 (+0200) Subject: Introduces ValidationResult interface to replace Result in validators. X-Git-Tag: 8.0.0.alpha6~25 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e4d132f4cb84a7aafdf70b5e5ca2121b66c87bfa;p=vaadin-framework.git Introduces ValidationResult interface to replace Result in validators. Fixes vaadin/framework8-issues#414 Change-Id: I2d4e101f29542fadfdd30ce63455a0e078e0e735 --- diff --git a/server/src/main/java/com/vaadin/data/Binder.java b/server/src/main/java/com/vaadin/data/Binder.java index e139bcd5c8..f8ddace8a6 100644 --- a/server/src/main/java/com/vaadin/data/Binder.java +++ b/server/src/main/java/com/vaadin/data/Binder.java @@ -619,6 +619,26 @@ public class Binder implements Serializable { return status; } + /** + * Returns the field value run through all converters and validators, + * but doesn't pass the {@link ValidationStatus} to any status handler. + * + * @return the conversation result + */ + private Result doConversation() { + FIELDVALUE fieldValue = field.getValue(); + return converterValidatorChain.convertToModel(fieldValue, + createValueContext()); + } + + private ValidationStatus toValidationStatus( + Result result) { + return new ValidationStatus<>(this, + result.isError() + ? ValidationResult.error(result.getMessage().get()) + : ValidationResult.ok()); + } + /** * Returns the field value run through all converters and validators, * but doesn't pass the {@link ValidationStatus} to any status handler. @@ -626,10 +646,7 @@ public class Binder implements Serializable { * @return the validation status */ private ValidationStatus doValidation() { - FIELDVALUE fieldValue = field.getValue(); - Result dataValue = converterValidatorChain - .convertToModel(fieldValue, createValueContext()); - return new ValidationStatus<>(this, dataValue); + return toValidationStatus(doConversation()); } /** @@ -677,7 +694,7 @@ public class Binder implements Serializable { // store field value if valid ValidationStatus fieldValidationStatus = writeFieldValue( bean); - List> binderValidationResults; + List binderValidationResults; // if all field level validations pass, run bean level validation if (!getBinder().bindings.stream().map(BindingImpl::doValidation) .anyMatch(ValidationStatus::isError)) { @@ -702,12 +719,11 @@ public class Binder implements Serializable { private ValidationStatus writeFieldValue(BEAN bean) { assert bean != null; - ValidationStatus validationStatus = doValidation(); + Result result = doConversation(); if (setter != null) { - validationStatus.getResult().ifPresent(result -> result - .ifOk(value -> setter.accept(bean, value))); + result.ifOk(value -> setter.accept(bean, value)); } - return validationStatus; + return toValidationStatus(result); } private void notifyStatusHandler(ValidationStatus status) { @@ -740,10 +756,9 @@ public class Binder implements Serializable { @Override public Result convertToModel(T value, ValueContext context) { - Result validationResult = validator.apply(value, - context); + ValidationResult validationResult = validator.apply(value, context); if (validationResult.isError()) { - return Result.error(validationResult.getMessage().get()); + return Result.error(validationResult.getErrorMessage()); } else { return Result.ok(value); } @@ -1069,9 +1084,9 @@ public class Binder implements Serializable { bindings.forEach(binding -> binding.writeFieldValue(bean)); // Now run bean level validation against the updated bean - List> binderResults = validateBean(bean); - boolean hasErrors = binderResults.stream().filter(Result::isError) - .findAny().isPresent(); + List binderResults = validateBean(bean); + boolean hasErrors = binderResults.stream() + .filter(ValidationResult::isError).findAny().isPresent(); if (hasErrors) { // Bean validator failed, revert values bindings.forEach((BindingImpl binding) -> binding.setter @@ -1216,9 +1231,9 @@ public class Binder implements Serializable { * @return a list of validation errors or an empty list if validation * succeeded */ - private List> validateBean(BEAN bean) { + private List validateBean(BEAN bean) { Objects.requireNonNull(bean, "bean cannot be null"); - List> results = Collections.unmodifiableList(validators + List results = Collections.unmodifiableList(validators .stream() .map(validator -> validator.apply(bean, new ValueContext())) .collect(Collectors.toList())); @@ -1444,7 +1459,7 @@ public class Binder implements Serializable { // show first possible error or OK status in the label if set if (getStatusLabel().isPresent()) { String statusMessage = binderStatus.getBeanValidationErrors() - .stream().findFirst().flatMap(Result::getMessage) + .stream().findFirst().map(ValidationResult::getErrorMessage) .orElse(""); getStatusLabel().get().setValue(statusMessage); } diff --git a/server/src/main/java/com/vaadin/data/BinderValidationStatus.java b/server/src/main/java/com/vaadin/data/BinderValidationStatus.java index 9b7e40f516..41a181dacf 100644 --- a/server/src/main/java/com/vaadin/data/BinderValidationStatus.java +++ b/server/src/main/java/com/vaadin/data/BinderValidationStatus.java @@ -53,7 +53,7 @@ public class BinderValidationStatus implements Serializable { private final Binder binder; private final List> bindingStatuses; - private final List> binderStatuses; + private final List binderStatuses; /** * Convenience method for creating a unresolved validation status for the @@ -90,7 +90,7 @@ public class BinderValidationStatus implements Serializable { */ public BinderValidationStatus(Binder source, List> bindingStatuses, - List> binderStatuses) { + List binderStatuses) { Objects.requireNonNull(binderStatuses, "binding statuses cannot be null"); Objects.requireNonNull(binderStatuses, @@ -116,8 +116,8 @@ public class BinderValidationStatus implements Serializable { * passed */ public boolean hasErrors() { - return binderStatuses.stream().filter(Result::isError).findAny() - .isPresent() + return binderStatuses.stream().filter(ValidationResult::isError) + .findAny().isPresent() || bindingStatuses.stream().filter(ValidationStatus::isError) .findAny().isPresent(); } @@ -136,10 +136,11 @@ public class BinderValidationStatus implements Serializable { * * @return a list of all validation errors */ - public List> getValidationErrors() { - ArrayList> errors = new ArrayList<>(getFieldValidationErrors() - .stream().map(s -> s.getResult().get()) - .collect(Collectors.toList())); + public List getValidationErrors() { + ArrayList errors = new ArrayList<>( + getFieldValidationErrors().stream() + .map(s -> s.getResult().get()) + .collect(Collectors.toList())); errors.addAll(getBeanValidationErrors()); return errors; } @@ -166,7 +167,7 @@ public class BinderValidationStatus implements Serializable { * * @return the bean level validation results */ - public List> getBeanValidationResults() { + public List getBeanValidationResults() { return binderStatuses; } @@ -193,8 +194,8 @@ public class BinderValidationStatus implements Serializable { * * @return a list of failed bean level validation results */ - public List> getBeanValidationErrors() { - return binderStatuses.stream().filter(Result::isError) + public List getBeanValidationErrors() { + return binderStatuses.stream().filter(ValidationResult::isError) .collect(Collectors.toList()); } } diff --git a/server/src/main/java/com/vaadin/data/Result.java b/server/src/main/java/com/vaadin/data/Result.java index 7f63efa6a6..e2e7c61e84 100644 --- a/server/src/main/java/com/vaadin/data/Result.java +++ b/server/src/main/java/com/vaadin/data/Result.java @@ -24,9 +24,9 @@ import java.util.function.Function; import java.util.function.Supplier; /** - * Represents the result of an operation that might fail, such as input - * validation or type conversion. A result may contain either a value, - * signifying a successful operation, or an error message in case of a failure. + * Represents the result of an operation that might fail, such as type + * conversion. A result may contain either a value, signifying a successful + * operation, or an error message in case of a failure. *

* Result instances are created using the factory methods {@link #ok(R)} and * {@link #error(String)}, denoting success and failure respectively. diff --git a/server/src/main/java/com/vaadin/data/ValidationException.java b/server/src/main/java/com/vaadin/data/ValidationException.java index 093d88283a..0f86dd5186 100644 --- a/server/src/main/java/com/vaadin/data/ValidationException.java +++ b/server/src/main/java/com/vaadin/data/ValidationException.java @@ -34,7 +34,7 @@ import java.util.stream.Collectors; public class ValidationException extends Exception { private final List> fieldValidationErrors; - private final List> beanValidationErrors; + private final List beanValidationErrors; /** * Constructs a new exception with validation {@code errors} list. @@ -45,7 +45,7 @@ public class ValidationException extends Exception { * binder validation errors list */ public ValidationException(List> fieldValidationErrors, - List> beanValidationErrors) { + List beanValidationErrors) { super("Validation has failed for some fields"); this.fieldValidationErrors = Collections .unmodifiableList(fieldValidationErrors); @@ -58,10 +58,11 @@ public class ValidationException extends Exception { * * @return a list of all validation errors */ - public List> getValidationErrors() { - ArrayList> errors = new ArrayList<>(getFieldValidationErrors() - .stream().map(s -> s.getResult().get()) - .collect(Collectors.toList())); + public List getValidationErrors() { + ArrayList errors = new ArrayList<>( + getFieldValidationErrors().stream() + .map(s -> s.getResult().get()) + .collect(Collectors.toList())); errors.addAll(getBeanValidationErrors()); return errors; } @@ -84,7 +85,7 @@ public class ValidationException extends Exception { * * @return binder validation errors list */ - public List> getBeanValidationErrors() { + public List getBeanValidationErrors() { return beanValidationErrors; } } diff --git a/server/src/main/java/com/vaadin/data/ValidationResult.java b/server/src/main/java/com/vaadin/data/ValidationResult.java new file mode 100644 index 0000000000..489631005f --- /dev/null +++ b/server/src/main/java/com/vaadin/data/ValidationResult.java @@ -0,0 +1,103 @@ +/* + * Copyright 2000-2016 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.io.Serializable; +import java.util.Objects; + +/** + * Represents the result of a validation. A result may be either successful or + * contain an error message in case of a failure. + *

+ * ValidationResult instances are created using the factory methods + * {@link #ok()} and {@link #error(String)}, denoting success and failure + * respectively. + * + * @author Vaadin Ltd + * @since 8.0 + * + */ +public interface ValidationResult extends Serializable { + + class SimpleValidationResult implements ValidationResult { + + private final String error; + + SimpleValidationResult(String error) { + this.error = error; + } + + @Override + public String getErrorMessage() { + if (error == null) { + throw new IllegalStateException("The result is not an error. " + + "It cannot contain error message"); + } else { + return error; + } + } + + @Override + public boolean isError() { + return error != null; + } + + } + + /** + * Returns the result message. + *

+ * Throws an {@link IllegalStateException} if the result represents success. + * + * @return the error message + * @throws IllegalStateException + * if the result represents success + */ + String getErrorMessage(); + + /** + * Checks if the result denotes an error. + * + * @return true if the result denotes an error, + * false otherwise + */ + boolean isError(); + + /** + * Returns a successful result. + * + * @return the successful result + */ + public static ValidationResult ok() { + return new SimpleValidationResult(null); + } + + /** + * Creates the validation result which represent an error with the given + * {@code errorMessage}. + * + * @param errorMessage + * error message, not {@code null} + * @return validation result which represent an error with the given + * {@code errorMessage} + * @throws NullPointerException + * if {@code errorMessage} is null + */ + public static ValidationResult error(String errorMessage) { + Objects.requireNonNull(errorMessage); + return new SimpleValidationResult(errorMessage); + } +} diff --git a/server/src/main/java/com/vaadin/data/ValidationStatus.java b/server/src/main/java/com/vaadin/data/ValidationStatus.java index f1f3da2d09..66e34eb6dd 100644 --- a/server/src/main/java/com/vaadin/data/ValidationStatus.java +++ b/server/src/main/java/com/vaadin/data/ValidationStatus.java @@ -22,8 +22,11 @@ import java.util.Optional; import com.vaadin.data.Binder.Binding; /** - * Represents the outcome of field level validation. Use - * {@link Binding#withValidationStatusHandler(ValidationStatusHandler)} to + * Represents the status of field validation. Status can be {@code Status.OK}, + * {@code Status.ERROR} or {@code Status.RESET}. Status OK and ERROR are always + * associated with a ValidationResult {@link #getResult}. + *

+ * Use {@link Binding#withValidationStatusHandler(ValidationStatusHandler)} to * register a handler for field level validation status changes. * * @author Vaadin Ltd @@ -65,7 +68,7 @@ public class ValidationStatus implements Serializable { } private final Status status; - private final Result result; + private final ValidationResult result; private final Binding binding; /** @@ -94,7 +97,7 @@ public class ValidationStatus implements Serializable { * the result of the validation */ public ValidationStatus(Binding source, - Result result) { + ValidationResult result) { this(source, result.isError() ? Status.ERROR : Status.OK, result); } @@ -112,7 +115,7 @@ public class ValidationStatus implements Serializable { * the related result, may be {@code null} */ public ValidationStatus(Binding source, Status status, - Result result) { + ValidationResult result) { Objects.requireNonNull(source, "Event source may not be null"); Objects.requireNonNull(status, "Status may not be null"); if (Objects.equals(status, Status.OK) && result.isError() @@ -154,7 +157,8 @@ public class ValidationStatus implements Serializable { * status is not an error */ public Optional getMessage() { - return Optional.ofNullable(result).flatMap(Result::getMessage); + return Optional.ofNullable(result).filter(ValidationResult::isError) + .map(ValidationResult::getErrorMessage); } /** @@ -164,7 +168,7 @@ public class ValidationStatus implements Serializable { * * @return the validation result */ - public Optional> getResult() { + public Optional getResult() { return Optional.ofNullable(result); } diff --git a/server/src/main/java/com/vaadin/data/Validator.java b/server/src/main/java/com/vaadin/data/Validator.java index 45a88bc7f3..33da20b945 100644 --- a/server/src/main/java/com/vaadin/data/Validator.java +++ b/server/src/main/java/com/vaadin/data/Validator.java @@ -34,9 +34,9 @@ import com.vaadin.server.SerializablePredicate; *

  * Validator<Integer> v = num -> {
  *     if (num >= 0)
- *         return Result.ok(num);
+ *         return ValidationResult.ok();
  *     else
- *         return Result.error("number must be positive");
+ *         return ValidationResult.error("number must be positive");
  * };
  * 
* @@ -45,15 +45,15 @@ import com.vaadin.server.SerializablePredicate; * @param * the type of the value to validate * - * @see Result + * @see ValidationResult */ @FunctionalInterface public interface Validator - extends BiFunction>, Serializable { + extends BiFunction, Serializable { /** - * Validates the given value. Returns a {@code Result} instance representing - * the outcome of the validation. + * Validates the given value. Returns a {@code ValidationResult} instance + * representing the outcome of the validation. * * @param value * the input value to validate @@ -62,7 +62,7 @@ public interface Validator * @return the validation result */ @Override - public Result apply(T value, ValueContext context); + public ValidationResult apply(T value, ValueContext context); /** * Returns a validator that passes any value. @@ -72,14 +72,15 @@ public interface Validator * @return an always-passing validator */ public static Validator alwaysPass() { - return (v, ctx) -> Result.ok(v); + return (value, context) -> ValidationResult.ok(); } /** * Builds a validator out of a conditional function and an error message. If * the function returns true, the validator returns {@code Result.ok()}; if - * it returns false or throws an exception, {@code Result.error()} is - * returned with the given message. + * it returns false or throws an exception, + * {@link ValidationResult#error(String)} is returned with the given + * message. *

* For instance, the following validator checks if a number is between 0 and * 10, inclusive: @@ -126,12 +127,14 @@ public interface Validator return (value, context) -> { try { if (guard.test(value)) { - return Result.ok(value); + return ValidationResult.ok(); } else { - return Result.error(errorMessageProvider.apply(context)); + return ValidationResult + .error(errorMessageProvider.apply(context)); } } catch (Exception e) { - return Result.error(errorMessageProvider.apply(context)); + return ValidationResult + .error(errorMessageProvider.apply(context)); } }; } diff --git a/server/src/main/java/com/vaadin/data/validator/AbstractValidator.java b/server/src/main/java/com/vaadin/data/validator/AbstractValidator.java index b798d2bf73..793797ee3f 100644 --- a/server/src/main/java/com/vaadin/data/validator/AbstractValidator.java +++ b/server/src/main/java/com/vaadin/data/validator/AbstractValidator.java @@ -17,7 +17,7 @@ package com.vaadin.data.validator; import java.util.Objects; -import com.vaadin.data.Result; +import com.vaadin.data.ValidationResult; import com.vaadin.data.Validator; import com.vaadin.server.SerializableFunction; @@ -79,7 +79,8 @@ public abstract class AbstractValidator implements Validator { * whether the value is valid or not * @return the validation result */ - protected Result toResult(T value, boolean isValid) { - return isValid ? Result.ok(value) : Result.error(getMessage(value)); + protected ValidationResult toResult(T value, boolean isValid) { + return isValid ? ValidationResult.ok() + : ValidationResult.error(getMessage(value)); } } diff --git a/server/src/main/java/com/vaadin/data/validator/BeanValidator.java b/server/src/main/java/com/vaadin/data/validator/BeanValidator.java index 53886c9840..a3f9b86a6e 100644 --- a/server/src/main/java/com/vaadin/data/validator/BeanValidator.java +++ b/server/src/main/java/com/vaadin/data/validator/BeanValidator.java @@ -19,8 +19,8 @@ package com.vaadin.data.validator; import java.io.Serializable; import java.util.Locale; import java.util.Objects; +import java.util.Optional; import java.util.Set; -import java.util.function.BinaryOperator; import javax.validation.ConstraintViolation; import javax.validation.MessageInterpolator.Context; @@ -28,7 +28,7 @@ import javax.validation.Validation; import javax.validation.ValidatorFactory; import javax.validation.metadata.ConstraintDescriptor; -import com.vaadin.data.Result; +import com.vaadin.data.ValidationResult; import com.vaadin.data.Validator; import com.vaadin.data.util.BeanUtil; import com.vaadin.data.util.converter.ValueContext; @@ -116,16 +116,17 @@ public class BeanValidator implements Validator { * @return the validation result */ @Override - public Result apply(final Object value, ValueContext context) { + public ValidationResult apply(final Object value, ValueContext context) { Set> violations = getJavaxBeanValidator() .validateValue(beanType, propertyName, value); - BinaryOperator> accumulator = (result1, - result2) -> result1.flatMap(val -> result2); Locale locale = context.getLocale().orElse(Locale.getDefault()); - return violations.stream().map(v -> Result.error(getMessage(v, locale))) - .reduce(Result.ok(value), accumulator); + Optional result = violations.stream() + .map(violation -> ValidationResult + .error(getMessage(violation, locale))) + .findFirst(); + return result.orElse(ValidationResult.ok()); } @Override diff --git a/server/src/main/java/com/vaadin/data/validator/NotEmptyValidator.java b/server/src/main/java/com/vaadin/data/validator/NotEmptyValidator.java index b033538949..2223c333cb 100644 --- a/server/src/main/java/com/vaadin/data/validator/NotEmptyValidator.java +++ b/server/src/main/java/com/vaadin/data/validator/NotEmptyValidator.java @@ -18,7 +18,7 @@ package com.vaadin.data.validator; import java.util.Objects; import com.vaadin.data.HasValue; -import com.vaadin.data.Result; +import com.vaadin.data.ValidationResult; import com.vaadin.data.Validator; import com.vaadin.data.util.converter.ValueContext; @@ -67,11 +67,11 @@ public class NotEmptyValidator implements Validator { } @Override - public Result apply(T value, ValueContext context) { + public ValidationResult apply(T value, ValueContext context) { if (Objects.isNull(value) || Objects.equals(value, "")) { - return Result.error(message); + return ValidationResult.error(message); } else { - return Result.ok(value); + return ValidationResult.ok(); } } diff --git a/server/src/main/java/com/vaadin/data/validator/NotNullValidator.java b/server/src/main/java/com/vaadin/data/validator/NotNullValidator.java index 349e000c68..c92aefac3b 100644 --- a/server/src/main/java/com/vaadin/data/validator/NotNullValidator.java +++ b/server/src/main/java/com/vaadin/data/validator/NotNullValidator.java @@ -18,7 +18,7 @@ package com.vaadin.data.validator; import java.util.Objects; -import com.vaadin.data.Result; +import com.vaadin.data.ValidationResult; import com.vaadin.data.util.converter.ValueContext; /** @@ -42,9 +42,9 @@ public class NotNullValidator extends AbstractValidator { } @Override - public Result apply(String value, ValueContext context) { - return Objects.isNull(value) ? Result.error(getMessage(value)) - : Result.ok(value); + public ValidationResult apply(String value, ValueContext context) { + return Objects.isNull(value) ? ValidationResult.error(getMessage(value)) + : ValidationResult.ok(); } } diff --git a/server/src/main/java/com/vaadin/data/validator/RangeValidator.java b/server/src/main/java/com/vaadin/data/validator/RangeValidator.java index 32effdd811..b8ad387308 100644 --- a/server/src/main/java/com/vaadin/data/validator/RangeValidator.java +++ b/server/src/main/java/com/vaadin/data/validator/RangeValidator.java @@ -18,7 +18,7 @@ package com.vaadin.data.validator; import java.util.Comparator; import java.util.Objects; -import com.vaadin.data.Result; +import com.vaadin.data.ValidationResult; import com.vaadin.data.util.converter.ValueContext; /** @@ -98,7 +98,7 @@ public class RangeValidator extends AbstractValidator { * behavior depends on the used comparator. */ @Override - public Result apply(T value, ValueContext context) { + public ValidationResult apply(T value, ValueContext context) { return toResult(value, isValid(value)); } diff --git a/server/src/main/java/com/vaadin/data/validator/RegexpValidator.java b/server/src/main/java/com/vaadin/data/validator/RegexpValidator.java index b9763e5199..89e4d81a53 100644 --- a/server/src/main/java/com/vaadin/data/validator/RegexpValidator.java +++ b/server/src/main/java/com/vaadin/data/validator/RegexpValidator.java @@ -18,7 +18,7 @@ package com.vaadin.data.validator; import java.util.regex.Matcher; import java.util.regex.Pattern; -import com.vaadin.data.Result; +import com.vaadin.data.ValidationResult; import com.vaadin.data.util.converter.ValueContext; /** @@ -71,7 +71,7 @@ public class RegexpValidator extends AbstractValidator { } @Override - public Result apply(String value, ValueContext context) { + public ValidationResult apply(String value, ValueContext context) { return toResult(value, isValid(value)); } diff --git a/server/src/main/java/com/vaadin/data/validator/StringLengthValidator.java b/server/src/main/java/com/vaadin/data/validator/StringLengthValidator.java index 421cf13a29..b3e1da18eb 100644 --- a/server/src/main/java/com/vaadin/data/validator/StringLengthValidator.java +++ b/server/src/main/java/com/vaadin/data/validator/StringLengthValidator.java @@ -16,7 +16,7 @@ package com.vaadin.data.validator; -import com.vaadin.data.Result; +import com.vaadin.data.ValidationResult; import com.vaadin.data.util.converter.ValueContext; /** @@ -50,11 +50,11 @@ public class StringLengthValidator extends AbstractValidator { } @Override - public Result apply(String value, ValueContext context) { + public ValidationResult apply(String value, ValueContext context) { if (value == null) { return toResult(value, true); } - Result lengthCheck = validator.apply(value.length(), context); + ValidationResult lengthCheck = validator.apply(value.length(), context); return toResult(value, !lengthCheck.isError()); } diff --git a/server/src/main/java/com/vaadin/ui/AbstractDateField.java b/server/src/main/java/com/vaadin/ui/AbstractDateField.java index bb90ce53be..6e59d9bbe0 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractDateField.java +++ b/server/src/main/java/com/vaadin/ui/AbstractDateField.java @@ -30,6 +30,7 @@ import java.util.logging.Logger; import org.jsoup.nodes.Element; import com.vaadin.data.Result; +import com.vaadin.data.ValidationResult; import com.vaadin.data.util.converter.ValueContext; import com.vaadin.data.validator.DateRangeValidator; import com.vaadin.event.FieldEvents.BlurEvent; @@ -678,7 +679,7 @@ public abstract class AbstractDateField extends AbstractField getDateOutOfRangeMessage(), getDate(getRangeStart(), getResolution()), getDate(getRangeEnd(), getResolution())); - Result result = validator.apply(value, + ValidationResult result = validator.apply(value, new ValueContext(this)); if (result.isError()) { setComponentError(new UserError(getDateOutOfRangeMessage())); diff --git a/server/src/test/java/com/vaadin/data/BinderBookOfVaadinTest.java b/server/src/test/java/com/vaadin/data/BinderBookOfVaadinTest.java index 9c0ffd31e7..8875cf3821 100644 --- a/server/src/test/java/com/vaadin/data/BinderBookOfVaadinTest.java +++ b/server/src/test/java/com/vaadin/data/BinderBookOfVaadinTest.java @@ -610,7 +610,7 @@ public class BinderBookOfVaadinTest { Assert.assertEquals(0, status.getFieldValidationErrors().size()); Assert.assertEquals(1, status.getBeanValidationErrors().size()); Assert.assertEquals( - status.getBeanValidationErrors().get(0).getMessage().get(), + status.getBeanValidationErrors().get(0).getErrorMessage(), message); Assert.assertEquals(message, formStatusLabel.getValue()); @@ -644,9 +644,10 @@ public class BinderBookOfVaadinTest { binder.setValidationStatusHandler(status -> { // create an error message on failed bean level validations - List> errors = status.getBeanValidationErrors(); - String errorMessage = errors.stream().map(Result::getMessage) - .map(o -> o.get()).collect(Collectors.joining("\n")); + List errors = status.getBeanValidationErrors(); + String errorMessage = errors.stream() + .map(ValidationResult::getErrorMessage) + .collect(Collectors.joining("\n")); // show error in a label formStatusLabel.setValue(errorMessage); formStatusLabel.setVisible(!errorMessage.isEmpty()); @@ -685,7 +686,7 @@ public class BinderBookOfVaadinTest { Assert.assertEquals(0, status.getFieldValidationErrors().size()); Assert.assertEquals(1, status.getBeanValidationErrors().size()); Assert.assertEquals(message, - status.getBeanValidationErrors().get(0).getMessage().get()); + status.getBeanValidationErrors().get(0).getErrorMessage()); Assert.assertEquals(message, formStatusLabel.getValue()); diff --git a/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java b/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java index a0fc5cc6fa..0711ae24bf 100644 --- a/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java +++ b/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java @@ -93,8 +93,8 @@ public class BinderConverterValidatorTest String msg2 = "bar"; binding.withValidator(new Validator() { @Override - public Result apply(String value, ValueContext context) { - return new SimpleResult<>(null, msg1); + public ValidationResult apply(String value, ValueContext context) { + return ValidationResult.error(msg1); } }); binding.withValidator(value -> false, msg2); @@ -128,9 +128,9 @@ public class BinderConverterValidatorTest TextField salaryField = new TextField(); Validator positiveNumberValidator = (value, context) -> { if (value.doubleValue() >= 0) { - return Result.ok(value); + return ValidationResult.ok(); } else { - return Result.error(NEGATIVE_ERROR_MESSAGE); + return ValidationResult.error(NEGATIVE_ERROR_MESSAGE); } }; binder.forField(salaryField) diff --git a/server/src/test/java/com/vaadin/data/BinderValidationStatusTest.java b/server/src/test/java/com/vaadin/data/BinderValidationStatusTest.java index 9066bf4dbe..b884d85990 100644 --- a/server/src/test/java/com/vaadin/data/BinderValidationStatusTest.java +++ b/server/src/test/java/com/vaadin/data/BinderValidationStatusTest.java @@ -386,7 +386,7 @@ public class BinderValidationStatusTest Assert.assertEquals(1, status.getBeanValidationErrors().size()); Assert.assertEquals("Need first name and age", - status.getBeanValidationErrors().get(0).getMessage().get()); + status.getBeanValidationErrors().get(0).getErrorMessage()); } @Test diff --git a/server/src/test/java/com/vaadin/data/ValidatorTest.java b/server/src/test/java/com/vaadin/data/ValidatorTest.java index fa533c5010..9640550d95 100644 --- a/server/src/test/java/com/vaadin/data/ValidatorTest.java +++ b/server/src/test/java/com/vaadin/data/ValidatorTest.java @@ -33,17 +33,15 @@ public class ValidatorTest extends ValidatorTestBase { @Test public void alwaysPass() { Validator alwaysPass = Validator.alwaysPass(); - Result result = alwaysPass.apply("foo", new ValueContext()); - Assert.assertTrue(result instanceof SimpleResult); - SimpleResult implRes = (SimpleResult) result; - Assert.assertFalse(implRes.getMessage().isPresent()); + ValidationResult result = alwaysPass.apply("foo", new ValueContext()); + Assert.assertFalse(result.isError()); } @Test public void from() { Validator validator = Validator.from(Objects::nonNull, "Cannot be null"); - Result result = validator.apply(null, new ValueContext()); + ValidationResult result = validator.apply(null, new ValueContext()); Assert.assertTrue(result.isError()); result = validator.apply("", new ValueContext()); diff --git a/server/src/test/java/com/vaadin/data/validator/NotEmptyValidatorTest.java b/server/src/test/java/com/vaadin/data/validator/NotEmptyValidatorTest.java index 85e43d2c65..ae1bb4fe17 100644 --- a/server/src/test/java/com/vaadin/data/validator/NotEmptyValidatorTest.java +++ b/server/src/test/java/com/vaadin/data/validator/NotEmptyValidatorTest.java @@ -18,7 +18,7 @@ package com.vaadin.data.validator; import org.junit.Assert; import org.junit.Test; -import com.vaadin.data.Result; +import com.vaadin.data.ValidationResult; import com.vaadin.data.util.converter.ValueContext; /** @@ -30,26 +30,25 @@ public class NotEmptyValidatorTest { @Test public void nullValueIsDisallowed() { NotEmptyValidator validator = new NotEmptyValidator<>("foo"); - Result result = validator.apply(null, new ValueContext()); + ValidationResult result = validator.apply(null, new ValueContext()); Assert.assertTrue(result.isError()); - Assert.assertEquals("foo", result.getMessage().get()); + Assert.assertEquals("foo", result.getErrorMessage()); } @Test public void emptyValueIsDisallowed() { NotEmptyValidator validator = new NotEmptyValidator<>("foo"); - Result result = validator.apply("", new ValueContext()); + ValidationResult result = validator.apply("", new ValueContext()); Assert.assertTrue(result.isError()); - Assert.assertEquals("foo", result.getMessage().get()); + Assert.assertEquals("foo", result.getErrorMessage()); } @Test public void nonNullValueIsAllowed() { NotEmptyValidator validator = new NotEmptyValidator<>("foo"); Object value = new Object(); - Result result = validator.apply(value, new ValueContext()); + ValidationResult result = validator.apply(value, new ValueContext()); + Assert.assertFalse(result.isError()); Assert.assertFalse(result.isError()); - result.ifOk(val -> Assert.assertEquals(value, val)); - result.ifError(msg -> Assert.fail()); } } diff --git a/server/src/test/java/com/vaadin/data/validator/NotNullValidatorTest.java b/server/src/test/java/com/vaadin/data/validator/NotNullValidatorTest.java index e3e76fbf53..24b95bf58a 100644 --- a/server/src/test/java/com/vaadin/data/validator/NotNullValidatorTest.java +++ b/server/src/test/java/com/vaadin/data/validator/NotNullValidatorTest.java @@ -18,7 +18,7 @@ package com.vaadin.data.validator; import org.junit.Assert; import org.junit.Test; -import com.vaadin.data.Result; +import com.vaadin.data.ValidationResult; import com.vaadin.data.util.converter.ValueContext; public class NotNullValidatorTest { @@ -26,18 +26,17 @@ public class NotNullValidatorTest { @Test public void nullValueIsDisallowed() { NotNullValidator validator = new NotNullValidator("foo"); - Result result = validator.apply(null, new ValueContext()); + ValidationResult result = validator.apply(null, new ValueContext()); Assert.assertTrue(result.isError()); - Assert.assertEquals("foo", result.getMessage().get()); + Assert.assertEquals("foo", result.getErrorMessage()); } @Test public void nonNullValueIsAllowed() { NotNullValidator validator = new NotNullValidator("foo"); - Result result = validator.apply("bar", new ValueContext()); + ValidationResult result = validator.apply("bar", new ValueContext()); + Assert.assertFalse(result.isError()); Assert.assertFalse(result.isError()); - result.ifOk(value -> Assert.assertEquals("bar", value)); - result.ifError(msg -> Assert.fail()); } } diff --git a/server/src/test/java/com/vaadin/data/validator/ValidatorTestBase.java b/server/src/test/java/com/vaadin/data/validator/ValidatorTestBase.java index 5a7198f33c..c8432324ad 100644 --- a/server/src/test/java/com/vaadin/data/validator/ValidatorTestBase.java +++ b/server/src/test/java/com/vaadin/data/validator/ValidatorTestBase.java @@ -5,6 +5,7 @@ import java.util.Locale; import org.junit.Assert; import org.junit.Before; +import com.vaadin.data.ValidationResult; import com.vaadin.data.Validator; import com.vaadin.data.util.converter.ValueContext; import com.vaadin.ui.Label; @@ -18,17 +19,20 @@ public class ValidatorTestBase { localeContext = new Label(); } - protected void assertPasses(T value, Validator v) { - v.apply(value, new ValueContext()) - .handle(val -> Assert.assertEquals(value, val), err -> Assert - .fail(value + " should pass " + v + " but got " + err)); + protected void assertPasses(T value, Validator validator) { + ValidationResult result = validator.apply(value, new ValueContext()); + if (result.isError()) { + Assert.fail(value + " should pass " + validator + " but got " + + result.getErrorMessage()); + } } protected void assertFails(T value, String errorMessage, - Validator v) { - v.apply(value, new ValueContext(localeContext)).handle( - val -> Assert.fail(value + " should fail " + v), - err -> Assert.assertEquals(errorMessage, err)); + Validator validator) { + ValidationResult result = validator.apply(value, + new ValueContext(localeContext)); + Assert.assertTrue(result.isError()); + Assert.assertEquals(errorMessage, result.getErrorMessage()); } protected void assertFails(T value, AbstractValidator v) {