[hibernate-commits] Hibernate SVN: r19650 - in validator/trunk/hibernate-validator/src: test/java/org/hibernate/validator/test/engine/messageinterpolation and 1 other directory.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Wed Jun 2 05:30:56 EDT 2010


Author: hardy.ferentschik
Date: 2010-06-02 05:30:55 -0400 (Wed, 02 Jun 2010)
New Revision: 19650

Modified:
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/messageinterpolation/ResourceBundleMessageInterpolator.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/engine/messageinterpolation/ResourceBundleMessageInterpolatorTest.java
Log:
HV-330

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/messageinterpolation/ResourceBundleMessageInterpolator.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/messageinterpolation/ResourceBundleMessageInterpolator.java	2010-06-02 05:15:54 UTC (rev 19649)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/messageinterpolation/ResourceBundleMessageInterpolator.java	2010-06-02 09:30:55 UTC (rev 19650)
@@ -74,6 +74,11 @@
 	 */
 	private final Map<LocalisedMessage, String> resolvedMessages = new WeakHashMap<LocalisedMessage, String>();
 
+	/**
+	 * Flag indicating whether this interpolator should chance some of the interpolation steps.
+	 */
+	private final boolean cacheMessages;
+
 	public ResourceBundleMessageInterpolator() {
 		this( ( ResourceBundleLocator ) null );
 	}
@@ -95,7 +100,11 @@
 	}
 
 	public ResourceBundleMessageInterpolator(ResourceBundleLocator userResourceBundleLocator) {
+		this( userResourceBundleLocator, true );
+	}
 
+	public ResourceBundleMessageInterpolator(ResourceBundleLocator userResourceBundleLocator, boolean cacheMessages) {
+
 		defaultLocale = Locale.getDefault();
 
 		if ( userResourceBundleLocator == null ) {
@@ -113,6 +122,8 @@
 				new CachingResourceBundleLocator(
 						new PlatformResourceBundleLocator( DEFAULT_VALIDATION_MESSAGES )
 				);
+
+		this.cacheMessages = cacheMessages;
 	}
 
 	public String interpolate(String message, Context context) {
@@ -134,14 +145,18 @@
 	 *
 	 * @param message the message to interpolate
 	 * @param annotationParameters the parameters of the annotation for which to interpolate this message
-	 * @param locale the <code>Locale</code> to use for the resource bundle.
+	 * @param locale the {@code Locale} to use for the resource bundle.
 	 *
 	 * @return the interpolated message.
 	 */
 	private String interpolateMessage(String message, Map<String, Object> annotationParameters, Locale locale) {
 		LocalisedMessage localisedMessage = new LocalisedMessage( message, locale );
-		String resolvedMessage = resolvedMessages.get( localisedMessage );
+		String resolvedMessage = null;
 
+		if ( cacheMessages ) {
+			resolvedMessage = resolvedMessages.get( localisedMessage );
+		}
+
 		// if the message is not already in the cache we have to run step 1-3 of the message resolution 
 		if ( resolvedMessage == null ) {
 			ResourceBundle userResourceBundle = userResourceBundleLocator
@@ -168,7 +183,9 @@
 				// search the default bundle non recursive (step2)
 				resolvedMessage = replaceVariables( userBundleResolvedMessage, defaultResourceBundle, locale, false );
 				evaluatedDefaultBundleOnce = true;
-				resolvedMessages.put( localisedMessage, resolvedMessage );
+				if ( cacheMessages ) {
+					resolvedMessages.put( localisedMessage, resolvedMessage );
+				}
 			} while ( true );
 		}
 

Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/engine/messageinterpolation/ResourceBundleMessageInterpolatorTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/engine/messageinterpolation/ResourceBundleMessageInterpolatorTest.java	2010-06-02 05:15:54 UTC (rev 19649)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/engine/messageinterpolation/ResourceBundleMessageInterpolatorTest.java	2010-06-02 09:30:55 UTC (rev 19650)
@@ -34,10 +34,10 @@
 
 import org.hibernate.validator.engine.MessageInterpolatorContext;
 import org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator;
+import org.hibernate.validator.metadata.ConstraintDescriptorImpl;
 import org.hibernate.validator.metadata.ConstraintHelper;
+import org.hibernate.validator.metadata.ConstraintOrigin;
 import org.hibernate.validator.resourceloading.ResourceBundleLocator;
-import org.hibernate.validator.metadata.ConstraintDescriptorImpl;
-import org.hibernate.validator.metadata.ConstraintOrigin;
 import org.hibernate.validator.util.annotationfactory.AnnotationDescriptor;
 import org.hibernate.validator.util.annotationfactory.AnnotationFactory;
 
@@ -264,12 +264,67 @@
 	}
 
 	/**
+	 * HV-330
+	 */
+	@Test
+	public void testMessageCaching() {
+
+		// do the whole tests first with caching enabled
+		TestResourceBundle testBundle = new TestResourceBundle();
+		interpolator = new ResourceBundleMessageInterpolator(
+				new TestResourceBundleLocator( testBundle )
+		);
+		MessageInterpolator.Context context = new MessageInterpolatorContext( notNullDescriptor, null );
+
+		String expected = "{hv-330}";
+		String actual = interpolator.interpolate( "{hv-330}", context );
+		assertEquals( actual, expected, "The key should not not exist in the bundle." );
+
+		testBundle.addOrUpdateMessage( "hv-330", "success" );
+		expected = "{hv-330}";
+		actual = interpolator.interpolate( "{hv-330}", context );
+		assertEquals(
+				actual,
+				expected,
+				"The message has not changed since per default the ResourceBundleMessageInterpolator caches the messages"
+		);
+
+		// now without caching
+		testBundle = new TestResourceBundle();
+		interpolator = new ResourceBundleMessageInterpolator(
+				new TestResourceBundleLocator( testBundle ), false
+		);
+		context = new MessageInterpolatorContext( notNullDescriptor, null );
+
+		expected = "{hv-330}";
+		actual = interpolator.interpolate( "{hv-330}", context );
+		assertEquals( actual, expected, "The key should not not exist in the bundle." );
+
+		testBundle.addOrUpdateMessage( "hv-330", "success" );
+		expected = "success";
+		actual = interpolator.interpolate( "{hv-330}", context );
+		assertEquals(
+				actual,
+				expected,
+				"The message should change since ResourceBundleMessageInterpolator does not cache"
+		);
+	}
+
+	/**
 	 * A dummy locator always returning a {@link TestResourceBundle}.
 	 */
 	private static class TestResourceBundleLocator implements ResourceBundleLocator {
 
-		private TestResourceBundle resourceBundle = new TestResourceBundle();
+		private final ResourceBundle resourceBundle;
 
+		public TestResourceBundleLocator() {
+			this( new TestResourceBundle() );
+		}
+
+		public TestResourceBundleLocator(ResourceBundle bundle) {
+			resourceBundle = bundle;
+		}
+
 		public ResourceBundle getResourceBundle(Locale locale) {
 			return resourceBundle;
 		}
@@ -315,5 +370,10 @@
 				throw new NoSuchElementException();
 			}
 		}
+
+		public void addOrUpdateMessage(String key, String message) {
+			testResources.put( key, message );
+			iter = testResources.keySet().iterator();
+		}
 	}
 }



More information about the hibernate-commits mailing list