[hibernate-commits] Hibernate SVN: r16962 - in beanvalidation/trunk/validation-tck/src/main: java/org/hibernate/jsr303/tck/tests/validation/messageinterpolation and 5 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Mon Jun 29 10:26:12 EDT 2009


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
+ */
+ at Artifact(artifactType = ArtifactType.JSR303)
+ at Classes({ TestUtil.class, TestUtil.PathImpl.class, TestUtil.NodeImpl.class })
+ at Resources({
+		@Resource(source = "ValidationMessages.properties",
+				destination = "WEB-INF/classes/ValidationMessages.properties"),
+		@Resource(source = "ValidationMessages_de.properties",
+				destination = "WEB-INF/classes/ValidationMessages_de.properties")
+})
+ at 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,




More information about the hibernate-commits mailing list