|
|
|
@ -16,19 +16,24 @@ |
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.orm.jpa.hibernate; |
|
|
|
package org.springframework.orm.jpa.hibernate; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.persistence.AttributeConverter; |
|
|
|
|
|
|
|
|
|
|
|
import org.hibernate.SessionFactory; |
|
|
|
import org.hibernate.SessionFactory; |
|
|
|
import org.hibernate.resource.beans.container.spi.BeanContainer; |
|
|
|
import org.hibernate.resource.beans.container.spi.BeanContainer; |
|
|
|
import org.hibernate.resource.beans.container.spi.ContainedBean; |
|
|
|
import org.hibernate.resource.beans.container.spi.ContainedBean; |
|
|
|
import org.hibernate.resource.beans.spi.BeanInstanceProducer; |
|
|
|
import org.hibernate.resource.beans.spi.BeanInstanceProducer; |
|
|
|
import org.hibernate.resource.beans.spi.ManagedBeanRegistry; |
|
|
|
import org.hibernate.resource.beans.spi.ManagedBeanRegistry; |
|
|
|
import org.hibernate.service.ServiceRegistry; |
|
|
|
import org.hibernate.service.ServiceRegistry; |
|
|
|
|
|
|
|
|
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.BeanCreationException; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.context.ApplicationContext; |
|
|
|
import org.springframework.context.ApplicationContext; |
|
|
|
import org.springframework.orm.jpa.AbstractEntityManagerFactoryIntegrationTests; |
|
|
|
import org.springframework.orm.jpa.AbstractEntityManagerFactoryIntegrationTests; |
|
|
|
import org.springframework.orm.jpa.hibernate.beans.*; |
|
|
|
import org.springframework.orm.jpa.hibernate.beans.BeanSource; |
|
|
|
|
|
|
|
import org.springframework.orm.jpa.hibernate.beans.MultiplePrototypesInSpringContextTestBean; |
|
|
|
|
|
|
|
import org.springframework.orm.jpa.hibernate.beans.NoDefinitionInSpringContextTestBean; |
|
|
|
|
|
|
|
import org.springframework.orm.jpa.hibernate.beans.SinglePrototypeInSpringContextTestBean; |
|
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.*; |
|
|
|
import static org.junit.Assert.*; |
|
|
|
|
|
|
|
|
|
|
|
@ -36,6 +41,7 @@ import static org.junit.Assert.*; |
|
|
|
* Hibernate-specific SpringBeanContainer integration tests. |
|
|
|
* Hibernate-specific SpringBeanContainer integration tests. |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Yoann Rodiere |
|
|
|
* @author Yoann Rodiere |
|
|
|
|
|
|
|
* @author Juergen Hoeller |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class HibernateNativeEntityManagerFactorySpringBeanContainerIntegrationTests |
|
|
|
public class HibernateNativeEntityManagerFactorySpringBeanContainerIntegrationTests |
|
|
|
extends AbstractEntityManagerFactoryIntegrationTests { |
|
|
|
extends AbstractEntityManagerFactoryIntegrationTests { |
|
|
|
@ -51,9 +57,9 @@ public class HibernateNativeEntityManagerFactorySpringBeanContainerIntegrationTe |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private ManagedBeanRegistry getManagedBeanRegistry() { |
|
|
|
private ManagedBeanRegistry getManagedBeanRegistry() { |
|
|
|
SessionFactory sessionFactory = entityManagerFactory.unwrap( SessionFactory.class ); |
|
|
|
SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class); |
|
|
|
ServiceRegistry serviceRegistry = sessionFactory.getSessionFactoryOptions().getServiceRegistry(); |
|
|
|
ServiceRegistry serviceRegistry = sessionFactory.getSessionFactoryOptions().getServiceRegistry(); |
|
|
|
return serviceRegistry.requireService( ManagedBeanRegistry.class ); |
|
|
|
return serviceRegistry.requireService(ManagedBeanRegistry.class); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private BeanContainer getBeanContainer() { |
|
|
|
private BeanContainer getBeanContainer() { |
|
|
|
@ -68,8 +74,7 @@ public class HibernateNativeEntityManagerFactorySpringBeanContainerIntegrationTe |
|
|
|
|
|
|
|
|
|
|
|
ContainedBean<SinglePrototypeInSpringContextTestBean> bean = beanContainer.getBean( |
|
|
|
ContainedBean<SinglePrototypeInSpringContextTestBean> bean = beanContainer.getBean( |
|
|
|
SinglePrototypeInSpringContextTestBean.class, |
|
|
|
SinglePrototypeInSpringContextTestBean.class, |
|
|
|
JpaLifecycleOptions.INSTANCE, |
|
|
|
JpaLifecycleOptions.INSTANCE, IneffectiveBeanInstanceProducer.INSTANCE |
|
|
|
IneffectiveBeanInstanceProducer.INSTANCE |
|
|
|
|
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
assertNotNull(bean); |
|
|
|
assertNotNull(bean); |
|
|
|
@ -85,8 +90,7 @@ public class HibernateNativeEntityManagerFactorySpringBeanContainerIntegrationTe |
|
|
|
|
|
|
|
|
|
|
|
ContainedBean<MultiplePrototypesInSpringContextTestBean> bean = beanContainer.getBean( |
|
|
|
ContainedBean<MultiplePrototypesInSpringContextTestBean> bean = beanContainer.getBean( |
|
|
|
"multiple-1", MultiplePrototypesInSpringContextTestBean.class, |
|
|
|
"multiple-1", MultiplePrototypesInSpringContextTestBean.class, |
|
|
|
JpaLifecycleOptions.INSTANCE, |
|
|
|
JpaLifecycleOptions.INSTANCE, IneffectiveBeanInstanceProducer.INSTANCE |
|
|
|
IneffectiveBeanInstanceProducer.INSTANCE |
|
|
|
|
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
assertNotNull(bean); |
|
|
|
assertNotNull(bean); |
|
|
|
@ -103,8 +107,7 @@ public class HibernateNativeEntityManagerFactorySpringBeanContainerIntegrationTe |
|
|
|
|
|
|
|
|
|
|
|
ContainedBean<SinglePrototypeInSpringContextTestBean> bean = beanContainer.getBean( |
|
|
|
ContainedBean<SinglePrototypeInSpringContextTestBean> bean = beanContainer.getBean( |
|
|
|
SinglePrototypeInSpringContextTestBean.class, |
|
|
|
SinglePrototypeInSpringContextTestBean.class, |
|
|
|
NativeLifecycleOptions.INSTANCE, |
|
|
|
NativeLifecycleOptions.INSTANCE, IneffectiveBeanInstanceProducer.INSTANCE |
|
|
|
IneffectiveBeanInstanceProducer.INSTANCE |
|
|
|
|
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
assertNotNull(bean); |
|
|
|
assertNotNull(bean); |
|
|
|
@ -115,8 +118,7 @@ public class HibernateNativeEntityManagerFactorySpringBeanContainerIntegrationTe |
|
|
|
|
|
|
|
|
|
|
|
ContainedBean<SinglePrototypeInSpringContextTestBean> bean2 = beanContainer.getBean( |
|
|
|
ContainedBean<SinglePrototypeInSpringContextTestBean> bean2 = beanContainer.getBean( |
|
|
|
SinglePrototypeInSpringContextTestBean.class, |
|
|
|
SinglePrototypeInSpringContextTestBean.class, |
|
|
|
NativeLifecycleOptions.INSTANCE, |
|
|
|
NativeLifecycleOptions.INSTANCE, IneffectiveBeanInstanceProducer.INSTANCE |
|
|
|
IneffectiveBeanInstanceProducer.INSTANCE |
|
|
|
|
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
assertNotNull(bean2); |
|
|
|
assertNotNull(bean2); |
|
|
|
@ -133,8 +135,7 @@ public class HibernateNativeEntityManagerFactorySpringBeanContainerIntegrationTe |
|
|
|
|
|
|
|
|
|
|
|
ContainedBean<MultiplePrototypesInSpringContextTestBean> bean = beanContainer.getBean( |
|
|
|
ContainedBean<MultiplePrototypesInSpringContextTestBean> bean = beanContainer.getBean( |
|
|
|
"multiple-1", MultiplePrototypesInSpringContextTestBean.class, |
|
|
|
"multiple-1", MultiplePrototypesInSpringContextTestBean.class, |
|
|
|
NativeLifecycleOptions.INSTANCE, |
|
|
|
NativeLifecycleOptions.INSTANCE, IneffectiveBeanInstanceProducer.INSTANCE |
|
|
|
IneffectiveBeanInstanceProducer.INSTANCE |
|
|
|
|
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
assertNotNull(bean); |
|
|
|
assertNotNull(bean); |
|
|
|
@ -145,8 +146,7 @@ public class HibernateNativeEntityManagerFactorySpringBeanContainerIntegrationTe |
|
|
|
|
|
|
|
|
|
|
|
ContainedBean<MultiplePrototypesInSpringContextTestBean> bean2 = beanContainer.getBean( |
|
|
|
ContainedBean<MultiplePrototypesInSpringContextTestBean> bean2 = beanContainer.getBean( |
|
|
|
"multiple-1", MultiplePrototypesInSpringContextTestBean.class, |
|
|
|
"multiple-1", MultiplePrototypesInSpringContextTestBean.class, |
|
|
|
NativeLifecycleOptions.INSTANCE, |
|
|
|
NativeLifecycleOptions.INSTANCE, IneffectiveBeanInstanceProducer.INSTANCE |
|
|
|
IneffectiveBeanInstanceProducer.INSTANCE |
|
|
|
|
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
assertNotNull(bean2); |
|
|
|
assertNotNull(bean2); |
|
|
|
@ -164,8 +164,7 @@ public class HibernateNativeEntityManagerFactorySpringBeanContainerIntegrationTe |
|
|
|
|
|
|
|
|
|
|
|
ContainedBean<NoDefinitionInSpringContextTestBean> bean = beanContainer.getBean( |
|
|
|
ContainedBean<NoDefinitionInSpringContextTestBean> bean = beanContainer.getBean( |
|
|
|
NoDefinitionInSpringContextTestBean.class, |
|
|
|
NoDefinitionInSpringContextTestBean.class, |
|
|
|
JpaLifecycleOptions.INSTANCE, |
|
|
|
JpaLifecycleOptions.INSTANCE, fallbackProducer |
|
|
|
fallbackProducer |
|
|
|
|
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(1, fallbackProducer.currentUnnamedInstantiationCount()); |
|
|
|
assertEquals(1, fallbackProducer.currentUnnamedInstantiationCount()); |
|
|
|
@ -186,8 +185,7 @@ public class HibernateNativeEntityManagerFactorySpringBeanContainerIntegrationTe |
|
|
|
|
|
|
|
|
|
|
|
ContainedBean<NoDefinitionInSpringContextTestBean> bean = beanContainer.getBean( |
|
|
|
ContainedBean<NoDefinitionInSpringContextTestBean> bean = beanContainer.getBean( |
|
|
|
"some name", NoDefinitionInSpringContextTestBean.class, |
|
|
|
"some name", NoDefinitionInSpringContextTestBean.class, |
|
|
|
JpaLifecycleOptions.INSTANCE, |
|
|
|
JpaLifecycleOptions.INSTANCE, fallbackProducer |
|
|
|
fallbackProducer |
|
|
|
|
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(0, fallbackProducer.currentUnnamedInstantiationCount()); |
|
|
|
assertEquals(0, fallbackProducer.currentUnnamedInstantiationCount()); |
|
|
|
@ -209,8 +207,7 @@ public class HibernateNativeEntityManagerFactorySpringBeanContainerIntegrationTe |
|
|
|
|
|
|
|
|
|
|
|
ContainedBean<NoDefinitionInSpringContextTestBean> bean = beanContainer.getBean( |
|
|
|
ContainedBean<NoDefinitionInSpringContextTestBean> bean = beanContainer.getBean( |
|
|
|
NoDefinitionInSpringContextTestBean.class, |
|
|
|
NoDefinitionInSpringContextTestBean.class, |
|
|
|
NativeLifecycleOptions.INSTANCE, |
|
|
|
NativeLifecycleOptions.INSTANCE, fallbackProducer |
|
|
|
fallbackProducer |
|
|
|
|
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(1, fallbackProducer.currentUnnamedInstantiationCount()); |
|
|
|
assertEquals(1, fallbackProducer.currentUnnamedInstantiationCount()); |
|
|
|
@ -231,8 +228,7 @@ public class HibernateNativeEntityManagerFactorySpringBeanContainerIntegrationTe |
|
|
|
|
|
|
|
|
|
|
|
ContainedBean<NoDefinitionInSpringContextTestBean> bean = beanContainer.getBean( |
|
|
|
ContainedBean<NoDefinitionInSpringContextTestBean> bean = beanContainer.getBean( |
|
|
|
"some name", NoDefinitionInSpringContextTestBean.class, |
|
|
|
"some name", NoDefinitionInSpringContextTestBean.class, |
|
|
|
NativeLifecycleOptions.INSTANCE, |
|
|
|
NativeLifecycleOptions.INSTANCE, fallbackProducer |
|
|
|
fallbackProducer |
|
|
|
|
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(0, fallbackProducer.currentUnnamedInstantiationCount()); |
|
|
|
assertEquals(0, fallbackProducer.currentUnnamedInstantiationCount()); |
|
|
|
@ -246,11 +242,40 @@ public class HibernateNativeEntityManagerFactorySpringBeanContainerIntegrationTe |
|
|
|
assertNull(instance.getApplicationContext()); |
|
|
|
assertNull(instance.getApplicationContext()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test(expected = UnsupportedOperationException.class) |
|
|
|
|
|
|
|
public void testFallbackExceptionInCaseOfNoSpringBeanFound() { |
|
|
|
|
|
|
|
getBeanContainer().getBean(NoDefinitionInSpringContextTestBean.class, |
|
|
|
|
|
|
|
NativeLifecycleOptions.INSTANCE, IneffectiveBeanInstanceProducer.INSTANCE |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test(expected = BeanCreationException.class) |
|
|
|
|
|
|
|
public void testOriginalExceptionInCaseOfFallbackProducerFailure() { |
|
|
|
|
|
|
|
getBeanContainer().getBean(AttributeConverter.class, |
|
|
|
|
|
|
|
NativeLifecycleOptions.INSTANCE, IneffectiveBeanInstanceProducer.INSTANCE |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test(expected = UnsupportedOperationException.class) |
|
|
|
|
|
|
|
public void testFallbackExceptionInCaseOfNoSpringBeanFoundByName() { |
|
|
|
|
|
|
|
getBeanContainer().getBean("some name", NoDefinitionInSpringContextTestBean.class, |
|
|
|
|
|
|
|
NativeLifecycleOptions.INSTANCE, IneffectiveBeanInstanceProducer.INSTANCE |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test(expected = BeanCreationException.class) |
|
|
|
|
|
|
|
public void testOriginalExceptionInCaseOfFallbackProducerFailureByName() { |
|
|
|
|
|
|
|
getBeanContainer().getBean("invalid", AttributeConverter.class, |
|
|
|
|
|
|
|
NativeLifecycleOptions.INSTANCE, IneffectiveBeanInstanceProducer.INSTANCE |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* The lifecycle options mandated by the JPA spec and used as a default in Hibernate ORM. |
|
|
|
* The lifecycle options mandated by the JPA spec and used as a default in Hibernate ORM. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private static class JpaLifecycleOptions implements BeanContainer.LifecycleOptions { |
|
|
|
private static class JpaLifecycleOptions implements BeanContainer.LifecycleOptions { |
|
|
|
|
|
|
|
|
|
|
|
public static final JpaLifecycleOptions INSTANCE = new JpaLifecycleOptions(); |
|
|
|
public static final JpaLifecycleOptions INSTANCE = new JpaLifecycleOptions(); |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
@ -264,12 +289,14 @@ public class HibernateNativeEntityManagerFactorySpringBeanContainerIntegrationTe |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* The lifecycle options used by libraries integrating into Hibernate ORM |
|
|
|
* The lifecycle options used by libraries integrating into Hibernate ORM |
|
|
|
* and that want a behavior closer to Spring's native behavior, |
|
|
|
* and that want a behavior closer to Spring's native behavior, |
|
|
|
* such as Hibernate Search. |
|
|
|
* such as Hibernate Search. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private static class NativeLifecycleOptions implements BeanContainer.LifecycleOptions { |
|
|
|
private static class NativeLifecycleOptions implements BeanContainer.LifecycleOptions { |
|
|
|
|
|
|
|
|
|
|
|
public static final NativeLifecycleOptions INSTANCE = new NativeLifecycleOptions(); |
|
|
|
public static final NativeLifecycleOptions INSTANCE = new NativeLifecycleOptions(); |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
@ -283,7 +310,9 @@ public class HibernateNativeEntityManagerFactorySpringBeanContainerIntegrationTe |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class IneffectiveBeanInstanceProducer implements BeanInstanceProducer { |
|
|
|
private static class IneffectiveBeanInstanceProducer implements BeanInstanceProducer { |
|
|
|
|
|
|
|
|
|
|
|
public static final IneffectiveBeanInstanceProducer INSTANCE = new IneffectiveBeanInstanceProducer(); |
|
|
|
public static final IneffectiveBeanInstanceProducer INSTANCE = new IneffectiveBeanInstanceProducer(); |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
@ -297,8 +326,11 @@ public class HibernateNativeEntityManagerFactorySpringBeanContainerIntegrationTe |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class NoDefinitionInSpringContextTestBeanInstanceProducer implements BeanInstanceProducer { |
|
|
|
private static class NoDefinitionInSpringContextTestBeanInstanceProducer implements BeanInstanceProducer { |
|
|
|
|
|
|
|
|
|
|
|
private int unnamedInstantiationCount = 0; |
|
|
|
private int unnamedInstantiationCount = 0; |
|
|
|
|
|
|
|
|
|
|
|
private int namedInstantiationCount = 0; |
|
|
|
private int namedInstantiationCount = 0; |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
@ -341,4 +373,5 @@ public class HibernateNativeEntityManagerFactorySpringBeanContainerIntegrationTe |
|
|
|
return namedInstantiationCount; |
|
|
|
return namedInstantiationCount; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|