SharedEntityManagerCreator's EntityManager proxies are fully serializable now (SPR-6684)

This commit is contained in:
Juergen Hoeller
2010-02-01 14:48:18 +00:00
parent df54c8613d
commit db71811c5a
5 changed files with 125 additions and 43 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,12 +24,14 @@ import javax.persistence.FlushModeType;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.orm.jpa.domain.DriversLicense;
import org.springframework.orm.jpa.domain.Person;
import org.springframework.test.annotation.ExpectedException;
import org.springframework.test.annotation.NotTransactional;
import org.springframework.test.annotation.Repeat;
import org.springframework.test.annotation.Timed;
import org.springframework.util.SerializationTestUtils;
/**
* Integration tests for LocalContainerEntityManagerFactoryBean.
@@ -252,4 +254,12 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests
}
}
public void testCanSerializeProxies() throws Exception {
// just necessary because of AbstractJpaTests magically cloning the BeanFactory
((DefaultListableBeanFactory) getApplicationContext().getBeanFactory()).setSerializationId("emf-it");
assertNotNull(SerializationTestUtils.serializeAndDeserialize(entityManagerFactory));
assertNotNull(SerializationTestUtils.serializeAndDeserialize(sharedEntityManager));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@ package org.springframework.orm.jpa;
import java.util.Map;
import java.util.Properties;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
@@ -30,12 +29,14 @@ import javax.persistence.spi.PersistenceUnitTransactionType;
import org.easymock.MockControl;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver;
import org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
import org.springframework.util.SerializationTestUtils;
/**
* @author Rod Johnson
@@ -74,6 +75,13 @@ public class LocalContainerEntityManagerFactoryBeanTests extends AbstractEntityM
assertNotSame("EMF must be proxied", mockEmf, emf);
assertTrue(emf.equals(emf));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.setSerializationId("emf-bf");
bf.registerSingleton("emf", cefb);
cefb.setBeanFactory(bf);
cefb.setBeanName("emf");
assertNotNull(SerializationTestUtils.serializeAndDeserialize(emf));
}
public void testApplicationManagedEntityManagerWithoutTransaction() throws Exception {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -56,7 +56,7 @@ import org.springframework.util.SerializationTestUtils;
*/
public class PersistenceInjectionTests extends AbstractEntityManagerFactoryBeanTests {
public void testPrivatePersistenceContextField() {
public void testPrivatePersistenceContextField() throws Exception {
GenericApplicationContext gac = new GenericApplicationContext();
gac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory", mockEmf);
gac.registerBeanDefinition("annotationProcessor",
@@ -73,6 +73,9 @@ public class PersistenceInjectionTests extends AbstractEntityManagerFactoryBeanT
"&" + FactoryBeanWithPersistenceContextField.class.getName());
assertNotNull(bean.em);
assertNotNull(bean2.em);
assertNotNull(SerializationTestUtils.serializeAndDeserialize(bean.em));
assertNotNull(SerializationTestUtils.serializeAndDeserialize(bean2.em));
}
public void testPrivateVendorSpecificPersistenceContextField() {
@@ -111,8 +114,7 @@ public class PersistenceInjectionTests extends AbstractEntityManagerFactoryBeanT
public void testPublicExtendedPersistenceContextSetterWithSerialization() throws Exception {
DummyInvocationHandler ih = new DummyInvocationHandler();
Object mockEm = (EntityManager) Proxy.newProxyInstance(
getClass().getClassLoader(), new Class[] {EntityManager.class}, ih);
Object mockEm = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] {EntityManager.class}, ih);
mockEmf.createEntityManager();
emfMc.setReturnValue(mockEm, 1);
emfMc.replay();
@@ -141,7 +143,7 @@ public class PersistenceInjectionTests extends AbstractEntityManagerFactoryBeanT
}
public void testPublicExtendedPersistenceContextSetterWithEntityManagerInfoAndSerialization() throws Exception {
Object mockEm = (EntityManager) Proxy.newProxyInstance(
Object mockEm = Proxy.newProxyInstance(
getClass().getClassLoader(), new Class[] {EntityManager.class}, new DummyInvocationHandler());
MockControl emfMc = MockControl.createControl(EntityManagerFactoryWithInfo.class);
EntityManagerFactoryWithInfo mockEmf = (EntityManagerFactoryWithInfo) emfMc.getMock();
@@ -175,7 +177,7 @@ public class PersistenceInjectionTests extends AbstractEntityManagerFactoryBeanT
}
public void testPublicExtendedPersistenceContextSetterWithOverriding() {
EntityManager mockEm2 = (EntityManager) MockControl.createControl(EntityManager.class).getMock();
EntityManager mockEm2 = MockControl.createControl(EntityManager.class).getMock();
GenericApplicationContext gac = new GenericApplicationContext();
gac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory", mockEmf);