Author: hardy.ferentschik
Date: 2009-08-06 09:04:10 -0400 (Thu, 06 Aug 2009)
New Revision: 17242
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/messageinterpolation/MessageInterpolationTest.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ResourceBundleMessageInterpolator.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/messageinterpolation/ResourceBundleMessageInterpolatorTest.java
Log:
HV-186 - ResourceBundleMessageInterpolator has to allow all characters as message
parameter
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/messageinterpolation/MessageInterpolationTest.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/messageinterpolation/MessageInterpolationTest.java 2009-08-06
12:21:47 UTC (rev 17241)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/messageinterpolation/MessageInterpolationTest.java 2009-08-06
13:04:10 UTC (rev 17242)
@@ -1,4 +1,4 @@
-// $Id:$
+// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
@@ -93,8 +93,8 @@
actual = interpolator.interpolate( "This {foo} just fine", context );
assertEquals( actual, expected, "Wrong substitution" );
- expected = "{} { replacement worked }";
- actual = interpolator.interpolate( "{} { {foo} }", context );
+ expected = "{} replacement worked {unkown}";
+ actual = interpolator.interpolate( "{} {foo} {unkown}", context );
assertEquals( actual, expected, "Wrong substitution" );
}
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ResourceBundleMessageInterpolator.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ResourceBundleMessageInterpolator.java 2009-08-06
12:21:47 UTC (rev 17241)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ResourceBundleMessageInterpolator.java 2009-08-06
13:04:10 UTC (rev 17242)
@@ -44,7 +44,7 @@
/**
* Regular expression used to do message interpolation.
*/
- private static final Pattern messageParameterPattern = Pattern.compile(
"(\\{[\\w\\.]+\\})" );
+ private static final Pattern messageParameterPattern = Pattern.compile(
"(\\{[^\\}]+?\\})" );
/**
* The default locale for the current user.
@@ -83,18 +83,12 @@
defaultBundlesMap.put( defaultLocale, ResourceBundle.getBundle(
DEFAULT_VALIDATION_MESSAGES, defaultLocale ) );
}
- /**
- * {@inheritDoc}
- */
public String interpolate(String message, Context context) {
// probably no need for caching, but it could be done by parameters since the map
// is immutable and uniquely built per Validation definition, the comparaison has to be
based on == and not equals though
return interpolateMessage( message, context.getConstraintDescriptor().getAttributes(),
defaultLocale );
}
- /**
- * {@inheritDoc}
- */
public String interpolate(String message, Context context, Locale locale) {
return interpolateMessage( message, context.getConstraintDescriptor().getAttributes(),
locale );
}
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/messageinterpolation/ResourceBundleMessageInterpolatorTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/messageinterpolation/ResourceBundleMessageInterpolatorTest.java 2009-08-06
12:21:47 UTC (rev 17241)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/messageinterpolation/ResourceBundleMessageInterpolatorTest.java 2009-08-06
13:04:10 UTC (rev 17242)
@@ -33,15 +33,15 @@
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
+import org.hibernate.validation.engine.MessageInterpolatorContext;
+import org.hibernate.validation.engine.ResourceBundleMessageInterpolator;
import org.hibernate.validation.metadata.ConstraintDescriptorImpl;
import org.hibernate.validation.metadata.ConstraintHelper;
import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
import org.hibernate.validation.util.annotationfactory.AnnotationFactory;
-import org.hibernate.validation.engine.ResourceBundleMessageInterpolator;
-import org.hibernate.validation.engine.MessageInterpolatorContext;
/**
- * Tests for message resolution.
+ * Tests for message interpolation.
*
* @author Hardy Ferentschik
*/
@@ -73,20 +73,20 @@
public void testSuccessfulInterpolation() {
interpolator = new ResourceBundleMessageInterpolator( new TestResourceBundle() );
MessageInterpolator.Context context = new MessageInterpolatorContext(
notNullDescriptor, null );
- String expected = "replacement worked";
- String actual = interpolator.interpolate( "{foo}", context );
+ String expected = "message interpolation successfull";
+ String actual = interpolator.interpolate( "{simple.key}", context );
assertEquals( actual, expected, "Wrong substitution" );
- expected = "replacement worked replacement worked";
- actual = interpolator.interpolate( "{foo} {foo}", context );
+ expected = "message interpolation successfull message interpolation
successfull";
+ actual = interpolator.interpolate( "{simple.key} {simple.key}", context );
assertEquals( actual, expected, "Wrong substitution" );
- expected = "This replacement worked just fine";
- actual = interpolator.interpolate( "This {foo} just fine", context );
+ expected = "The message interpolation successfull completed";
+ actual = interpolator.interpolate( "The {simple.key} completed", context );
assertEquals( actual, expected, "Wrong substitution" );
- expected = "{} { replacement worked }";
- actual = interpolator.interpolate( "{} { {foo} }", context );
+ expected = "{{simple.key}}";
+ actual = interpolator.interpolate( "{{simple.key}}", context );
assertEquals( actual, expected, "Wrong substitution" );
}
@@ -134,6 +134,26 @@
}
@Test
+ public void testKeyWithDashes() {
+ interpolator = new ResourceBundleMessageInterpolator( new TestResourceBundle() );
+ MessageInterpolator.Context context = new MessageInterpolatorContext(
notNullDescriptor, null );
+
+ String expected = "message interpolation successfull"; // unkown token {}
+ String actual = interpolator.interpolate( "{key-with-dashes}", context );
+ assertEquals( actual, expected, "Wrong substitution" );
+ }
+
+ @Test
+ public void testKeyWithSpaces() {
+ interpolator = new ResourceBundleMessageInterpolator( new TestResourceBundle() );
+ MessageInterpolator.Context context = new MessageInterpolatorContext(
notNullDescriptor, null );
+
+ String expected = "message interpolation successfull"; // unkown token {}
+ String actual = interpolator.interpolate( "{key with spaces}", context );
+ assertEquals( actual, expected, "Wrong substitution" );
+ }
+
+ @Test
public void testDefaultInterpolation() {
interpolator = new ResourceBundleMessageInterpolator( new TestResourceBundle() );
MessageInterpolator.Context context = new MessageInterpolatorContext(
notNullDescriptor, null );
@@ -239,7 +259,9 @@
public TestResourceBundle() {
testResources = new HashMap<String, String>();
// add some test messages
- testResources.put( "foo", "replacement worked" );
+ testResources.put( "simple.key", "message interpolation
successfull" );
+ testResources.put( "key-with-dashes", "message interpolation
successfull" );
+ testResources.put( "key with spaces", "message interpolation
successfull" );
testResources.put( "replace.in.user.bundle1",
"{replace.in.user.bundle2}" );
testResources.put( "replace.in.user.bundle2",
"{replace.in.default.bundle1}" );