Author: hardy.ferentschik
Date: 2009-08-06 08:18:35 -0400 (Thu, 06 Aug 2009)
New Revision: 17240
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/ConstraintValidatorContextTest.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/DummyBean.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/DummyValidator.java
beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml
Log:
ConstraintValidatorContext tests
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/ConstraintValidatorContextTest.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/ConstraintValidatorContextTest.java 2009-08-06
10:32:05 UTC (rev 17239)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/ConstraintValidatorContextTest.java 2009-08-06
12:18:35 UTC (rev 17240)
@@ -23,20 +23,19 @@
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
+import org.jboss.test.audit.annotations.SpecAssertion;
+import org.jboss.test.audit.annotations.SpecAssertions;
import org.jboss.testharness.AbstractTest;
import org.jboss.testharness.impl.packaging.Artifact;
import org.jboss.testharness.impl.packaging.ArtifactType;
import org.jboss.testharness.impl.packaging.Classes;
import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.assertTrue;
-import static org.testng.Assert.fail;
import org.testng.annotations.Test;
import org.hibernate.jsr303.tck.util.TestUtil;
import static
org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintViolationMessages;
import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectNumberOfViolations;
import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectPropertyPaths;
-import static org.hibernate.jsr303.tck.util.TestUtil.assertEqualPaths;
/**
* @author Hardy Ferentschik
@@ -46,11 +45,12 @@
public class ConstraintValidatorContextTest extends AbstractTest {
@Test
- public void testNoCustomization() {
+ @SpecAssertion(section = "2.4", id = "l")
+ public void testDefaultError() {
Validator validator = TestUtil.getValidatorUnderTest();
DummyValidator.disableDefaultError( false );
- DummyValidator.setErrorMessages( null );
+ DummyValidator.setCustomErrorMessages( null );
DummyBean bean = new DummyBean( "foobar" );
@@ -59,16 +59,14 @@
assertCorrectConstraintViolationMessages( constraintViolations, "dummy
message" );
}
- /**
- * @todo Is this the right behaviour? The spec is not quite clear about this.
- */
@Test
- public void testDisableDefaultErrorWithoutCustomError() {
+ @SpecAssertion(section = "2.4", id = "m")
+ public void testDisableDefaultErrorWithoutAddingCustomError() {
Validator validator = TestUtil.getValidatorUnderTest();
DummyValidator.disableDefaultError( true );
Map<String, String> errors = new HashMap<String, String>();
- DummyValidator.setErrorMessages( errors );
+ DummyValidator.setCustomErrorMessages( errors );
DummyBean bean = new DummyBean( "foobar" );
@@ -77,50 +75,43 @@
}
@Test
- public void testDisableDefaultErrorWithCustomErrors() {
+ @SpecAssertions({
+ @SpecAssertion(section = "2.4", id = "m")
+ })
+ public void testDisableDefaultErrorWithCustomErrorNoSubNode() {
Validator validator = TestUtil.getValidatorUnderTest();
DummyValidator.disableDefaultError( true );
Map<String, String> errors = new HashMap<String, String>();
- errors.put( "message1", "property1" );
- DummyValidator.setErrorMessages( errors );
+ errors.put( null, "message1" );
+ DummyValidator.setCustomErrorMessages( errors );
DummyBean bean = new DummyBean( "foobar" );
Set<ConstraintViolation<DummyBean>> constraintViolations =
validator.validate( bean );
assertCorrectNumberOfViolations( constraintViolations, 1 );
assertCorrectConstraintViolationMessages( constraintViolations, "message1"
);
- assertCorrectPropertyPaths( constraintViolations, "property1" );
+ assertCorrectPropertyPaths( constraintViolations, "value" );
}
@Test
- public void testNestedValidation() {
+ @SpecAssertions({
+ @SpecAssertion(section = "2.4", id = "m"),
+ @SpecAssertion(section = "2.4", id = "q")
+ })
+ public void testDisableDefaultErrorWithCustomErrorWithSubNode() {
Validator validator = TestUtil.getValidatorUnderTest();
- DummyValidator.disableDefaultError( false );
- DummyValidator.setErrorMessages( null );
+ DummyValidator.disableDefaultError( true );
+ Map<String, String> errors = new HashMap<String, String>();
+ errors.put( "subnode", "subnode message" );
+ DummyValidator.setCustomErrorMessages( errors );
- DummyBean bean = new DummyBean( "foo" );
- bean.setNestedDummy( new DummyBean( "bar" ) );
+ DummyBean bean = new DummyBean( "foobar" );
Set<ConstraintViolation<DummyBean>> constraintViolations =
validator.validate( bean );
- assertEquals( constraintViolations.size(), 2, "Wrong number of constraints"
);
- boolean validatedNestedBean = false;
- for ( ConstraintViolation<DummyBean> violation : constraintViolations ) {
-
- if ( assertEqualPaths( violation.getPropertyPath(),
TestUtil.PathImpl.createPathFromString( "value" ) ) ) {
- assertEquals( "dummy message", violation.getMessage(), "Wrong
message" );
- }
- else if ( assertEqualPaths(
- violation.getPropertyPath(), TestUtil.PathImpl.createPathFromString(
"nestedDummy.value" )
- ) ) {
- assertEquals( "dummy message", violation.getMessage(), "Wrong
message" );
- validatedNestedBean = true;
- }
- else {
- fail( "Wrong property " + violation.getMessage() );
- }
- }
- assertTrue( validatedNestedBean );
+ assertCorrectNumberOfViolations( constraintViolations, 1 );
+ assertCorrectConstraintViolationMessages( constraintViolations, "subnode
message" );
+ assertCorrectPropertyPaths( constraintViolations, "value.subnode" );
}
}
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/DummyBean.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/DummyBean.java 2009-08-06
10:32:05 UTC (rev 17239)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/DummyBean.java 2009-08-06
12:18:35 UTC (rev 17240)
@@ -17,8 +17,6 @@
*/
package org.hibernate.jsr303.tck.tests.validation.validatorcontext;
-import javax.validation.Valid;
-
/**
* @author Hardy Ferentschik
*/
@@ -27,14 +25,8 @@
@Dummy
String value;
- @Valid
- DummyBean nestedDummy;
public DummyBean(String value) {
this.value = value;
}
-
- public void setNestedDummy(DummyBean nestedDummy) {
- this.nestedDummy = nestedDummy;
- }
}
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/DummyValidator.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/DummyValidator.java 2009-08-06
10:32:05 UTC (rev 17239)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/DummyValidator.java 2009-08-06
12:18:35 UTC (rev 17240)
@@ -41,8 +41,14 @@
if ( errorMessages != null ) {
for ( Map.Entry<String, String> entry : errorMessages.entrySet() ) {
- constraintValidatorContext.buildErrorWithMessageTemplate( entry.getKey() )
- .addSubNode( entry.getValue() ).addError();
+ if ( entry.getKey() == null ) {
+ constraintValidatorContext.buildErrorWithMessageTemplate( entry.getValue()
).addError();
+ }
+ else {
+ constraintValidatorContext.buildErrorWithMessageTemplate( entry.getValue() )
+ .addSubNode( entry.getKey() )
+ .addError();
+ }
}
}
@@ -53,7 +59,7 @@
disableDefaultError = b;
}
- public static void setErrorMessages(Map<String, String> errorMessages) {
+ public static void setCustomErrorMessages(Map<String, String> errorMessages) {
DummyValidator.errorMessages = errorMessages;
}
}
\ No newline at end of file
Modified: beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml 2009-08-06
10:32:05 UTC (rev 17239)
+++ beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml 2009-08-06
12:18:35 UTC (rev 17240)
@@ -200,11 +200,8 @@
path to the bean or property hosting the constraint</text>
</assertion>
<assertion id="q">
- <text>The leaf of the propertyPath can be adjusted by use of
ConstraintValidatorCon-
- text.addError(String, String). In this case, propertyPath is the
concatenation of
- the default propertyPath value . (dot) if propertyPath is not an empty
string (ie
- not representing the root object) and the property string passed to the
addError
- method </text>
+ <text>You can point an error to a subpath of this default path by using
the er-
+ ror builder fluent API.</text>
</assertion>
</section>
<section id="2.5" title="The ConstraintValidatorFactory">