Skip to content
Permalink
Browse files
BVAL-168: more notempty align with spec
  • Loading branch information
mbenson committed Nov 9, 2018
1 parent dc00fb7 commit 83f28d84c4b32a123de24ec45b253ed1079c5a62
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 15 deletions.
@@ -29,6 +29,6 @@

@Override
public boolean isValid(CharSequence value, ConstraintValidatorContext context) {
return value == null || value.length() > 0;
return value != null && value.length() > 0;
}
}
@@ -30,6 +30,6 @@

@Override
public boolean isValid(Collection<?> value, ConstraintValidatorContext context) {
return value == null || !value.isEmpty();
return !(value == null || value.isEmpty());
}
}
@@ -29,6 +29,6 @@ public class NotEmptyValidatorForMap implements ConstraintValidator<javax.valida

@Override
public boolean isValid(Map<?, ?> value, ConstraintValidatorContext context) {
return value == null || !value.isEmpty();
return !(value == null || value.isEmpty());
}
}
@@ -38,14 +38,15 @@

import javax.validation.ConstraintViolation;
import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.validation.groups.Default;
import javax.validation.metadata.BeanDescriptor;
import javax.validation.metadata.ConstraintDescriptor;
import javax.validation.metadata.PropertyDescriptor;

import org.apache.bval.constraints.NotNullValidator;
import org.apache.bval.constraints.NotEmptyValidatorForCharSequence;
import org.apache.bval.jsr.example.AccessTestBusinessObject;
import org.apache.bval.jsr.example.AccessTestBusinessObjectSub;
import org.apache.bval.jsr.example.Address;
@@ -252,6 +253,8 @@ public void testNullElementInCollection() {
@Test
public void testGroups() {
final Author author = new Author();
author.setFirstName("Wile");
author.setLastName("Coyote");
author.setCompany("ACME");
final Book book = new Book();
book.setTitle("");
@@ -262,7 +265,7 @@ public void testGroups() {
// assuming an english locale, the interpolated message is returned
for (ConstraintViolation<Book> constraintViolation : constraintViolations) {
if (Book.class.equals(constraintViolation.getRootBean().getClass())) {
assertEquals("may not be empty", constraintViolation.getMessage());
assertEquals("must not be empty", constraintViolation.getMessage());
assertSame(book, constraintViolation.getRootBean());

// the offending property
@@ -649,21 +652,20 @@ public void testMetadataAPI() {
// property with no constraint
assertNull(bookBeanDescriptor.getConstraintsForProperty("description"));
PropertyDescriptor propertyDescriptor = bookBeanDescriptor.getConstraintsForProperty("title");
assertEquals(2, propertyDescriptor.getConstraintDescriptors().size());
assertEquals(1, propertyDescriptor.getConstraintDescriptors().size());
assertEquals("title", propertyDescriptor.getPropertyName());
// assuming the implementation returns the NotEmpty constraint first
Iterator<ConstraintDescriptor<?>> iter = propertyDescriptor.getConstraintDescriptors().iterator();
ConstraintDescriptor<?> constraintDescriptor = null;
while (iter.hasNext()) {
constraintDescriptor = iter.next();
if (constraintDescriptor.getAnnotation().annotationType().equals(NotNull.class)) {
if (constraintDescriptor.getAnnotation().annotationType().equals(NotEmpty.class)) {
break;
}

}
assertNotNull(constraintDescriptor);
assertEquals(1, constraintDescriptor.getGroups().size()); // "first"
assertEquals(NotNullValidator.class, constraintDescriptor.getConstraintValidatorClasses().get(0));
assertTrue(constraintDescriptor.getConstraintValidatorClasses().contains(NotEmptyValidatorForCharSequence.class));
// assuming the implementation returns the Size constraint first
propertyDescriptor = bookBeanDescriptor.getConstraintsForProperty("subtitle");
Iterator<ConstraintDescriptor<?>> iterator = propertyDescriptor.getConstraintDescriptors().iterator();
@@ -18,16 +18,14 @@
*/
package org.apache.bval.jsr.example;

import org.apache.bval.constraints.NotEmpty;

import javax.validation.GroupSequence;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@GroupSequence({ First.class, Second.class, Book.class, Last.class })
public class Book {
@NotNull(groups = First.class)
@NotEmpty(groups = First.class)
private String title;

@@ -134,7 +134,7 @@ public void testValidateMapAndRedefinedDefaultGroupOnNonRootBean() {
book1.setSubtitle("How it really works");
Author hawking = new Author();
hawking.setFirstName("Stephen");
hawking.setFirstName("Hawking");
hawking.setLastName("Hawking");
hawking.setAddresses(new ArrayList<Address>(1));
Address adr = new Address();
adr.setAddressline1("Street 1");
@@ -115,7 +115,7 @@ public void testGroups() {
constraintViolations = validator.validate(book, First.class, Second.class, Last.class);
ConstraintViolation<?> constraintViolation = constraintViolations.iterator().next();
assertEquals(1, constraintViolations.size());
assertEquals("may not be empty", constraintViolation.getMessage());
assertEquals("must not be empty", constraintViolation.getMessage());
assertEquals(book, constraintViolation.getRootBean());
assertEquals(book.getTitle(), constraintViolation.getInvalidValue());
assertEquals("title", constraintViolation.getPropertyPath().toString());
@@ -165,7 +165,7 @@ public void testGroupSequence() {
constraintViolations = validator.validate(book, Book.All.class);
ConstraintViolation<?> constraintViolation = constraintViolations.iterator().next();
assertEquals(1, constraintViolations.size());
assertEquals("may not be null", constraintViolation.getMessage());
assertEquals("must not be empty", constraintViolation.getMessage());
assertEquals(book, constraintViolation.getRootBean());
assertEquals(book.getTitle(), constraintViolation.getInvalidValue());
assertEquals("title", constraintViolation.getPropertyPath().toString());

0 comments on commit 83f28d8

Please sign in to comment.