Skip to content

Commit

Permalink
#194 skip constraint rule
Browse files Browse the repository at this point in the history
  • Loading branch information
dmzaytsev committed Jan 8, 2016
1 parent 0e97779 commit 4eb18e3
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/main/java/com/jcabi/aspects/aj/MethodValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Set;
import javax.validation.ConstraintDeclarationException;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Validation;
Expand Down Expand Up @@ -148,18 +149,28 @@ public void after(final JoinPoint point, final Object result) {
* @param object Object at pointcut
* @param method Method at pointcut
* @param args Parameters of the method
* @todo We need to find a way to disable the constraint rule
* OverridingMethodMustNotAlterParameterConstraints instead
* of catching the exception.
* See https://hibernate.atlassian.net/browse/HV-1044
* After that don't forget to enable test JSR303Test#overridesMessage()
*/
private void validateMethod(final Object object, final Method method,
final Object... args) {
this.checkForViolations(
this.validator
try {
this.checkForViolations(this.validator
.forExecutables()
.validateParameters(
object,
method,
args
)
);
);
} catch (final ConstraintDeclarationException ex) {
if (!ex.getMessage().startsWith("HV000151")) {
throw new ConstraintDeclarationException(ex);
}
}
}

/**
Expand Down
54 changes: 54 additions & 0 deletions src/test/java/com/jcabi/aspects/JSR303Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,27 @@
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

/**
* Test case for {@link JSR-303} annotations and their implementations.
* @author Yegor Bugayenko ([email protected])
* @version $Id$
*/
public final class JSR303Test {
/**
* The test message.
*/
private static final String OVERRIDEN_MSG = "this is a message";

/**
* Expected exception rule.
*/
@Rule
public ExpectedException thrown = ExpectedException.none();

/**
* NotNull can throw when invalid method parameters.
Expand Down Expand Up @@ -107,6 +120,25 @@ public void validatesChainedConstructorParameters() {
new JSR303Test.ConstructorValidation(null);
}

/**
* NotNull can override the message.
*/
@Test
@Ignore
public void overridesMessage() {
this.thrown.expect(ConstraintViolationException.class);
this.thrown.expectMessage(JSR303Test.OVERRIDEN_MSG);
new JSR303Test.Bar().test(null);
}

/**
* Validator can skip a constraint rule.
*/
@Test
public void skipsConstraintRule() {
new JSR303Test.Bar().test("value");
}

/**
* Annotation.
*/
Expand Down Expand Up @@ -176,4 +208,26 @@ public ConstructorValidation(@NotNull final String param) {
}
}

/**
* Dummy interface for testing messages overriding.
*/
private interface Fum {
/**
* Test method.
* @param value Value
*/
void test(@NotNull(message = JSR303Test.OVERRIDEN_MSG) String value);
}

/**
* Dummy class for testing messages overriding.
*/
@Loggable(Loggable.INFO)
private static class Bar implements JSR303Test.Fum {
@Override
public void test(@NotNull final String value) {
//Nothing to do.
}
}

}

0 comments on commit 4eb18e3

Please sign in to comment.