@@ -18,24 +18,23 @@

import java.util.List;
import java.util.Set;
import javax.validation.metadata.ParameterDescriptor;

import org.hibernate.validator.internal.metadata.core.ConstraintHelper;
import org.hibernate.validator.internal.metadata.core.MetaConstraint;
import org.hibernate.validator.internal.metadata.descriptor.ParameterDescriptorImpl;
import org.hibernate.validator.internal.metadata.raw.ConstrainedElement;
import org.hibernate.validator.internal.metadata.raw.ConstrainedElement.ConstrainedElementKind;
import org.hibernate.validator.internal.metadata.raw.ConstrainedParameter;
import org.hibernate.validator.method.metadata.ParameterDescriptor;

import static org.hibernate.validator.internal.util.CollectionHelper.newHashSet;

/**
* <p>
* An aggregated view of the constraint related meta data for a single method
* parameter.
* </p>
*
* @author Gunnar Morling
* @author Hardy Ferentschik
*/
public class ParameterMetaData extends AbstractConstraintMetaData {

@@ -78,21 +77,14 @@ public ParameterDescriptor asDescriptor(boolean defaultGroupSequenceRedefined, L
}

public static class Builder extends MetaDataBuilder {

private final Class<?> rootClass;

private final Class<?> parameterType;

private final int parameterIndex;

private final Set<MetaConstraint<?>> constraints = newHashSet();

private String name;

private boolean isCascading = false;

public Builder(Class<?> rootClass, ConstrainedParameter constrainedParameter, ConstraintHelper constraintHelper) {

super( constraintHelper );

this.rootClass = rootClass;
@@ -104,7 +96,6 @@ public Builder(Class<?> rootClass, ConstrainedParameter constrainedParameter, Co

@Override
public boolean accepts(ConstrainedElement constrainedElement) {

if ( constrainedElement.getKind() != ConstrainedElementKind.PARAMETER ) {
return false;
}
@@ -114,7 +105,6 @@ public boolean accepts(ConstrainedElement constrainedElement) {

@Override
public void add(ConstrainedElement constrainedElement) {

ConstrainedParameter constrainedParameter = (ConstrainedParameter) constrainedElement;

constraints.addAll( constrainedParameter.getConstraints() );
@@ -137,7 +127,5 @@ public ParameterMetaData build() {
isCascading
);
}

}

}
@@ -53,8 +53,6 @@ public BeanDescriptorImpl(Class<T> beanClass, Set<ConstraintDescriptorImpl<?>> c
this.constrainedMethods = Collections.unmodifiableSet( getConstrainedMethods( methods.values() ) );
}

//BeanDescriptor methods

public final boolean isBeanConstrained() {
return hasConstraints() || !constrainedProperties.isEmpty();
}
@@ -82,22 +80,12 @@ public Set<ConstructorDescriptor> getConstrainedConstructors() {
throw new IllegalArgumentException( "Not yet implemented" );
}

//TypeDescriptor methods

public boolean isTypeConstrained() {
return isBeanConstrained() || !constrainedMethods.isEmpty();
}

public Set<MethodDescriptor> getConstrainedMethods() {
return constrainedMethods;
}

//TODO GM: to be compatible with getConstraintsForProperty() this method should only return
//a descriptor if the given method is constrained.
public MethodDescriptor getConstraintsForMethod(String methodName, Class<?>... parameterTypes) {

Contracts.assertNotNull( methodName, MESSAGES.methodNameMustNotBeNull() );

return methods.get( methodName + Arrays.toString( parameterTypes ) );
}

@@ -19,9 +19,9 @@
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.hibernate.validator.method.metadata.MethodDescriptor;
import org.hibernate.validator.method.metadata.ParameterDescriptor;
import javax.validation.metadata.MethodDescriptor;
import javax.validation.metadata.ParameterDescriptor;
import javax.validation.metadata.ReturnValueDescriptor;

/**
* Describes a validated method.
@@ -39,12 +39,18 @@ public MethodDescriptorImpl(Class<?> returnType, String name, Set<ConstraintDesc
this.parameters = Collections.unmodifiableList( parameters );
}

public String getMethodName() {
@Override
public String getName() {
return name;
}

@Override
public List<ParameterDescriptor> getParameterDescriptors() {
return parameters;
}

@Override
public ReturnValueDescriptor getReturnValueDescriptor() {
return null;
}
}
@@ -18,25 +18,31 @@

import java.util.List;
import java.util.Set;
import javax.validation.metadata.ParameterDescriptor;

import org.hibernate.validator.method.metadata.ParameterDescriptor;

/**
* Describes a validated method parameter.
*
* @author Gunnar Morling
* @author Hardy Ferentschik
*/
public class ParameterDescriptorImpl extends ElementDescriptorImpl implements ParameterDescriptor {

private final int index;

public ParameterDescriptorImpl(Class<?> type, int index, Set<ConstraintDescriptorImpl<?>> constraints, boolean isCascaded, boolean defaultGroupSequenceRedefined, List<Class<?>> defaultGroupSequence) {
super( type, constraints, isCascaded, defaultGroupSequenceRedefined, defaultGroupSequence );

this.index = index;
}

@Override
public int getIndex() {
return index;
}

@Override
public String getName() {
return null;
}
}
@@ -19,9 +19,7 @@
<html>
<head/>
<body>
<p>Implementation of the Bean Validation meta data API and its Hibernate Validator specific extension for the
representation of method level constraints.
</p>
<p>Implementation of the Bean Validation meta data API.</p>

<p>The types in this package are generally immutable. Also any collections returned from methods in this package are
unmodifiable and an exception will be thrown when trying to alter them.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

@@ -120,11 +120,10 @@ public void testPropertyPathDoesNotStartWithLeadingDot() {
}

@Test(groups = "BV-1.1-Migration-Test-Failure")
@TestForIssue(jiraKey = "HV-372")
public void testHasBoolean() {
public void testIsBeanConstrained() {
Validator validator = getValidator();
BeanDescriptor beanDescr = validator.getConstraintsForClass( B.class );
assertTrue( beanDescr.isBeanConstrained() );
BeanDescriptor beanDescriptor = validator.getConstraintsForClass( B.class );
assertTrue( beanDescriptor.isBeanConstrained() );
}

@Test
@@ -187,6 +186,7 @@ public void testConstraintDefinedOnEntityNotFollowingBeanNotation() {
assertNumberOfViolations( constraintViolations, 0 );
}

@SuppressWarnings("unused")
class A {
@NotNull
String b;
@@ -198,6 +198,7 @@ class A {
D d;
}

@SuppressWarnings("unused")
class B {
private boolean b;

@@ -251,6 +252,7 @@ class Ticket {
interface TestGroup {
}

@SuppressWarnings("unused")
interface F {
@CountValidationCalls
String getFoo();
@@ -283,11 +285,13 @@ public Foo() {
}
}

@SuppressWarnings("unused")
class Bar {
@NotNull
String alwaysNull;
}

@SuppressWarnings("unused")
class NotFollowingJavaBeanNotation {
@NotNull
String m_foo;
@@ -24,6 +24,7 @@
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Payload;
import javax.validation.Valid;
import javax.validation.ValidationException;
@@ -43,7 +44,6 @@
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;
import org.hibernate.validator.method.MethodConstraintViolationException;
import org.hibernate.validator.testutil.TestForIssue;
import org.hibernate.validator.testutil.ValidatorUtil;

@@ -89,7 +89,7 @@ public void testFailFastMethodValidationDefaultBehaviour() {
service.testMethod( " ", null );
fail();
}
catch ( MethodConstraintViolationException e ) {
catch ( ConstraintViolationException e ) {
assertNumberOfViolations( e.getConstraintViolations(), 3 );
}
}
@@ -119,7 +119,7 @@ public void testFailFastMethodValidationOnConfiguration() {
service.testMethod( "a", null );
fail();
}
catch ( MethodConstraintViolationException e ) {
catch ( ConstraintViolationException e ) {
assertNumberOfViolations( e.getConstraintViolations(), 1 );
}
}
@@ -157,7 +157,7 @@ public void testFailFastMethodValidationSetOnValidatorFactory() {
service.testMethod( " ", null );
fail();
}
catch ( MethodConstraintViolationException e ) {
catch ( ConstraintViolationException e ) {
assertNumberOfViolations( e.getConstraintViolations(), 1 );
}
}
@@ -200,7 +200,7 @@ public void testFailFastMethodValidationSetWithProperty() {
service.testMethod( " ", null );
fail();
}
catch ( MethodConstraintViolationException e ) {
catch ( ConstraintViolationException e ) {
assertNumberOfViolations( e.getConstraintViolations(), 1 );
}
}

Large diffs are not rendered by default.

@@ -26,7 +26,6 @@
* @author Gunnar Morling
*/
public interface RepositoryBase<T> {

/**
* Used to test, that constraints at methods from base interfaces/classes are evaluated.
*/
@@ -22,99 +22,82 @@
import javax.validation.constraints.NotNull;
import javax.validation.metadata.BeanDescriptor;
import javax.validation.metadata.ConstraintDescriptor;
import javax.validation.metadata.MethodDescriptor;

import org.testng.annotations.Test;

import org.hibernate.validator.constraints.ScriptAssert;
import org.hibernate.validator.method.metadata.MethodDescriptor;
import org.hibernate.validator.method.metadata.TypeDescriptor;
import org.hibernate.validator.test.internal.metadata.CustomerRepository;
import org.hibernate.validator.test.internal.metadata.CustomerRepositoryExt;

import static org.hibernate.validator.internal.util.CollectionHelper.asSet;
import static org.hibernate.validator.internal.util.CollectionHelper.newHashSet;
import static org.hibernate.validator.internal.util.Contracts.assertNotNull;
import static org.hibernate.validator.testutil.ValidatorUtil.getTypeDescriptor;
import static org.hibernate.validator.testutil.ValidatorUtil.getBeanDescriptor;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;

/**
* Unit test for {@link TypeDescriptor} and its creation.
* Unit test for {@link BeanDescriptor} and its creation.
*
* @author Gunnar Morling
*/
@Test(groups = "BV-1.1-Migration-Test-Failure")
public class TypeDescriptorTest {
public class BeanDescriptorTest {

@Test
public void testGetElementClass() {

TypeDescriptor descriptor = getTypeDescriptor( CustomerRepository.class );
BeanDescriptor descriptor = getBeanDescriptor( CustomerRepository.class );
assertEquals( descriptor.getElementClass(), CustomerRepository.class );
}

@Test
public void testIsTypeConstrainedForUnconstrainedType() {

TypeDescriptor descriptor = getTypeDescriptor( UnconstrainedType.class );

assertFalse( descriptor.isTypeConstrained() );
BeanDescriptor descriptor = getBeanDescriptor( UnconstrainedType.class );
assertFalse( descriptor.isBeanConstrained() );
}

@Test
public void testIsTypeConstrainedForBeanConstrainedType() {

TypeDescriptor descriptor = getTypeDescriptor( CustomerRepository.class );

assertTrue( descriptor.isTypeConstrained() );
BeanDescriptor descriptor = getBeanDescriptor( CustomerRepository.class );
assertTrue( descriptor.isBeanConstrained() );
}

@Test
public void testIsTypeConstrainedForParameterConstrainedType() {

TypeDescriptor descriptor = getTypeDescriptor( ParameterConstrainedType.class );

assertTrue( descriptor.isTypeConstrained() );
BeanDescriptor descriptor = getBeanDescriptor( ParameterConstrainedType.class );
assertTrue( descriptor.isBeanConstrained() );
}

@Test
public void testIsTypeConstrainedForReturnValueConstrainedType() {

TypeDescriptor descriptor = getTypeDescriptor( ReturnValueConstrainedType.class );

assertTrue( descriptor.isTypeConstrained() );
BeanDescriptor descriptor = getBeanDescriptor( ReturnValueConstrainedType.class );
assertTrue( descriptor.isBeanConstrained() );
}

@Test
public void testIsTypeConstrainedForCascadingParameterType() {

TypeDescriptor descriptor = getTypeDescriptor( CascadingParameterType.class );

assertTrue( descriptor.isTypeConstrained() );
BeanDescriptor descriptor = getBeanDescriptor( CascadingParameterType.class );
assertTrue( descriptor.isBeanConstrained() );
}

@Test
public void testIsTypeConstrainedForCascadingReturnValueType() {

TypeDescriptor descriptor = getTypeDescriptor( CascadingReturnValueType.class );

assertTrue( descriptor.isTypeConstrained() );
BeanDescriptor descriptor = getBeanDescriptor( CascadingReturnValueType.class );
assertTrue( descriptor.isBeanConstrained() );
}

@Test
public void testIsTypeConstrainedForDerivedConstrainedType() {

TypeDescriptor descriptor = getTypeDescriptor( DerivedConstrainedType.class );

assertTrue( descriptor.isTypeConstrained() );
BeanDescriptor descriptor = getBeanDescriptor( DerivedConstrainedType.class );
assertTrue( descriptor.isBeanConstrained() );
}

@Test
public void testGetConstraintDescriptors() {

TypeDescriptor descriptor = getTypeDescriptor( CustomerRepository.class );
BeanDescriptor descriptor = getBeanDescriptor( CustomerRepository.class );
Set<ConstraintDescriptor<?>> constraintDescriptors = descriptor.getConstraintDescriptors();

assertEquals( constraintDescriptors.size(), 1 );
@@ -123,30 +106,25 @@ public void testGetConstraintDescriptors() {

@Test
public void testGetBeanDescriptor() {

TypeDescriptor descriptor = getTypeDescriptor( CustomerRepository.class );
BeanDescriptor beanDescriptor = descriptor.getBeanDescriptor();
BeanDescriptor beanDescriptor = getBeanDescriptor( CustomerRepository.class );

assertNotNull( beanDescriptor );
assertEquals( beanDescriptor.getElementClass(), CustomerRepository.class );
}

@Test
public void testGetConstraintsForMethod() throws Exception {

TypeDescriptor descriptor = getTypeDescriptor( CustomerRepository.class );
BeanDescriptor descriptor = getBeanDescriptor( CustomerRepository.class );
MethodDescriptor methodDescriptor = descriptor.getConstraintsForMethod( "foo" );

assertNotNull( methodDescriptor );
}


// A method descriptor can be retrieved by specifying an overridden method
// from a base type.
@Test
public void testGetConstraintsForOverriddenMethod() throws Exception {

TypeDescriptor descriptor = getTypeDescriptor( CustomerRepositoryExt.class );
BeanDescriptor descriptor = getBeanDescriptor( CustomerRepositoryExt.class );
MethodDescriptor methodDescriptor = descriptor.getConstraintsForMethod( "foo" );

assertNotNull( methodDescriptor );
@@ -157,31 +135,27 @@ public void testGetConstraintsForOverriddenMethod() throws Exception {
// CustomerRepository).
@Test
public void testGetConstraintsForMethodFromBaseType() throws Exception {

TypeDescriptor descriptor = getTypeDescriptor( CustomerRepositoryExt.class );
BeanDescriptor descriptor = getBeanDescriptor( CustomerRepositoryExt.class );
MethodDescriptor methodDescriptor = descriptor.getConstraintsForMethod( "qux" );

assertNotNull( methodDescriptor );
}

@Test
public void testGetConstraintsForUnknownMethod() throws Exception {

TypeDescriptor descriptor = getTypeDescriptor( CustomerRepository.class );
BeanDescriptor descriptor = getBeanDescriptor( CustomerRepository.class );
assertNull( descriptor.getConstraintsForMethod( "zap" ) );
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void testGetConstraintsFailsForNullMethod() throws Exception {

TypeDescriptor descriptor = getTypeDescriptor( CustomerRepository.class );
BeanDescriptor descriptor = getBeanDescriptor( CustomerRepository.class );
descriptor.getConstraintsForMethod( null );
}

@Test
public void testGetConstrainedMethods() {

TypeDescriptor descriptor = getTypeDescriptor( CustomerRepository.class );
BeanDescriptor descriptor = getBeanDescriptor( CustomerRepository.class );
Set<MethodDescriptor> constrainedMethods = descriptor.getConstrainedMethods();

assertEquals( constrainedMethods.size(), 6 );
@@ -193,8 +167,7 @@ public void testGetConstrainedMethods() {

@Test
public void testGetConstrainedMethodsForDerivedType() {

TypeDescriptor descriptor = getTypeDescriptor( CustomerRepositoryExt.class );
BeanDescriptor descriptor = getBeanDescriptor( CustomerRepositoryExt.class );
Set<MethodDescriptor> constrainedMethods = descriptor.getConstrainedMethods();

assertEquals( constrainedMethods.size(), 7 );
@@ -205,42 +178,36 @@ public void testGetConstrainedMethodsForDerivedType() {
}

private Set<String> getMethodNames(Set<MethodDescriptor> descriptors) {

Set<String> theValue = newHashSet();
Set<String> methodNames = newHashSet();

for ( MethodDescriptor methodDescriptor : descriptors ) {
theValue.add( methodDescriptor.getMethodName() );
methodNames.add( methodDescriptor.getName() );
}

return theValue;
return methodNames;
}

private static class UnconstrainedType {

@SuppressWarnings("unused")
public void foo(String foo) {

}
}

private static class ParameterConstrainedType {

@SuppressWarnings("unused")
public void foo(@NotNull String foo) {

}
}

private static class CascadingParameterType {

@SuppressWarnings("unused")
public void foo(@Valid List<String> foo) {

}
}

private static class ReturnValueConstrainedType {

@NotNull
@SuppressWarnings("unused")
public String foo(String foo) {
@@ -249,7 +216,6 @@ public String foo(String foo) {
}

private static class CascadingReturnValueType {

@Valid
@SuppressWarnings("unused")
public List<String> foo(String foo) {
@@ -258,9 +224,7 @@ public List<String> foo(String foo) {
}

private static class DerivedConstrainedType extends ParameterConstrainedType {

public void foo(String foo) {

}
}
}
@@ -21,12 +21,12 @@
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.metadata.ConstraintDescriptor;
import javax.validation.metadata.MethodDescriptor;
import javax.validation.metadata.ParameterDescriptor;
import javax.validation.metadata.Scope;

import org.testng.annotations.Test;

import org.hibernate.validator.method.metadata.MethodDescriptor;
import org.hibernate.validator.method.metadata.ParameterDescriptor;
import org.hibernate.validator.test.internal.metadata.Customer;
import org.hibernate.validator.test.internal.metadata.CustomerRepository;
import org.hibernate.validator.test.internal.metadata.CustomerRepository.ValidationGroup;
@@ -48,17 +48,18 @@ public class MethodDescriptorTest {
@Test
public void testGetMethod() throws Exception {
MethodDescriptor methodDescriptor = getMethodDescriptor( CustomerRepositoryExt.class, "foo" );
assertEquals( methodDescriptor.getMethodName(), "foo" );
assertEquals( methodDescriptor.getName(), "foo" );
}

@Test
public void testIsCascaded() {
MethodDescriptor cascadingMethodDescriptor = getMethodDescriptor( CustomerRepositoryExt.class, "foo" );
assertTrue( cascadingMethodDescriptor.isCascaded() );

MethodDescriptor nonCascadingMethodDescriptor = getMethodDescriptor( CustomerRepositoryExt.class, "baz" );
assertFalse( nonCascadingMethodDescriptor.isCascaded() );
}
// TODO - HV-571
// @Test
// public void testIsCascaded() {
// MethodDescriptor cascadingMethodDescriptor = getMethodDescriptor( CustomerRepositoryExt.class, "foo" );
// assertTrue( cascadingMethodDescriptor.isCascaded() );
//
// MethodDescriptor nonCascadingMethodDescriptor = getMethodDescriptor( CustomerRepositoryExt.class, "baz" );
// assertFalse( nonCascadingMethodDescriptor.isCascaded() );
// }

@Test
public void testHasConstraints() {
@@ -19,12 +19,12 @@
import java.util.Set;
import javax.validation.constraints.NotNull;
import javax.validation.metadata.ConstraintDescriptor;
import javax.validation.metadata.ParameterDescriptor;
import javax.validation.metadata.Scope;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import org.hibernate.validator.method.metadata.ParameterDescriptor;
import org.hibernate.validator.test.internal.metadata.Customer;
import org.hibernate.validator.test.internal.metadata.CustomerRepository;
import org.hibernate.validator.test.internal.metadata.CustomerRepositoryExt;
@@ -22,16 +22,15 @@
import javax.validation.Configuration;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.metadata.BeanDescriptor;
import javax.validation.metadata.MethodDescriptor;
import javax.validation.metadata.ParameterDescriptor;
import javax.validation.metadata.PropertyDescriptor;
import javax.validation.spi.ValidationProvider;

import org.hibernate.validator.HibernateValidator;
import org.hibernate.validator.HibernateValidatorConfiguration;
import org.hibernate.validator.cfg.ConstraintMapping;
import org.hibernate.validator.internal.engine.ValidatorImpl;
import org.hibernate.validator.method.metadata.MethodDescriptor;
import org.hibernate.validator.method.metadata.ParameterDescriptor;
import org.hibernate.validator.method.metadata.TypeDescriptor;

import static org.hibernate.validator.internal.util.Contracts.assertNotNull;

@@ -144,14 +143,14 @@ public static PropertyDescriptor getPropertyDescriptor(Class<?> clazz, String pr
}

/**
* Returns the {@code TypeDescriptor} corresponding to the given type.
* Returns the {@code BeanDescriptor} corresponding to the given type.
*
* @param clazz The type.
*
* @return an instance of {@code TypeDescriptor} for the given type, never {@code null}
* @return an instance of {@code BeanDescriptor} for the given type, never {@code null}
*/
public static TypeDescriptor getTypeDescriptor(Class<?> clazz) {
return getValidator().unwrap( ValidatorImpl.class ).getConstraintsForType( clazz );
public static BeanDescriptor getBeanDescriptor(Class<?> clazz) {
return getValidator().getConstraintsForClass( clazz );
}

/**
@@ -162,11 +161,9 @@ public static TypeDescriptor getTypeDescriptor(Class<?> clazz) {
* @param parameterTypes The method parameter types.
*
* @return an instance of {@code MethodDescriptor} for the given method signature or {@code null} if does not exists.
*
* @see TypeDescriptor#getConstraintsForMethod(String, Class[])
*/
public static MethodDescriptor getMethodDescriptor(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
return getTypeDescriptor( clazz ).getConstraintsForMethod( methodName, parameterTypes );
return getBeanDescriptor( clazz ).getConstraintsForMethod( methodName, parameterTypes );
}

/**
@@ -208,14 +205,12 @@ public static <T, I extends T> T getValidatingProxy(I implementor, ConstraintMap
* @param <I> The type of the object to be proxied.
* @param implementor The object to be proxied.
* @param methodValidator The validator to use for method validation.
* @param parameterIndex Optionally the index of the parameter to which validation shall apply.
* @param validationGroups Optionally the groups which shall be evaluated.
*
* @return A proxy performing an automatic method validation.
*/
@SuppressWarnings("unchecked")
public static <T, I extends T> T getValidatingProxy(I implementor, Validator methodValidator, Class<?>... validationGroups) {

InvocationHandler handler = new ValidationInvocationHandler(
implementor, methodValidator, validationGroups
);