PageRenderTime 165ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/beanvalidation/hibernate/validator/MessageInterpolationValidationTestCase.java

https://bitbucket.org/cprenzberg/wildfly
Java | 75 lines | 51 code | 16 blank | 8 comment | 0 complexity | 554b5ad745b4b49ec2f291fedc0627eb MD5 | raw file
  1. package org.jboss.as.test.integration.beanvalidation.hibernate.validator;
  2. import static java.lang.annotation.ElementType.FIELD;
  3. import java.util.Set;
  4. import javax.validation.ConstraintViolation;
  5. import javax.validation.MessageInterpolator;
  6. import javax.validation.Validation;
  7. import javax.validation.Validator;
  8. import javax.validation.ValidatorFactory;
  9. import org.junit.Assert;
  10. import org.hibernate.validator.HibernateValidator;
  11. import org.hibernate.validator.HibernateValidatorConfiguration;
  12. import org.hibernate.validator.cfg.ConstraintMapping;
  13. import org.hibernate.validator.cfg.defs.EmailDef;
  14. import org.hibernate.validator.messageinterpolation.ValueFormatterMessageInterpolator;
  15. import org.jboss.arquillian.container.test.api.Deployment;
  16. import org.jboss.arquillian.junit.Arquillian;
  17. import org.jboss.shrinkwrap.api.Archive;
  18. import org.jboss.shrinkwrap.api.ShrinkWrap;
  19. import org.jboss.shrinkwrap.api.spec.WebArchive;
  20. import org.junit.Test;
  21. import org.junit.runner.RunWith;
  22. /**
  23. *
  24. * Tests that message interpolation works correctly for hibernate validator
  25. *
  26. *
  27. * @author Madhumita Sadhukhan
  28. */
  29. @RunWith(Arquillian.class)
  30. public class MessageInterpolationValidationTestCase {
  31. @Deployment
  32. public static Archive<?> deploy() {
  33. final WebArchive war = ShrinkWrap.create(WebArchive.class, "testmessageinterpolationvalidation.war");
  34. war.addPackage(MessageInterpolationValidationTestCase.class.getPackage());
  35. return war;
  36. }
  37. @Test
  38. public void testCustomMessageInterpolation() {
  39. HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure();
  40. Assert.assertNotNull(configuration);
  41. ConstraintMapping mapping = new ConstraintMapping();
  42. mapping.type(Employee.class).property("firstName", FIELD)
  43. .constraint(new EmailDef().message("Invalid Email!You have entered:--\\{${validatedValue}\\}"));
  44. final MessageInterpolator messageInterpolator = new ValueFormatterMessageInterpolator();
  45. configuration.messageInterpolator(messageInterpolator);
  46. configuration.addMapping(mapping);
  47. ValidatorFactory factory = configuration.buildValidatorFactory();
  48. Validator validator = factory.getValidator();
  49. Employee emp = new Employee();
  50. // create employee
  51. emp.setEmpId("M1234");
  52. emp.setFirstName("MADHUMITA");
  53. String email = "MADHUMITA";
  54. Set<ConstraintViolation<Employee>> constraintViolations = validator.validate(emp);
  55. Assert.assertEquals("Wrong number of constraints", constraintViolations.size(), 1);
  56. Assert.assertEquals("Invalid Email!You have entered:--{" + email + "}", constraintViolations.iterator().next()
  57. .getMessage());
  58. }
  59. }