Skip to content

Commit

Permalink
BVTCK-32 Adding test for getInvalidValue() for cross-parameter constr…
Browse files Browse the repository at this point in the history
…aints
  • Loading branch information
gunnarmorling committed Jan 17, 2013
1 parent 935ad0c commit a8d1099
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
Expand Up @@ -34,9 +34,6 @@ public interface Basic {
public interface Extended {
}

public User() {
}

//testOneViolation
public void setFirstName(@NotNull String firstName) {
}
Expand Down Expand Up @@ -86,6 +83,7 @@ public User(@NotNull(groups = Basic.class) String firstName, @Size(min = 3,
}

//testOneViolationFromCrossParameterConstraint
//testGetInvalidValueForCrossParameterConstraint
@MyCrossParameterConstraint
public void setAddress(String street, String houseNo) {
}
Expand Down Expand Up @@ -117,4 +115,13 @@ public void setAddress(String street, String houseNo, String city) {
})
public User(String street, String houseNo, String city) {
}

//testGetInvalidValueForCrossParameterConstraintOnParameterlessMethod
@MyCrossParameterConstraint
public void setAddress() {
}

@MyCrossParameterConstraint
public User() {
}
}
Expand Up @@ -48,6 +48,7 @@
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;
import static org.testng.Assert.assertEquals;

/**
* @author Gunnar Morling
Expand Down Expand Up @@ -109,6 +110,8 @@ public void testOneViolationFromCrossParameterConstraint() throws Exception {
assertCorrectConstraintTypes( violations, MyCrossParameterConstraint.class );
assertCorrectPathNodeNames( violations, names( "User" ) );
assertCorrectPathDescriptorKinds( violations, kinds( Kind.CONSTRUCTOR ) );

assertEquals( violations.iterator().next().getInvalidValue(), parameterValues );
}

@Test
Expand Down Expand Up @@ -337,4 +340,35 @@ public void testUnexpectedType() throws Exception {

executableValidator.validateConstructorParameters( constructor, parameterValues );
}

@Test
@SpecAssertion(section = "5.2", id = "e")
public void testGetInvalidValueForCrossParameterConstraint() throws Exception {
Constructor<User> constructor = User.class.getConstructor( String.class, String.class );
Object[] parameterValues = new Object[] { "Bob", "Alice" };

Set<ConstraintViolation<User>> violations = executableValidator.validateConstructorParameters(
constructor,
parameterValues
);

assertCorrectNumberOfViolations( violations, 1 );
assertEquals( violations.iterator().next().getInvalidValue(), parameterValues );
}

@Test
@SpecAssertion(section = "5.2", id = "e")
public void testGetInvalidValueForCrossParameterConstraintOnParameterlessMethod()
throws Exception {
Constructor<User> constructor = User.class.getConstructor();
Object[] parameterValues = new Object[] { };

Set<ConstraintViolation<User>> violations = executableValidator.validateConstructorParameters(
constructor,
parameterValues
);

assertCorrectNumberOfViolations( violations, 1 );
assertEquals( violations.iterator().next().getInvalidValue(), parameterValues );
}
}
Expand Up @@ -48,6 +48,7 @@
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;
import static org.testng.Assert.assertEquals;

/**
* @author Gunnar Morling
Expand Down Expand Up @@ -365,4 +366,44 @@ public void testUnexpectedType() throws Exception {

executableValidator.validateParameters( object, method, parameterValues );
}


@Test
@SpecAssertion(section = "5.2", id = "e")
public void testGetInvalidValueForCrossParameterConstraint() throws Exception {
String methodName = "setAddress";

Object object = new User();
Method method = User.class.getMethod( methodName, String.class, String.class );
Object[] parameterValues = new Object[] { "Bob", "Alice" };

Set<ConstraintViolation<Object>> violations = executableValidator.validateParameters(
object,
method,
parameterValues
);

assertCorrectNumberOfViolations( violations, 1 );
assertEquals( violations.iterator().next().getInvalidValue(), parameterValues );
}

@Test
@SpecAssertion(section = "5.2", id = "e")
public void testGetInvalidValueForCrossParameterConstraintOnParameterlessMethod()
throws Exception {
String methodName = "setAddress";

Object object = new User();
Method method = User.class.getMethod( methodName );
Object[] parameterValues = new Object[] { };

Set<ConstraintViolation<Object>> violations = executableValidator.validateParameters(
object,
method,
parameterValues
);

assertCorrectNumberOfViolations( violations, 1 );
assertEquals( violations.iterator().next().getInvalidValue(), parameterValues );
}
}

0 comments on commit a8d1099

Please sign in to comment.