Return-Path: X-Original-To: apmail-bval-commits-archive@www.apache.org Delivered-To: apmail-bval-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id B7C0DD6D6 for ; Sun, 16 Sep 2012 16:12:39 +0000 (UTC) Received: (qmail 65633 invoked by uid 500); 16 Sep 2012 16:12:39 -0000 Delivered-To: apmail-bval-commits-archive@bval.apache.org Received: (qmail 65595 invoked by uid 500); 16 Sep 2012 16:12:39 -0000 Mailing-List: contact commits-help@bval.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: bval-dev@bval.apache.org Delivered-To: mailing list commits@bval.apache.org Received: (qmail 65584 invoked by uid 99); 16 Sep 2012 16:12:39 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 16 Sep 2012 16:12:39 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 16 Sep 2012 16:12:37 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id AC9E22388AA9 for ; Sun, 16 Sep 2012 16:11:54 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1385302 - in /bval/trunk/bval-jsr303/src: main/java/org/apache/bval/jsr303/AnnotationProcessor.java test/java/org/apache/bval/jsr303/ContextConstraintValidatorFactoryTest.java Date: Sun, 16 Sep 2012 16:11:54 -0000 To: commits@bval.apache.org From: mbenson@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120916161154.AC9E22388AA9@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: mbenson Date: Sun Sep 16 16:11:53 2012 New Revision: 1385302 URL: http://svn.apache.org/viewvc?rev=1385302&view=rev Log: [BVAL-111] regression resulting in bypassed context-specific ConstraintValidatorFactory Added: bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ContextConstraintValidatorFactoryTest.java (with props) Modified: bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationProcessor.java Modified: bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationProcessor.java URL: http://svn.apache.org/viewvc/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationProcessor.java?rev=1385302&r1=1385301&r2=1385302&view=diff ============================================================================== --- bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationProcessor.java (original) +++ bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationProcessor.java Sun Sep 16 16:11:53 2012 @@ -304,7 +304,7 @@ public final class AnnotationProcessor { @SuppressWarnings("unchecked") final ConstraintValidator validator = - (ConstraintValidator) factoryContext.getFactory().getConstraintValidatorFactory() + (ConstraintValidator) factoryContext.getConstraintValidatorFactory() .getInstance(validatorTypes.get(assignableTypes.get(0))); if (validator == null) { throw new ValidationException("Factory returned null validator for: " Added: bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ContextConstraintValidatorFactoryTest.java URL: http://svn.apache.org/viewvc/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ContextConstraintValidatorFactoryTest.java?rev=1385302&view=auto ============================================================================== --- bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ContextConstraintValidatorFactoryTest.java (added) +++ bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ContextConstraintValidatorFactoryTest.java Sun Sep 16 16:11:53 2012 @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.bval.jsr303; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.util.Locale; +import java.util.Set; + +import javax.validation.Constraint; +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; +import javax.validation.ConstraintValidatorFactory; +import javax.validation.ConstraintViolation; +import javax.validation.Payload; +import javax.validation.Validation; +import javax.validation.ValidatorFactory; + +import junit.framework.TestCase; + +/** + * https://issues.apache.org/jira/browse/BVAL-111 + * was a serious regression that resulted in BVal's bypassing the context-specific {@link ConstraintValidatorFactory}, + * rather using the instance available from the {@link ValidatorFactory}. Thus any solutions to e.g. inject + * collaborators into {@link ConstraintValidator} implementations would fail. + */ +public class ContextConstraintValidatorFactoryTest extends TestCase { + static ValidatorFactory factory; + + static { + factory = Validation.buildDefaultValidatorFactory(); + ((DefaultMessageInterpolator) factory.getMessageInterpolator()).setLocale(Locale.ENGLISH); + } + + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD }) + @Constraint(validatedBy = { Contrived.Validator.class }) + public @interface Contrived { + String message() default "{org.apache.bval.constraints.Contrived.message}"; + + Class[] groups() default {}; + + Class[] payload() default {}; + + public static class Validator implements ConstraintValidator { + private Object requiredCollaborator; + + public Object getRequiredCollaborator() { + return requiredCollaborator; + } + + public void setRequiredCollaborator(Object requiredCollaborator) { + this.requiredCollaborator = requiredCollaborator; + } + + public void initialize(Contrived constraintAnnotation) { + } + + public boolean isValid(Object value, ConstraintValidatorContext context) { + getRequiredCollaborator().toString(); + return true; + } + + } + + } + + @Contrived + public static class Example { + } + + public void testContextBoundConstraintValidatorFactory() { + final ConstraintValidatorFactory constraintValidatorFactory = new ConstraintValidatorFactory() { + + public > T getInstance(Class key) { + if (key.equals(Contrived.Validator.class)) { + final Contrived.Validator result = new Contrived.Validator(); + result.setRequiredCollaborator(new Object()); + @SuppressWarnings("unchecked") + final T t = (T) result; + return t; + } + return null; + } + }; + final Set> violations = factory.usingContext().constraintValidatorFactory(constraintValidatorFactory) + .getValidator().validate(new Example()); + assertTrue(violations.isEmpty()); + } +} Propchange: bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ContextConstraintValidatorFactoryTest.java ------------------------------------------------------------------------------ svn:eol-style = native