/** * {@inheritDoc} */ @Override protected boolean prove(String value) { return value.isEmpty() || (getInvalidEmailAddress(value) == null); }
/** * {@inheritDoc} */ @Override protected void compare(SingleProperty<? extends T> property, T value, T against, PropertyValidation propertyValidation) { if (!matches(value, against)) { ValidationError validationError = new ValidationError(Collections.<SingleProperty<?>> singletonList(property), "Value of property '" + property.getName() + "' must be greater than " + against); propertyValidation.addValidationError(validationError); } }
/** * {@inheritDoc} */ @Override protected void compare(SingleProperty<? extends T> property, T value, T against, PropertyValidation propertyValidation) { if (!matches(value, against)) { ValidationError validationError = new ValidationError(Collections.<SingleProperty<?>> singletonList(property), "Value of property '" + property.getName() + "' must be greater or equal than " + against); propertyValidation.addValidationError(validationError); } }
property1.addValidator(new NotEmptyValidator<String>()); section.addProperty(property1); property2.addValidator(new PositiveValidator<Long>()); section.addProperty(property2); SingleProperty<Long> property4 = new LongProperty("Min rotation", "Define min rotation", "properties.rotation.min", 0L, false, false); groupedProperty.addSingleProperty(property4); LessValidator<Long> lessValidator = new LessValidator<>(); lessValidator.setProperty("properties.rotation.min"); lessValidator.setThan("properties.rotation.max"); groupedProperty.addValidator(lessValidator);
@Test public void validatorGroupValidation() { when(singleProperty.getValue()).thenReturn(Integer.valueOf(10)); when(singleProperty.getLogicalName()).thenReturn("my.property1"); when(singleProperty2.getValue()).thenReturn(Integer.valueOf(20)); when(singleProperty2.getLogicalName()).thenReturn("my.property2"); Set<SingleProperty<?>> properties = new HashSet<>(); properties.add(singleProperty); properties.add(singleProperty2); when(groupedProperty.forLogicalname("my.property1")).thenReturn(singleProperty); when(groupedProperty.forLogicalname("my.property2")).thenReturn(singleProperty2); LessValidator<Number> validator = new LessValidator<>(); validator.setProperty("my.property1"); validator.setThan("my.property2"); validator.validate(groupedProperty, propertyValidation); verifyZeroInteractions(propertyValidation); // make it fail when(singleProperty.getValue()).thenReturn(Integer.valueOf(30)); validator.validate(groupedProperty, propertyValidation); verify(propertyValidation, times(1)).addValidationError(Matchers.<ValidationError> anyObject()); }
@Test public void validatorIsPercentage() { when(singleProperty.getValue()).thenReturn(0.75f); when(singleProperty.getLogicalName()).thenReturn("my.property"); PercentageValidator<Number> validator = new PercentageValidator<>(); validator.validate(singleProperty, propertyValidation); verifyZeroInteractions(propertyValidation); }
@Test public void validatorIsGreaterThanOrEqual() { when(singleProperty.getValue()).thenReturn(Integer.valueOf(10)); when(singleProperty.getLogicalName()).thenReturn("my.property"); GreaterOrEqualValidator<Number> validator = new GreaterOrEqualValidator<>(); when(singleProperty.parseLiteral(null)).thenReturn(9, 10, 11); validator.validate(singleProperty, propertyValidation); verifyZeroInteractions(propertyValidation); validator.validate(singleProperty, propertyValidation); validator.validate(singleProperty, propertyValidation); verify(propertyValidation, times(1)).addValidationError(Matchers.<ValidationError> anyObject()); }
@Test public void validatorIsLessThan() { when(singleProperty.getValue()).thenReturn(Integer.valueOf(10)); when(singleProperty.getLogicalName()).thenReturn("my.property"); LessValidator<Number> validator = new LessValidator<>(); when(singleProperty.parseLiteral(null)).thenReturn(11, 10, 9); validator.validate(singleProperty, propertyValidation); verifyZeroInteractions(propertyValidation); validator.validate(singleProperty, propertyValidation); validator.validate(singleProperty, propertyValidation); verify(propertyValidation, times(2)).addValidationError(Matchers.<ValidationError> anyObject()); }
@Test public void validatorIsLessThanOrEqual() { when(singleProperty.getValue()).thenReturn(Integer.valueOf(10)); when(singleProperty.getLogicalName()).thenReturn("my.property"); LessOrEqualValidator<Number> validator = new LessOrEqualValidator<>(); when(singleProperty.parseLiteral(null)).thenReturn(11, 10, 9); validator.validate(singleProperty, propertyValidation); validator.validate(singleProperty, propertyValidation); verifyZeroInteractions(propertyValidation); validator.validate(singleProperty, propertyValidation); verify(propertyValidation, times(1)).addValidationError(Matchers.<ValidationError> anyObject()); }
@Test public void validatorIsGreaterThan() { when(singleProperty.getValue()).thenReturn(Integer.valueOf(10)); when(singleProperty.getLogicalName()).thenReturn("my.property"); GreaterValidator<Number> validator = new GreaterValidator<>(); when(singleProperty.parseLiteral(null)).thenReturn(9, 10, 11); validator.validate(singleProperty, propertyValidation); verifyZeroInteractions(propertyValidation); validator.validate(singleProperty, propertyValidation); validator.validate(singleProperty, propertyValidation); verify(propertyValidation, times(2)).addValidationError(Matchers.<ValidationError> anyObject()); }
@Test public void validatorIsNegative() { when(singleProperty.getLogicalName()).thenReturn("my.property"); NegativeValidator<Number> validator = new NegativeValidator<>(); when(singleProperty.getValue()).thenReturn(Integer.valueOf(-10)); validator.validate(singleProperty, propertyValidation); verifyZeroInteractions(propertyValidation); when(singleProperty.getValue()).thenReturn(Integer.valueOf(10)); validator.validate(singleProperty, propertyValidation); verify(propertyValidation, times(1)).addValidationError(Matchers.<ValidationError> anyObject()); }
@Test public void validatorIsPositive() { when(singleProperty.getLogicalName()).thenReturn("my.property"); PositiveValidator<Number> validator = new PositiveValidator<>(); when(singleProperty.getValue()).thenReturn(Integer.valueOf(10)); validator.validate(singleProperty, propertyValidation); verifyZeroInteractions(propertyValidation); when(singleProperty.getValue()).thenReturn(Integer.valueOf(-10)); validator.validate(singleProperty, propertyValidation); verify(propertyValidation, times(1)).addValidationError(Matchers.<ValidationError> anyObject()); }
@Test public void validatorIsNotEmpty() { when(singleProperty.getLogicalName()).thenReturn("my.property"); NotEmptyValidator<Object> validator = new NotEmptyValidator<>(); when(singleProperty.getValue()).thenReturn("Some string"); verifyZeroInteractions(propertyValidation); when(singleProperty.getValue()).thenReturn(""); validator.validate(singleProperty, propertyValidation); when(singleProperty.getValue()).thenReturn(Collections.emptyList()); validator.validate(singleProperty, propertyValidation); when(singleProperty.getValue()).thenReturn(Collections.emptyMap()); validator.validate(singleProperty, propertyValidation); verify(propertyValidation, times(3)).addValidationError(Matchers.<ValidationError> anyObject()); }
@Test public void validatorIsFullyQualifiedClassName() { SingleProperty<String> singleProperty = mock(SingleProperty.class); when(singleProperty.getLogicalName()).thenReturn("my.property"); FullyQualifiedClassNameValidator validator = new FullyQualifiedClassNameValidator(); when(singleProperty.getValue()).thenReturn(getClass().getName()); validator.validate(singleProperty, propertyValidation); verifyZeroInteractions(propertyValidation); // test not FQN when(singleProperty.getValue()).thenReturn("package,Class"); validator.validate(singleProperty, propertyValidation); when(singleProperty.getValue()).thenReturn("1canot.start.with.Number"); validator.validate(singleProperty, propertyValidation); verify(propertyValidation, times(2)).addValidationError(Matchers.<ValidationError> anyObject()); }
/** * {@inheritDoc} */ @Override protected void compare(SingleProperty<? extends T> property, T value, T against, PropertyValidation propertyValidation) { if (!matches(value, against)) { ValidationError validationError = new ValidationError(Collections.<SingleProperty<?>> singletonList(property), "Value of property '" + property.getName() + "' must be less than " + against); propertyValidation.addValidationError(validationError); } }
/** * {@inheritDoc} */ @Override protected void compare(SingleProperty<? extends T> property, T value, T against, PropertyValidation propertyValidation) { if (!matches(value, against)) { ValidationError validationError = new ValidationError(Collections.<SingleProperty<?>> singletonList(property), "Value of property '" + property.getName() + "' must be less or equal than " + against); propertyValidation.addValidationError(validationError); } }
/** * {@inheritDoc} */ @Override protected void compare(SingleProperty<? extends T> property, SingleProperty<? extends T> againstProperty, T value, T against, PropertyValidation propertyValidation) { if (!matches(value, against)) { List<SingleProperty<?>> properties = new ArrayList<>(2); properties.add(property); properties.add(againstProperty); ValidationError validationError = new ValidationError(properties, "Value of property '" + property.getName() + "' must be greater than value of property '" + againstProperty.getName() + "'"); propertyValidation.addValidationError(validationError); } }
/** * {@inheritDoc} */ @Override protected void compare(SingleProperty<? extends T> property, SingleProperty<? extends T> againstProperty, T value, T against, PropertyValidation propertyValidation) { if (!matches(value, against)) { List<SingleProperty<?>> properties = new ArrayList<>(2); properties.add(property); properties.add(againstProperty); ValidationError validationError = new ValidationError(properties, "Value of property '" + property.getName() + "' must be greater or equal than value of property '" + againstProperty.getName() + "'"); propertyValidation.addValidationError(validationError); } }
/** * {@inheritDoc} */ @Override protected void compare(SingleProperty<? extends T> property, SingleProperty<? extends T> againstProperty, T value, T against, PropertyValidation propertyValidation) { if (!matches(value, against)) { List<SingleProperty<?>> properties = new ArrayList<>(2); properties.add(property); properties.add(againstProperty); ValidationError validationError = new ValidationError(properties, "Value of property '" + property.getName() + "' must be less than value of property '" + againstProperty.getName() + "'"); propertyValidation.addValidationError(validationError); } }
/** * {@inheritDoc} */ @Override protected void compare(SingleProperty<? extends T> property, SingleProperty<? extends T> againstProperty, T value, T against, PropertyValidation propertyValidation) { if (!matches(value, against)) { List<SingleProperty<?>> properties = new ArrayList<>(2); properties.add(property); properties.add(againstProperty); ValidationError validationError = new ValidationError(properties, "Value of property '" + property.getName() + "' must be less or equal than value of property '" + againstProperty.getName() + "'"); propertyValidation.addValidationError(validationError); } }