Hibernate SVN: r16964 - beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/messageinterpolation.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-06-29 10:49:25 -0400 (Mon, 29 Jun 2009)
New Revision: 16964
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/messageinterpolation/MessageInterpolationTest.java
Log:
added tests for message interpolation
Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/messageinterpolation/MessageInterpolationTest.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/messageinterpolation/MessageInterpolationTest.java 2009-06-29 14:26:51 UTC (rev 16963)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/messageinterpolation/MessageInterpolationTest.java 2009-06-29 14:49:25 UTC (rev 16964)
@@ -91,6 +91,31 @@
assertEquals( actual, expected, "Wrong substitution" );
}
+ @Test(enabled = false)
+ @SpecAssertions({
+ @SpecAssertion(section = "4.3.1", id = "f"),
+ @SpecAssertion(section = "4.3.1", id = "g"),
+ @SpecAssertion(section = "4.3.1", id = "h")
+ })
+ public void testLiteralCurlyBraces() {
+
+ MessageInterpolator interpolator = getDefaultMessageInterpolator();
+ ConstraintDescriptor<?> descriptor = getDescriptorFor( "foo" );
+ MessageInterpolator.Context context = new TestContext( descriptor );
+
+ String expected = "{";
+ String actual = interpolator.interpolate( "\\{", context );
+ assertEquals( actual, expected, "Wrong substitution" );
+
+ expected = "}";
+ actual = interpolator.interpolate( "\\}", context );
+ assertEquals( actual, expected, "Wrong substitution" );
+
+ expected = "\\";
+ actual = interpolator.interpolate( "\\", context );
+ assertEquals( actual, expected, "Wrong substitution" );
+ }
+
@Test
@SpecAssertion(section = "4.3.1.1", id = "a")
public void testUnSuccessfulInterpolation() {
15 years, 6 months
Hibernate SVN: r16963 - validator/trunk/hibernate-validator-legacy/src/test/java/org/hibernate/validator/test/haintegration.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-06-29 10:26:51 -0400 (Mon, 29 Jun 2009)
New Revision: 16963
Modified:
validator/trunk/hibernate-validator-legacy/src/test/java/org/hibernate/validator/test/haintegration/HibernateAnnotationIntegrationTest.java
Log:
import cleanup
Modified: validator/trunk/hibernate-validator-legacy/src/test/java/org/hibernate/validator/test/haintegration/HibernateAnnotationIntegrationTest.java
===================================================================
--- validator/trunk/hibernate-validator-legacy/src/test/java/org/hibernate/validator/test/haintegration/HibernateAnnotationIntegrationTest.java 2009-06-29 14:26:12 UTC (rev 16962)
+++ validator/trunk/hibernate-validator-legacy/src/test/java/org/hibernate/validator/test/haintegration/HibernateAnnotationIntegrationTest.java 2009-06-29 14:26:51 UTC (rev 16963)
@@ -4,13 +4,10 @@
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
-import org.hibernate.event.PreInsertEventListener;
-import org.hibernate.event.PreUpdateEventListener;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.validator.Environment;
import org.hibernate.validator.InvalidStateException;
-import org.hibernate.validator.event.ValidateEventListener;
import org.hibernate.validator.test.HANTestCase;
/**
15 years, 6 months
Hibernate SVN: r16962 - in beanvalidation/trunk/validation-tck/src/main: java/org/hibernate/jsr303/tck/tests/validation/messageinterpolation and 5 other directories.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-06-29 10:26:12 -0400 (Mon, 29 Jun 2009)
New Revision: 16962
Added:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/messageinterpolation/MessageInterpolationTest.java
beanvalidation/trunk/validation-tck/src/main/resources/org/hibernate/jsr303/tck/tests/validation/
beanvalidation/trunk/validation-tck/src/main/resources/org/hibernate/jsr303/tck/tests/validation/messageinterpolation/
beanvalidation/trunk/validation-tck/src/main/resources/org/hibernate/jsr303/tck/tests/validation/messageinterpolation/ValidationMessages.properties
beanvalidation/trunk/validation-tck/src/main/resources/org/hibernate/jsr303/tck/tests/validation/messageinterpolation/ValidationMessages_de.properties
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/invalidconstraintdefinitions/InvalidConstraintDefinitionsTest.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/util/TestUtil.java
beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml
Log:
added tests for message interpolation
Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/invalidconstraintdefinitions/InvalidConstraintDefinitionsTest.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/invalidconstraintdefinitions/InvalidConstraintDefinitionsTest.java 2009-06-29 11:51:09 UTC (rev 16961)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/invalidconstraintdefinitions/InvalidConstraintDefinitionsTest.java 2009-06-29 14:26:12 UTC (rev 16962)
@@ -59,6 +59,8 @@
@SpecAssertions({
@SpecAssertion(section = "2.1", id = "c"),
@SpecAssertion(section = "2.1.1", id = "c"),
+ @SpecAssertion(section = "4.3.1", id = "b"),
+ @SpecAssertion(section = "4.3.1", id = "c"),
@SpecAssertion(section = "8.2", id = "a")
})
public void testConstraintDefinitionWithoutMessageParameter() {
Added: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/messageinterpolation/MessageInterpolationTest.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/messageinterpolation/MessageInterpolationTest.java (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/messageinterpolation/MessageInterpolationTest.java 2009-06-29 14:26:12 UTC (rev 16962)
@@ -0,0 +1,215 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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.jsr303.tck.tests.validation.messageinterpolation;
+
+import java.util.Locale;
+import javax.validation.MessageInterpolator;
+import javax.validation.Validator;
+import javax.validation.constraints.Max;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Size;
+import javax.validation.metadata.ConstraintDescriptor;
+
+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 org.jboss.testharness.impl.packaging.IntegrationTest;
+import org.jboss.testharness.impl.packaging.Resource;
+import org.jboss.testharness.impl.packaging.Resources;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import org.testng.annotations.Test;
+
+import org.hibernate.jsr303.tck.util.TestUtil;
+import static org.hibernate.jsr303.tck.util.TestUtil.getDefaultMessageInterpolator;
+import static org.hibernate.jsr303.tck.util.TestUtil.getDefaultValidator;
+
+/**
+ * @author Hardy Ferentschik
+ */
+@Artifact(artifactType = ArtifactType.JSR303)
+@Classes({ TestUtil.class, TestUtil.PathImpl.class, TestUtil.NodeImpl.class })
+@Resources({
+ @Resource(source = "ValidationMessages.properties",
+ destination = "WEB-INF/classes/ValidationMessages.properties"),
+ @Resource(source = "ValidationMessages_de.properties",
+ destination = "WEB-INF/classes/ValidationMessages_de.properties")
+})
+@IntegrationTest
+public class MessageInterpolationTest extends AbstractTest {
+
+ @Test
+ @SpecAssertion(section = "4.3.1", id = "a")
+ public void testDefaultMessageInterpolatorIsNotNull() {
+ MessageInterpolator interpolator = getDefaultMessageInterpolator();
+ assertNotNull( interpolator, "Each bean validation provider must provide a default message interpolator." );
+ }
+
+ @Test
+ @SpecAssertions({
+ @SpecAssertion(section = "4.3.1", id = "e"),
+ @SpecAssertion(section = "4.3.1.1", id = "a")
+ })
+ public void testSuccessfullInterpolationOfValidationMessagesValue() {
+
+ MessageInterpolator interpolator = getDefaultMessageInterpolator();
+ ConstraintDescriptor<?> descriptor = getDescriptorFor( "foo" );
+ MessageInterpolator.Context context = new TestContext( descriptor );
+
+ String expected = "replacement worked";
+ String actual = interpolator.interpolate( "{foo}", context );
+ assertEquals( actual, expected, "Wrong substitution" );
+
+ expected = "replacement worked replacement worked";
+ actual = interpolator.interpolate( "{foo} {foo}", context );
+ assertEquals( actual, expected, "Wrong substitution" );
+
+ expected = "This replacement worked just fine";
+ actual = interpolator.interpolate( "This {foo} just fine", context );
+ assertEquals( actual, expected, "Wrong substitution" );
+
+ expected = "{} { replacement worked }";
+ actual = interpolator.interpolate( "{} { {foo} }", context );
+ assertEquals( actual, expected, "Wrong substitution" );
+ }
+
+ @Test
+ @SpecAssertion(section = "4.3.1.1", id = "a")
+ public void testUnSuccessfulInterpolation() {
+ MessageInterpolator interpolator = getDefaultMessageInterpolator();
+ ConstraintDescriptor<?> descriptor = getDescriptorFor( "foo" );
+ MessageInterpolator.Context context = new TestContext( descriptor );
+
+ String expected = "foo"; // missing {}
+ String actual = interpolator.interpolate( "foo", context );
+ assertEquals( actual, expected, "Wrong substitution" );
+
+ expected = "#{foo {}";
+ actual = interpolator.interpolate( "#{foo {}", context );
+ assertEquals( actual, expected, "Wrong substitution" );
+ }
+
+ @Test
+ @SpecAssertion(section = "4.3.1.1", id = "a")
+ public void testUnkownTokenInterpolation() {
+ MessageInterpolator interpolator = getDefaultMessageInterpolator();
+ ConstraintDescriptor<?> descriptor = getDescriptorFor( "foo" );
+ MessageInterpolator.Context context = new TestContext( descriptor );
+
+ String expected = "{bar}"; // unkown token {}
+ String actual = interpolator.interpolate( "{bar}", context );
+ assertEquals( actual, expected, "Wrong substitution" );
+ }
+
+ @Test
+ @SpecAssertion(section = "4.3.1.1", id = "g")
+ public void testDefaultInterpolation() {
+ MessageInterpolator interpolator = getDefaultMessageInterpolator();
+ ConstraintDescriptor<?> descriptor = getDescriptorFor( "bar" );
+ MessageInterpolator.Context context = new TestContext( descriptor );
+
+ String expected = "size must be between 5 and 10";
+ String actual = interpolator.interpolate( ( String ) descriptor.getAttributes().get( "message" ), context );
+ assertEquals( actual, expected, "Wrong substitution" );
+ }
+
+ @Test
+ @SpecAssertion(section = "4.3.1.1", id = "h")
+ public void testMessageInterpolationWithLocale() {
+ MessageInterpolator interpolator = getDefaultMessageInterpolator();
+ ConstraintDescriptor<?> descriptor = getDescriptorFor( "foo" );
+ MessageInterpolator.Context context = new TestContext( descriptor );
+
+ String expected = "kann nicht null sein";
+ String actual = interpolator.interpolate(
+ ( String ) descriptor.getAttributes().get( "message" ), context, Locale.GERMAN
+ );
+ assertEquals( actual, expected, "Wrong substitution" );
+ }
+
+ @Test
+ @SpecAssertion(section = "4.3.1.1", id = "i")
+ public void testFallbackToDefaultLocale() {
+ MessageInterpolator interpolator = getDefaultMessageInterpolator();
+ ConstraintDescriptor<?> descriptor = getDescriptorFor( "foo" );
+ MessageInterpolator.Context context = new TestContext( descriptor );
+
+ String expected = "may not be null";
+ String actual = interpolator.interpolate( ( String ) descriptor.getAttributes().get( "message" ), context );
+ assertEquals( actual, expected, "Wrong substitution" );
+
+ expected = "may not be null";
+ actual = interpolator.interpolate(
+ ( String ) descriptor.getAttributes().get( "message" ), context, Locale.JAPAN
+ );
+ assertEquals( actual, expected, "Wrong substitution" );
+ }
+
+ @Test
+ @SpecAssertion(section = "4.3.1.1", id = "b")
+ public void testRecursiveMessageInterpoliation() {
+ MessageInterpolator interpolator = getDefaultMessageInterpolator();
+ ConstraintDescriptor<?> descriptor = getDescriptorFor( "fubar" );
+ MessageInterpolator.Context context = new TestContext( descriptor );
+
+ String expected = "recursion worked";
+ String actual = interpolator.interpolate( ( String ) descriptor.getAttributes().get( "message" ), context );
+ assertEquals(
+ expected, actual, "Expansion should be recursive"
+ );
+ }
+
+ private ConstraintDescriptor<?> getDescriptorFor(String propertyName) {
+ Validator validator = getDefaultValidator();
+ return validator.getConstraintsForClass( DummyEntity.class )
+ .getConstraintsForProperty( propertyName )
+ .getConstraintDescriptors()
+ .iterator()
+ .next();
+ }
+
+ public class TestContext implements MessageInterpolator.Context {
+ ConstraintDescriptor<?> descriptor;
+
+ TestContext(ConstraintDescriptor<?> descriptor) {
+ this.descriptor = descriptor;
+ }
+
+ public ConstraintDescriptor<?> getConstraintDescriptor() {
+ return descriptor;
+ }
+
+ public Object getValidatedValue() {
+ return null;
+ }
+ }
+
+ public class DummyEntity {
+ @NotNull
+ String foo;
+
+ @Size(min = 5, max = 10, message = "size must be between {min} and {max}")
+ String bar;
+
+ @Max(value = 10, message = "{replace.in.user.bundle1}")
+ String fubar;
+ }
+}
Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/util/TestUtil.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/util/TestUtil.java 2009-06-29 11:51:09 UTC (rev 16961)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/util/TestUtil.java 2009-06-29 14:26:12 UTC (rev 16962)
@@ -25,7 +25,9 @@
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import javax.validation.Configuration;
import javax.validation.ConstraintViolation;
+import javax.validation.MessageInterpolator;
import javax.validation.Path;
import javax.validation.Validation;
import javax.validation.Validator;
@@ -49,6 +51,11 @@
return Validation.buildDefaultValidatorFactory().getValidator();
}
+ public static MessageInterpolator getDefaultMessageInterpolator() {
+ Configuration<?> config = ( Configuration<?> ) Validation.byDefaultProvider().configure();
+ return config.getDefaultMessageInterpolator();
+ }
+
public static <T> void assertCorrectNumberOfViolations(Set<ConstraintViolation<T>> violations, int expectedViolations) {
assertEquals( violations.size(), expectedViolations, "Wrong number of constraint violations" );
}
Added: beanvalidation/trunk/validation-tck/src/main/resources/org/hibernate/jsr303/tck/tests/validation/messageinterpolation/ValidationMessages.properties
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/resources/org/hibernate/jsr303/tck/tests/validation/messageinterpolation/ValidationMessages.properties (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/resources/org/hibernate/jsr303/tck/tests/validation/messageinterpolation/ValidationMessages.properties 2009-06-29 14:26:12 UTC (rev 16962)
@@ -0,0 +1,4 @@
+javax.validation.constraints.NotNull.message=may not be null
+foo=replacement worked
+replace.in.user.bundle1={replace.in.user.bundle2}
+replace.in.user.bundle2=recursion worked
\ No newline at end of file
Added: beanvalidation/trunk/validation-tck/src/main/resources/org/hibernate/jsr303/tck/tests/validation/messageinterpolation/ValidationMessages_de.properties
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/resources/org/hibernate/jsr303/tck/tests/validation/messageinterpolation/ValidationMessages_de.properties (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/resources/org/hibernate/jsr303/tck/tests/validation/messageinterpolation/ValidationMessages_de.properties 2009-06-29 14:26:12 UTC (rev 16962)
@@ -0,0 +1 @@
+javax.validation.constraints.NotNull.message=kann nicht null sein
\ 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-06-29 11:51:09 UTC (rev 16961)
+++ beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml 2009-06-29 14:26:12 UTC (rev 16962)
@@ -599,7 +599,7 @@
<text>Each constraint defines a message descriptor via its message property</text>
</assertion>
<assertion id="c">
- <text>Every constraint definition shall define a de- fault message descriptor for that
+ <text>Every constraint definition shall define a default message descriptor for that
constraint.</text>
</assertion>
<assertion id="d">
@@ -625,32 +625,27 @@
</section>
<section id="4.3.1.1" title="Default message interpolation algorithm">
<assertion id="a">
- <text>Message parameters are extracted from the message string and used as keys to
- search the ResourceBundle named ValidationMessages (often materialized as the
- property file /ValidationMessages.properties and its locale variations) using the
- defined locale (see below). If a property is found, the message parameter is re-
- placed with the property value in the message string</text>
+ <text>Message parameters are extracted from the message string and used as keys to search the ResourceBundle
+ named ValidationMessages (step1)</text>
</assertion>
<assertion id="b">
- <text>Step 1 is applied recursively until no replacement is per- formed</text>
+ <text>Step 1 is applied recursively until no replacement is performed</text>
</assertion>
<assertion id="c">
<text>Message parameters are extracted from the message string and used as keys to
- search the Bean Validation pro- vider's built-in ResourceBundle using the defined
- locale (see below). If a property is found, the message para- meter is replaced with
- the property value in the message string</text>
+ search the Bean Validation provider's built-in ResourceBundle using the defined
+ locale. If a property is found, the message parameter is replaced with
+ the property value in the message string (step 2)</text>
</assertion>
<assertion id="e">
- <text>Contrary to step 1, step 2 is not processed re- cursively</text>
+ <text>Contrary to step 1, step 2 is not processed recursively</text>
</assertion>
<assertion id="f">
- <text>If step 2 triggers a replacement, then step 1 is applied again. Otherwise step 4
- is performed</text>
+ <text>If step 2 triggers a replacement, then step 1 is applied again</text>
</assertion>
<assertion id="g">
- <text>Message parameters are extracted from the message string. Those matching the name
- of an attribute of the constraint declaration are replaced by the value of that
- attribute.</text>
+ <text>As last step message parameters are extracted from the message string. Those matching the name
+ of an attribute of the constraint declaration are replaced by the value of that attribute.</text>
</assertion>
<assertion id="h">
<text>if the locale is passed to the interpolator method interpolate(String,
15 years, 6 months
Hibernate SVN: r16961 - core/trunk/testsuite/src/test/java/org/hibernate/test/legacy.
by hibernate-commits@lists.jboss.org
Author: steve.ebersole(a)jboss.com
Date: 2009-06-29 07:51:09 -0400 (Mon, 29 Jun 2009)
New Revision: 16961
Modified:
core/trunk/testsuite/src/test/java/org/hibernate/test/legacy/ParentChildTest.java
Log:
test data cleanup
Modified: core/trunk/testsuite/src/test/java/org/hibernate/test/legacy/ParentChildTest.java
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/legacy/ParentChildTest.java 2009-06-29 11:48:29 UTC (rev 16960)
+++ core/trunk/testsuite/src/test/java/org/hibernate/test/legacy/ParentChildTest.java 2009-06-29 11:51:09 UTC (rev 16961)
@@ -1063,6 +1063,7 @@
try {
Simple dummy = (Simple) session.get( Simple.class, new Long(-1) );
assertNotNull("Unable to locate entity Simple with id = -1", dummy);
+ session.delete( dummy );
}
catch(ObjectNotFoundException onfe) {
fail("Unable to locate entity Simple with id = -1");
15 years, 6 months
Hibernate SVN: r16960 - validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine and 1 other directories.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-06-29 07:48:29 -0400 (Mon, 29 Jun 2009)
New Revision: 16960
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/B3.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/GroupSequenceIsolationTest.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/SafeEncryption.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/LocalExecutionContext.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanDescriptorImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanMetaData.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanMetaDataImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/MetaConstraint.java
Log:
HV-164 - Made sure that the default group sequence applies when a super class gets validated where @GroupSequence is used, but not on the subclass.
Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/B3.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/B3.java 2009-06-26 15:56:27 UTC (rev 16959)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/B3.java 2009-06-29 11:48:29 UTC (rev 16960)
@@ -27,5 +27,5 @@
String encryptionKey;
@Size(max = 20)
- String nickname; //B1 group
+ String nickname;
}
\ No newline at end of file
Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/GroupSequenceIsolationTest.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/GroupSequenceIsolationTest.java 2009-06-26 15:56:27 UTC (rev 16959)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/GroupSequenceIsolationTest.java 2009-06-29 11:48:29 UTC (rev 16960)
@@ -110,7 +110,7 @@
assertCorrectNumberOfViolations( violations, 0 );
}
- @Test(enabled = false)
+ @Test
public void testCorrectDefaultSequenceInheritance3() {
Validator validator = TestUtil.getDefaultValidator();
B3 b = new B3();
@@ -120,8 +120,9 @@
b.encryptionKey = "not safe";
Set<ConstraintViolation<B3>> violations = validator.validate( b );
- assertCorrectNumberOfViolations( violations, 1 );
- assertCorrectConstraintTypes( violations, Max.class );
+ assertCorrectNumberOfViolations( violations, 2 );
+ assertCorrectConstraintTypes( violations, Max.class, Size.class );
+ assertCorrectPropertyPaths( violations, "size", "nickname" );
}
@Test
@@ -192,6 +193,5 @@
e.name = "Johnatan";
violations = validator.validate( e );
assertCorrectNumberOfViolations( violations, 0 );
-
}
}
\ No newline at end of file
Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/SafeEncryption.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/SafeEncryption.java 2009-06-26 15:56:27 UTC (rev 16959)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/SafeEncryption.java 2009-06-29 11:48:29 UTC (rev 16960)
@@ -36,7 +36,7 @@
@Retention(RUNTIME)
@Documented
public @interface SafeEncryption {
- public abstract String message() default "encryption faild";
+ public abstract String message() default "encryption failed";
public abstract Class<?>[] groups() default { };
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/LocalExecutionContext.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/LocalExecutionContext.java 2009-06-26 15:56:27 UTC (rev 16959)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/LocalExecutionContext.java 2009-06-29 11:48:29 UTC (rev 16960)
@@ -1,4 +1,4 @@
-// $Id:$
+// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
@@ -17,12 +17,14 @@
*/
package org.hibernate.validation.engine;
+import javax.validation.groups.Default;
+
/**
* An instance of this class is used to collect all the relevant information for validating a single entity/bean.
*
* @author Hardy Ferentschik
*/
-public class LocalExecutionContext<T,V> {
+public class LocalExecutionContext<T, V> {
/**
* The current bean which gets validated. This is the bean hosting the constraints which get validated.
@@ -49,14 +51,14 @@
*/
private V currentValue;
- public static <T,V> LocalExecutionContext<T,V> getLocalExecutionContext(T value) {
+ public static <T, V> LocalExecutionContext<T, V> getLocalExecutionContext(T value) {
@SuppressWarnings("unchecked")
Class<T> rootBeanClass = ( Class<T> ) value.getClass();
- return new LocalExecutionContext<T,V>( value, rootBeanClass );
+ return new LocalExecutionContext<T, V>( value, rootBeanClass );
}
- public static <T,V> LocalExecutionContext<T,V> getLocalExecutionContext(Class<T> type) {
- return new LocalExecutionContext<T,V>( null, type );
+ public static <T, V> LocalExecutionContext<T, V> getLocalExecutionContext(Class<T> type) {
+ return new LocalExecutionContext<T, V>( null, type );
}
public LocalExecutionContext(T currentBean, Class<T> currentBeanType) {
@@ -100,6 +102,10 @@
propertyPath.getLeafNode().setInIterable( true );
}
+ public boolean validatingDefault() {
+ return getCurrentGroup() != null && getCurrentGroup().getName().equals( Default.class.getName() );
+ }
+
@Override
public String toString() {
return "LocalExecutionContext{" +
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImpl.java 2009-06-26 15:56:27 UTC (rev 16959)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImpl.java 2009-06-29 11:48:29 UTC (rev 16960)
@@ -24,18 +24,18 @@
import javax.validation.ConstraintValidatorFactory;
import javax.validation.MessageInterpolator;
import javax.validation.TraversableResolver;
+import javax.validation.ValidationException;
import javax.validation.Validator;
import javax.validation.ValidatorContext;
import javax.validation.ValidatorFactory;
-import javax.validation.ValidationException;
import javax.validation.spi.ConfigurationState;
import org.hibernate.validation.metadata.AnnotationIgnores;
-import org.hibernate.validation.xml.XmlMappingParser;
+import org.hibernate.validation.metadata.BeanMetaDataCache;
import org.hibernate.validation.metadata.BeanMetaDataImpl;
-import org.hibernate.validation.metadata.BeanMetaDataCache;
+import org.hibernate.validation.metadata.ConstraintHelper;
import org.hibernate.validation.metadata.MetaConstraint;
-import org.hibernate.validation.metadata.ConstraintHelper;
+import org.hibernate.validation.xml.XmlMappingParser;
/**
* Factory returning initialized <code>Validator</code> instances.
@@ -70,7 +70,7 @@
}
public <T> T unwrap(Class<T> type) {
- throw new ValidationException( "Type " + type + " not supported");
+ throw new ValidationException( "Type " + type + " not supported" );
}
public ValidatorContext usingContext() {
@@ -89,13 +89,14 @@
mappingParser.parse( mappingStreams );
AnnotationIgnores annotationIgnores = mappingParser.getAnnotationIgnores();
- for ( Class<?> beanClass : mappingParser.getProcessedClasses() ) {
+ for ( Class<?> clazz : mappingParser.getProcessedClasses() ) {
+ Class<T> beanClass = ( Class<T> ) clazz;
BeanMetaDataImpl<T> metaData = new BeanMetaDataImpl<T>(
- ( Class<T> ) beanClass, constraintHelper, annotationIgnores
+ beanClass, constraintHelper, annotationIgnores
);
- for ( MetaConstraint<T, ? extends Annotation> constraint : mappingParser.getConstraintsForClass( ( Class<T> ) beanClass ) ) {
- metaData.addMetaConstraint( constraint );
+ for ( MetaConstraint<T, ? extends Annotation> constraint : mappingParser.getConstraintsForClass( beanClass ) ) {
+ metaData.addMetaConstraint( beanClass, constraint );
}
for ( Member m : mappingParser.getCascadedMembersForClass( beanClass ) ) {
@@ -105,7 +106,7 @@
if ( !mappingParser.getDefaultSequenceForClass( beanClass ).isEmpty() ) {
metaData.setDefaultGroupSequence( mappingParser.getDefaultSequenceForClass( beanClass ) );
}
- beanMetaDataCache.addBeanMetaData( ( Class<T> ) beanClass, ( BeanMetaDataImpl<T> ) metaData );
+ beanMetaDataCache.addBeanMetaData( beanClass, metaData );
}
}
}
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java 2009-06-26 15:56:27 UTC (rev 16959)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java 2009-06-29 11:48:29 UTC (rev 16960)
@@ -41,7 +41,6 @@
import javax.validation.metadata.BeanDescriptor;
import com.googlecode.jtype.TypeUtils;
-import org.slf4j.Logger;
import org.hibernate.validation.engine.groups.Group;
import org.hibernate.validation.engine.groups.GroupChain;
@@ -52,7 +51,6 @@
import org.hibernate.validation.metadata.BeanMetaDataImpl;
import org.hibernate.validation.metadata.ConstraintHelper;
import org.hibernate.validation.metadata.MetaConstraint;
-import org.hibernate.validation.util.LoggerFactory;
import org.hibernate.validation.util.ReflectionHelper;
/**
@@ -62,7 +60,6 @@
* @author Hardy Ferentschik
*/
public class ValidatorImpl implements Validator {
- private static final Logger log = LoggerFactory.make();
/**
* The default group array used in case any of the validate methods is called without a group.
@@ -215,7 +212,7 @@
while ( groupIterator.hasNext() ) {
Group group = groupIterator.next();
localExecutionContext.setCurrentGroup( group.getGroup() );
- validateConstraints( context, localExecutionContext, path );
+ validateConstraintsForCurrentGroup( context, localExecutionContext, path );
}
groupIterator = groupChain.getGroupIterator();
while ( groupIterator.hasNext() ) {
@@ -232,7 +229,7 @@
int numberOfViolations = context.getFailingConstraints().size();
localExecutionContext.setCurrentGroup( group.getGroup() );
- validateConstraints( context, localExecutionContext, path );
+ validateConstraintsForCurrentGroup( context, localExecutionContext, path );
validateCascadedConstraints( context, localExecutionContext, path );
if ( context.getFailingConstraints().size() > numberOfViolations ) {
@@ -243,60 +240,99 @@
return context.getFailingConstraints();
}
- /**
- * Validates non cascaded constraints
- *
- * @param executionContext The global execution context
- * @param localExecutionContext Collected information for single validation
- * @param path The current path of the validation
- * @param <T> The type of the root bean
- */
- private <T, U, V> void validateConstraints(GlobalExecutionContext<T> executionContext, LocalExecutionContext<U, V> localExecutionContext, PathImpl path) {
+ private <T, U, V> void validateConstraintsForCurrentGroup(GlobalExecutionContext<T> globalExecutionContext, LocalExecutionContext<U, V> localExecutionContext, PathImpl path) {
BeanMetaData<U> beanMetaData = getBeanMetaData( localExecutionContext.getCurrentBeanType() );
- if ( localExecutionContext.getCurrentGroup().getName().equals( Default.class.getName() ) ) {
- List<Class<?>> defaultGroupSequence = beanMetaData.getDefaultGroupSequence();
- if ( log.isTraceEnabled() && defaultGroupSequence.size() > 0 && defaultGroupSequence.get( 0 ) != Default.class ) {
- log.trace(
- "Executing re-defined Default group for bean {} as sequence {}",
- beanMetaData.getBeanClass().getName(),
- defaultGroupSequence
- );
- }
+ boolean validatingDefault = localExecutionContext.validatingDefault();
+ boolean validatedBeanRedefinesDefault = beanMetaData.defaultGroupSequenceIsRedefined();
+
+ // if we are not validating the default group there is nothing slecial to consider
+ if ( !validatingDefault ) {
+ validateConstraintsForNonDefaultGroup( globalExecutionContext, localExecutionContext, path );
+ return;
+ }
+
+ // if we are validating the default group we have to distinguish between the case where the main entity type redefines the default group and
+ // where not
+ if ( validatedBeanRedefinesDefault ) {
+ validateConstraintsForRedefinedDefaultGroupOnMainEntity(
+ globalExecutionContext, localExecutionContext, path, beanMetaData
+ );
+ }
+ else {
+ validateConstraintsForRedefinedDefaultGroup(
+ globalExecutionContext, localExecutionContext, path, beanMetaData
+ );
+ }
+ }
+
+ private <T, U, V> void validateConstraintsForRedefinedDefaultGroup(GlobalExecutionContext<T> globalExecutionContext, LocalExecutionContext<U, V> localExecutionContext, PathImpl path, BeanMetaData<U> beanMetaData) {
+ // in the case where the main entity does not redefine the default group we have to check whether the entity which defines the constraint does
+ for ( Map.Entry<Class<?>, List<MetaConstraint<U, ? extends Annotation>>> entry : beanMetaData.geMetaConstraintsAsMap()
+ .entrySet() ) {
+ Class<?> hostingBeanClass = entry.getKey();
+ List<MetaConstraint<U, ? extends Annotation>> constraints = entry.getValue();
+
+ List<Class<?>> defaultGroupSequence = getBeanMetaData( hostingBeanClass ).getDefaultGroupSequence();
for ( Class<?> defaultSequenceMember : defaultGroupSequence ) {
localExecutionContext.setCurrentGroup( defaultSequenceMember );
- boolean validationSuccessful = validateConstraintsForCurrentGroup(
- executionContext, localExecutionContext, beanMetaData, path
- );
+ boolean validationSuccessful = true;
+ for ( MetaConstraint<U, ? extends Annotation> metaConstraint : constraints ) {
+ boolean tmp = validateConstraint(
+ globalExecutionContext, localExecutionContext, metaConstraint, path
+ );
+ validationSuccessful = validationSuccessful && tmp;
+ }
if ( !validationSuccessful ) {
break;
}
}
}
- else {
- validateConstraintsForCurrentGroup( executionContext, localExecutionContext, beanMetaData, path );
- }
}
- private <T, U, V> boolean validateConstraintsForCurrentGroup(GlobalExecutionContext<T> globalExecutionContext, LocalExecutionContext<U, V> localExecutionContext, BeanMetaData<U> beanMetaData, PathImpl path) {
- boolean validationSuccessful = true;
- for ( MetaConstraint<U, ?> metaConstraint : beanMetaData.geMetaConstraintList() ) {
- PathImpl newPath = PathImpl.createShallowCopy( path );
- if ( !"".equals( metaConstraint.getPropertyName() ) ) {
- newPath.addNode( new NodeImpl( metaConstraint.getPropertyName() ) );
- }
- localExecutionContext.setPropertyPath( newPath );
- if ( isValidationRequired( globalExecutionContext, localExecutionContext, metaConstraint ) ) {
- Object valueToValidate = metaConstraint.getValue( localExecutionContext.getCurrentBean() );
- localExecutionContext.setCurrentValidatedValue( ( V ) valueToValidate );
- boolean tmp = metaConstraint.validateConstraint( globalExecutionContext, localExecutionContext );
+ private <T, U, V> void validateConstraintsForRedefinedDefaultGroupOnMainEntity(GlobalExecutionContext<T> globalExecutionContext, LocalExecutionContext<U, V> localExecutionContext, PathImpl path, BeanMetaData<U> beanMetaData) {
+ // in the case where the main entity redefines the default group we can interate over all constraints independend of the bean they are
+ // defined in. The redefined group sequence applies for all constraints.
+ List<Class<?>> defaultGroupSequence = beanMetaData.getDefaultGroupSequence();
+ for ( Class<?> defaultSequenceMember : defaultGroupSequence ) {
+ localExecutionContext.setCurrentGroup( defaultSequenceMember );
+ boolean validationSuccessful = true;
+ for ( MetaConstraint<U, ? extends Annotation> metaConstraint : beanMetaData.geMetaConstraintsAsList() ) {
+ boolean tmp = validateConstraint(
+ globalExecutionContext, localExecutionContext, metaConstraint, path
+ );
validationSuccessful = validationSuccessful && tmp;
}
- globalExecutionContext.markProcessed(
- localExecutionContext.getCurrentBean(),
- localExecutionContext.getCurrentGroup(),
- localExecutionContext.getPropertyPath()
- );
+ if ( !validationSuccessful ) {
+ break;
+ }
}
+ }
+
+ private <T, U, V> void validateConstraintsForNonDefaultGroup(GlobalExecutionContext<T> globalExecutionContext, LocalExecutionContext<U, V> localExecutionContext, PathImpl path) {
+ BeanMetaData<U> beanMetaData = getBeanMetaData( localExecutionContext.getCurrentBeanType() );
+ for ( MetaConstraint<U, ? extends Annotation> metaConstraint : beanMetaData.geMetaConstraintsAsList() ) {
+ validateConstraint( globalExecutionContext, localExecutionContext, metaConstraint, path );
+ }
+ }
+
+ private <T, U, V> boolean validateConstraint(GlobalExecutionContext<T> globalExecutionContext, LocalExecutionContext<U, V> localExecutionContext, MetaConstraint<U, ?> metaConstraint, PathImpl path) {
+ boolean validationSuccessful = true;
+ PathImpl newPath = PathImpl.createShallowCopy( path );
+ if ( !"".equals( metaConstraint.getPropertyName() ) ) {
+ newPath.addNode( new NodeImpl( metaConstraint.getPropertyName() ) );
+ }
+ localExecutionContext.setPropertyPath( newPath );
+ if ( isValidationRequired( globalExecutionContext, localExecutionContext, metaConstraint ) ) {
+ Object valueToValidate = metaConstraint.getValue( localExecutionContext.getCurrentBean() );
+ localExecutionContext.setCurrentValidatedValue( ( V ) valueToValidate );
+ validationSuccessful = metaConstraint.validateConstraint( globalExecutionContext, localExecutionContext );
+ }
+ globalExecutionContext.markProcessed(
+ localExecutionContext.getCurrentBean(),
+ localExecutionContext.getCurrentGroup(),
+ localExecutionContext.getPropertyPath()
+ );
+
return validationSuccessful;
}
@@ -647,7 +683,7 @@
);
}
- List<MetaConstraint<T, ? extends Annotation>> metaConstraintList = metaData.geMetaConstraintList();
+ List<MetaConstraint<T, ? extends Annotation>> metaConstraintList = metaData.geMetaConstraintsAsList();
for ( MetaConstraint<T, ?> metaConstraint : metaConstraintList ) {
if ( metaConstraint.getPropertyName().equals( elem.getName() ) ) {
metaConstraints.add( metaConstraint );
@@ -685,10 +721,10 @@
return value;
}
- private <T> BeanMetaData<T> getBeanMetaData(Class<T> beanClass) {
- BeanMetaDataImpl<T> metadata = beanMetaDataCache.getBeanMetaData( beanClass );
+ private <U> BeanMetaData<U> getBeanMetaData(Class<U> beanClass) {
+ BeanMetaDataImpl<U> metadata = beanMetaDataCache.getBeanMetaData( beanClass );
if ( metadata == null ) {
- metadata = new BeanMetaDataImpl<T>( beanClass, constraintHelper );
+ metadata = new BeanMetaDataImpl<U>( beanClass, constraintHelper );
beanMetaDataCache.addBeanMetaData( beanClass, metadata );
}
return metadata;
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanDescriptorImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanDescriptorImpl.java 2009-06-26 15:56:27 UTC (rev 16959)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanDescriptorImpl.java 2009-06-29 11:48:29 UTC (rev 16960)
@@ -17,7 +17,7 @@
}
public boolean isBeanConstrained() {
- return metadataBean.geMetaConstraintList().size() > 0;
+ return metadataBean.geMetaConstraintsAsMap().size() > 0;
}
public PropertyDescriptor getConstraintsForProperty(String propertyName) {
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanMetaData.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanMetaData.java 2009-06-26 15:56:27 UTC (rev 16959)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanMetaData.java 2009-06-29 11:48:29 UTC (rev 16960)
@@ -20,6 +20,7 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Member;
import java.util.List;
+import java.util.Map;
import java.util.Set;
import javax.validation.metadata.BeanDescriptor;
import javax.validation.metadata.PropertyDescriptor;
@@ -57,10 +58,16 @@
boolean defaultGroupSequenceIsRedefined();
/**
+ * @return A map of {@code MetaConstraint} instances encapsulating the information of all the constraints
+ * defined on the bean mapped to the class in which the constraints is defined.
+ */
+ Map<Class<?>, List<MetaConstraint<T, ? extends Annotation>>> geMetaConstraintsAsMap();
+
+ /**
* @return A list of {@code MetaConstraint} instances encapsulating the information of all the constraints
* defined on the bean.
*/
- List<MetaConstraint<T, ? extends Annotation>> geMetaConstraintList();
+ List<MetaConstraint<T, ? extends Annotation>> geMetaConstraintsAsList();
/**
* Return {@code PropertyDescriptor} for the given property.
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanMetaDataImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanMetaDataImpl.java 2009-06-26 15:56:27 UTC (rev 16959)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanMetaDataImpl.java 2009-06-29 11:48:29 UTC (rev 16960)
@@ -66,9 +66,10 @@
private BeanDescriptorImpl<T> beanDescriptor;
/**
- * List of constraints.
+ * Map of all direct constraints which belong to the entity {@code beanClass}. The constraints are mapped to the class
+ * (eg super class or interface) in which they are defined.
*/
- private List<MetaConstraint<T, ? extends Annotation>> metaConstraintList = new ArrayList<MetaConstraint<T, ? extends Annotation>>();
+ private Map<Class<?>, List<MetaConstraint<T, ? extends Annotation>>> metaConstraints = new HashMap<Class<?>, List<MetaConstraint<T, ? extends Annotation>>>();
/**
* List of cascaded members.
@@ -117,14 +118,30 @@
return Collections.unmodifiableList( cascadedMembers );
}
- public List<MetaConstraint<T, ? extends Annotation>> geMetaConstraintList() {
- return Collections.unmodifiableList( metaConstraintList );
+ public Map<Class<?>, List<MetaConstraint<T, ? extends Annotation>>> geMetaConstraintsAsMap() {
+ return Collections.unmodifiableMap( metaConstraints );
}
- public void addMetaConstraint(MetaConstraint<T, ? extends Annotation> metaConstraint) {
- metaConstraintList.add( metaConstraint );
+ public List<MetaConstraint<T, ? extends Annotation>> geMetaConstraintsAsList() {
+ List<MetaConstraint<T, ? extends Annotation>> constraintList = new ArrayList<MetaConstraint<T, ? extends Annotation>>();
+ for ( List<MetaConstraint<T, ? extends Annotation>> list : metaConstraints.values() ) {
+ constraintList.addAll( list );
+ }
+ return Collections.unmodifiableList( constraintList );
}
+ public void addMetaConstraint(Class<?> clazz, MetaConstraint<T, ? extends Annotation> metaConstraint) {
+ List<MetaConstraint<T, ? extends Annotation>> constraintList;
+ if ( !metaConstraints.containsKey( clazz ) ) {
+ constraintList = new ArrayList<MetaConstraint<T, ? extends Annotation>>();
+ metaConstraints.put( clazz, constraintList );
+ }
+ else {
+ constraintList = metaConstraints.get( clazz );
+ }
+ constraintList.add( metaConstraint );
+ }
+
public void addCascadedMember(Member member) {
cascadedMembers.add( member );
}
@@ -231,7 +248,7 @@
setDefaultGroupSequence( groupSequence );
}
- private void initFieldConstraints(Class clazz, AnnotationIgnores annotationIgnores) {
+ private void initFieldConstraints(Class<?> clazz, AnnotationIgnores annotationIgnores) {
for ( Field field : clazz.getDeclaredFields() ) {
List<ConstraintDescriptorImpl<?>> fieldMetadata = findConstraints( field );
for ( ConstraintDescriptorImpl<?> constraintDescription : fieldMetadata ) {
@@ -240,7 +257,7 @@
}
ReflectionHelper.setAccessibility( field );
MetaConstraint<T, ?> metaConstraint = createMetaConstraint( field, constraintDescription );
- metaConstraintList.add( metaConstraint );
+ addMetaConstraint( clazz, metaConstraint );
}
if ( field.isAnnotationPresent( Valid.class ) ) {
ReflectionHelper.setAccessibility( field );
@@ -250,7 +267,7 @@
}
}
- private void initMethodConstraints(Class clazz, AnnotationIgnores annotationIgnores) {
+ private void initMethodConstraints(Class<?> clazz, AnnotationIgnores annotationIgnores) {
for ( Method method : clazz.getDeclaredMethods() ) {
List<ConstraintDescriptorImpl<?>> methodMetadata = findConstraints( method );
for ( ConstraintDescriptorImpl<?> constraintDescription : methodMetadata ) {
@@ -259,7 +276,7 @@
}
ReflectionHelper.setAccessibility( method );
MetaConstraint<T, ?> metaConstraint = createMetaConstraint( method, constraintDescription );
- metaConstraintList.add( metaConstraint );
+ addMetaConstraint( clazz, metaConstraint );
}
if ( method.isAnnotationPresent( Valid.class ) ) {
ReflectionHelper.setAccessibility( method );
@@ -292,7 +309,7 @@
List<ConstraintDescriptorImpl<?>> classMetadata = findClassLevelConstraints( clazz );
for ( ConstraintDescriptorImpl<?> constraintDescription : classMetadata ) {
MetaConstraint<T, ?> metaConstraint = createMetaConstraint( constraintDescription );
- metaConstraintList.add( metaConstraint );
+ addMetaConstraint( clazz, metaConstraint );
}
}
@@ -402,7 +419,7 @@
sb.append( "BeanMetaDataImpl" );
sb.append( "{beanClass=" ).append( beanClass );
sb.append( ", beanDescriptor=" ).append( beanDescriptor );
- sb.append( ", metaConstraintList=" ).append( metaConstraintList );
+ sb.append( ", metaConstraints=" ).append( metaConstraints );
sb.append( ", cascadedMembers=" ).append( cascadedMembers );
sb.append( ", propertyDescriptors=" ).append( propertyDescriptors );
sb.append( ", defaultGroupSequence=" ).append( defaultGroupSequence );
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/MetaConstraint.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/MetaConstraint.java 2009-06-26 15:56:27 UTC (rev 16959)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/MetaConstraint.java 2009-06-29 11:48:29 UTC (rev 16960)
@@ -35,7 +35,7 @@
import org.hibernate.validation.util.ReflectionHelper;
/**
- * Instances of this class abstract the constraint type (class, method or field constraint) and gives access to
+ * Instances of this class abstract the constraint type (class, method or field constraint) and give access to
* meta data about the constraint. This allows a unified handling of constraints in the validator imlpementation.
*
* @author Hardy Ferentschik
@@ -171,11 +171,9 @@
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append( "MetaConstraint" );
- sb.append( "{member=" ).append( member );
- sb.append( ", propertyName='" ).append( propertyName ).append( '\'' );
+ sb.append( "{propertyName='" ).append( propertyName ).append( '\'' );
sb.append( ", elementType=" ).append( elementType );
sb.append( ", beanClass=" ).append( beanClass );
- sb.append( ", constraintTree=" ).append( constraintTree );
sb.append( '}' );
return sb.toString();
}
15 years, 6 months
Hibernate SVN: r16959 - in beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck: tests/constraints/groups and 3 other directories.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-06-26 11:56:27 -0400 (Fri, 26 Jun 2009)
New Revision: 16959
Added:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/A.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/B1.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/B2.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/B3.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/C.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/D1.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/E.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/F1.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/GroupSequenceIsolationTest.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/IsAdult.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/SafeEncryption.java
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/ConstraintCompositionTest.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatePropertyTest.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/util/TestUtil.java
Log:
HV-164 - Add tests for default group sequence isolation
Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/ConstraintCompositionTest.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/ConstraintCompositionTest.java 2009-06-26 12:23:06 UTC (rev 16958)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/ConstraintCompositionTest.java 2009-06-26 15:56:27 UTC (rev 16959)
@@ -43,7 +43,6 @@
import org.hibernate.jsr303.tck.util.TestUtil;
import static org.hibernate.jsr303.tck.util.TestUtil.assertConstraintViolation;
-import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintType;
import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintTypes;
import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintViolationMessages;
import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectNumberOfViolations;
@@ -69,7 +68,7 @@
Set<ConstraintViolation<FrenchAddress>> constraintViolations = validator.validate( address );
assertCorrectNumberOfViolations( constraintViolations, 1 );
ConstraintViolation<FrenchAddress> constraintViolation = constraintViolations.iterator().next();
- assertCorrectConstraintType( constraintViolation, NotNull.class );
+ assertCorrectConstraintTypes( constraintViolations, NotNull.class );
assertCorrectConstraintViolationMessages( constraintViolations, "may not be null" );
assertConstraintViolation(
constraintViolation,
@@ -179,7 +178,7 @@
Set<ConstraintViolation<FrenchAddress>> constraintViolations = validator.validate( address );
assertCorrectNumberOfViolations( constraintViolations, 1 );
ConstraintViolation<FrenchAddress> constraintViolation = constraintViolations.iterator().next();
- assertCorrectConstraintType( constraintViolation, NotNull.class );
+ assertCorrectConstraintTypes( constraintViolations, NotNull.class );
NotNull notNull = ( NotNull ) constraintViolation.getConstraintDescriptor().getAnnotation();
List<Class<?>> groups = Arrays.asList( notNull.groups() );
assertTrue( groups.size() == 2, "There should be two groups" );
Added: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/A.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/A.java (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/A.java 2009-06-26 15:56:27 UTC (rev 16959)
@@ -0,0 +1,40 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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.jsr303.tck.tests.constraints.groups.groupsequenceisolation;
+
+import javax.validation.GroupSequence;
+import javax.validation.constraints.Max;
+import javax.validation.constraints.Size;
+
+/**
+ * @author Hardy Ferentschik
+ */
+@GroupSequence({ Minimal.class, A.class })
+public class A {
+ @Max(value = 10, groups = Minimal.class)
+ int size;
+
+ @Size(max = 20)
+ String name; //A group
+}
+
+interface Minimal {
+}
+
+interface Heavy {
+}
Added: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/B1.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/B1.java (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/B1.java 2009-06-26 15:56:27 UTC (rev 16959)
@@ -0,0 +1,34 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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.jsr303.tck.tests.constraints.groups.groupsequenceisolation;
+
+import javax.validation.GroupSequence;
+import javax.validation.constraints.Size;
+
+/**
+ * @author Hardy Ferentschik
+ */
+@GroupSequence({ B1.class, Heavy.class })
+public class B1 extends A {
+ @SafeEncryption(groups = Heavy.class)
+ String encryptionKey;
+
+ @Size(max = 20)
+ String nickname; //B1 group
+}
+
Added: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/B2.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/B2.java (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/B2.java 2009-06-26 15:56:27 UTC (rev 16959)
@@ -0,0 +1,33 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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.jsr303.tck.tests.constraints.groups.groupsequenceisolation;
+
+import javax.validation.GroupSequence;
+import javax.validation.constraints.Size;
+
+/**
+ * @author Hardy Ferentschik
+ */
+@GroupSequence({ Minimal.class, B2.class, Heavy.class })
+public class B2 extends A {
+ @SafeEncryption(groups = Heavy.class)
+ String encryptionKey;
+
+ @Size(max = 20)
+ String nickname; //B1 group
+}
\ No newline at end of file
Added: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/B3.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/B3.java (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/B3.java 2009-06-26 15:56:27 UTC (rev 16959)
@@ -0,0 +1,31 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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.jsr303.tck.tests.constraints.groups.groupsequenceisolation;
+
+import javax.validation.constraints.Size;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class B3 extends A {
+ @SafeEncryption(groups = Heavy.class)
+ String encryptionKey;
+
+ @Size(max = 20)
+ String nickname; //B1 group
+}
\ No newline at end of file
Added: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/C.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/C.java (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/C.java 2009-06-26 15:56:27 UTC (rev 16959)
@@ -0,0 +1,51 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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.jsr303.tck.tests.constraints.groups.groupsequenceisolation;
+
+import javax.validation.GroupSequence;
+import javax.validation.Valid;
+import javax.validation.constraints.Max;
+import javax.validation.constraints.Size;
+import javax.validation.groups.Default;
+
+/**
+ * @author Hardy Ferentschik
+ */
+@GroupSequence({ Minimal.class, C.class })
+public class C {
+
+ public C() {
+ d = new D1();
+ }
+
+ @Max(value = 10, groups = Minimal.class)
+ int size;
+
+ @Size(max = 20)
+ String name;
+
+ @Valid
+ D1 d;
+}
+
+@GroupSequence({ Default.class, Heavy.class })
+interface Complete {
+}
+
+interface Later {
+}
Added: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/D1.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/D1.java (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/D1.java 2009-06-26 15:56:27 UTC (rev 16959)
@@ -0,0 +1,31 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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.jsr303.tck.tests.constraints.groups.groupsequenceisolation;
+
+import javax.validation.constraints.Size;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class D1 {
+ @SafeEncryption(groups = Heavy.class)
+ String encryptionKey;
+
+ @Size(max = 20)
+ String nickname;
+}
\ No newline at end of file
Added: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/E.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/E.java (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/E.java 2009-06-26 15:56:27 UTC (rev 16959)
@@ -0,0 +1,44 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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.jsr303.tck.tests.constraints.groups.groupsequenceisolation;
+
+import javax.validation.GroupSequence;
+import javax.validation.Valid;
+import javax.validation.constraints.Max;
+import javax.validation.constraints.Size;
+
+
+/**
+ * @author Hardy Ferentschik
+ */
+@GroupSequence({ Minimal.class, E.class })
+public class E {
+
+ public E() {
+ f = new F1();
+ }
+
+ @Max(value = 10, groups = Minimal.class)
+ int size;
+
+ @Size(max = 20)
+ String name;
+
+ @Valid
+ F1 f;
+}
\ No newline at end of file
Added: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/F1.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/F1.java (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/F1.java 2009-06-26 15:56:27 UTC (rev 16959)
@@ -0,0 +1,36 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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.jsr303.tck.tests.constraints.groups.groupsequenceisolation;
+
+import javax.validation.GroupSequence;
+import javax.validation.constraints.Size;
+
+/**
+ * @author Hardy Ferentschik
+ */
+@GroupSequence({ F1.class, Later.class })
+public class F1 {
+ @SafeEncryption(groups = Heavy.class)
+ String encryptionKey;
+
+ @Size(max = 20)
+ String nickname;
+
+ @IsAdult(groups = Later.class)
+ int age;
+}
Copied: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/GroupSequenceIsolationTest.java (from rev 16957, beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/ValidationRequirementTest.java)
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/GroupSequenceIsolationTest.java (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/GroupSequenceIsolationTest.java 2009-06-26 15:56:27 UTC (rev 16959)
@@ -0,0 +1,197 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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.jsr303.tck.tests.constraints.groups.groupsequenceisolation;
+
+import java.util.Set;
+import javax.validation.ConstraintViolation;
+import javax.validation.Validator;
+import javax.validation.constraints.Max;
+import javax.validation.constraints.Size;
+
+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 org.testng.annotations.Test;
+
+import org.hibernate.jsr303.tck.util.TestUtil;
+import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintTypes;
+import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectNumberOfViolations;
+import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectPropertyPaths;
+
+/**
+ * @author Hardy Ferentschik
+ */
+@Artifact(artifactType = ArtifactType.JSR303)
+@Classes({ TestUtil.class, TestUtil.PathImpl.class, TestUtil.NodeImpl.class })
+public class GroupSequenceIsolationTest extends AbstractTest {
+
+ @Test
+ public void testCorrectDefaultSequenceInheritance() {
+ Validator validator = TestUtil.getDefaultValidator();
+ B1 b = new B1();
+ b.name = "this name is too long";
+ b.nickname = "and this nickname as well";
+ b.size = 20;
+ b.encryptionKey = "not safe";
+
+ // when validating Default.class on B1, the following has to be validated sequentially:
+ // - @Size on name and @Size on nickname (A is part of B1)
+ // - @SafeEncryption on encryptionKey
+ // note that @Max on size is not validated as it's not part of the sequence nor the group A
+ Set<ConstraintViolation<B1>> violations = validator.validate( b );
+ assertCorrectNumberOfViolations( violations, 2 );
+ assertCorrectConstraintTypes( violations, Size.class, Size.class );
+
+ b.name = "Jonathan";
+ violations = validator.validate( b );
+ assertCorrectNumberOfViolations( violations, 1 );
+ assertCorrectConstraintTypes( violations, Size.class );
+
+ b.nickname = "Jon";
+ violations = validator.validate( b );
+ assertCorrectNumberOfViolations( violations, 1 );
+ assertCorrectConstraintTypes( violations, SafeEncryption.class );
+
+ b.encryptionKey = "secret";
+ violations = validator.validate( b );
+ assertCorrectNumberOfViolations( violations, 0 );
+ }
+
+ @Test
+ public void testCorrectDefaultSequenceInheritance2() {
+ Validator validator = TestUtil.getDefaultValidator();
+ B2 b = new B2();
+ b.name = "this name is too long";
+ b.nickname = "and this nickname as well";
+ b.size = 20;
+ b.encryptionKey = "not safe";
+
+ // when validating Default.class on B, you need to validate sequentially:
+ // - @Max on size (Minnimal group)
+ // - @Size on name and @Size on nickname (A is part of B)
+ // - @SafeEncryption on encryptionKey
+ Set<ConstraintViolation<B2>> violations = validator.validate( b );
+ assertCorrectNumberOfViolations( violations, 1 );
+ assertCorrectConstraintTypes( violations, Max.class );
+
+ b.size = 10;
+ violations = validator.validate( b );
+ assertCorrectNumberOfViolations( violations, 2 );
+ assertCorrectConstraintTypes( violations, Size.class, Size.class );
+
+ b.name = "Jonathan";
+ violations = validator.validate( b );
+ assertCorrectNumberOfViolations( violations, 1 );
+ assertCorrectConstraintTypes( violations, Size.class );
+
+ b.nickname = "Jon";
+ violations = validator.validate( b );
+ assertCorrectNumberOfViolations( violations, 1 );
+ assertCorrectConstraintTypes( violations, SafeEncryption.class );
+
+ b.encryptionKey = "secret";
+ violations = validator.validate( b );
+ assertCorrectNumberOfViolations( violations, 0 );
+ }
+
+ @Test(enabled = false)
+ public void testCorrectDefaultSequenceInheritance3() {
+ Validator validator = TestUtil.getDefaultValidator();
+ B3 b = new B3();
+ b.name = "this name is too long";
+ b.nickname = "and this nickname as well";
+ b.size = 20;
+ b.encryptionKey = "not safe";
+
+ Set<ConstraintViolation<B3>> violations = validator.validate( b );
+ assertCorrectNumberOfViolations( violations, 1 );
+ assertCorrectConstraintTypes( violations, Max.class );
+ }
+
+ @Test
+ public void testCorrectDefaultSequenceContainedCaseWithoutGroupRedefinitionOnContainedEntity() {
+ Validator validator = TestUtil.getDefaultValidator();
+ C c = new C();
+ c.name = "this name is too long";
+ c.d.nickname = "and this nickname as well";
+ c.size = 20;
+ c.d.encryptionKey = "not safe";
+
+ Set<ConstraintViolation<C>> violations = validator.validate( c );
+ assertCorrectNumberOfViolations( violations, 2 );
+ assertCorrectConstraintTypes( violations, Size.class, Max.class );
+ assertCorrectPropertyPaths( violations, "size", "d.nickname" );
+
+ c.size = 10;
+ violations = validator.validate( c );
+ assertCorrectNumberOfViolations( violations, 2 );
+ assertCorrectConstraintTypes( violations, Size.class, Size.class );
+ assertCorrectPropertyPaths( violations, "name", "d.nickname" );
+
+ c.d.nickname = "Jon";
+ violations = validator.validate( c );
+ assertCorrectNumberOfViolations( violations, 1 );
+ assertCorrectConstraintTypes( violations, Size.class );
+ assertCorrectPropertyPaths( violations, "name" );
+
+ c.name = "Johnatan";
+ violations = validator.validate( c );
+ assertCorrectNumberOfViolations( violations, 0 );
+ }
+
+ @Test
+ public void testCorrectDefaultSequenceContainedCaseWithGroupRedefinitionOnContainedEntity() {
+ Validator validator = TestUtil.getDefaultValidator();
+ E e = new E();
+ e.name = "this name is too long";
+ e.f.nickname = "and this nickname as well";
+ e.size = 20;
+ e.f.encryptionKey = "not safe";
+ e.f.age = 16;
+
+
+ Set<ConstraintViolation<E>> violations = validator.validate( e );
+ assertCorrectNumberOfViolations( violations, 2 );
+ assertCorrectConstraintTypes( violations, Size.class, Max.class );
+ assertCorrectPropertyPaths( violations, "size", "f.nickname" );
+
+ e.size = 10;
+ violations = validator.validate( e );
+ assertCorrectNumberOfViolations( violations, 2 );
+ assertCorrectConstraintTypes( violations, Size.class, Size.class );
+ assertCorrectPropertyPaths( violations, "name", "f.nickname" );
+
+ e.f.nickname = "Jon";
+ violations = validator.validate( e );
+ assertCorrectNumberOfViolations( violations, 2 );
+ assertCorrectConstraintTypes( violations, Size.class, IsAdult.class );
+ assertCorrectPropertyPaths( violations, "name", "f.age" );
+
+ e.f.age = 18;
+ violations = validator.validate( e );
+ assertCorrectNumberOfViolations( violations, 1 );
+ assertCorrectConstraintTypes( violations, Size.class );
+ assertCorrectPropertyPaths( violations, "name" );
+
+ e.name = "Johnatan";
+ violations = validator.validate( e );
+ assertCorrectNumberOfViolations( violations, 0 );
+
+ }
+}
\ No newline at end of file
Added: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/IsAdult.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/IsAdult.java (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/IsAdult.java 2009-06-26 15:56:27 UTC (rev 16959)
@@ -0,0 +1,55 @@
+// $Id: Positive.java 16835 2009-06-18 16:29:46Z hardy.ferentschik $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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.jsr303.tck.tests.constraints.groups.groupsequenceisolation;
+
+import java.lang.annotation.Documented;
+import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import java.lang.annotation.Retention;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import java.lang.annotation.Target;
+import javax.validation.Constraint;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+/**
+ * @author Hardy Ferentschik
+ */
+@Constraint(validatedBy = { IsAdult.IsAdultValidator.class })
+@Target({ METHOD, FIELD, ANNOTATION_TYPE })
+@Retention(RUNTIME)
+@Documented
+public @interface IsAdult {
+ public abstract String message() default "You are too young!";
+
+ public abstract Class<?>[] groups() default { };
+
+
+ public class IsAdultValidator implements ConstraintValidator<IsAdult, Integer> {
+ public void initialize(IsAdult parameters) {
+ }
+
+ public boolean isValid(Integer value, ConstraintValidatorContext constraintValidatorContext) {
+ if ( value == null ) {
+ return true;
+ }
+ return value >= 18;
+ }
+ }
+}
\ No newline at end of file
Copied: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/SafeEncryption.java (from rev 16948, beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/customconstraint/Positive.java)
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/SafeEncryption.java (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/groupsequenceisolation/SafeEncryption.java 2009-06-26 15:56:27 UTC (rev 16959)
@@ -0,0 +1,56 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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.jsr303.tck.tests.constraints.groups.groupsequenceisolation;
+
+import java.lang.annotation.Documented;
+import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import java.lang.annotation.Retention;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import java.lang.annotation.Target;
+import javax.validation.Constraint;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+/**
+ * @author Hardy Ferentschik
+ */
+@Constraint(validatedBy = { SafeEncryption.SafeEncryptionValidator.class })
+@Target({ METHOD, FIELD, ANNOTATION_TYPE })
+@Retention(RUNTIME)
+@Documented
+public @interface SafeEncryption {
+ public abstract String message() default "encryption faild";
+
+ public abstract Class<?>[] groups() default { };
+
+
+ public class SafeEncryptionValidator implements ConstraintValidator<SafeEncryption, String> {
+ public void initialize(SafeEncryption parameters) {
+ }
+
+ public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
+ if ( value == null ) {
+ return true;
+ }
+ return value.equals( "secret" );
+ }
+ }
+}
+
Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatePropertyTest.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatePropertyTest.java 2009-06-26 12:23:06 UTC (rev 16958)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatePropertyTest.java 2009-06-26 15:56:27 UTC (rev 16959)
@@ -34,7 +34,7 @@
import org.hibernate.jsr303.tck.util.TestUtil;
import static org.hibernate.jsr303.tck.util.TestUtil.assertConstraintViolation;
-import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintType;
+import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintTypes;
import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintViolationMessages;
import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectNumberOfViolations;
@@ -135,9 +135,8 @@
Set<ConstraintViolation<Address>> constraintViolations = validator.validateProperty( address, "city" );
assertCorrectNumberOfViolations( constraintViolations, 1 );
-
+ assertCorrectConstraintTypes( constraintViolations, Size.class );
ConstraintViolation<Address> violation = constraintViolations.iterator().next();
- assertCorrectConstraintType( violation, Size.class );
assertConstraintViolation( violation, Address.class, townInNorthWales, "city" );
assertCorrectConstraintViolationMessages(
constraintViolations, "City name cannot be longer than 30 characters."
@@ -172,9 +171,9 @@
clint, property
);
assertCorrectNumberOfViolations( constraintViolations, 1 );
+ assertCorrectConstraintTypes( constraintViolations, NotNull.class );
ConstraintViolation<ActorListBased> violation = constraintViolations.iterator().next();
- assertCorrectConstraintType( violation, NotNull.class );
assertConstraintViolation( violation, ActorListBased.class, null, property );
assertCorrectConstraintViolationMessages( constraintViolations, "Everyone has a last name." );
}
Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/util/TestUtil.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/util/TestUtil.java 2009-06-26 12:23:06 UTC (rev 16958)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/util/TestUtil.java 2009-06-26 15:56:27 UTC (rev 16959)
@@ -49,12 +49,8 @@
return Validation.buildDefaultValidatorFactory().getValidator();
}
- public static <T> void assertCorrectConstraintType(ConstraintViolation<T> violation, Class<?> expectedConstraintType) {
- assertEquals(
- ( ( Annotation ) violation.getConstraintDescriptor().getAnnotation() ).annotationType().getName(),
- expectedConstraintType.getName(),
- "Wrong constraint type"
- );
+ public static <T> void assertCorrectNumberOfViolations(Set<ConstraintViolation<T>> violations, int expectedViolations) {
+ assertEquals( violations.size(), expectedViolations, "Wrong number of constraint violations" );
}
public static <T> void assertCorrectConstraintViolationMessages(Set<ConstraintViolation<T>> violations, String... messages) {
@@ -70,26 +66,28 @@
actualMessages.contains( expectedMessage ),
"The message " + expectedMessage + " should have been in the st of error messages"
);
+ actualMessages.remove( expectedMessage );
}
+ assertTrue(
+ actualMessages.isEmpty(), "Actual messages contained more messages as specidied expected messages"
+ );
}
- public static <T> void assertCorrectNumberOfViolations(Set<ConstraintViolation<T>> violations, int expectedViolations) {
- assertEquals( violations.size(), expectedViolations, "Wrong number of constraint violations" );
- }
-
public static <T> void assertCorrectConstraintTypes(Set<ConstraintViolation<T>> violations, Class<?>... expectedConsraintTypes) {
- List<String> constraintTypes = new ArrayList<String>();
+ List<String> actualConstraintTypes = new ArrayList<String>();
for ( ConstraintViolation<?> violation : violations ) {
- constraintTypes.add(
+ actualConstraintTypes.add(
( ( Annotation ) violation.getConstraintDescriptor().getAnnotation() ).annotationType().getName()
);
}
- assertEquals( expectedConsraintTypes.length, constraintTypes.size(), "Wrong number of constraint types." );
+ assertEquals(
+ expectedConsraintTypes.length, actualConstraintTypes.size(), "Wrong number of constraint types."
+ );
for ( Class<?> expectedConstraintType : expectedConsraintTypes ) {
assertTrue(
- constraintTypes.contains( expectedConstraintType.getName() ),
+ actualConstraintTypes.contains( expectedConstraintType.getName() ),
"The constraint type " + expectedConstraintType.getName() + " should have been violated."
);
}
15 years, 6 months
Hibernate SVN: r16958 - in validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation: metadata and 1 other directory.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-06-26 08:23:06 -0400 (Fri, 26 Jun 2009)
New Revision: 16958
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanMetaDataImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/MetaConstraint.java
Log:
HV-165 - Added toString methods to make debugging easier. Tests for this issue are part of TCK.
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java 2009-06-26 12:20:03 UTC (rev 16957)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java 2009-06-26 12:23:06 UTC (rev 16958)
@@ -289,4 +289,16 @@
throw new ValidationException( "Unable to intialize " + constraintValidator.getClass().getName(), e );
}
}
+
+ @Override
+ public String toString() {
+ final StringBuilder sb = new StringBuilder();
+ sb.append( "ConstraintTree" );
+ sb.append( "{ descriptor=" ).append( descriptor );
+ sb.append( ", parent=" ).append( parent );
+ sb.append( ", children=" ).append( children );
+ sb.append( ", constraintValidatorCache=" ).append( constraintValidatorCache );
+ sb.append( '}' );
+ return sb.toString();
+ }
}
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanMetaDataImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanMetaDataImpl.java 2009-06-26 12:20:03 UTC (rev 16957)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/BeanMetaDataImpl.java 2009-06-26 12:23:06 UTC (rev 16958)
@@ -395,4 +395,19 @@
}
return metadata;
}
+
+ @Override
+ public String toString() {
+ final StringBuilder sb = new StringBuilder();
+ sb.append( "BeanMetaDataImpl" );
+ sb.append( "{beanClass=" ).append( beanClass );
+ sb.append( ", beanDescriptor=" ).append( beanDescriptor );
+ sb.append( ", metaConstraintList=" ).append( metaConstraintList );
+ sb.append( ", cascadedMembers=" ).append( cascadedMembers );
+ sb.append( ", propertyDescriptors=" ).append( propertyDescriptors );
+ sb.append( ", defaultGroupSequence=" ).append( defaultGroupSequence );
+ sb.append( ", constraintHelper=" ).append( constraintHelper );
+ sb.append( '}' );
+ return sb.toString();
+ }
}
\ No newline at end of file
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/MetaConstraint.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/MetaConstraint.java 2009-06-26 12:20:03 UTC (rev 16957)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/MetaConstraint.java 2009-06-26 12:23:06 UTC (rev 16958)
@@ -43,11 +43,6 @@
public class MetaConstraint<T, A extends Annotation> {
/**
- * The constraint tree created from the constraint annotation.
- */
- private final ConstraintTree<A> constraintTree;
-
- /**
* The member the constraint was defined on.
*/
private final Member member;
@@ -68,6 +63,11 @@
*/
private final Class<T> beanClass;
+ /**
+ * The constraint tree created from the constraint annotation.
+ */
+ private final ConstraintTree<A> constraintTree;
+
public MetaConstraint(Class<T> beanClass, ConstraintDescriptor<A> constraintDescriptor) {
this.elementType = ElementType.TYPE;
this.member = null;
@@ -166,4 +166,17 @@
}
return t;
}
+
+ @Override
+ public String toString() {
+ final StringBuilder sb = new StringBuilder();
+ sb.append( "MetaConstraint" );
+ sb.append( "{member=" ).append( member );
+ sb.append( ", propertyName='" ).append( propertyName ).append( '\'' );
+ sb.append( ", elementType=" ).append( elementType );
+ sb.append( ", beanClass=" ).append( beanClass );
+ sb.append( ", constraintTree=" ).append( constraintTree );
+ sb.append( '}' );
+ return sb.toString();
+ }
}
15 years, 6 months
Hibernate SVN: r16957 - in beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints: application and 1 other directory.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-06-26 08:20:03 -0400 (Fri, 26 Jun 2009)
New Revision: 16957
Added:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Building.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Citizen.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Person.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/SecurityCheck.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/SuperWoman.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/ValidationRequirementTest.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Woman.java
Log:
Tests for validation requirements. Includes test for HV-165 (tests for class level constraints using groups)
Added: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Building.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Building.java (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Building.java 2009-06-26 12:20:03 UTC (rev 16957)
@@ -0,0 +1,37 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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.jsr303.tck.tests.constraints.application;
+
+import javax.validation.constraints.Max;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class Building {
+ @Max(value = 5000000, message = "Building costs are max {max} dollars.")
+ long buildingCosts;
+
+ @Max(value = 5000000, message = "Building costs are max {max} dollars.")
+ public long getBuildingCosts() {
+ return buildingCosts;
+ }
+
+ public Building(long buildingCosts) {
+ this.buildingCosts = buildingCosts;
+ }
+}
Added: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Citizen.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Citizen.java (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Citizen.java 2009-06-26 12:20:03 UTC (rev 16957)
@@ -0,0 +1,34 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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.jsr303.tck.tests.constraints.application;
+
+import javax.validation.constraints.Pattern;
+import javax.validation.groups.Default;
+
+/**
+ * @author Hardy Ferentschik
+ */
+@SecurityCheck(groups = { Default.class, TightSecurity.class })
+public interface Citizen {
+ @Pattern(regexp = "^[0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]$",
+ message = "Personal number must be 10 digits with the last 4 seperated by a dash.")
+ public String getPersonalNumber();
+}
+
+interface TightSecurity {
+}
Added: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Person.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Person.java (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Person.java 2009-06-26 12:20:03 UTC (rev 16957)
@@ -0,0 +1,64 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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.jsr303.tck.tests.constraints.application;
+
+import javax.validation.constraints.NotNull;
+import javax.validation.groups.Default;
+
+/**
+ * @author Hardy Ferentschik
+ */
+@SecurityCheck(groups = Default.class)
+public abstract class Person implements Citizen {
+ @NotNull
+ String firstName;
+ String lastName;
+ String personalNumber;
+
+ public String getFirstName() {
+ return firstName;
+ }
+
+ public void setFirstName(String firstName) {
+ this.firstName = firstName;
+ }
+
+ @NotNull
+ public String getLastName() {
+ return lastName;
+ }
+
+ public void setLastName(String lastName) {
+ this.lastName = lastName;
+ }
+
+ public String getPersonalNumber() {
+ return personalNumber;
+ }
+
+ public void setPersonalNumber(String personalNumber) {
+ this.personalNumber = personalNumber;
+ }
+
+ public abstract Gender getGender();
+
+ enum Gender {
+ MALE,
+ FEMALE
+ }
+}
Copied: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/SecurityCheck.java (from rev 16948, beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintdefinition/AlwaysValid.java)
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/SecurityCheck.java (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/SecurityCheck.java 2009-06-26 12:20:03 UTC (rev 16957)
@@ -0,0 +1,63 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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.jsr303.tck.tests.constraints.application;
+
+import java.lang.annotation.Documented;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.TYPE;
+import java.lang.annotation.Retention;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import java.lang.annotation.Target;
+import javax.validation.Constraint;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+/**
+ * @author Hardy Ferentschik
+ */
+@Documented
+@Constraint(validatedBy = SecurityCheck.SecurityCheckValidator.class)
+@Target({ TYPE, METHOD, FIELD })
+@Retention(RUNTIME)
+public @interface SecurityCheck {
+ public abstract String message() default "Security check failed.";
+
+ public abstract Class<?>[] groups() default { };
+
+ public class SecurityCheckValidator implements ConstraintValidator<SecurityCheck, Object> {
+
+
+ public void initialize(SecurityCheck parameters) {
+
+ }
+
+ public boolean isValid(Object object, ConstraintValidatorContext constraintValidatorContext) {
+ if ( object == null ) {
+ return true;
+ }
+
+ if ( !( object instanceof Person ) && !( object instanceof Citizen ) ) {
+ return false;
+ }
+
+ Citizen citizen = ( Citizen ) object;
+ return !"000000-0000".equals( citizen.getPersonalNumber() );
+ }
+ }
+}
\ No newline at end of file
Added: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/SuperWoman.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/SuperWoman.java (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/SuperWoman.java 2009-06-26 12:20:03 UTC (rev 16957)
@@ -0,0 +1,36 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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.jsr303.tck.tests.constraints.application;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class SuperWoman extends Woman {
+ public SuperWoman() {
+ firstName = "Lois";
+ lastName = null;
+ }
+
+ public String getFirstName() {
+ return null;
+ }
+
+ public String getLastName() {
+ return "Lane";
+ }
+}
\ No newline at end of file
Copied: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/ValidationRequirementTest.java (from rev 16948, beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/builtinconstraints/BuiltinValidatorOverrideTest.java)
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/ValidationRequirementTest.java (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/ValidationRequirementTest.java 2009-06-26 12:20:03 UTC (rev 16957)
@@ -0,0 +1,128 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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.jsr303.tck.tests.constraints.application;
+
+import java.util.Set;
+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 org.testng.annotations.Test;
+
+import org.hibernate.jsr303.tck.util.TestUtil;
+import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintTypes;
+import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintViolationMessages;
+import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectNumberOfViolations;
+
+/**
+ * @author Hardy Ferentschik
+ */
+@Artifact(artifactType = ArtifactType.JSR303)
+@Classes({ TestUtil.class, TestUtil.PathImpl.class, TestUtil.NodeImpl.class })
+public class ValidationRequirementTest extends AbstractTest {
+
+ @Test
+ @SpecAssertions({
+ @SpecAssertion(section = "3.1", id = "c"),
+ @SpecAssertion(section = "3.1", id = "d"),
+ @SpecAssertion(section = "3.1.1", id = "a")
+ })
+ public void testClassLevelConstraints() {
+ Woman sarah = new Woman();
+ sarah.setFirstName( "Sarah" );
+ sarah.setLastName( "Jones" );
+ sarah.setPersonalNumber( "000000-0000" );
+
+ Validator validator = TestUtil.getDefaultValidator();
+ Set<ConstraintViolation<Woman>> violations = validator.validate( sarah );
+
+ assertCorrectNumberOfViolations(
+ violations, 1
+ ); // SecurityCheck for Default in Person
+ assertCorrectConstraintTypes( violations, SecurityCheck.class );
+
+ violations = validator.validate( sarah, TightSecurity.class );
+ assertCorrectNumberOfViolations(
+ violations, 1
+ ); // SecurityCheck for TightSecurity in Citizen
+ assertCorrectConstraintTypes( violations, SecurityCheck.class );
+
+ // just to make sure - validating against a group which does not have any constraints assigned to it
+ violations = validator.validate( sarah, DummyGroup.class );
+ assertCorrectNumberOfViolations( violations, 0 );
+
+ sarah.setPersonalNumber( "740523-1234" );
+ violations = validator.validate( sarah );
+ assertCorrectNumberOfViolations( violations, 0 );
+
+ violations = validator.validate( sarah, TightSecurity.class );
+ assertCorrectNumberOfViolations( violations, 0 );
+ }
+
+
+ @Test
+ @SpecAssertions({
+ @SpecAssertion(section = "3.1", id = "d"),
+ @SpecAssertion(section = "3.1.2", id = "a"),
+ @SpecAssertion(section = "3.1.2", id = "c")
+ })
+ public void testFieldAccess() {
+ SuperWoman superwoman = new SuperWoman();
+
+ Validator validator = TestUtil.getDefaultValidator();
+ Set<ConstraintViolation<SuperWoman>> violations = validator.validateProperty( superwoman, "firstName" );
+ assertCorrectNumberOfViolations( violations, 0 );
+ }
+
+ @Test
+ @SpecAssertions({
+ @SpecAssertion(section = "3.1", id = "d"),
+ @SpecAssertion(section = "3.1.2", id = "a"),
+ @SpecAssertion(section = "3.1.2", id = "d")
+ })
+ public void testPropertyAccess() {
+ SuperWoman superwoman = new SuperWoman();
+
+ Validator validator = TestUtil.getDefaultValidator();
+ Set<ConstraintViolation<SuperWoman>> violations = validator.validateProperty( superwoman, "firstName" );
+ assertCorrectNumberOfViolations( violations, 0 );
+ }
+
+ @Test(enabled = false)
+ @SpecAssertions({
+ @SpecAssertion(section = "3.1.2", id = "a"),
+ @SpecAssertion(section = "3.1.2", id = "b")
+ })
+ public void testConstraintAppliedOnFieldAndProperty() {
+ Building building = new Building( 10000000 );
+
+ Validator validator = TestUtil.getDefaultValidator();
+ Set<ConstraintViolation<Building>> violations = validator.validate( building );
+ assertCorrectNumberOfViolations( violations, 2 );
+ String expectedMessage = "Building costs are max {max} dollars.";
+ assertCorrectConstraintViolationMessages( violations, expectedMessage, expectedMessage );
+ }
+}
+
+interface DummyGroup {
+}
\ No newline at end of file
Added: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Woman.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Woman.java (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Woman.java 2009-06-26 12:20:03 UTC (rev 16957)
@@ -0,0 +1,27 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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.jsr303.tck.tests.constraints.application;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class Woman extends Person {
+ public Gender getGender() {
+ return Gender.FEMALE;
+ }
+}
15 years, 6 months
Hibernate SVN: r16956 - in beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck: tests/constraints/constraintcomposition and 5 other directories.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-06-26 08:18:52 -0400 (Fri, 26 Jun 2009)
New Revision: 16956
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/defaultprovider/BootstrapTest.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/ConstraintCompositionTest.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/DefaultGroupRedefinitionTest.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/GroupTest.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatePropertyTest.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateTest.java
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/xmlconfiguration/XmlConfigurationTest.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/util/TestUtil.java
Log:
Cleanup. Adding more helper methods to TestUtil and making sure that the tests are calling ther helpers were possible
Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/defaultprovider/BootstrapTest.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/defaultprovider/BootstrapTest.java 2009-06-26 08:29:41 UTC (rev 16955)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/defaultprovider/BootstrapTest.java 2009-06-26 12:18:52 UTC (rev 16956)
@@ -53,6 +53,8 @@
import org.hibernate.jsr303.tck.util.TestUtil;
import static org.hibernate.jsr303.tck.util.TestUtil.assertConstraintViolation;
import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectPropertyPaths;
+import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintViolationMessages;
+import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectNumberOfViolations;
/**
* @author Hardy Ferentschik
@@ -185,9 +187,8 @@
person.setPersonalNumber( 1234567890l );
Set<ConstraintViolation<Person>> constraintViolations = validator.validate( person );
- assertEquals( constraintViolations.size(), 1, "Wrong number of constraints" );
- ConstraintViolation<Person> constraintViolation = constraintViolations.iterator().next();
- assertEquals( "my custom message", constraintViolation.getMessage(), "Wrong message" );
+ assertCorrectNumberOfViolations( constraintViolations, 1 );
+ assertCorrectConstraintViolationMessages( constraintViolations, "my custom message" );
}
private void addProviderToList(List<ValidationProvider> providers, URL url)
Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/ConstraintCompositionTest.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/ConstraintCompositionTest.java 2009-06-26 08:29:41 UTC (rev 16955)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/ConstraintCompositionTest.java 2009-06-26 12:18:52 UTC (rev 16956)
@@ -21,15 +21,15 @@
import java.util.Arrays;
import java.util.List;
import java.util.Set;
-import javax.validation.metadata.BeanDescriptor;
import javax.validation.ConstraintDefinitionException;
-import javax.validation.metadata.ConstraintDescriptor;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import javax.validation.groups.Default;
+import javax.validation.metadata.BeanDescriptor;
+import javax.validation.metadata.ConstraintDescriptor;
import org.jboss.test.audit.annotations.SpecAssertion;
import org.jboss.test.audit.annotations.SpecAssertions;
@@ -37,7 +37,6 @@
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;
@@ -46,6 +45,7 @@
import static org.hibernate.jsr303.tck.util.TestUtil.assertConstraintViolation;
import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintType;
import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintTypes;
+import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintViolationMessages;
import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectNumberOfViolations;
/**
@@ -54,7 +54,7 @@
* @author Hardy Ferentschik
*/
@Artifact(artifactType = ArtifactType.JSR303)
-(a)Classes({TestUtil.class, TestUtil.PathImpl.class, TestUtil.NodeImpl.class})
+@Classes({ TestUtil.class, TestUtil.PathImpl.class, TestUtil.NodeImpl.class })
public class ConstraintCompositionTest extends AbstractTest {
@Test
@@ -70,7 +70,7 @@
assertCorrectNumberOfViolations( constraintViolations, 1 );
ConstraintViolation<FrenchAddress> constraintViolation = constraintViolations.iterator().next();
assertCorrectConstraintType( constraintViolation, NotNull.class );
- assertEquals( constraintViolation.getMessage(), "may not be null", "Wrong error message" );
+ assertCorrectConstraintViolationMessages( constraintViolations, "may not be null" );
assertConstraintViolation(
constraintViolation,
FrenchAddress.class,
@@ -102,7 +102,10 @@
}
@Test
- @SpecAssertion(section = "2.3", id = "b")
+ @SpecAssertions({
+ @SpecAssertion(section = "2.3", id = "b"),
+ @SpecAssertion(section = "2.4", id = "m")
+ })
public void testValidationOfMainAnnotationIsAlsoApplied() {
Validator validator = TestUtil.getDefaultValidator();
@@ -113,8 +116,7 @@
assertCorrectConstraintTypes(
constraintViolations, new Class<?>[] { FrenchZipcode.class }
);
- ConstraintViolation<FrenchAddress> constraintViolation = constraintViolations.iterator().next();
- assertEquals( constraintViolation.getMessage(), "00000 is a reserved code", "Wrong error message" );
+ assertCorrectConstraintViolationMessages( constraintViolations, "00000 is a reserved code" );
}
@Test
Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/DefaultGroupRedefinitionTest.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/DefaultGroupRedefinitionTest.java 2009-06-26 08:29:41 UTC (rev 16955)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/DefaultGroupRedefinitionTest.java 2009-06-26 12:18:52 UTC (rev 16956)
@@ -40,7 +40,7 @@
import org.hibernate.jsr303.tck.util.TestUtil;
import static org.hibernate.jsr303.tck.util.TestUtil.assertConstraintViolation;
-import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintViolationMessage;
+import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintViolationMessages;
import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectNumberOfViolations;
/**
@@ -49,7 +49,7 @@
* @author Hardy Ferentschik
*/
@Artifact(artifactType = ArtifactType.JSR303)
-(a)Classes({TestUtil.class, TestUtil.PathImpl.class, TestUtil.NodeImpl.class})
+@Classes({ TestUtil.class, TestUtil.PathImpl.class, TestUtil.NodeImpl.class })
public class DefaultGroupRedefinitionTest extends AbstractTest {
@Test
@@ -70,7 +70,7 @@
ConstraintViolation<Address> violation = constraintViolations.iterator().next();
assertConstraintViolation( violation, Address.class, null, "zipcode" );
- assertCorrectConstraintViolationMessage( violation, "may not be null" );
+ assertCorrectConstraintViolationMessages( constraintViolations, "may not be null" );
address.setZipcode( "41841" );
@@ -84,7 +84,7 @@
violation = constraintViolations.iterator().next();
assertConstraintViolation( violation, Address.class, address, "" );
- assertCorrectConstraintViolationMessage( violation, "Zip code is not coherent." );
+ assertCorrectConstraintViolationMessages( constraintViolations, "Zip code is not coherent." );
}
@Test(enabled = false)
@@ -119,16 +119,22 @@
// if the group sequence would not be properly redefined there would be no error when validating default.
Set<ConstraintViolation<Car>> constraintViolations = validator.validate( car );
- assertCorrectNumberOfViolations(constraintViolations, 1);
- assertCorrectConstraintViolationMessage( constraintViolations.iterator().next(), "Car type has to be betweeb 2 and 20 characters." );
+ assertCorrectNumberOfViolations( constraintViolations, 1 );
+ assertCorrectConstraintViolationMessages(
+ constraintViolations, "Car type has to be betweeb 2 and 20 characters."
+ );
constraintViolations = validator.validateProperty( car, "type" );
- assertCorrectNumberOfViolations(constraintViolations, 1);
- assertCorrectConstraintViolationMessage( constraintViolations.iterator().next(), "Car type has to be betweeb 2 and 20 characters.");
+ assertCorrectNumberOfViolations( constraintViolations, 1 );
+ assertCorrectConstraintViolationMessages(
+ constraintViolations, "Car type has to be betweeb 2 and 20 characters."
+ );
constraintViolations = validator.validateValue( Car.class, "type", "A" );
- assertCorrectNumberOfViolations(constraintViolations, 1);
- assertCorrectConstraintViolationMessage( constraintViolations.iterator().next(), "Car type has to be betweeb 2 and 20 characters.");
+ assertCorrectNumberOfViolations( constraintViolations, 1 );
+ assertCorrectConstraintViolationMessages(
+ constraintViolations, "Car type has to be betweeb 2 and 20 characters."
+ );
}
@Test
Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/GroupTest.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/GroupTest.java 2009-06-26 08:29:41 UTC (rev 16955)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/groups/GroupTest.java 2009-06-26 12:18:52 UTC (rev 16956)
@@ -38,6 +38,8 @@
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;
@@ -189,9 +191,9 @@
book.setSubtitle( "Revised Edition of Hibernate in Action" );
constraintViolations = validator.validate( book, First.class, Second.class, Last.class );
- assertEquals( constraintViolations.size(), 1, "Wrong number of constraints" );
+ assertCorrectNumberOfViolations( constraintViolations, 1 );
+ assertCorrectConstraintViolationMessages( constraintViolations, "size must be between 0 and 30" );
constraintViolation = constraintViolations.iterator().next();
- assertEquals( constraintViolation.getMessage(), "size must be between 0 and 30", "Wrong message" );
assertEquals( constraintViolation.getRootBean(), book, "Wrong root entity" );
assertEquals( constraintViolation.getInvalidValue(), book.getSubtitle(), "Wrong value" );
assertCorrectPropertyPaths( constraintViolations, "subtitle" );
@@ -230,9 +232,9 @@
author.setLastName( "King" );
constraintViolations = validator.validate( book, Book.All.class );
+ assertCorrectNumberOfViolations( constraintViolations, 1 );
+ assertCorrectConstraintViolationMessages( constraintViolations, "may not be null" );
ConstraintViolation constraintViolation = constraintViolations.iterator().next();
- assertEquals( constraintViolations.size(), 1, "Wrong number of constraints" );
- assertEquals( constraintViolation.getMessage(), "may not be null", "Wrong message" );
assertEquals( constraintViolation.getRootBean(), book, "Wrong root entity" );
assertEquals( constraintViolation.getInvalidValue(), book.getTitle(), "Wrong value" );
assertCorrectPropertyPaths( constraintViolations, "title" );
Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatePropertyTest.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatePropertyTest.java 2009-06-26 08:29:41 UTC (rev 16955)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatePropertyTest.java 2009-06-26 12:18:52 UTC (rev 16956)
@@ -35,7 +35,7 @@
import org.hibernate.jsr303.tck.util.TestUtil;
import static org.hibernate.jsr303.tck.util.TestUtil.assertConstraintViolation;
import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintType;
-import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintViolationMessage;
+import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintViolationMessages;
import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectNumberOfViolations;
/**
@@ -44,7 +44,7 @@
* @author Hardy Ferentschik
*/
@Artifact(artifactType = ArtifactType.JSR303)
-(a)Classes({TestUtil.class, TestUtil.PathImpl.class, TestUtil.NodeImpl.class})
+@Classes({ TestUtil.class, TestUtil.PathImpl.class, TestUtil.NodeImpl.class })
public class ValidatePropertyTest extends AbstractTest {
//TODO Needs verification
@@ -64,7 +64,6 @@
}
-
@Test
@SpecAssertion(section = "4.1.1", id = "e")
public void testIllegalArgumentExceptionIsThrownForNullValue() {
@@ -140,7 +139,9 @@
ConstraintViolation<Address> violation = constraintViolations.iterator().next();
assertCorrectConstraintType( violation, Size.class );
assertConstraintViolation( violation, Address.class, townInNorthWales, "city" );
- assertCorrectConstraintViolationMessage( violation, "City name cannot be longer than 30 characters." );
+ assertCorrectConstraintViolationMessages(
+ constraintViolations, "City name cannot be longer than 30 characters."
+ );
address.setCity( "London" );
constraintViolations = validator.validateProperty( address, "city" );
@@ -175,6 +176,6 @@
ConstraintViolation<ActorListBased> violation = constraintViolations.iterator().next();
assertCorrectConstraintType( violation, NotNull.class );
assertConstraintViolation( violation, ActorListBased.class, null, property );
- assertCorrectConstraintViolationMessage( violation, "Everyone has a last name." );
+ assertCorrectConstraintViolationMessages( constraintViolations, "Everyone has a last name." );
}
}
\ No newline at end of file
Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateTest.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateTest.java 2009-06-26 08:29:41 UTC (rev 16955)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateTest.java 2009-06-26 12:18:52 UTC (rev 16956)
@@ -55,6 +55,7 @@
@Test
@SpecAssertions({
@SpecAssertion(section = "3.1", id = "a"),
+ @SpecAssertion(section = "3.1.2", id = "f"),
@SpecAssertion(section = "5.1", id = "a")
})
public void testValidatedPropertyDoesNotFollowJavaBeansConvention() {
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-06-26 08:29:41 UTC (rev 16955)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/ConstraintValidatorContextTest.java 2009-06-26 12:18:52 UTC (rev 16956)
@@ -33,6 +33,8 @@
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;
@@ -53,9 +55,8 @@
DummyBean bean = new DummyBean( "foobar" );
Set<ConstraintViolation<DummyBean>> constraintViolations = validator.validate( bean );
- assertEquals( constraintViolations.size(), 1, "Wrong number of constraints" );
- ConstraintViolation constraintViolation = constraintViolations.iterator().next();
- assertEquals( "dummy message", constraintViolation.getMessage(), "Wrong message" );
+ assertCorrectNumberOfViolations( constraintViolations, 1 );
+ assertCorrectConstraintViolationMessages( constraintViolations, "dummy message" );
}
/**
@@ -87,9 +88,8 @@
DummyBean bean = new DummyBean( "foobar" );
Set<ConstraintViolation<DummyBean>> constraintViolations = validator.validate( bean );
- assertEquals( constraintViolations.size(), 1, "Wrong number of constraints" );
- ConstraintViolation constraintViolation = constraintViolations.iterator().next();
- assertEquals( constraintViolation.getMessage(), "message1", "Wrong message" );
+ assertCorrectNumberOfViolations( constraintViolations, 1 );
+ assertCorrectConstraintViolationMessages( constraintViolations, "message1" );
assertCorrectPropertyPaths( constraintViolations, "property1" );
}
Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/xmlconfiguration/XmlConfigurationTest.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/xmlconfiguration/XmlConfigurationTest.java 2009-06-26 08:29:41 UTC (rev 16955)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/xmlconfiguration/XmlConfigurationTest.java 2009-06-26 12:18:52 UTC (rev 16956)
@@ -31,20 +31,21 @@
import org.testng.annotations.Test;
import org.hibernate.jsr303.tck.util.TestUtil;
-import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintViolationMessage;
+import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintViolationMessages;
import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectNumberOfViolations;
/**
* @author Hardy Ferentschik
*/
@Artifact(artifactType = ArtifactType.JSR303)
-(a)Classes({TestUtil.class, TestUtil.PathImpl.class, TestUtil.NodeImpl.class})
+@Classes({ TestUtil.class, TestUtil.PathImpl.class, TestUtil.NodeImpl.class })
@ValidationXml(value = "validation.xml")
@Resources(
{
@Resource(source = "user-constraints.xml",
destination = "WEB-INF/classes/org/hibernate/jsr303/tck/tests/xmlconfiguration/user-constraints.xml"),
- @Resource(source = "order-constraints.xml", destination = "WEB-INF/classes/org/hibernate/jsr303/tck/tests/xmlconfiguration/order-constraints.xml")
+ @Resource(source = "order-constraints.xml",
+ destination = "WEB-INF/classes/org/hibernate/jsr303/tck/tests/xmlconfiguration/order-constraints.xml")
}
)
public class XmlConfigurationTest extends AbstractTest {
@@ -56,8 +57,8 @@
User user = new User();
Set<ConstraintViolation<User>> constraintViolations = validator.validate( user );
assertCorrectNumberOfViolations( constraintViolations, 1 );
- assertCorrectConstraintViolationMessage(
- constraintViolations.iterator().next(), "Message from xml"
+ assertCorrectConstraintViolationMessages(
+ constraintViolations, "Message from xml"
);
user.setConsistent( true );
@@ -75,7 +76,7 @@
Set<ConstraintViolation<User>> constraintViolations = validator.validate( user );
assertCorrectNumberOfViolations( constraintViolations, 1 );
- assertCorrectConstraintViolationMessage( constraintViolations.iterator().next(), "Size is limited!" );
+ assertCorrectConstraintViolationMessages( constraintViolations, "Size is limited!" );
user.setFirstname( "Wolfgang" );
constraintViolations = validator.validate( user );
@@ -93,8 +94,8 @@
Set<ConstraintViolation<User>> constraintViolations = validator.validate( user );
assertCorrectNumberOfViolations( constraintViolations, 1 );
- assertCorrectConstraintViolationMessage(
- constraintViolations.iterator().next(), "Last name has to start with with a capital letter."
+ assertCorrectConstraintViolationMessages(
+ constraintViolations, "Last name has to start with with a capital letter."
);
user.setLastname( "Doe" );
@@ -112,9 +113,8 @@
Set<ConstraintViolation<User>> constraintViolations = validator.validate( user );
assertCorrectNumberOfViolations( constraintViolations, 1 );
- assertCorrectConstraintViolationMessage(
- constraintViolations.iterator().next(),
- "A phone number can only contain numbers, whitespaces and dashes."
+ assertCorrectConstraintViolationMessages(
+ constraintViolations, "A phone number can only contain numbers, whitespaces and dashes."
);
user.setPhoneNumber( "112" );
@@ -134,10 +134,7 @@
Set<ConstraintViolation<User>> constraintViolations = validator.validate( user );
assertCorrectNumberOfViolations( constraintViolations, 1 );
- assertCorrectConstraintViolationMessage(
- constraintViolations.iterator().next(),
- "Not a credit casrd number."
- );
+ assertCorrectConstraintViolationMessages( constraintViolations, "Not a credit casrd number." );
card.setNumber( "1234567890" );
constraintViolations = validator.validate( user );
Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/util/TestUtil.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/util/TestUtil.java 2009-06-26 08:29:41 UTC (rev 16955)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/util/TestUtil.java 2009-06-26 12:18:52 UTC (rev 16956)
@@ -57,17 +57,27 @@
);
}
- public static <T> void assertCorrectConstraintViolationMessage(ConstraintViolation<T> violation, String message) {
- assertEquals(
- violation.getMessage(), message, "Wrong error message"
- );
+ public static <T> void assertCorrectConstraintViolationMessages(Set<ConstraintViolation<T>> violations, String... messages) {
+ List<String> actualMessages = new ArrayList<String>();
+ for ( ConstraintViolation<?> violation : violations ) {
+ actualMessages.add( violation.getMessage() );
+ }
+
+ assertTrue( actualMessages.size() == messages.length, "Wrong number or error messages" );
+
+ for ( String expectedMessage : messages ) {
+ assertTrue(
+ actualMessages.contains( expectedMessage ),
+ "The message " + expectedMessage + " should have been in the st of error messages"
+ );
+ }
}
public static <T> void assertCorrectNumberOfViolations(Set<ConstraintViolation<T>> violations, int expectedViolations) {
assertEquals( violations.size(), expectedViolations, "Wrong number of constraint violations" );
}
- public static <T> void assertCorrectConstraintTypes(Set<ConstraintViolation<T>> violations, Class<?>[] expectedConsraintTypes) {
+ public static <T> void assertCorrectConstraintTypes(Set<ConstraintViolation<T>> violations, Class<?>... expectedConsraintTypes) {
List<String> constraintTypes = new ArrayList<String>();
for ( ConstraintViolation<?> violation : violations ) {
constraintTypes.add(
@@ -75,7 +85,7 @@
);
}
- assertEquals( expectedConsraintTypes.length, constraintTypes.size(), "Wring number of constraint types." );
+ assertEquals( expectedConsraintTypes.length, constraintTypes.size(), "Wrong number of constraint types." );
for ( Class<?> expectedConstraintType : expectedConsraintTypes ) {
assertTrue(
15 years, 6 months
Hibernate SVN: r16955 - core/trunk/annotations/src/test/java/org/hibernate/test/annotations/beanvalidation.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-06-26 04:29:41 -0400 (Fri, 26 Jun 2009)
New Revision: 16955
Modified:
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/beanvalidation/HibernateTraversableResolverTest.java
Log:
fixed the tests after the latest bean validation updates
Modified: core/trunk/annotations/src/test/java/org/hibernate/test/annotations/beanvalidation/HibernateTraversableResolverTest.java
===================================================================
--- core/trunk/annotations/src/test/java/org/hibernate/test/annotations/beanvalidation/HibernateTraversableResolverTest.java 2009-06-26 08:23:23 UTC (rev 16954)
+++ core/trunk/annotations/src/test/java/org/hibernate/test/annotations/beanvalidation/HibernateTraversableResolverTest.java 2009-06-26 08:29:41 UTC (rev 16955)
@@ -52,7 +52,8 @@
assertEquals( 1, e.getConstraintViolations().size() );
ConstraintViolation<?> cv = e.getConstraintViolations().iterator().next();
assertEquals( Screen.class, cv.getRootBeanClass() );
- assertEquals( "stopButton.name", cv.getPropertyPath() );
+ // toString works since hibernate validator's Path implementation works accordingly. Should do a Path comparison though
+ assertEquals( "stopButton.name", cv.getPropertyPath().toString() );
}
tx.rollback();
@@ -131,7 +132,8 @@
assertEquals( 1, e.getConstraintViolations().size() );
final ConstraintViolation constraintViolation = e.getConstraintViolations().iterator().next();
assertEquals( Screen.class, constraintViolation.getRootBeanClass() );
- assertEquals( "connectors[].number", constraintViolation.getPropertyPath() );
+ // toString works since hibernate validator's Path implementation works accordingly. Should do a Path comparison though
+ assertEquals( "connectors[].number", constraintViolation.getPropertyPath().toString() );
}
tx.rollback();
15 years, 6 months