Author: drobiazko Date: Thu Mar 5 07:40:27 2009 New Revision: 750355 URL: http://svn.apache.org/viewvc?rev=750355&view=rev Log: TAP5-520: Using regular expressions with the @Validate annotation causes odd parse errors if the regexp includes common characters (including commas)
Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/beaneditor/ValidateAnnotationConstraintGenerator.java tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/beaneditor/ValidateAnnotationConstraintGeneratorTest.java Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/beaneditor/ValidateAnnotationConstraintGenerator.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/beaneditor/ValidateAnnotationConstraintGenerator.java?rev=750355&r1=750354&r2=750355&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/beaneditor/ValidateAnnotationConstraintGenerator.java (original) +++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/beaneditor/ValidateAnnotationConstraintGenerator.java Thu Mar 5 07:40:27 2009 @@ -34,7 +34,11 @@ if (annotation == null) return null; - return Arrays.asList(annotation.value().split(",")); + //TAP5-520: Commas within regular expressions like {n,m} or {n,} or a\,b . + //We use Negative Lookahead to avoid matching the case a\,b . + //We use Positive Lookahead to avoid matching cases {n,m} and {n,}. + //http://www.regular-expressions.info/lookaround.html + return Arrays.asList(annotation.value().split("(?<!\\\\),(?!([0-9]*\\}))")); } } Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/beaneditor/ValidateAnnotationConstraintGeneratorTest.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/beaneditor/ValidateAnnotationConstraintGeneratorTest.java?rev=750355&r1=750354&r2=750355&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/beaneditor/ValidateAnnotationConstraintGeneratorTest.java (original) +++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/beaneditor/ValidateAnnotationConstraintGeneratorTest.java Thu Mar 5 07:40:27 2009 @@ -61,7 +61,7 @@ public void multiple_constraints() { PropertyConduit conduit = mockPropertyConduit(); - Validate validate = newValidate("required,minlength=3"); + Validate validate = newValidate("required,minlength=3,regexp=^([a-zA-Z0-9]{2,4})+$"); train_getAnnotation(conduit, Validate.class, validate); @@ -69,7 +69,26 @@ ValidationConstraintGenerator gen = new ValidateAnnotationConstraintGenerator(); - assertEquals(gen.buildConstraints(null, conduit), Arrays.asList("required", "minlength=3")); + assertEquals(gen.buildConstraints(null, conduit), Arrays.asList("required", "minlength=3", "regexp=^([a-zA-Z0-9]{2,4})+$")); + + verify(); + } + + + @Test + public void regex_ranges_constraints() + { + PropertyConduit conduit = mockPropertyConduit(); + Validate validate = newValidate("regexp=^([a]{50,125}[0-9]{2,4})+$,required,567matcher,regexp=a\\,b,regexp=a{1,}"); + + train_getAnnotation(conduit, Validate.class, validate); + + replay(); + + ValidationConstraintGenerator gen = new ValidateAnnotationConstraintGenerator(); + + assertEquals(gen.buildConstraints(null, conduit), + Arrays.asList("regexp=^([a]{50,125}[0-9]{2,4})+$","required", "567matcher", "regexp=a\\,b", "regexp=a{1,}")); verify(); }