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<TARGET> doConversation() {
+ FIELDVALUE fieldValue = field.getValue();
+ return converterValidatorChain.convertToModel(fieldValue,
+ createValueContext());
+ }
+
+ private ValidationStatus<TARGET> toValidationStatus(
+ Result<TARGET> 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.
* @return the validation status
*/
private ValidationStatus<TARGET> doValidation() {
- FIELDVALUE fieldValue = field.getValue();
- Result<TARGET> dataValue = converterValidatorChain
- .convertToModel(fieldValue, createValueContext());
- return new ValidationStatus<>(this, dataValue);
+ return toValidationStatus(doConversation());
}
/**
// store field value if valid
ValidationStatus<TARGET> fieldValidationStatus = writeFieldValue(
bean);
- List<Result<?>> binderValidationResults;
+ List<ValidationResult> binderValidationResults;
// if all field level validations pass, run bean level validation
if (!getBinder().bindings.stream().map(BindingImpl::doValidation)
.anyMatch(ValidationStatus::isError)) {
private ValidationStatus<TARGET> writeFieldValue(BEAN bean) {
assert bean != null;
- ValidationStatus<TARGET> validationStatus = doValidation();
+ Result<TARGET> 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) {
@Override
public Result<T> convertToModel(T value, ValueContext context) {
- Result<? super T> 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);
}
bindings.forEach(binding -> binding.writeFieldValue(bean));
// Now run bean level validation against the updated bean
- List<Result<?>> binderResults = validateBean(bean);
- boolean hasErrors = binderResults.stream().filter(Result::isError)
- .findAny().isPresent();
+ List<ValidationResult> 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
* @return a list of validation errors or an empty list if validation
* succeeded
*/
- private List<Result<?>> validateBean(BEAN bean) {
+ private List<ValidationResult> validateBean(BEAN bean) {
Objects.requireNonNull(bean, "bean cannot be null");
- List<Result<?>> results = Collections.unmodifiableList(validators
+ List<ValidationResult> results = Collections.unmodifiableList(validators
.stream()
.map(validator -> validator.apply(bean, new ValueContext()))
.collect(Collectors.toList()));
// 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);
}
private final Binder<BEAN> binder;
private final List<ValidationStatus<?>> bindingStatuses;
- private final List<Result<?>> binderStatuses;
+ private final List<ValidationResult> binderStatuses;
/**
* Convenience method for creating a unresolved validation status for the
*/
public BinderValidationStatus(Binder<BEAN> source,
List<ValidationStatus<?>> bindingStatuses,
- List<Result<?>> binderStatuses) {
+ List<ValidationResult> binderStatuses) {
Objects.requireNonNull(binderStatuses,
"binding statuses cannot be null");
Objects.requireNonNull(binderStatuses,
* 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();
}
*
* @return a list of all validation errors
*/
- public List<Result<?>> getValidationErrors() {
- ArrayList<Result<?>> errors = new ArrayList<>(getFieldValidationErrors()
- .stream().map(s -> s.getResult().get())
- .collect(Collectors.toList()));
+ public List<ValidationResult> getValidationErrors() {
+ ArrayList<ValidationResult> errors = new ArrayList<>(
+ getFieldValidationErrors().stream()
+ .map(s -> s.getResult().get())
+ .collect(Collectors.toList()));
errors.addAll(getBeanValidationErrors());
return errors;
}
*
* @return the bean level validation results
*/
- public List<Result<?>> getBeanValidationResults() {
+ public List<ValidationResult> getBeanValidationResults() {
return binderStatuses;
}
*
* @return a list of failed bean level validation results
*/
- public List<Result<?>> getBeanValidationErrors() {
- return binderStatuses.stream().filter(Result::isError)
+ public List<ValidationResult> getBeanValidationErrors() {
+ return binderStatuses.stream().filter(ValidationResult::isError)
.collect(Collectors.toList());
}
}
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.
* <p>
* Result instances are created using the factory methods {@link #ok(R)} and
* {@link #error(String)}, denoting success and failure respectively.
public class ValidationException extends Exception {
private final List<ValidationStatus<?>> fieldValidationErrors;
- private final List<Result<?>> beanValidationErrors;
+ private final List<ValidationResult> beanValidationErrors;
/**
* Constructs a new exception with validation {@code errors} list.
* binder validation errors list
*/
public ValidationException(List<ValidationStatus<?>> fieldValidationErrors,
- List<Result<?>> beanValidationErrors) {
+ List<ValidationResult> beanValidationErrors) {
super("Validation has failed for some fields");
this.fieldValidationErrors = Collections
.unmodifiableList(fieldValidationErrors);
*
* @return a list of all validation errors
*/
- public List<Result<?>> getValidationErrors() {
- ArrayList<Result<?>> errors = new ArrayList<>(getFieldValidationErrors()
- .stream().map(s -> s.getResult().get())
- .collect(Collectors.toList()));
+ public List<ValidationResult> getValidationErrors() {
+ ArrayList<ValidationResult> errors = new ArrayList<>(
+ getFieldValidationErrors().stream()
+ .map(s -> s.getResult().get())
+ .collect(Collectors.toList()));
errors.addAll(getBeanValidationErrors());
return errors;
}
*
* @return binder validation errors list
*/
- public List<Result<?>> getBeanValidationErrors() {
+ public List<ValidationResult> getBeanValidationErrors() {
return beanValidationErrors;
}
}
--- /dev/null
+/*
+ * 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.
+ * <p>
+ * 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.
+ * <p>
+ * 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 <code>true</code> if the result denotes an error,
+ * <code>false</code> 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);
+ }
+}
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}.
+ * <p>
+ * Use {@link Binding#withValidationStatusHandler(ValidationStatusHandler)} to
* register a handler for field level validation status changes.
*
* @author Vaadin Ltd
}
private final Status status;
- private final Result<TARGET> result;
+ private final ValidationResult result;
private final Binding<?, ?, TARGET> binding;
/**
* the result of the validation
*/
public ValidationStatus(Binding<?, ?, TARGET> source,
- Result<TARGET> result) {
+ ValidationResult result) {
this(source, result.isError() ? Status.ERROR : Status.OK, result);
}
* the related result, may be {@code null}
*/
public ValidationStatus(Binding<?, ?, TARGET> source, Status status,
- Result<TARGET> 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()
* status is not an error
*/
public Optional<String> getMessage() {
- return Optional.ofNullable(result).flatMap(Result::getMessage);
+ return Optional.ofNullable(result).filter(ValidationResult::isError)
+ .map(ValidationResult::getErrorMessage);
}
/**
*
* @return the validation result
*/
- public Optional<Result<TARGET>> getResult() {
+ public Optional<ValidationResult> getResult() {
return Optional.ofNullable(result);
}
* <pre>
* 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");
* };
* </pre>
*
* @param <T>
* the type of the value to validate
*
- * @see Result
+ * @see ValidationResult
*/
@FunctionalInterface
public interface Validator<T>
- extends BiFunction<T, ValueContext, Result<T>>, Serializable {
+ extends BiFunction<T, ValueContext, ValidationResult>, 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
* @return the validation result
*/
@Override
- public Result<T> apply(T value, ValueContext context);
+ public ValidationResult apply(T value, ValueContext context);
/**
* Returns a validator that passes any value.
* @return an always-passing validator
*/
public static <T> Validator<T> 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.
* <p>
* For instance, the following validator checks if a number is between 0 and
* 10, inclusive:
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));
}
};
}
import java.util.Objects;
-import com.vaadin.data.Result;
+import com.vaadin.data.ValidationResult;
import com.vaadin.data.Validator;
import com.vaadin.server.SerializableFunction;
* whether the value is valid or not
* @return the validation result
*/
- protected Result<T> 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));
}
}
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;
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;
* @return the validation result
*/
@Override
- public Result<Object> apply(final Object value, ValueContext context) {
+ public ValidationResult apply(final Object value, ValueContext context) {
Set<? extends ConstraintViolation<?>> violations = getJavaxBeanValidator()
.validateValue(beanType, propertyName, value);
- BinaryOperator<Result<Object>> 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<ValidationResult> result = violations.stream()
+ .map(violation -> ValidationResult
+ .error(getMessage(violation, locale)))
+ .findFirst();
+ return result.orElse(ValidationResult.ok());
}
@Override
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;
}
@Override
- public Result<T> 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();
}
}
import java.util.Objects;
-import com.vaadin.data.Result;
+import com.vaadin.data.ValidationResult;
import com.vaadin.data.util.converter.ValueContext;
/**
}
@Override
- public Result<String> 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();
}
}
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;
/**
* behavior depends on the used comparator.
*/
@Override
- public Result<T> apply(T value, ValueContext context) {
+ public ValidationResult apply(T value, ValueContext context) {
return toResult(value, isValid(value));
}
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;
/**
}
@Override
- public Result<String> apply(String value, ValueContext context) {
+ public ValidationResult apply(String value, ValueContext context) {
return toResult(value, isValid(value));
}
package com.vaadin.data.validator;
-import com.vaadin.data.Result;
+import com.vaadin.data.ValidationResult;
import com.vaadin.data.util.converter.ValueContext;
/**
}
@Override
- public Result<String> 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());
}
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;
getDateOutOfRangeMessage(),
getDate(getRangeStart(), getResolution()),
getDate(getRangeEnd(), getResolution()));
- Result<LocalDate> result = validator.apply(value,
+ ValidationResult result = validator.apply(value,
new ValueContext(this));
if (result.isError()) {
setComponentError(new UserError(getDateOutOfRangeMessage()));
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());
binder.setValidationStatusHandler(status -> {
// create an error message on failed bean level validations
- List<Result<?>> errors = status.getBeanValidationErrors();
- String errorMessage = errors.stream().map(Result::getMessage)
- .map(o -> o.get()).collect(Collectors.joining("\n"));
+ List<ValidationResult> 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());
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());
String msg2 = "bar";
binding.withValidator(new Validator<String>() {
@Override
- public Result<String> 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);
TextField salaryField = new TextField();
Validator<Number> 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)
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
@Test
public void alwaysPass() {
Validator<String> alwaysPass = Validator.alwaysPass();
- Result<String> result = alwaysPass.apply("foo", new ValueContext());
- Assert.assertTrue(result instanceof SimpleResult);
- SimpleResult<String> implRes = (SimpleResult<String>) result;
- Assert.assertFalse(implRes.getMessage().isPresent());
+ ValidationResult result = alwaysPass.apply("foo", new ValueContext());
+ Assert.assertFalse(result.isError());
}
@Test
public void from() {
Validator<String> validator = Validator.from(Objects::nonNull,
"Cannot be null");
- Result<String> result = validator.apply(null, new ValueContext());
+ ValidationResult result = validator.apply(null, new ValueContext());
Assert.assertTrue(result.isError());
result = validator.apply("", new ValueContext());
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;
/**
@Test
public void nullValueIsDisallowed() {
NotEmptyValidator<String> validator = new NotEmptyValidator<>("foo");
- Result<String> 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<String> validator = new NotEmptyValidator<>("foo");
- Result<String> 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<Object> validator = new NotEmptyValidator<>("foo");
Object value = new Object();
- Result<Object> 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());
}
}
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 {
@Test
public void nullValueIsDisallowed() {
NotNullValidator validator = new NotNullValidator("foo");
- Result<String> 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<String> 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());
}
}
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;
localeContext = new Label();
}
- protected <T> void assertPasses(T value, Validator<? super T> v) {
- v.apply(value, new ValueContext())
- .handle(val -> Assert.assertEquals(value, val), err -> Assert
- .fail(value + " should pass " + v + " but got " + err));
+ protected <T> void assertPasses(T value, Validator<? super T> validator) {
+ ValidationResult result = validator.apply(value, new ValueContext());
+ if (result.isError()) {
+ Assert.fail(value + " should pass " + validator + " but got "
+ + result.getErrorMessage());
+ }
}
protected <T> void assertFails(T value, String errorMessage,
- Validator<? super T> v) {
- v.apply(value, new ValueContext(localeContext)).handle(
- val -> Assert.fail(value + " should fail " + v),
- err -> Assert.assertEquals(errorMessage, err));
+ Validator<? super T> validator) {
+ ValidationResult result = validator.apply(value,
+ new ValueContext(localeContext));
+ Assert.assertTrue(result.isError());
+ Assert.assertEquals(errorMessage, result.getErrorMessage());
}
protected <T> void assertFails(T value, AbstractValidator<? super T> v) {