-
Notifications
You must be signed in to change notification settings - Fork 571
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HV-1772 Allow overriding MessageInterpolator when config is reused
- Loading branch information
Showing
2 changed files
with
68 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...hibernate/validator/test/internal/bootstrap/ConfigurationReuseHibernateValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |