incubator-bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r1066255 - in /incubator/bval/sandbox/lang3-work: bval-jsr303/src/test/java/org/apache/bval/jsr303/util/TestUtils.java bval-jsr303d/src/test/java/org/apache/bval/jsr303/dynamic/DynamicCollectionValidationTest.java
Date Tue, 01 Feb 2011 22:52:48 GMT
Author: mbenson
Date: Tue Feb  1 22:52:48 2011
New Revision: 1066255

URL: http://svn.apache.org/viewvc?rev=1066255&view=rev
Log:
add more dynamic tests

Modified:
    incubator/bval/sandbox/lang3-work/bval-jsr303/src/test/java/org/apache/bval/jsr303/util/TestUtils.java
    incubator/bval/sandbox/lang3-work/bval-jsr303d/src/test/java/org/apache/bval/jsr303/dynamic/DynamicCollectionValidationTest.java

Modified: incubator/bval/sandbox/lang3-work/bval-jsr303/src/test/java/org/apache/bval/jsr303/util/TestUtils.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303/src/test/java/org/apache/bval/jsr303/util/TestUtils.java?rev=1066255&r1=1066254&r2=1066255&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303/src/test/java/org/apache/bval/jsr303/util/TestUtils.java
(original)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303/src/test/java/org/apache/bval/jsr303/util/TestUtils.java
Tue Feb  1 22:52:48 2011
@@ -44,6 +44,27 @@ public class TestUtils {
         return null;
     }
 
+    /**
+     * @param violations
+     * @param propertyPath
+     * @return count of violations
+     */
+    public static <T> int countViolations(Set<ConstraintViolation<T>> violations,
String propertyPath) {
+        int result = 0;
+        for (ConstraintViolation<T> each : violations) {
+            if (each.getPropertyPath().toString().equals(propertyPath)) {
+                result++;
+            }
+        }
+        return result;
+    }
+
+    /**
+     * @param <T>
+     * @param violations
+     * @param message
+     * @return the constraint violation with the specified message found, if any
+     */
     public static <T> ConstraintViolation<T> getViolationWithMessage(Set<ConstraintViolation<T>>
violations,
         String message) {
         for (ConstraintViolation<T> each : violations) {

Modified: incubator/bval/sandbox/lang3-work/bval-jsr303d/src/test/java/org/apache/bval/jsr303/dynamic/DynamicCollectionValidationTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303d/src/test/java/org/apache/bval/jsr303/dynamic/DynamicCollectionValidationTest.java?rev=1066255&r1=1066254&r2=1066255&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303d/src/test/java/org/apache/bval/jsr303/dynamic/DynamicCollectionValidationTest.java
(original)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303d/src/test/java/org/apache/bval/jsr303/dynamic/DynamicCollectionValidationTest.java
Tue Feb  1 22:52:48 2011
@@ -23,6 +23,7 @@ import java.util.Set;
 import javax.validation.ConstraintViolation;
 import javax.validation.Validation;
 import javax.validation.Validator;
+import javax.validation.constraints.Min;
 import javax.validation.constraints.Pattern;
 
 import org.apache.bval.constraints.AuthorAddressKnown;
@@ -38,12 +39,13 @@ import org.apache.bval.jsr303.example.Co
 import org.apache.bval.jsr303.example.Library;
 import org.apache.bval.jsr303.groups.CollectionValidationTest;
 import org.apache.bval.jsr303.util.TestUtils;
+import org.apache.commons.proxy2.stub.AnnotationConfigurer;
 import org.apache.commons.proxy2.stub.AnnotationFactory;
 import org.apache.commons.proxy2.stub.StubConfigurer;
 
 /**
- *
- *
+ * 
+ * 
  * @version $Rev$ $Date$
  */
 public class DynamicCollectionValidationTest extends CollectionValidationTest {
@@ -57,9 +59,12 @@ public class DynamicCollectionValidation
     @Override
     public void setUp() throws Exception {
         DynamicValidatorFactory factory =
-            Validation.byProvider(ApacheValidationProvider.class).configure().addProperty(
-                ApacheValidatorConfiguration.Properties.VALIDATOR_FACTORY_CLASSNAME,
-                DynamicValidatorFactory.class.getName()).buildValidatorFactory().unwrap(DynamicValidatorFactory.class);
+            Validation
+                .byProvider(ApacheValidationProvider.class)
+                .configure()
+                .addProperty(ApacheValidatorConfiguration.Properties.VALIDATOR_FACTORY_CLASSNAME,
+                    DynamicValidatorFactory.class.getName()).buildValidatorFactory()
+                .unwrap(DynamicValidatorFactory.class);
         ((DefaultMessageInterpolator) factory.getMessageInterpolator()).setLocale(Locale.ENGLISH);
         validatorContext = factory.usingContext();
         super.setUp();
@@ -147,4 +152,82 @@ public class DynamicCollectionValidation
         assertEquals(3, violations.size());
         assertNotNull(TestUtils.getViolation(violations, "taggedBooks[science].author.addresses[0].addressline1"));
     }
+    
+    public void testDynamicMultilevelGenericIterable() {
+        Library lib = new Library();
+        lib.setLibraryName("Leibnitz Bibliothek");
+
+        Book book1, book2, book3;
+
+        book1 = new Book();
+        book1.setTitle("History of time");
+        book1.setSubtitle("How it really works");
+        Author hawking = new Author();
+        hawking.setFirstName("Stephen");
+        hawking.setFirstName("Hawking");
+        hawking.setAddresses(new ArrayList<Address>(1));
+        Address addressHawking = new Address();
+        addressHawking.setAddressline1("Street 1");
+        addressHawking.setCity("London");
+        addressHawking.setCountry(new Country());
+        addressHawking.getCountry().setName("England");
+        hawking.getAddresses().add(addressHawking);
+        book1.setAuthor(hawking);
+
+        book2 = new Book();
+        Author castro = new Author();
+        castro.setFirstName("Fidel");
+        castro.setLastName("Castro Ruz");
+        book2.setAuthor(castro);
+        book2.setTitle("My life");
+        Address addressCastro = new Address();
+        addressCastro.setAddressline1("1 Cigar Street");
+        addressCastro.setCity("havana");
+        addressCastro.setCountry(new Country());
+        addressCastro.getCountry().setName("Cuba");
+        castro.setAddresses(new ArrayList<Address>());
+        castro.getAddresses().add(addressCastro);
+
+        book3 = new Book();
+        book3.setTitle("How To Make Friends And Influence People");
+        Author blag = new Author();
+        blag.setFirstName("Blag");
+        blag.setLastName("Dahlia");
+        book3.setAuthor(blag);
+        Address addressBlag = new Address();
+        addressBlag.setAddressline1("1 Dwarves Lane");
+        addressBlag.setCity("");
+        addressBlag.setCountry(new Country());
+        addressBlag.getCountry().setName("USA");
+        blag.setAddresses(new ArrayList<Address>());
+        blag.getAddresses().add(addressBlag);
+
+        lib.getTaggedBooks().put("science", book1);
+        lib.getTaggedBooks().put("politics", book2);
+        lib.getTaggedBooks().put("self-help", book3);
+
+        validatorContext.constrain(Library.class, "taggedBooks[].author.addresses[].city",
annotationFactory.create(new AnnotationConfigurer<Pattern>() {
+            
+            @Override
+            protected void configure(Pattern stub) {
+                when(stub.regexp()).thenReturn("[A-Z].*");
+            }
+        }));
+        
+        validatorContext.constrain(Library.class, "taggedBooks[self-help].author.addresses[].city",
annotationFactory.create(new AnnotationConfigurer<Min>() {
+            
+            @Override
+            protected void configure(Min stub) {
+                when(stub.value()).thenReturn(1L);
+            }
+        }));
+        
+        Set<ConstraintViolation<Library>> violations;
+
+        violations = validator.validate(lib);
+        assertEquals(0, TestUtils.countViolations(violations, "taggedBooks[science].author.addresses[0].city"));
+        assertEquals(1, TestUtils.countViolations(violations, "taggedBooks[politics].author.addresses[0].city"));
+        assertEquals(2, TestUtils.countViolations(violations, "taggedBooks[self-help].author.addresses[0].city"));
+    }
+
 }



Mime
View raw message