bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
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 GMT
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<A, ? super T> validator =
-                (ConstraintValidator<A, ? super T>) factoryContext.getFactory().getConstraintValidatorFactory()
+                (ConstraintValidator<A, ? super T>) 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;
+
+/**
+ * <a href="https://issues.apache.org/jira/browse/BVAL-111">https://issues.apache.org/jira/browse/BVAL-111</a>
+ * 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<? extends Payload>[] payload() default {};
+
+		public static class Validator implements ConstraintValidator<Contrived, Object> {
+			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 extends ConstraintValidator<?, ?>> T getInstance(Class<T>
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<ConstraintViolation<Example>> 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



Mime
View raw message