### Question:
PatternValidator implements ConstraintValidator<Pattern, String> { public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) { if ( value == null ) { return true; } Matcher m = pattern.matcher( value ); return m.matches(); }  void initialize(Pattern parameters); boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext);  }### Answer:
@Test public void testIsValid() { AnnotationDescriptor<Pattern> descriptor = new AnnotationDescriptor<Pattern>( Pattern.class ); descriptor.setValue( "regexp", "foobar" ); descriptor.setValue( "message", "{validator.pattern}" ); Pattern p = AnnotationFactory.create( descriptor ); PatternValidator constraint = new PatternValidator(); constraint.initialize( p ); assertTrue( constraint.isValid( null, null ) ); assertFalse( constraint.isValid( "", null ) ); assertFalse( constraint.isValid( "bla bla", null ) ); assertFalse( constraint.isValid( "This test is not foobar", null ) ); }
@Test public void testIsValid() { AnnotationDescriptor<Pattern> descriptor = new AnnotationDescriptor<Pattern>( Pattern.class ); descriptor.setValue( "regexp", "foobar" ); descriptor.setValue( "message", "pattern does not match" ); Pattern p = AnnotationFactory.create( descriptor ); PatternValidator constraint = new PatternValidator(); constraint.initialize( p ); assertTrue( constraint.isValid( null, null ) ); assertFalse( constraint.isValid( "", null ) ); assertFalse( constraint.isValid( "bla bla", null ) ); assertFalse( constraint.isValid( "This test is not foobar", null ) ); }