diff --git a/pom.xml b/pom.xml index 0f20d9a7..558c9b34 100644 --- a/pom.xml +++ b/pom.xml @@ -120,6 +120,11 @@ shrinkwrap-descriptors-impl-javaee ${shrinkwrap.descriptors.version} + + javax.enterprise + cdi-api + 1.1-PFD + diff --git a/tests/pom.xml b/tests/pom.xml index 821a9d1d..7094dac6 100644 --- a/tests/pom.xml +++ b/tests/pom.xml @@ -40,6 +40,11 @@ org.jboss.shrinkwrap.descriptors shrinkwrap-descriptors-impl-javaee + + javax.enterprise + cdi-api + provided + diff --git a/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/integration/cdi/ConstantMessageInterpolator.java b/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/integration/cdi/ConstantMessageInterpolator.java new file mode 100644 index 00000000..d5ab510b --- /dev/null +++ b/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/integration/cdi/ConstantMessageInterpolator.java @@ -0,0 +1,36 @@ +/* +* JBoss, Home of Professional Open Source +* Copyright 2013, Red Hat, Inc. and/or its affiliates, and individual contributors +* by the @authors tag. See the copyright.txt in the distribution for a +* full listing of individual contributors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* http://www.apache.org/licenses/LICENSE-2.0 +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package org.hibernate.beanvalidation.tck.tests.integration.cdi; + +import java.util.Locale; +import javax.validation.MessageInterpolator; + +/** + * @author Gunnar Morling + */ +public class ConstantMessageInterpolator implements MessageInterpolator { + + @Override + public String interpolate(String messageTemplate, Context context) { + return "Invalid constraint"; + } + + @Override + public String interpolate(String messageTemplate, Context context, Locale locale) { + return "Invalid constraint"; + } +} diff --git a/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/integration/cdi/DefaultInjectionTest.java b/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/integration/cdi/DefaultInjectionTest.java new file mode 100644 index 00000000..02a9d9f5 --- /dev/null +++ b/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/integration/cdi/DefaultInjectionTest.java @@ -0,0 +1,135 @@ +/* +* JBoss, Home of Professional Open Source +* Copyright 2013, Red Hat, Inc. and/or its affiliates, and individual contributors +* by the @authors tag. See the copyright.txt in the distribution for a +* full listing of individual contributors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* http://www.apache.org/licenses/LICENSE-2.0 +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package org.hibernate.beanvalidation.tck.tests.integration.cdi; + +import java.util.Set; +import javax.enterprise.inject.Default; +import javax.inject.Inject; +import javax.validation.ConstraintViolation; +import javax.validation.Validator; +import javax.validation.ValidatorFactory; +import javax.validation.constraints.NotNull; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.testng.Arquillian; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.jboss.test.audit.annotations.SpecAssertion; +import org.jboss.test.audit.annotations.SpecVersion; +import org.testng.annotations.Test; + +import org.hibernate.beanvalidation.tck.util.IntegrationTest; +import org.hibernate.beanvalidation.tck.util.shrinkwrap.WebArchiveBuilder; + +import static org.hibernate.beanvalidation.tck.util.TestUtil.assertCorrectConstraintViolationMessages; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; + +/** + * @author Gunnar Morling + */ +@IntegrationTest +@SpecVersion(spec = "beanvalidation", version = "1.1.0") +public class DefaultInjectionTest extends Arquillian { + + @Inject + private ValidatorFactory defaultValidatorFactory; + + @Inject + @Default + private ValidatorFactory qualifiedDefaultValidatorFactory; + + @Inject + private Validator defaultValidator; + + @Inject + @Default + private Validator qualifiedDefaultValidator; + + @Deployment + public static WebArchive createTestArchive() { + return new WebArchiveBuilder() + .withTestClassPackage( DefaultInjectionTest.class ) + .withValidationXml( "test-validation.xml" ) + .withEmptyBeansXml() + .build(); + } + + @SpecAssertion(section = "10.3.1", id = "a") + @Test + private void testDefaultValidatorFactoryGetsInjected() { + assertNotNull( defaultValidatorFactory, "Default validator factory should be injectable." ); + assertTrue( + defaultValidatorFactory.getMessageInterpolator() instanceof ConstantMessageInterpolator, + "Injected default validator factory should be configured based on META-INF/validation.xml." + ); + + Set> violations = defaultValidatorFactory.getValidator() + .validate( new Foo() ); + + //expecting message from interpolator configured in META-INF/validation.xml + assertCorrectConstraintViolationMessages( violations, "Invalid constraint" ); + } + + @SpecAssertion(section = "10.3.1", id = "a") + @Test + private void testQualifiedDefaultValidatorFactoryGetsInjected() { + assertNotNull( + qualifiedDefaultValidatorFactory, + "Qualified default validator factory should be injectable." + ); + assertTrue( + qualifiedDefaultValidatorFactory.getMessageInterpolator() instanceof ConstantMessageInterpolator, + "Injected qualified default validator factory should be configured based on META-INF/validation.xml." + ); + + Set> violations = qualifiedDefaultValidatorFactory.getValidator() + .validate( new Foo() ); + + //expecting message from interpolator configured in META-INF/validation.xml + assertCorrectConstraintViolationMessages( violations, "Invalid constraint" ); + } + + @SpecAssertion(section = "10.3.1", id = "a") + @Test + private void testDefaultValidatorGetsInjected() { + assertNotNull( defaultValidator, "Default validator should be injectable." ); + + Set> violations = defaultValidator.validate( new Foo() ); + + //expecting message from interpolator configured in META-INF/validation.xml + assertCorrectConstraintViolationMessages( violations, "Invalid constraint" ); + } + + @SpecAssertion(section = "10.3.1", id = "a") + @Test + private void testQualifiedDefaultValidatorGetsInjected() { + assertNotNull( + qualifiedDefaultValidator, + "Qualified default validator should be injectable." + ); + + Set> violations = qualifiedDefaultValidator.validate( new Foo() ); + + //expecting message from interpolator configured in META-INF/validation.xml + assertCorrectConstraintViolationMessages( violations, "Invalid constraint" ); + } + + private static class Foo { + @NotNull + public String bar; + } +} diff --git a/tests/src/main/resources/org/hibernate/beanvalidation/tck/tests/integration/cdi/test-validation.xml b/tests/src/main/resources/org/hibernate/beanvalidation/tck/tests/integration/cdi/test-validation.xml new file mode 100644 index 00000000..a511eb8a --- /dev/null +++ b/tests/src/main/resources/org/hibernate/beanvalidation/tck/tests/integration/cdi/test-validation.xml @@ -0,0 +1,8 @@ + + + org.hibernate.beanvalidation.tck.tests.integration.cdi.ConstantMessageInterpolator +