Skip to content

Commit

Permalink
HV-1772 Allow overriding MessageInterpolator when config is reused
Browse files Browse the repository at this point in the history
  • Loading branch information
gsmet committed Apr 30, 2020
1 parent f99e866 commit 6702a5e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ public HibernateValidatorConfiguration externalClassLoader(ClassLoader externalC
Contracts.assertNotNull( externalClassLoader, MESSAGES.parameterMustNotBeNull( "externalClassLoader" ) );
this.externalClassLoader = externalClassLoader;

// we need to reset the messageInterpolator field as it might vary depending on the class loader
this.messageInterpolator = null;

return this;
}

Expand Down Expand Up @@ -406,15 +409,13 @@ public final boolean isIgnoreXmlConfiguration() {

@Override
public final MessageInterpolator getMessageInterpolator() {
// apply explicitly given MI, otherwise use default one
MessageInterpolator selectedInterpolator = validationBootstrapParameters.getMessageInterpolator();
if ( selectedInterpolator != null ) {
return selectedInterpolator;
}
if ( messageInterpolator == null ) {
// apply explicitly given MI, otherwise use default one
MessageInterpolator interpolator = validationBootstrapParameters.getMessageInterpolator();
if ( interpolator != null ) {
messageInterpolator = interpolator;
}
else {
messageInterpolator = getDefaultMessageInterpolatorConfiguredWithClassLoader();
}
messageInterpolator = getDefaultMessageInterpolatorConfiguredWithClassLoader();
}

return messageInterpolator;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.test.internal.bootstrap;

import static org.testng.Assert.assertSame;

import java.util.Locale;

import javax.validation.Configuration;
import javax.validation.MessageInterpolator;
import javax.validation.Validation;
import javax.validation.ValidatorFactory;

import org.testng.annotations.Test;

/**
* @author Steven Walters
* @author Guillaume Smet
*/
public class ConfigurationReuseHibernateValidatorTest {

public static class MessageInterpolatorImpl implements MessageInterpolator {

private final String prefix;

public MessageInterpolatorImpl(String prefix) {
this.prefix = prefix;
}

@Override
public String interpolate(String messageTemplate, Context context) {
return prefix + ": " + messageTemplate;
}

@Override
public String interpolate(String messageTemplate, Context context, Locale locale) {
return prefix + ": " + messageTemplate + locale.toLanguageTag();
}

public String toString() {
return getClass().getSimpleName() + prefix;
}
}

@Test
public void testMessageInterpolatorChange() {
Configuration<?> config = Validation.byDefaultProvider().configure();
MessageInterpolator interpolator1 = new MessageInterpolatorImpl( "One" );
MessageInterpolator interpolator2 = new MessageInterpolatorImpl( "Two" );
ValidatorFactory factory1 = config.messageInterpolator( interpolator1 ).buildValidatorFactory();
ValidatorFactory factory2 = config.messageInterpolator( interpolator2 ).buildValidatorFactory();
assertSame( factory1.getMessageInterpolator(), interpolator1 );
assertSame( factory2.getMessageInterpolator(), interpolator2 );
}
}

0 comments on commit 6702a5e

Please sign in to comment.