From 149e670dabbf6dde3a342b8d202b9653ef207f1e Mon Sep 17 00:00:00 2001 From: Gunnar Morling Date: Tue, 5 Mar 2013 11:27:50 +0100 Subject: [PATCH] BVTCK-42 Adding tests for JNDI look-up of Validator(Factory) and injection via @Resource --- pom.xml | 5 ++ tests/pom.xml | 5 ++ .../ee/ConstantMessageInterpolator.java | 36 ++++++++ .../integration/ee/DefaultInjectionTest.java | 68 ++++++++++++++ .../tck/tests/integration/ee/Foo.java | 27 ++++++ .../integration/ee/JndiRetrievalTest.java | 88 +++++++++++++++++++ .../integration/ee/ValidationTestEjb.java | 67 ++++++++++++++ .../tests/integration/ee/test-validation.xml | 8 ++ 8 files changed, 304 insertions(+) create mode 100644 tests/src/main/java/org/hibernate/beanvalidation/tck/tests/integration/ee/ConstantMessageInterpolator.java create mode 100644 tests/src/main/java/org/hibernate/beanvalidation/tck/tests/integration/ee/DefaultInjectionTest.java create mode 100644 tests/src/main/java/org/hibernate/beanvalidation/tck/tests/integration/ee/Foo.java create mode 100644 tests/src/main/java/org/hibernate/beanvalidation/tck/tests/integration/ee/JndiRetrievalTest.java create mode 100644 tests/src/main/java/org/hibernate/beanvalidation/tck/tests/integration/ee/ValidationTestEjb.java create mode 100644 tests/src/main/resources/org/hibernate/beanvalidation/tck/tests/integration/ee/test-validation.xml diff --git a/pom.xml b/pom.xml index 558c9b34..6a8ef6ce 100644 --- a/pom.xml +++ b/pom.xml @@ -125,6 +125,11 @@ cdi-api 1.1-PFD + + org.jboss.spec.javax.ejb + jboss-ejb-api_3.2_spec + 1.0.0.Alpha2 + diff --git a/tests/pom.xml b/tests/pom.xml index 7094dac6..0357708c 100644 --- a/tests/pom.xml +++ b/tests/pom.xml @@ -45,6 +45,11 @@ cdi-api provided + + org.jboss.spec.javax.ejb + jboss-ejb-api_3.2_spec + provided + diff --git a/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/integration/ee/ConstantMessageInterpolator.java b/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/integration/ee/ConstantMessageInterpolator.java new file mode 100644 index 00000000..e9746d6a --- /dev/null +++ b/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/integration/ee/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.ee; + +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/ee/DefaultInjectionTest.java b/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/integration/ee/DefaultInjectionTest.java new file mode 100644 index 00000000..c1b349fd --- /dev/null +++ b/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/integration/ee/DefaultInjectionTest.java @@ -0,0 +1,68 @@ +/* +* 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.ee; + +import javax.ejb.EJB; + +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.testng.Assert.assertNotNull; + +/** + * @author Gunnar Morling + */ +@IntegrationTest +@SpecVersion(spec = "beanvalidation", version = "1.1.0") +public class DefaultInjectionTest extends Arquillian { + + @EJB + private ValidationTestEjb testEjb; + + @Deployment + public static WebArchive createTestArchive() { + return new WebArchiveBuilder() + .withTestClass( DefaultInjectionTest.class ) + .withClass( ConstantMessageInterpolator.class ) + .withClass( Foo.class ) + .withClass( ValidationTestEjb.class ) + .withValidationXml( "test-validation.xml" ) + .withEmptyBeansXml() + .build(); + } + + @Test + @SpecAssertion(section = "10.2", id = "b") + private void testDefaultValidatorFactoryGetsInjectedAtResource() throws Exception { + assertNotNull( testEjb ); + testEjb.assertDefaultValidatorFactoryGetsInjected(); + } + + @Test + @SpecAssertion(section = "10.2", id = "b") + private void testDefaultValidatorGetsInjectedWithAtResource() { + assertNotNull( testEjb ); + testEjb.assertDefaultValidatorGetsInjected(); + } +} diff --git a/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/integration/ee/Foo.java b/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/integration/ee/Foo.java new file mode 100644 index 00000000..5070a463 --- /dev/null +++ b/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/integration/ee/Foo.java @@ -0,0 +1,27 @@ +/* +* 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.ee; + +import javax.validation.constraints.NotNull; + +/** + * @author Gunnar Morling + */ +public class Foo { + @NotNull + public String bar; +} \ No newline at end of file diff --git a/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/integration/ee/JndiRetrievalTest.java b/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/integration/ee/JndiRetrievalTest.java new file mode 100644 index 00000000..7c2edb6f --- /dev/null +++ b/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/integration/ee/JndiRetrievalTest.java @@ -0,0 +1,88 @@ +/* +* 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.ee; + +import java.util.Set; +import javax.naming.InitialContext; +import javax.validation.ConstraintViolation; +import javax.validation.Validator; +import javax.validation.ValidatorFactory; + +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 JndiRetrievalTest extends Arquillian { + + @Deployment + public static WebArchive createTestArchive() { + return new WebArchiveBuilder() + .withTestClass( JndiRetrievalTest.class ) + .withClass( ConstantMessageInterpolator.class ) + .withClass( Foo.class ) + .withValidationXml( "test-validation.xml" ) + .withEmptyBeansXml() + .build(); + } + + @Test + @SpecAssertion(section = "10.2", id = "a") + private void testDefaultValidatorFactoryCanBeRetrievedFromJndi() throws Exception { + ValidatorFactory validatorFactory = InitialContext.doLookup( "java:comp/ValidatorFactory" ); + assertNotNull( + validatorFactory, + "Default validator factory should be bound to JNDI tree." + ); + assertTrue( + validatorFactory.getMessageInterpolator() instanceof ConstantMessageInterpolator, + "Default validator factory bound to JNDI should be configured based on META-INF/validation.xml." + ); + + Set> violations = validatorFactory.getValidator() + .validate( new Foo() ); + + //expecting message from interpolator configured in META-INF/validation.xml + assertCorrectConstraintViolationMessages( violations, "Invalid constraint" ); + } + + @Test + @SpecAssertion(section = "10.2", id = "a") + private void testDefaultValidatorCanBeRetrievedFromJndi() throws Exception { + Validator validator = InitialContext.doLookup( "java:comp/Validator" ); + assertNotNull( validator, "Default validator should be bound to JNDI tree." ); + + Set> violations = validator.validate( new Foo() ); + + //expecting message from interpolator configured in META-INF/validation.xml + assertCorrectConstraintViolationMessages( violations, "Invalid constraint" ); + } +} diff --git a/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/integration/ee/ValidationTestEjb.java b/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/integration/ee/ValidationTestEjb.java new file mode 100644 index 00000000..effdb1d7 --- /dev/null +++ b/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/integration/ee/ValidationTestEjb.java @@ -0,0 +1,67 @@ +/* +* 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.ee; + +import java.util.Set; +import javax.annotation.Resource; +import javax.ejb.Stateless; +import javax.validation.ConstraintViolation; +import javax.validation.Validator; +import javax.validation.ValidatorFactory; + +import static org.hibernate.beanvalidation.tck.util.TestUtil.assertCorrectConstraintViolationMessages; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; + +/** + * A test EJB which retrieves validator and validator factory via + * {@code @Resource} injection. + * + * @author Gunnar Morling + */ +@Stateless +public class ValidationTestEjb { + + @Resource + public ValidatorFactory defaultValidatorFactory; + + @Resource + public Validator defaultValidator; + + public void assertDefaultValidatorFactoryGetsInjected() { + 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" ); + } + + public void assertDefaultValidatorGetsInjected() { + 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" ); + } +} diff --git a/tests/src/main/resources/org/hibernate/beanvalidation/tck/tests/integration/ee/test-validation.xml b/tests/src/main/resources/org/hibernate/beanvalidation/tck/tests/integration/ee/test-validation.xml new file mode 100644 index 00000000..1c91cbb8 --- /dev/null +++ b/tests/src/main/resources/org/hibernate/beanvalidation/tck/tests/integration/ee/test-validation.xml @@ -0,0 +1,8 @@ + + + org.hibernate.beanvalidation.tck.tests.integration.ee.ConstantMessageInterpolator +