incubator-bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r996245 - /incubator/bval/sandbox/lang3-work/bval-jsr303/src/test/java/org/apache/bval/jsr303/ValidationTest.java
Date Sun, 12 Sep 2010 03:52:42 GMT
Author: mbenson
Date: Sun Sep 12 03:52:42 2010
New Revision: 996245

URL: http://svn.apache.org/viewvc?rev=996245&view=rev
Log:
rework for extensibility + cleanup generics warnings + reformat

Modified:
    incubator/bval/sandbox/lang3-work/bval-jsr303/src/test/java/org/apache/bval/jsr303/ValidationTest.java

Modified: incubator/bval/sandbox/lang3-work/bval-jsr303/src/test/java/org/apache/bval/jsr303/ValidationTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303/src/test/java/org/apache/bval/jsr303/ValidationTest.java?rev=996245&r1=996244&r2=996245&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303/src/test/java/org/apache/bval/jsr303/ValidationTest.java
(original)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303/src/test/java/org/apache/bval/jsr303/ValidationTest.java
Sun Sep 12 03:52:42 2010
@@ -48,29 +48,46 @@ public class ValidationTest extends Test
 
     static {
         factory = Validation.buildDefaultValidatorFactory();
-        ((DefaultMessageInterpolator)factory.getMessageInterpolator()).setLocale(Locale.ENGLISH);
+        ((DefaultMessageInterpolator) factory.getMessageInterpolator()).setLocale(Locale.ENGLISH);
     }
 
-    private Validator getValidator() {
+    /**
+     * Validator instance to test
+     */
+    protected Validator validator;
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        validator = createValidator();
+    }
+
+    /**
+     * Create the validator instance.
+     * 
+     * @return Validator
+     */
+    protected Validator createValidator() {
         return factory.getValidator();
     }
 
     public void testAccessStrategies_field_method() {
         AccessTestBusinessObject o1 = new AccessTestBusinessObject("1");
         AccessTestBusinessObjectSub o2 = new AccessTestBusinessObjectSub("3");
-        Validator validator = getValidator();
-        Set<ConstraintViolation<AccessTestBusinessObject>> errors =
-              validator.validate(o1);
+        Set<ConstraintViolation<AccessTestBusinessObject>> errors = validator.validate(o1);
         assertTrue(errors.isEmpty());
-        Set<ConstraintViolation<AccessTestBusinessObjectSub>> errors2 =
-              validator.validate(o2);
+        Set<ConstraintViolation<AccessTestBusinessObjectSub>> errors2 = validator.validate(o2);
         assertTrue(errors2.isEmpty());
 
         o2 = new AccessTestBusinessObjectSub("1");
         errors2 = validator.validate(o2);
         assertEquals(1, errors2.size());
 
-        // assert, that getvar2() and getVar2() are both validated with their getter method
+        // assert, that getvar2() and getVar2() are both validated with their
+        // getter method
         o2 = new AccessTestBusinessObjectSub("3");
         o2.setVar2("1");
         o2.setvar2("2");
@@ -92,17 +109,17 @@ public class ValidationTest extends Test
         AccessTestBusinessObject o1 = new AccessTestBusinessObject("1");
         AccessTestBusinessObject o2 = new AccessTestBusinessObject("2");
         o1.next(o2);
-        Validator validator = getValidator();
-        Set<ConstraintViolation<AccessTestBusinessObject>> errors =
-              validator.validate(o1);
-        // assert, that field access 'next' is used and not getNext() is called!!!
+        Set<ConstraintViolation<AccessTestBusinessObject>> errors = validator.validate(o1);
+        // assert, that field access 'next' is used and not getNext() is
+        // called!!!
         assertEquals(1, errors.size());
         o2 = new AccessTestBusinessObject("1");
         o1.next(o2);
         errors = validator.validate(o1);
         assertEquals(0, errors.size());
 
-        // assert that toBeIgnored not validated, because not annotated with @Valid
+        // assert that toBeIgnored not validated, because not annotated with
+        // @Valid
         o1.setToBeIgnored(new AccessTestBusinessObject("99"));
         errors = validator.validate(o1);
         assertEquals(0, errors.size());
@@ -113,7 +130,6 @@ public class ValidationTest extends Test
     }
 
     public void testBook() {
-        Validator validator = getValidator();
         Author author = new Author();
         author.setLastName("Baudelaire");
         author.setFirstName("");
@@ -129,18 +145,17 @@ public class ValidationTest extends Test
         author.setCompany("Some random publisher with a very very very long name");
 
         // author.firstName fails to pass the NotEmpty constraint
-        //  author.company fails to pass the Size constraint
+        // author.company fails to pass the Size constraint
     }
 
     /**
-     * test:
-     * - dynamic resolution of associated object types.
-     * - inheritance of validation constraints
-     * - complex valiation, different groups, nested object net
+     * test: - dynamic resolution of associated object types. - inheritance of
+     * validation constraints - complex valiation, different groups, nested
+     * object net
      */
     public void testValidAnnotation() {
         Author a = new Author();
-        a.setAddresses(new ArrayList());
+        a.setAddresses(new ArrayList<Address>());
         BusinessAddress adr = new BusinessAddress();
         adr.setCountry(new Country());
         adr.setAddressline1("line1");
@@ -152,23 +167,22 @@ public class ValidationTest extends Test
         a.setFirstName("Karl");
         a.setLastName("May");
 
-        Validator v = getValidator();
-        Set found = v.validate(a, Default.class, First.class, Last.class);
+        Set<ConstraintViolation<Author>> found = validator.validate(a, Default.class,
First.class, Last.class);
         Assert.assertTrue(!found.isEmpty());
         Assert.assertEquals(4, found.size());
 
         adr.setCity("Berlin");
         adr.setZipCode("12345");
         adr.setCompany("apache");
-        found = v.validate(a, Default.class, First.class, Last.class);
+        found = validator.validate(a, Default.class, First.class, Last.class);
         Assert.assertEquals(1, found.size());
-        ConstraintViolation ic = (ConstraintViolation) found.iterator().next();
+        ConstraintViolation<Author> ic = found.iterator().next();
         Assert.assertEquals("addresses[0].country.name", ic.getPropertyPath().toString());
     }
 
     public void testPropertyPathWithIndex() {
         Author a = new Author();
-        a.setAddresses(new ArrayList());
+        a.setAddresses(new ArrayList<Address>());
         Address adr = new Address();
         adr.setAddressline1("adr1");
         adr.setCity("Santiago");
@@ -182,14 +196,14 @@ public class ValidationTest extends Test
         adr.setCity("Trinidad");
         a.getAddresses().add(adr);
 
-        Set<ConstraintViolation<Author>> constraints = getValidator().validate(a);
+        Set<ConstraintViolation<Author>> constraints = validator.validate(a);
         Assert.assertTrue(!constraints.isEmpty());
 
         assertPropertyPath("addresses[0].country", constraints);
         assertPropertyPath("addresses[1].country", constraints);
         assertPropertyPath("addresses[2].country", constraints);
     }
-    
+
     /**
      * Check correct path reporting when validating a set of beans.
      */
@@ -201,17 +215,17 @@ public class ValidationTest extends Test
         country.setISO3Code("xxx");
         country.setName(null);
         c.countries.add(country);
-        
-        Set<ConstraintViolation<Continent>> constraints = getValidator().validate(c);
+
+        Set<ConstraintViolation<Continent>> constraints = validator.validate(c);
         Assert.assertEquals("Incorrect number of violations detected", 1, constraints.size());
         assertPropertyPath("countries[].name", constraints);
-        
+
     }
 
-    private <T> void assertPropertyPath(String propertyPath,
-                                        Set<ConstraintViolation<T>> constraints)
{
-        for (ConstraintViolation each : constraints) {
-            if (each.getPropertyPath().toString().equals(propertyPath)) return;
+    private <T> void assertPropertyPath(String propertyPath, Set<ConstraintViolation<T>>
constraints) {
+        for (ConstraintViolation<T> each : constraints) {
+            if (each.getPropertyPath().toString().equals(propertyPath))
+                return;
         }
         Assert.fail(propertyPath + " not found in " + constraints);
     }
@@ -225,49 +239,44 @@ public class ValidationTest extends Test
         RecursiveFoo foo2 = new RecursiveFoo();
         foo11.getFoos().add(foo2);
 
-        Set<ConstraintViolation<RecursiveFoo>> constraints =
-              getValidator().validate(foo1);
+        Set<ConstraintViolation<RecursiveFoo>> constraints = validator.validate(foo1);
         assertPropertyPath("foos[0].foos[0].foos", constraints);
         assertPropertyPath("foos[1].foos", constraints);
     }
 
     public void testNullElementInCollection() {
         try {
-            getValidator().validate(null);
+            validator.validate(null);
             Assert.fail();
         } catch (IllegalArgumentException ex) {
         }
         RecursiveFoo foo = new RecursiveFoo();
         foo.getFoos().add(new RecursiveFoo());
         foo.getFoos().add(null);
-        Assert.assertTrue(!getValidator().validate(foo).isEmpty());
+        Assert.assertTrue(!validator.validate(foo).isEmpty());
         // check that no nullpointer exception gets thrown
     }
 
     public void testGroups() {
-        Validator validator = getValidator();
         Author author = new Author();
         author.setCompany("ACME");
         Book book = new Book();
         book.setTitle("");
         book.setAuthor(author);
         boolean foundTitleConstraint = false;
-        Set<ConstraintViolation<Book>> constraintViolations =
-              validator.validate(book, Book.All.class);
+        Set<ConstraintViolation<Book>> constraintViolations = validator.validate(book,
Book.All.class);
         assertEquals(1, constraintViolations.size());
-        //assuming an english locale, the interpolated message is returned
-        for (ConstraintViolation constraintViolation : constraintViolations) {
+        // assuming an english locale, the interpolated message is returned
+        for (ConstraintViolation<Book> constraintViolation : constraintViolations)
{
             if (constraintViolation.getRootBean().getClass() == Book.class) {
-                Assert.assertEquals(
-                      "may not be empty", constraintViolation.getMessage());
+                Assert.assertEquals("may not be empty", constraintViolation.getMessage());
                 Assert.assertTrue(book == constraintViolation.getRootBean());
 
-                //the offending property
+                // the offending property
                 if (constraintViolation.getPropertyPath().toString().equals("title")) {
                     foundTitleConstraint = true;
-                    //the offending value
-                    Assert.assertEquals(book.getTitle(),
-                          constraintViolation.getInvalidValue());
+                    // the offending value
+                    Assert.assertEquals(book.getTitle(), constraintViolation.getInvalidValue());
                 }
             }
         }
@@ -276,8 +285,8 @@ public class ValidationTest extends Test
 
     /**
      * Example 2.14. Using the fluent API to build custom constraint violations.
-     * test that:
-     * the {@link org.apache.bval.constraints.ZipCodeCityCoherenceValidator} adds
+     * test that: the
+     * {@link org.apache.bval.constraints.ZipCodeCityCoherenceValidator} adds
      * custom messages to the context and suppresses the default message
      */
     public void testConstraintValidatorContextFluentAPI() {
@@ -287,30 +296,28 @@ public class ValidationTest extends Test
         ad.setAddressline1("something");
         ad.setCountry(new Country());
         ad.getCountry().setName("something");
-        Validator v = getValidator();
-        Set<ConstraintViolation<Address>> violations = v.validate(ad);
+        Set<ConstraintViolation<Address>> violations = validator.validate(ad);
         Assert.assertEquals(2, violations.size());
-        for (ConstraintViolation each : violations) {
+        for (ConstraintViolation<Address> each : violations) {
             Assert.assertTrue(each.getMessage().endsWith(" not OK"));
         }
         assertNotNull(TestUtils.getViolation(violations, "city"));
         assertNotNull(TestUtils.getViolation(violations, ""));
     }
 
-    public void testValidateNestedPropertyPath() throws InvocationTargetException,
-          NoSuchMethodException, IllegalAccessException {
+    public void testValidateNestedPropertyPath() throws InvocationTargetException, NoSuchMethodException,
+        IllegalAccessException {
         final String propPath = "addresses[0].country.ISO2Code";
 
-        Validator v = getValidator();
         Author author = new Author();
-        author.setAddresses(new ArrayList());
+        author.setAddresses(new ArrayList<Address>());
         Address adr = new Address();
         author.getAddresses().add(adr);
         Country country = new Country();
         adr.setCountry(country);
         country.setISO2Code("too_long");
 
-        Set<ConstraintViolation<Author>> iv = v.validateProperty(author, propPath);
+        Set<ConstraintViolation<Author>> iv = validator.validateProperty(author,
propPath);
         Assert.assertEquals(1, iv.size());
         ConstraintViolation<Author> vio = iv.iterator().next();
         assertEquals(propPath, vio.getPropertyPath().toString());
@@ -318,17 +325,17 @@ public class ValidationTest extends Test
         assertSame(author.getAddresses().get(0).getCountry(), vio.getLeafBean());
 
         country.setISO2Code("23");
-        iv = v.validateProperty(author, propPath);
+        iv = validator.validateProperty(author, propPath);
         Assert.assertEquals(0, iv.size());
 
-        iv = v.validateValue(Author.class, propPath, "345");
+        iv = validator.validateValue(Author.class, propPath, "345");
         Assert.assertEquals(1, iv.size());
         vio = iv.iterator().next();
         assertEquals(propPath, vio.getPropertyPath().toString());
         assertNull(vio.getRootBean());
         assertNull(vio.getLeafBean());
 
-        iv = v.validateValue(Author.class, propPath, "34");
+        iv = validator.validateValue(Author.class, propPath, "34");
         Assert.assertEquals(0, iv.size());
     }
 
@@ -336,7 +343,7 @@ public class ValidationTest extends Test
         IllegalAccessException {
         final String propPath = "addresses[0]";
 
-        CascadingPropertyValidator v = getValidator().unwrap(CascadingPropertyValidator.class);
+        CascadingPropertyValidator v = validator.unwrap(CascadingPropertyValidator.class);
         Author author = new Author();
         author.setAddresses(new ArrayList<Address>());
         Address adr = new Address();
@@ -376,53 +383,42 @@ public class ValidationTest extends Test
     }
 
     public void testMetadataAPI() {
-        Validator bookValidator = getValidator();
-        BeanDescriptor bookBeanDescriptor =
-              bookValidator.getConstraintsForClass(Book.class);
+        BeanDescriptor bookBeanDescriptor = validator.getConstraintsForClass(Book.class);
 
         // expect no constraints on Book's Class-Level
         Assert.assertFalse(bookBeanDescriptor.hasConstraints());
         // but there are constraints on Book's Property-Level
         Assert.assertTrue(bookBeanDescriptor.isBeanConstrained());
-        Assert.assertTrue(
-              bookBeanDescriptor.getConstraintDescriptors().size() == 0); //no constraint
-        //more specifically "author" and "title"
+        Assert.assertTrue(bookBeanDescriptor.getConstraintDescriptors().size() == 0); //
no
+        // constraint
+        // more specifically "author" and "title"
         Assert.assertEquals(4, bookBeanDescriptor.getConstrainedProperties().size());
-        //not a property
-        Assert.assertTrue(
-              bookBeanDescriptor.getConstraintsForProperty("doesNotExist") == null);
-        //property with no constraint
-        Assert.assertTrue(
-              bookBeanDescriptor.getConstraintsForProperty("description") == null);
-        PropertyDescriptor propertyDescriptor =
-              bookBeanDescriptor.getConstraintsForProperty("title");
+        // not a property
+        Assert.assertTrue(bookBeanDescriptor.getConstraintsForProperty("doesNotExist") ==
null);
+        // property with no constraint
+        Assert.assertTrue(bookBeanDescriptor.getConstraintsForProperty("description") ==
null);
+        PropertyDescriptor propertyDescriptor = bookBeanDescriptor.getConstraintsForProperty("title");
         Assert.assertEquals(2, propertyDescriptor.getConstraintDescriptors().size());
         Assert.assertTrue("title".equals(propertyDescriptor.getPropertyName()));
-        //assuming the implementation returns the NotEmpty constraint first
-        Iterator<ConstraintDescriptor<?>> iter =
-              propertyDescriptor.getConstraintDescriptors().iterator();
-        ConstraintDescriptor constraintDescriptor = null;
+        // 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(NotNull.class))
{
                 break;
             }
 
         }
         Assert.assertTrue(constraintDescriptor != null);
-        Assert.assertTrue(constraintDescriptor.getGroups().size() == 1); //"first"
-        Assert.assertEquals(NotNullValidator.class,
-              constraintDescriptor.getConstraintValidatorClasses().get(0));
-        //assuming the implementation returns the Size constraint first
+        Assert.assertTrue(constraintDescriptor.getGroups().size() == 1); // "first"
+        Assert.assertEquals(NotNullValidator.class, constraintDescriptor.getConstraintValidatorClasses().get(0));
+        // assuming the implementation returns the Size constraint first
         propertyDescriptor = bookBeanDescriptor.getConstraintsForProperty("subtitle");
-        Iterator<ConstraintDescriptor<?>> iterator =
-              propertyDescriptor.getConstraintDescriptors().iterator();
+        Iterator<ConstraintDescriptor<?>> iterator = propertyDescriptor.getConstraintDescriptors().iterator();
         constraintDescriptor = iterator.next();
-        Assert.assertTrue(
-              constraintDescriptor.getAnnotation().annotationType().equals(Size.class));
-        Assert.assertTrue(
-              ((Integer) constraintDescriptor.getAttributes().get("max")) == 30);
+        Assert.assertTrue(constraintDescriptor.getAnnotation().annotationType().equals(Size.class));
+        Assert.assertTrue(((Integer) constraintDescriptor.getAttributes().get("max")) ==
30);
         Assert.assertTrue(constraintDescriptor.getGroups().size() == 1);
         propertyDescriptor = bookBeanDescriptor.getConstraintsForProperty("author");
         Assert.assertTrue(propertyDescriptor.getConstraintDescriptors().size() == 1);



Mime
View raw message