/impl/src/test/java/org/jboss/seam/validation/di/InjectingConstraintValidatorFactoryTest.java

https://github.com/jharting/seam-validation · Java · 128 lines · 58 code · 16 blank · 54 comment · 0 complexity · 4f5880978804ee0adf38340b6bc1179f MD5 · raw file

  1. /**
  2. * JBoss, Home of Professional Open Source
  3. *
  4. * Copyright 2011, Red Hat, Inc., and individual contributors
  5. * by the @authors tag. See the copyright.txt in the distribution for a
  6. * full listing of individual contributors.
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. package org.jboss.seam.validation.di;
  21. import static org.junit.Assert.assertEquals;
  22. import java.io.File;
  23. import java.util.Set;
  24. import javax.enterprise.inject.spi.BeanManager;
  25. import javax.inject.Inject;
  26. import javax.validation.ConstraintViolation;
  27. import javax.validation.Validation;
  28. import javax.validation.Validator;
  29. import org.jboss.arquillian.container.test.api.Deployment;
  30. import org.jboss.arquillian.junit.Arquillian;
  31. import org.jboss.solder.beanManager.BeanManagerLocator;
  32. import org.jboss.seam.validation.InjectingConstraintValidatorFactory;
  33. import org.jboss.seam.validation.di.constraint.ValidHello;
  34. import org.jboss.seam.validation.di.service.HelloWorldService;
  35. import org.jboss.seam.validation.testutil.StaticBeanManagerProvider;
  36. import org.jboss.shrinkwrap.api.ArchivePaths;
  37. import org.jboss.shrinkwrap.api.ShrinkWrap;
  38. import org.jboss.shrinkwrap.api.asset.EmptyAsset;
  39. import org.jboss.shrinkwrap.api.spec.JavaArchive;
  40. import org.junit.Test;
  41. import org.junit.runner.RunWith;
  42. /**
  43. * Test for {@link InjectingConstraintValidatorFactory}.
  44. *
  45. * @author Gunnar Morling
  46. *
  47. */
  48. @RunWith(Arquillian.class)
  49. public class InjectingConstraintValidatorFactoryTest {
  50. @Inject
  51. private Validator validator;
  52. @Inject
  53. private BeanManager beanManager;
  54. @Deployment
  55. public static JavaArchive createTestArchive() throws Exception {
  56. return ShrinkWrap
  57. .create(JavaArchive.class, "test.jar")
  58. .addAsManifestResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"))
  59. .addAsManifestResource(new File("src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension"))
  60. .addAsManifestResource(
  61. new File("src/test/resources/META-INF/services/org.jboss.solder.beanManager.BeanManagerProvider"))
  62. .addPackage(InjectingConstraintValidatorFactoryTest.class.getPackage())
  63. .addPackage(HelloWorldService.class.getPackage()).addPackage(ValidHello.class.getPackage());
  64. }
  65. @Test
  66. public void constraintValidatorWithInjectedDependency() {
  67. Set<ConstraintViolation<Model>> violations = validator.validate(new Model());
  68. assertEquals(2, violations.size());
  69. }
  70. /**
  71. * <p>
  72. * The {@link Validator} is not retrieved via CDI, but using the plain Bean Validation API. Nevertheless
  73. * {@link InjectingConstraintValidatorFactory} is configured in <code>validation.xml</code>.
  74. * </p>
  75. * <p>
  76. * In this case the {@link BeanManager} used in <code>InjectingConstraintValidatorFactory</code> is obtained via
  77. * {@link BeanManagerLocator}.
  78. * </p>
  79. * <p>
  80. * An example for this scenario would be a validation triggered by JSF which retrieves the validator in that way.
  81. * </p>
  82. *
  83. * @see SEAMVALIDATE-14
  84. */
  85. @Test
  86. public void unmanagedValidator() {
  87. // set the BM to be returned by BeanManagerLocator (normally the BM would be retrieved from JNDI)
  88. StaticBeanManagerProvider.setBeanManager(beanManager);
  89. Validator unmanagedValidator = Validation.buildDefaultValidatorFactory().getValidator();
  90. Set<ConstraintViolation<Model>> violations = unmanagedValidator.validate(new Model());
  91. assertEquals(2, violations.size());
  92. }
  93. /**
  94. * <p>
  95. * The {@link Validator} is not retrieved via CDI, but using the plain Bean Validation API. Nevertheless
  96. * {@link InjectingConstraintValidatorFactory} is configured in <code>validation.xml</code>.
  97. * </p>
  98. * <p>
  99. * In this case the {@link BeanManager} used in <code>InjectingConstraintValidatorFactory</code> is obtained via
  100. * {@link BeanManagerLocator}, but no <code>BeanManager</code> can be found there. An exception is expected in this case as
  101. * <code>InjectingConstraintValidatorFactory</code> can't be used without a bean manager.
  102. * </p>
  103. *
  104. * @see SEAMVALIDATE-14
  105. */
  106. @Test(expected = IllegalStateException.class)
  107. public void unmanagedValidatorWithoutBeanManager() {
  108. StaticBeanManagerProvider.setBeanManager(null);
  109. Validator unmanagedValidator = Validation.buildDefaultValidatorFactory().getValidator();
  110. unmanagedValidator.validate(new Model());
  111. }
  112. }