You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bval.apache.org by rm...@apache.org on 2019/06/10 05:02:19 UTC

[bval] 05/16: [BVAL-111] regression resulting in bypassed context-specific ConstraintValidatorFactory

This is an automated email from the ASF dual-hosted git repository.

rmannibucau pushed a commit to tag 0.5
in repository https://gitbox.apache.org/repos/asf/bval.git

commit 4e62c130a8a5bc6048b49f646ab7c8fa5c65849a
Author: mbenson <mb...@13f79535-47bb-0310-9956-ffa450edef68>
AuthorDate: Sun Sep 16 16:11:53 2012 +0000

    [BVAL-111] regression resulting in bypassed context-specific ConstraintValidatorFactory
    
    git-svn-id: http://svn.apache.org/repos/asf/bval/trunk@1385302 13f79535-47bb-0310-9956-ffa450edef68
---
 .../apache/bval/jsr303/AnnotationProcessor.java    |   2 +-
 .../ContextConstraintValidatorFactoryTest.java     | 110 +++++++++++++++++++++
 2 files changed, 111 insertions(+), 1 deletion(-)

diff --git a/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationProcessor.java b/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationProcessor.java
index 5629e80..8f5ec34 100644
--- a/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationProcessor.java
+++ b/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationProcessor.java
@@ -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: "
diff --git a/bval-jsr303/src/test/java/org/apache/bval/jsr303/ContextConstraintValidatorFactoryTest.java b/bval-jsr303/src/test/java/org/apache/bval/jsr303/ContextConstraintValidatorFactoryTest.java
new file mode 100644
index 0000000..2700664
--- /dev/null
+++ b/bval-jsr303/src/test/java/org/apache/bval/jsr303/ContextConstraintValidatorFactoryTest.java
@@ -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());
+	}
+}