Skip to content

Commit

Permalink
HV-1220 Make programmatically defined cross parameter constraints on …
Browse files Browse the repository at this point in the history
…the parameters of a method returning void work as expected

The location used was the one of a return value so it kinda worked if the
method had parameters as the return value type was Object which is
supported for cross parameter constraints but it failed as long as the
return value type was void.
  • Loading branch information
gsmet committed Jan 20, 2017
1 parent 8b89047 commit 8e717e4
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,18 @@ public static <A extends Annotation> ConfiguredConstraint<A> forParameter(Constr
);
}

public static <A extends Annotation> ConfiguredConstraint<A> forExecutable(ConstraintDef<?, A> constraint, ExecutableElement executable) {
public static <A extends Annotation> ConfiguredConstraint<A> forReturnValue(ConstraintDef<?, A> constraint, ExecutableElement executable) {
return new ConfiguredConstraint<A>(
constraint, ConstraintLocation.forReturnValue( executable ), executable.getElementType()
);
}

public static <A extends Annotation> ConfiguredConstraint<A> forCrossParameter(ConstraintDef<?, A> constraint, ExecutableElement executable) {
return new ConfiguredConstraint<A>(
constraint, ConstraintLocation.forCrossParameter( executable ), executable.getElementType()
);
}

public ConstraintDef<?, A> getConstraint() {
return constraint;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class CrossParameterConstraintMappingContextImpl

@Override
public CrossParameterConstraintMappingContext constraint(ConstraintDef<?, ?> definition) {
super.addConstraint( ConfiguredConstraint.forExecutable( definition, executableContext.getExecutable() ) );
super.addConstraint( ConfiguredConstraint.forCrossParameter( definition, executableContext.getExecutable() ) );
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public PropertyConstraintMappingContext constraint(ConstraintDef<?, ?> definitio
}
else {
super.addConstraint(
ConfiguredConstraint.forExecutable(
ConfiguredConstraint.forReturnValue(
definition, ExecutableElement.forMethod( (Method) member )
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected ReturnValueConstraintMappingContext getThis() {

@Override
public ReturnValueConstraintMappingContext constraint(ConstraintDef<?, ?> definition) {
super.addConstraint( ConfiguredConstraint.forExecutable( definition, executableContext.getExecutable() ) );
super.addConstraint( ConfiguredConstraint.forReturnValue( definition, executableContext.getExecutable() ) );
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,38 @@ public void crossParameterConstraint() {
}
}

@Test
@TestForIssue(jiraKey = "HV-1220")
public void crossParameterConstraintOnMethodReturningVoid() {
ConstraintMapping mapping = config.createConstraintMapping();
mapping.type( GreetingService.class )
.method( "sayNothing", String.class )
.crossParameter()
.constraint(
new GenericConstraintDef<GenericAndCrossParameterConstraint>(
GenericAndCrossParameterConstraint.class
)
);
config.addMapping( mapping );

try {
GreetingService service = getValidatingProxy(
wrappedObject,
config.buildValidatorFactory().getValidator()
);
service.sayNothing( "" );

fail( "Expected exception wasn't thrown." );
}
catch (ConstraintViolationException e) {

assertCorrectConstraintViolationMessages(
e, "default message"
);
assertCorrectPropertyPaths( e, "sayNothing.<cross-parameter>" );
}
}

private interface TestGroup {
}

Expand Down Expand Up @@ -708,6 +740,8 @@ public interface GreetingService {

User getUser();

void sayNothing(String string1);

}

public class GreetingServiceImpl implements GreetingService {
Expand Down Expand Up @@ -747,5 +781,10 @@ public Message getHello() {
public User getUser() {
return new User( null );
}

@Override
public void sayNothing(String string1) {
// Nothing to do
}
}
}

0 comments on commit 8e717e4

Please sign in to comment.