Skip to content

Commit

Permalink
BVTCK-32 Adding tests for ExecutableValidator#validateReturnValue() a…
Browse files Browse the repository at this point in the history
…nd validateConstructorReturnValue()
  • Loading branch information
gunnarmorling committed Jan 17, 2013
1 parent 6e116b6 commit 935ad0c
Show file tree
Hide file tree
Showing 6 changed files with 732 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* 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.methodvalidation;

import java.util.Date;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import org.hibernate.beanvalidation.tck.tests.methodvalidation.constraint.ValidCustomer;

/**
* @author Gunnar Morling
*/
public class Customer {

private String name;

public interface Basic {
}

public interface Extended {
}

//testOneViolation
@NotNull
public Address getAddress() {
return null;
}

@ValidCustomer
public Customer() {
}

//testTwoViolations
//testNoViolations
@Size(min = 3)
@Pattern(regexp = "aaa")
public String getFirstName(String s) {
return null;
}

@NotNull
@ValidCustomer
public Customer(String name) {
this.name = name;
}

//testValidationWithGroup
@Size(min = 3, groups = Extended.class)
public String getLastName(long l) {
return null;
}

@NotNull(groups = Extended.class)
public Customer(long l) {
}

//testTwoConstraintsOfSameType
@Size.List({
@Size(min = 3),
@Size(min = 6)
})
public String getLastName(CharSequence lastName) {
return null;
}

@ValidCustomer.List({
@ValidCustomer(message = "1"),
@ValidCustomer(message = "2")
})
public Customer(CharSequence lastName) {
}

//testValidationWithSeveralGroups
@Size(min = 3, groups = Extended.class)
@Pattern(regexp = "aaa", groups = Basic.class)
public String getAllData(Date dateOfBirth) {
return null;
}

@NotNull(groups = Extended.class)
@ValidCustomer(groups = Basic.class)
public Customer(Date dateOfBirth) {
}

public String getName() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.methodvalidation;

import javax.validation.constraints.Past;

/**
* @author Gunnar Morling
*/
public class Email {

@Past
public Email() {
}

@Past
public String getValue() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
/*
* 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.methodvalidation;

import java.lang.reflect.Constructor;
import java.util.Date;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.MethodValidator;
import javax.validation.ValidationException;
import javax.validation.constraints.NotNull;
import javax.validation.metadata.ElementDescriptor.Kind;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.test.audit.annotations.SpecAssertion;
import org.jboss.test.audit.annotations.SpecAssertions;
import org.jboss.test.audit.annotations.SpecVersion;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import org.hibernate.beanvalidation.tck.tests.methodvalidation.Customer.Basic;
import org.hibernate.beanvalidation.tck.tests.methodvalidation.Customer.Extended;
import org.hibernate.beanvalidation.tck.tests.methodvalidation.constraint.MyCrossParameterConstraint;
import org.hibernate.beanvalidation.tck.tests.methodvalidation.constraint.ValidCustomer;
import org.hibernate.beanvalidation.tck.util.Groups;
import org.hibernate.beanvalidation.tck.util.TestUtil;
import org.hibernate.beanvalidation.tck.util.shrinkwrap.WebArchiveBuilder;

import static org.hibernate.beanvalidation.tck.util.TestUtil.assertCorrectConstraintTypes;
import static org.hibernate.beanvalidation.tck.util.TestUtil.assertCorrectNumberOfViolations;
import static org.hibernate.beanvalidation.tck.util.TestUtil.assertCorrectPathDescriptorKinds;
import static org.hibernate.beanvalidation.tck.util.TestUtil.assertCorrectPathNodeNames;
import static org.hibernate.beanvalidation.tck.util.TestUtil.kinds;
import static org.hibernate.beanvalidation.tck.util.TestUtil.names;

/**
* @author Gunnar Morling
*/
@SpecVersion(spec = "beanvalidation", version = "1.1.0")
public class ValidateConstructorReturnValueTest extends Arquillian {

private MethodValidator executableValidator;

@Deployment
public static WebArchive createTestArchive() {
return new WebArchiveBuilder()
.withTestClass( ValidateConstructorReturnValueTest.class )
.withPackage( MyCrossParameterConstraint.class.getPackage() )
.withClass( Address.class )
.withClass( Customer.class )
.withClass( Email.class )
.build();
}

@BeforeMethod
public void setupValidator() {
executableValidator = TestUtil.getValidatorUnderTest().forMethods();
}

//fails on RI due to wrong return value node name
@Test(groups = Groups.FAILING_IN_RI)
@SpecAssertions({
@SpecAssertion(section = "5.1.2", id = "g"),
@SpecAssertion(section = "5.1.2", id = "h")
})
public void testOneViolation() throws Exception {
Constructor<Customer> constructor = Customer.class.getConstructor();
Customer returnValue = new Customer();

Set<ConstraintViolation<Customer>> violations = executableValidator.validateConstructorReturnValue(
constructor,
returnValue
);

assertCorrectNumberOfViolations( violations, 1 );

assertCorrectConstraintTypes( violations, ValidCustomer.class );
assertCorrectPathNodeNames( violations, names( "Customer", null ) );
assertCorrectPathDescriptorKinds(
violations,
kinds( Kind.CONSTRUCTOR, Kind.RETURN_VALUE )
);
}

//fails on RI due to wrong return value node name
@Test(groups = Groups.FAILING_IN_RI)
@SpecAssertion(section = "5.1.2", id = "h")
public void testTwoViolations() throws Exception {
Constructor<Customer> constructor = Customer.class.getConstructor( String.class );
Customer returnValue = null;

Set<ConstraintViolation<Customer>> violations = executableValidator.validateConstructorReturnValue(
constructor,
returnValue
);

assertCorrectNumberOfViolations( violations, 2 );

assertCorrectConstraintTypes( violations, NotNull.class, ValidCustomer.class );
assertCorrectPathNodeNames(
violations,
names( "Customer", null ),
names( "Customer", null )
);
assertCorrectPathDescriptorKinds(
violations,
kinds( Kind.CONSTRUCTOR, Kind.RETURN_VALUE ),
kinds( Kind.CONSTRUCTOR, Kind.RETURN_VALUE )
);
}

//fails on RI due to wrong return value node name
@Test(groups = Groups.FAILING_IN_RI)
@SpecAssertion(section = "5.1.2", id = "h")
public void testTwoConstraintsOfSameType() throws Exception {
Constructor<Customer> constructor = Customer.class.getConstructor( CharSequence.class );
Customer returnValue = new Customer();

Set<ConstraintViolation<Customer>> violations = executableValidator.validateConstructorReturnValue(
constructor,
returnValue
);

assertCorrectNumberOfViolations( violations, 2 );

assertCorrectConstraintTypes( violations, ValidCustomer.class, ValidCustomer.class );
assertCorrectPathNodeNames(
violations,
names( "Customer", null ),
names( "Customer", null )
);
assertCorrectPathDescriptorKinds(
violations,
kinds( Kind.CONSTRUCTOR, Kind.RETURN_VALUE ),
kinds( Kind.CONSTRUCTOR, Kind.RETURN_VALUE )
);
}

@Test
@SpecAssertion(section = "5.1.2", id = "h")
public void testNoViolations() throws Exception {
Constructor<Customer> constructor = Customer.class.getConstructor();
Customer returnValue = new Customer( "Bob" );

Set<ConstraintViolation<Customer>> violations = executableValidator.validateConstructorReturnValue(
constructor,
returnValue
);

assertCorrectNumberOfViolations( violations, 0 );
}

//fails on RI due to wrong return value node name
@Test(groups = Groups.FAILING_IN_RI)
@SpecAssertion(section = "5.1.2", id = "h")
public void testValidationWithGroup() throws Exception {
Constructor<Customer> constructor = Customer.class.getConstructor( long.class );
Customer returnValue = null;

Set<ConstraintViolation<Customer>> violations = executableValidator.validateConstructorReturnValue(
constructor,
returnValue
);

assertCorrectNumberOfViolations( violations, 0 );

violations = executableValidator.validateConstructorReturnValue(
constructor,
returnValue,
Extended.class
);

assertCorrectConstraintTypes( violations, NotNull.class );
assertCorrectPathNodeNames( violations, names( "Customer", null ) );
assertCorrectPathDescriptorKinds(
violations,
kinds( Kind.CONSTRUCTOR, Kind.RETURN_VALUE )
);
}

//fails on RI due to wrong return value node name
@Test(groups = Groups.FAILING_IN_RI)
@SpecAssertion(section = "5.1.2", id = "h")
public void testValidationWithSeveralGroups() throws Exception {
Constructor<Customer> constructor = Customer.class.getConstructor( Date.class );
Customer returnValue = null;

Set<ConstraintViolation<Customer>> violations = executableValidator.validateConstructorReturnValue(
constructor,
returnValue
);

assertCorrectNumberOfViolations( violations, 0 );

violations = executableValidator.validateConstructorReturnValue(
constructor,
returnValue,
Basic.class,
Extended.class
);

assertCorrectConstraintTypes( violations, NotNull.class, ValidCustomer.class );
assertCorrectPathNodeNames(
violations,
names( "Customer", null ),
names( "Customer", null )
);
assertCorrectPathDescriptorKinds(
violations,
kinds( Kind.CONSTRUCTOR, Kind.RETURN_VALUE ),
kinds( Kind.CONSTRUCTOR, Kind.RETURN_VALUE )
);
}

@Test(expectedExceptions = ValidationException.class)
@SpecAssertion(section = "5.1.2", id = "g")
public void testUnexpectedType() throws Exception {
Constructor<Email> constructor = Email.class.getConstructor();
Email returnValue = new Email();

executableValidator.validateConstructorReturnValue( constructor, returnValue );
}
}

0 comments on commit 935ad0c

Please sign in to comment.