/java/

Custom PlatformResourceBundleLocator

2014-01-11 16:58:35

CarDto

package pl.btbw.dto;

import javax.validation.constraints.NotNull;

public class CarDto {

    @NotNull(message = "{error.message}")
    private String manufacturer;

    public CarDto(String manufacturer) {
        this.manufacturer = manufacturer;
    }
}

CarDtoTest

package pl.btbw.dto;

import org.hibernate.validator.HibernateValidator;
import org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator;
import org.junit.Test;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Locale;
import java.util.Set;

import static org.junit.Assert.assertEquals;

public class CarDtoTest {

    @Test
    public void shouldReturn_Nl_Error() {
        CarDto car = new CarDto(null);
        Validator validator = getValidator(new Locale("nl", "BE"));
        Set

<constraintviolation<cardto>&gt; constraintViolations = validator.validate(car);
        assertEquals("NL NL NL NL", constraintViolations.iterator().next().getMessage());
    }

    @Test
    public void shouldReturn_FR_Error() {
        CarDto car = new CarDto(null);
        Validator validator = getValidator(new Locale("fr", "BE"));
        Set<constraintviolation<cardto>&gt; constraintViolations = validator.validate(car);
        assertEquals("FR FR FR FR", constraintViolations.iterator().next().getMessage());
    }

    @Test
    public void shouldReturn_EN_Error() {
        CarDto car = new CarDto(null);
        Validator validator = getValidator(new Locale("en", "US"));
        Set<constraintviolation<cardto>&gt; constraintViolations = validator.validate(car);
        assertEquals("EN EN EN EN", constraintViolations.iterator().next().getMessage());
    }

    private Validator getValidator(Locale mainLocale) {
        return Validation
                .byProvider(HibernateValidator.class)
                .configure()
                .messageInterpolator(
                        new ResourceBundleMessageInterpolator(
                            new MyPlatformResourceBundleLocator(mainLocale)
                        )
                )
                .buildValidatorFactory()
                .getValidator();
    }

}

MyPlatformResourceBundleLocator

package pl.btbw.dto;

import java.util.Locale;
import java.util.ResourceBundle;

public class MyPlatformResourceBundleLocator implements org.hibernate.validator.spi.resourceloading.ResourceBundleLocator {

    private Locale mainLocale;

    public MyPlatformResourceBundleLocator(Locale mainLocale){
        this.mainLocale = mainLocale;
    }

    @Override
    public ResourceBundle getResourceBundle(Locale locale) {
        return ResourceBundle.getBundle("ValidationMessages", mainLocale);
    }
}

ValidationMessages.properties error.message = Def Def Def

ValidationMessagesenUS.properties error.message = EN EN EN EN

ValidationMessagesfrBE.properties error.message = FR FR FR FR

ValidationMessagesnlBE.properties error.message = NL NL NL NL

pom.xml

<dependencies>
<dependency>
<groupid>org.hibernate</groupid>
<artifactid>hibernate-validator</artifactid>
<version>5.0.2.Final</version>
</dependency>
<dependency>
<groupid>junit</groupid>
<artifactid>junit-dep</artifactid>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupid>org.mockito</groupid>
<artifactid>mockito-core</artifactid>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupid>org.easytesting</groupid>
<artifactid>fest-assert</artifactid>
<version>1.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupid>org.hamcrest</groupid>
<artifactid>hamcrest-core</artifactid>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupid>org.hamcrest</groupid>
<artifactid>hamcrest-library</artifactid>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>