|
| 1 | +package org.javaee7.validation.methods; |
| 2 | + |
| 3 | +import static org.hamcrest.CoreMatchers.*; |
| 4 | +import static org.junit.Assert.*; |
| 5 | + |
| 6 | +import java.lang.reflect.Constructor; |
| 7 | +import java.util.Set; |
| 8 | + |
| 9 | +import javax.inject.Inject; |
| 10 | +import javax.validation.ConstraintViolation; |
| 11 | +import javax.validation.Validator; |
| 12 | +import javax.validation.executable.ExecutableValidator; |
| 13 | + |
| 14 | +import org.jboss.arquillian.container.test.api.Deployment; |
| 15 | +import org.jboss.arquillian.junit.Arquillian; |
| 16 | +import org.jboss.shrinkwrap.api.Archive; |
| 17 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 18 | +import org.jboss.shrinkwrap.api.asset.EmptyAsset; |
| 19 | +import org.jboss.shrinkwrap.api.spec.JavaArchive; |
| 20 | +import org.junit.Rule; |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.rules.ExpectedException; |
| 23 | +import org.junit.runner.RunWith; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author Jakub Marchwicki |
| 27 | + */ |
| 28 | +@RunWith(Arquillian.class) |
| 29 | +public class ConstructorParametersConstraintsTest { |
| 30 | + |
| 31 | + @Inject |
| 32 | + Validator validator; |
| 33 | + |
| 34 | + @Rule |
| 35 | + public ExpectedException thrown = ExpectedException.none(); |
| 36 | + |
| 37 | + @Deployment |
| 38 | + public static Archive<?> deployment() { |
| 39 | + return ShrinkWrap.create(JavaArchive.class).addClasses(MyBean2.class) |
| 40 | + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void constructorViolationsWhenNullParameters() throws NoSuchMethodException, SecurityException { |
| 45 | + ExecutableValidator methodValidator = validator.forExecutables(); |
| 46 | + Constructor<MyBean2> constructor = MyBean2.class |
| 47 | + .getConstructor(String.class); |
| 48 | + |
| 49 | + Set<ConstraintViolation<MyBean2>> constraints = methodValidator |
| 50 | + .validateConstructorParameters(constructor, new Object[] {null}); |
| 51 | + |
| 52 | + ConstraintViolation<MyBean2> violarion = constraints.iterator().next(); |
| 53 | + assertThat(constraints.size(), equalTo(1)); |
| 54 | + assertThat(violarion.getMessageTemplate(), equalTo("{javax.validation.constraints.NotNull.message}")); |
| 55 | + assertThat(violarion.getPropertyPath().toString(), equalTo("MyBean2.arg0")); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void constructorViolationsWhenNotNullParameters() throws NoSuchMethodException, SecurityException { |
| 60 | + ExecutableValidator methodValidator = validator.forExecutables(); |
| 61 | + Constructor<MyBean2> constructor = MyBean2.class |
| 62 | + .getConstructor(String.class); |
| 63 | + |
| 64 | + Set<ConstraintViolation<MyBean2>> constraints = methodValidator |
| 65 | + .validateConstructorParameters(constructor, new Object[] {"foo"}); |
| 66 | + |
| 67 | + assertThat(constraints.isEmpty(), equalTo(true)); |
| 68 | + } |
| 69 | + |
| 70 | +} |
0 commit comments