Skip to content

Commit

Permalink
BVTCK-32 Adding tests for definition of cross-parameter constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarmorling committed Jan 15, 2013
1 parent 600f73a commit 4cad1dd
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 81 deletions.
@@ -0,0 +1,71 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2013, Red Hat, Inc. and/or its affiliates, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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.hibernate.beanvalidation.tck.tests.constraints.invalidconstraintdefinitions;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.CrossParameterConstraint;
import javax.validation.Payload;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* @author Gunnar Morling
*/
@Documented
@Constraint(validatedBy = GenericAndCrossParameterConstraint.GenericAndCrossParameterConstraintObjectValidator.class)
@CrossParameterConstraint(validatedBy = GenericAndCrossParameterConstraint.GenericAndCrossParameterConstraintParametersValidator.class)
@Target({ TYPE })
@Retention(RUNTIME)
public @interface GenericAndCrossParameterConstraint {
String message() default "default message";

Class<?>[] groups() default { };

Class<? extends Payload>[] payload() default { };

public static class GenericAndCrossParameterConstraintObjectValidator
implements ConstraintValidator<GenericAndCrossParameterConstraint, Object> {

@Override
public void initialize(GenericAndCrossParameterConstraint parameters) {
}

@Override
public boolean isValid(Object object, ConstraintValidatorContext constraintValidatorContext) {
return false;
}
}

public static class GenericAndCrossParameterConstraintParametersValidator
implements ConstraintValidator<GenericAndCrossParameterConstraint, Object[]> {

@Override
public void initialize(GenericAndCrossParameterConstraint parameters) {
}

@Override
public boolean isValid(Object[] parameters, ConstraintValidatorContext constraintValidatorContext) {
return false;
}
}
}
Expand Up @@ -16,7 +16,10 @@
*/
package org.hibernate.beanvalidation.tck.tests.constraints.invalidconstraintdefinitions;

import java.lang.reflect.Method;
import java.util.Date;
import javax.validation.ConstraintDefinitionException;
import javax.validation.MethodValidator;
import javax.validation.Validator;

import org.jboss.arquillian.container.test.api.Deployment;
Expand All @@ -27,6 +30,7 @@
import org.jboss.test.audit.annotations.SpecVersion;
import org.testng.annotations.Test;

import org.hibernate.beanvalidation.tck.util.Groups;
import org.hibernate.beanvalidation.tck.util.TestUtil;
import org.hibernate.beanvalidation.tck.util.shrinkwrap.WebArchiveBuilder;

Expand All @@ -45,24 +49,20 @@ public static WebArchive createTestArchive() {
.build();
}

@Test
@Test(expectedExceptions = ConstraintDefinitionException.class)
@SpecAssertions({
@SpecAssertion(section = "3.1", id = "g"),
@SpecAssertion(section = "3.1.1", id = "b"),
@SpecAssertion(section = "9.2", id = "a")
})
public void testConstraintDefinitionWithParameterStartingWithValid() {
try {
Validator validator = TestUtil.getValidatorUnderTest();
validator.validate( new DummyEntityValidProperty() );
fail( "The used constraint does use an invalid property name. The validation should have failed." );
}
catch ( ConstraintDefinitionException e ) {
// success
}

Validator validator = TestUtil.getValidatorUnderTest();
validator.validate( new DummyEntityValidProperty() );
fail( "The used constraint does use an invalid property name. The validation should have failed." );
}

@Test
@Test(expectedExceptions = ConstraintDefinitionException.class)
@SpecAssertions({
@SpecAssertion(section = "3.1", id = "g"),
@SpecAssertion(section = "3.1.1.1", id = "a"),
Expand All @@ -71,133 +71,120 @@ public void testConstraintDefinitionWithParameterStartingWithValid() {
@SpecAssertion(section = "9.2", id = "a")
})
public void testConstraintDefinitionWithoutMessageParameter() {
try {
Validator validator = TestUtil.getValidatorUnderTest();
validator.validate( new DummyEntityNoMessage() );
fail( "The used constraint does not define a message parameter. The validation should have failed." );
}
catch ( ConstraintDefinitionException e ) {
// success
}
Validator validator = TestUtil.getValidatorUnderTest();
validator.validate( new DummyEntityNoMessage() );
fail( "The used constraint does not define a message parameter. The validation should have failed." );
}

@Test
@Test(expectedExceptions = ConstraintDefinitionException.class)
@SpecAssertions({
@SpecAssertion(section = "3.1", id = "g"),
@SpecAssertion(section = "3.1.1.2", id = "a"),
@SpecAssertion(section = "9.2", id = "a")
})
public void testConstraintDefinitionWithoutGroupParameter() {
try {
Validator validator = TestUtil.getValidatorUnderTest();
validator.validate( new DummyEntityNoGroups() );
fail( "The used constraint does not define a groups parameter. The validation should have failed." );
}
catch ( ConstraintDefinitionException e ) {
// success
}
Validator validator = TestUtil.getValidatorUnderTest();
validator.validate( new DummyEntityNoGroups() );
fail( "The used constraint does not define a groups parameter. The validation should have failed." );
}

@Test
@Test(expectedExceptions = ConstraintDefinitionException.class)
@SpecAssertions({
@SpecAssertion(section = "3.1", id = "g"),
@SpecAssertion(section = "3.1.1.3", id = "a"),
@SpecAssertion(section = "9.2", id = "a")
})
public void testConstraintDefinitionWithoutPayloadParameter() {
try {
Validator validator = TestUtil.getValidatorUnderTest();
validator.validate( new DummyEntityNoGroups() );
fail( "The used constraint does not define a payload parameter. The validation should have failed." );
}
catch ( ConstraintDefinitionException e ) {
// success
}
Validator validator = TestUtil.getValidatorUnderTest();
validator.validate( new DummyEntityNoGroups() );
fail( "The used constraint does not define a payload parameter. The validation should have failed." );
}

@Test
@Test(expectedExceptions = ConstraintDefinitionException.class)
@SpecAssertions({
@SpecAssertion(section = "3.1", id = "g"),
@SpecAssertion(section = "3.1.1.2", id = "c"),
@SpecAssertion(section = "9.2", id = "a")
})
public void testConstraintDefinitionWithWrongDefaultGroupValue() {
try {
Validator validator = TestUtil.getValidatorUnderTest();
validator.validate( new DummyEntityInvalidDefaultGroup() );
fail( "The default groups parameter is not the empty array. The validation should have failed." );
}
catch ( ConstraintDefinitionException e ) {
// success
}
Validator validator = TestUtil.getValidatorUnderTest();
validator.validate( new DummyEntityInvalidDefaultGroup() );
fail( "The default groups parameter is not the empty array. The validation should have failed." );
}

@Test
@Test(expectedExceptions = ConstraintDefinitionException.class)
@SpecAssertions({
@SpecAssertion(section = "3.1", id = "g"),
@SpecAssertion(section = "3.1.1.3", id = "b"),
@SpecAssertion(section = "9.2", id = "a")
})
public void testConstraintDefinitionWithWrongDefaultPayloadValue() {
try {
Validator validator = TestUtil.getValidatorUnderTest();
validator.validate( new DummyEntityInvalidDefaultPayload() );
fail( "The default payload parameter is not the empty array. The validation should have failed." );
}
catch ( ConstraintDefinitionException e ) {
// success
}
Validator validator = TestUtil.getValidatorUnderTest();
validator.validate( new DummyEntityInvalidDefaultPayload() );
fail( "The default payload parameter is not the empty array. The validation should have failed." );
}

@Test
@Test(expectedExceptions = ConstraintDefinitionException.class)
@SpecAssertions({
@SpecAssertion(section = "3.1", id = "g"),
@SpecAssertion(section = "3.1.1.3", id = "c"),
@SpecAssertion(section = "9.2", id = "a")
})
public void testConstraintDefinitionWithWrongPayloadClass() {
try {
Validator validator = TestUtil.getValidatorUnderTest();
validator.validate( new DummyEntityInvalidPayloadClass() );
fail( "The default payload parameter has to be of type Class<? extends Payload>[]. The validation should have failed." );
}
catch ( ConstraintDefinitionException e ) {
// success
}
Validator validator = TestUtil.getValidatorUnderTest();
validator.validate( new DummyEntityInvalidPayloadClass() );
fail( "The default payload parameter has to be of type Class<? extends Payload>[]. The validation should have failed." );
}

@Test
@Test(expectedExceptions = ConstraintDefinitionException.class)
@SpecAssertions({
@SpecAssertion(section = "3.1", id = "g"),
@SpecAssertion(section = "3.1.1.1", id = "a"),
@SpecAssertion(section = "9.2", id = "a")
})
public void testConstraintDefinitionWithWrongMessageType() {
try {
Validator validator = TestUtil.getValidatorUnderTest();
validator.validate( new DummyEntityInvalidMessageType() );
fail( "The message parameter has to be of type String. The validation should have failed." );
}
catch ( ConstraintDefinitionException e ) {
// success
}
Validator validator = TestUtil.getValidatorUnderTest();
validator.validate( new DummyEntityInvalidMessageType() );
fail( "The message parameter has to be of type String. The validation should have failed." );
}

@Test
@Test(expectedExceptions = ConstraintDefinitionException.class)
@SpecAssertions({
@SpecAssertion(section = "3.1", id = "g"),
@SpecAssertion(section = "3.1.1.2", id = "b"),
@SpecAssertion(section = "9.2", id = "a")
})
public void testConstraintDefinitionWithWrongGroupType() {
try {
Validator validator = TestUtil.getValidatorUnderTest();
validator.validate( new DummyEntityInvalidGroupsType() );
fail( "The groups parameter has to be of type Class<?>[]. The validation should have failed." );
}
catch ( ConstraintDefinitionException e ) {
// success
}
Validator validator = TestUtil.getValidatorUnderTest();
validator.validate( new DummyEntityInvalidGroupsType() );
fail( "The groups parameter has to be of type Class<?>[]. The validation should have failed." );
}

@Test(expectedExceptions = ConstraintDefinitionException.class, groups = Groups.FAILING_IN_RI)
@SpecAssertions({
@SpecAssertion(section = "3.1", id = "c"),
@SpecAssertion(section = "3.1", id = "g")
})
public void testConstraintDefinitionGenericAndCrossParameterConstraint() {
Validator validator = TestUtil.getValidatorUnderTest();
validator.validate( new DummyGenericAndCrossParameterConstraint() );
fail( "A constraint may either be annotated with @Constraint or @CrossParameterConstraint, but not both. The validation should have failed." );
}

//TODO: Should we more clearly specify which exception shall be raised? The RI throws a declaration
//exception as no validator for type Object[] can be found, but one could also specify a definition
//exception
@Test(expectedExceptions = Exception.class)
@SpecAssertion(section = "3.4", id = "e")
public void testValidatorForCrossParameterConstraintMustValidateObjectArray() throws Exception {
Object object = new CalendarService();
Method method = CalendarService.class.getMethod( "createEvent", Date.class, Date.class );
Object[] parameterValues = new Object[2];

MethodValidator executableValidator = TestUtil.getValidatorUnderTest().forMethods();

executableValidator.validateParameters( object, method, parameterValues );
fail( "Validators for cross-parameter constraints must validate the type Object[]. Expected exception wasn't thrown." );
}

@InvalidDefaultGroup
Expand Down Expand Up @@ -235,4 +222,14 @@ public class DummyEntityInvalidMessageType {
@InvalidGroupsType
public class DummyEntityInvalidGroupsType {
}

@GenericAndCrossParameterConstraint
public class DummyGenericAndCrossParameterConstraint {
}

public static class CalendarService {
@InvalidCrossParameterConstraint
public void createEvent(Date start, Date end) {
}
}
}
@@ -0,0 +1,42 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2013, Red Hat, Inc. and/or its affiliates, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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.hibernate.beanvalidation.tck.tests.constraints.invalidconstraintdefinitions;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.validation.CrossParameterConstraint;
import javax.validation.Payload;

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* @author Gunnar Morling
*/
@CrossParameterConstraint(validatedBy = InvalidCrossParameterConstraintValidator.class)
@Target({ METHOD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Documented
public @interface InvalidCrossParameterConstraint {
String message() default "{validation.invalidCrossParameterConstraint}";

Class<?>[] groups() default { };

Class<? extends Payload>[] payload() default { };
}

0 comments on commit 4cad1dd

Please sign in to comment.