|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.jpa.test.beanvalidation; |
| 8 | + |
| 9 | +import java.net.URL; |
| 10 | +import java.util.Collections; |
| 11 | + |
| 12 | +import javax.persistence.EntityManagerFactory; |
| 13 | +import javax.validation.Validation; |
| 14 | +import javax.validation.ValidatorFactory; |
| 15 | + |
| 16 | +import org.hibernate.jpa.AvailableSettings; |
| 17 | +import org.hibernate.jpa.HibernatePersistenceProvider; |
| 18 | +import org.hibernate.jpa.boot.spi.Bootstrap; |
| 19 | +import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder; |
| 20 | +import org.hibernate.jpa.test.jee.OrmVersionTest; |
| 21 | + |
| 22 | +import org.hibernate.testing.junit4.BaseUnitTestCase; |
| 23 | +import org.junit.After; |
| 24 | +import org.junit.Before; |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +import static org.junit.Assert.assertSame; |
| 28 | + |
| 29 | +/** |
| 30 | + * Test injection of ValidatorFactory using WF/Hibernate 2-phase boot process |
| 31 | + * |
| 32 | + * @author Steve Ebersole |
| 33 | + */ |
| 34 | +public class ValidatorFactory2PhaseInjectionTest extends BaseUnitTestCase { |
| 35 | + private ValidatorFactory vf; |
| 36 | + |
| 37 | + @Before |
| 38 | + public void before() { |
| 39 | + vf = Validation.byDefaultProvider().configure().buildValidatorFactory(); |
| 40 | + } |
| 41 | + |
| 42 | + @After |
| 43 | + public void after() { |
| 44 | + if ( vf != null ) { |
| 45 | + vf.close(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testInjectionAvailabilityFromEmf() { |
| 51 | + EntityManagerFactoryBuilder emfb = Bootstrap.getEntityManagerFactoryBuilder( |
| 52 | + new OrmVersionTest.PersistenceUnitInfoImpl( "my-test" ) { |
| 53 | + @Override |
| 54 | + public URL getPersistenceUnitRootUrl() { |
| 55 | + // just get any known url... |
| 56 | + return HibernatePersistenceProvider.class.getResource( "/org/hibernate/jpa/persistence_1_0.xsd" ); |
| 57 | + } |
| 58 | + }, |
| 59 | + Collections.emptyMap() |
| 60 | + ); |
| 61 | + |
| 62 | + emfb.withValidatorFactory( vf ); |
| 63 | + |
| 64 | + EntityManagerFactory emf = emfb.build(); |
| 65 | + assertSame( vf, emf.getProperties().get( AvailableSettings.VALIDATION_FACTORY ) ); |
| 66 | + emf.close(); |
| 67 | + } |
| 68 | +} |
0 commit comments