|
| 1 | +/* |
| 2 | + * Hibernate Validator, declare and validate application constraints |
| 3 | + * |
| 4 | + * License: Apache License, Version 2.0 |
| 5 | + * See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. |
| 6 | + */ |
| 7 | +package org.hibernate.validator.integration.wildfly.xml; |
| 8 | + |
| 9 | +import static org.junit.Assert.assertEquals; |
| 10 | +import static org.junit.Assert.assertSame; |
| 11 | + |
| 12 | +import java.util.Set; |
| 13 | + |
| 14 | +import javax.inject.Inject; |
| 15 | +import javax.validation.ConstraintViolation; |
| 16 | +import javax.validation.Validator; |
| 17 | +import javax.validation.constraints.NotNull; |
| 18 | + |
| 19 | +import org.jboss.arquillian.container.test.api.Deployer; |
| 20 | +import org.jboss.arquillian.container.test.api.Deployment; |
| 21 | +import org.jboss.arquillian.container.test.api.RunAsClient; |
| 22 | +import org.jboss.arquillian.junit.Arquillian; |
| 23 | +import org.jboss.arquillian.junit.InSequence; |
| 24 | +import org.jboss.arquillian.test.api.ArquillianResource; |
| 25 | +import org.jboss.shrinkwrap.api.Archive; |
| 26 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 27 | +import org.jboss.shrinkwrap.api.asset.Asset; |
| 28 | +import org.jboss.shrinkwrap.api.asset.EmptyAsset; |
| 29 | +import org.jboss.shrinkwrap.api.asset.StringAsset; |
| 30 | +import org.jboss.shrinkwrap.api.spec.WebArchive; |
| 31 | +import org.jboss.shrinkwrap.descriptor.api.Descriptors; |
| 32 | +import org.jboss.shrinkwrap.descriptor.api.validationConfiguration11.ValidationConfigurationDescriptor; |
| 33 | +import org.jboss.shrinkwrap.descriptor.api.validationMapping11.ValidationMappingDescriptor; |
| 34 | +import org.jboss.shrinkwrap.resolver.api.maven.Maven; |
| 35 | +import org.junit.Test; |
| 36 | +import org.junit.runner.RunWith; |
| 37 | + |
| 38 | +/** |
| 39 | + * Test for https://hibernate.atlassian.net/browse/HV-1280. To reproduce the issue, the deployment must be done twice |
| 40 | + * (it will only show up during the 2nd deploy), which is why the test is managing the deployment itself via client-side |
| 41 | + * test methods. |
| 42 | + * |
| 43 | + * @author Gunnar Morling |
| 44 | + */ |
| 45 | +@RunWith(Arquillian.class) |
| 46 | +public class JaxpContainedInDeploymentIT { |
| 47 | + |
| 48 | + private static final String WAR_FILE_NAME = JaxpContainedInDeploymentIT.class.getSimpleName() + ".war"; |
| 49 | + |
| 50 | + @ArquillianResource |
| 51 | + private Deployer deployer; |
| 52 | + |
| 53 | + @Inject |
| 54 | + private Validator validator; |
| 55 | + |
| 56 | + @Deployment(name = "jaxpit", managed = false) |
| 57 | + public static Archive<?> createTestArchive() { |
| 58 | + return ShrinkWrap |
| 59 | + .create( WebArchive.class, WAR_FILE_NAME ) |
| 60 | + .addClass( Camera.class ) |
| 61 | + .addAsResource( validationXml(), "META-INF/validation.xml" ) |
| 62 | + .addAsResource( mappingXml(), "META-INF/my-mapping.xml" ) |
| 63 | + .addAsLibrary( Maven.resolver().resolve( "xerces:xercesImpl:2.9.1" ).withoutTransitivity().asSingleFile() ) |
| 64 | + .addAsResource( "log4j.properties" ) |
| 65 | + .addAsWebInfResource( EmptyAsset.INSTANCE, "beans.xml" ); |
| 66 | + } |
| 67 | + |
| 68 | + private static Asset validationXml() { |
| 69 | + String validationXml = Descriptors.create( ValidationConfigurationDescriptor.class ) |
| 70 | + .version( "1.1" ) |
| 71 | + .constraintMapping( "META-INF/my-mapping.xml" ) |
| 72 | + .exportAsString(); |
| 73 | + return new StringAsset( validationXml ); |
| 74 | + } |
| 75 | + |
| 76 | + private static Asset mappingXml() { |
| 77 | + String mappingXml = Descriptors.create( ValidationMappingDescriptor.class ) |
| 78 | + .version( "1.1" ) |
| 79 | + .createBean() |
| 80 | + .clazz( Camera.class.getName() ) |
| 81 | + .createField() |
| 82 | + .name( "brand" ) |
| 83 | + .createConstraint() |
| 84 | + .annotation( "javax.validation.constraints.NotNull" ) |
| 85 | + .up() |
| 86 | + .up() |
| 87 | + .up() |
| 88 | + .exportAsString(); |
| 89 | + return new StringAsset( mappingXml ); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + @RunAsClient |
| 94 | + @InSequence(0) |
| 95 | + public void deploy1() throws Exception { |
| 96 | + deployer.deploy( "jaxpit" ); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + @InSequence(1) |
| 101 | + public void test1() throws Exception { |
| 102 | + doTest(); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + @RunAsClient |
| 107 | + @InSequence(2) |
| 108 | + public void undeploy1() throws Exception { |
| 109 | + deployer.undeploy( "jaxpit" ); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + @RunAsClient |
| 114 | + @InSequence(3) |
| 115 | + public void deploy2() throws Exception { |
| 116 | + deployer.deploy( "jaxpit" ); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + @InSequence(4) |
| 121 | + public void test2() throws Exception { |
| 122 | + doTest(); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + @RunAsClient |
| 127 | + @InSequence(5) |
| 128 | + public void undeploy2() throws Exception { |
| 129 | + deployer.undeploy( "jaxpit" ); |
| 130 | + } |
| 131 | + |
| 132 | + private void doTest() { |
| 133 | + Set<ConstraintViolation<Camera>> violations = validator.validate( new Camera() ); |
| 134 | + |
| 135 | + assertEquals( 1, violations.size() ); |
| 136 | + assertSame( NotNull.class, violations.iterator() |
| 137 | + .next() |
| 138 | + .getConstraintDescriptor() |
| 139 | + .getAnnotation() |
| 140 | + .annotationType() ); |
| 141 | + } |
| 142 | +} |
0 commit comments