Removed deprecated JpaTemplate and JpaInterceptor classes; dropped unused EntityManager(Factory)Plus mechanism

This commit is contained in:
Juergen Hoeller
2013-03-29 13:18:33 +01:00
parent 7ceb02257e
commit 9caa514c69
28 changed files with 90 additions and 2350 deletions

View File

@@ -1,267 +0,0 @@
/*
* Copyright 2002-2013 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.
* 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.springframework.orm.jpa;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceException;
import org.aopalliance.intercept.Interceptor;
import org.aopalliance.intercept.Invocation;
import org.aopalliance.intercept.MethodInvocation;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
/**
* @author Costin Leau
* @author Phillip Webb
*/
public class JpaInterceptorTests {
private EntityManagerFactory factory;
private EntityManager entityManager;
@Before
public void setUp() throws Exception {
factory = mock(EntityManagerFactory.class);
entityManager = mock(EntityManager.class);
}
@After
public void tearDown() throws Exception {
assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
}
@Test
public void testInterceptorWithNewEntityManager() throws PersistenceException {
given(factory.createEntityManager()).willReturn(entityManager);
given(entityManager.isOpen()).willReturn(true);
JpaInterceptor interceptor = new JpaInterceptor();
interceptor.setEntityManagerFactory(factory);
try {
interceptor.invoke(new TestInvocation(factory));
}
catch (Throwable t) {
fail("Should not have thrown Throwable: " + t.getMessage());
}
verify(entityManager).close();
}
@Test
public void testInterceptorWithNewEntityManagerAndLazyFlush() throws PersistenceException {
given(factory.createEntityManager()).willReturn(entityManager);
given(entityManager.isOpen()).willReturn(true);
JpaInterceptor interceptor = new JpaInterceptor();
interceptor.setFlushEager(false);
interceptor.setEntityManagerFactory(factory);
try {
interceptor.invoke(new TestInvocation(factory));
}
catch (Throwable t) {
fail("Should not have thrown Throwable: " + t.getMessage());
}
verify(entityManager).close();
}
@Test
public void testInterceptorWithThreadBound() {
TransactionSynchronizationManager.bindResource(factory, new EntityManagerHolder(entityManager));
JpaInterceptor interceptor = new JpaInterceptor();
interceptor.setEntityManagerFactory(factory);
try {
interceptor.invoke(new TestInvocation(factory));
}
catch (Throwable t) {
fail("Should not have thrown Throwable: " + t.getMessage());
}
finally {
TransactionSynchronizationManager.unbindResource(factory);
}
}
@Test
public void testInterceptorWithThreadBoundAndFlushEager() throws PersistenceException {
TransactionSynchronizationManager.bindResource(factory, new EntityManagerHolder(entityManager));
JpaInterceptor interceptor = new JpaInterceptor();
interceptor.setFlushEager(true);
interceptor.setEntityManagerFactory(factory);
try {
interceptor.invoke(new TestInvocation(factory));
}
catch (Throwable t) {
fail("Should not have thrown Throwable: " + t.getMessage());
}
finally {
TransactionSynchronizationManager.unbindResource(factory);
}
verify(entityManager).flush();
}
@Test
public void testInterceptorWithThreadBoundAndFlushCommit() {
TransactionSynchronizationManager.bindResource(factory, new EntityManagerHolder(entityManager));
JpaInterceptor interceptor = new JpaInterceptor();
interceptor.setFlushEager(false);
interceptor.setEntityManagerFactory(factory);
try {
interceptor.invoke(new TestInvocation(factory));
}
catch (Throwable t) {
fail("Should not have thrown Throwable: " + t.getMessage());
}
finally {
TransactionSynchronizationManager.unbindResource(factory);
}
}
@Test
public void testInterceptorWithFlushFailure() throws Throwable {
given(factory.createEntityManager()).willReturn(entityManager);
PersistenceException exception = new PersistenceException();
willThrow(exception).given(entityManager).flush();
given(entityManager.isOpen()).willReturn(true);
JpaInterceptor interceptor = new JpaInterceptor();
interceptor.setFlushEager(true);
interceptor.setEntityManagerFactory(factory);
try {
interceptor.invoke(new TestInvocation(factory));
//fail("Should have thrown JpaSystemException");
}
catch (JpaSystemException ex) {
// expected
assertEquals(exception, ex.getCause());
}
verify(entityManager).close();
}
@Test
public void testInterceptorWithFlushFailureWithoutConversion() throws Throwable {
given(factory.createEntityManager()).willReturn(entityManager);
PersistenceException exception = new PersistenceException();
willThrow(exception).given(entityManager).flush();
given(entityManager.isOpen()).willReturn(true);
JpaInterceptor interceptor = new JpaInterceptor();
interceptor.setFlushEager(true);
interceptor.setExceptionConversionEnabled(false);
interceptor.setEntityManagerFactory(factory);
try {
interceptor.invoke(new TestInvocation(factory));
//fail("Should have thrown JpaSystemException");
}
catch (PersistenceException ex) {
// expected
assertEquals(exception, ex);
}
verify(entityManager).close();
}
@SuppressWarnings("unused")
private static class TestInvocation implements MethodInvocation {
private EntityManagerFactory entityManagerFactory;
public TestInvocation(EntityManagerFactory entityManagerFactory) {
this.entityManagerFactory = entityManagerFactory;
}
@Override
public Object proceed() throws Throwable {
if (!TransactionSynchronizationManager.hasResource(this.entityManagerFactory)) {
throw new IllegalStateException("Session not bound");
}
return null;
}
public int getCurrentInterceptorIndex() {
return 0;
}
public int getNumberOfInterceptors() {
return 0;
}
public Interceptor getInterceptor(int i) {
return null;
}
@Override
public Method getMethod() {
return null;
}
@Override
public AccessibleObject getStaticPart() {
return null;
}
public Object getArgument(int i) {
return null;
}
@Override
public Object[] getArguments() {
return null;
}
public void setArgument(int i, Object handler) {
}
public int getArgumentCount() {
return 0;
}
@Override
public Object getThis() {
return null;
}
public Object getProxy() {
return null;
}
public Invocation cloneInstance() {
return null;
}
public void release() {
}
}
}

View File

@@ -1,412 +0,0 @@
/*
* Copyright 2002-2013 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.
* 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.springframework.orm.jpa;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceException;
import javax.persistence.Query;
import org.junit.Before;
import org.junit.Test;
import org.springframework.dao.DataAccessException;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
/**
* @author Costin Leau
* @author Phillip Webb
*/
public class JpaTemplateTests {
private JpaTemplate template;
private EntityManager manager;
private EntityManagerFactory factory;
@Before
public void setUp() throws Exception {
template = new JpaTemplate();
factory = mock(EntityManagerFactory.class);
manager = mock(EntityManager.class);
template.setEntityManager(manager);
template.afterPropertiesSet();
}
/*
* Test method for
* 'org.springframework.orm.jpa.JpaTemplate.JpaTemplate(EntityManagerFactory)'
*/
@Test
public void testJpaTemplateEntityManagerFactory() {
}
/*
* Test method for
* 'org.springframework.orm.jpa.JpaTemplate.JpaTemplate(EntityManager)'
*/
@Test
public void testJpaTemplateEntityManager() {
}
/*
* Test method for
* 'org.springframework.orm.jpa.JpaTemplate.execute(JpaCallback)'
*/
@Test
public void testExecuteJpaCallback() {
template.setExposeNativeEntityManager(true);
template.setEntityManager(manager);
template.afterPropertiesSet();
template.execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em) throws PersistenceException {
assertSame(em, manager);
return null;
}
});
template.setExposeNativeEntityManager(false);
template.execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em) throws PersistenceException {
assertNotSame(em, manager);
return null;
}
});
}
/*
* Test method for
* 'org.springframework.orm.jpa.JpaTemplate.executeFind(JpaCallback)'
*/
@Test
public void testExecuteFind() {
template.setEntityManager(manager);
template.setExposeNativeEntityManager(true);
template.afterPropertiesSet();
try {
template.executeFind(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em) throws PersistenceException {
assertSame(em, manager);
return new Object();
}
});
fail("should have thrown exception");
}
catch (DataAccessException e) {
// expected
}
}
/*
* Test method for
* 'org.springframework.orm.jpa.JpaTemplate.execute(JpaCallback, boolean)'
*/
@Test
public void testExecuteJpaCallbackBoolean() {
template = new JpaTemplate();
template.setExposeNativeEntityManager(false);
template.setEntityManagerFactory(factory);
template.afterPropertiesSet();
given(factory.createEntityManager()).willReturn(manager);
given(manager.isOpen()).willReturn(true);
manager.close();
template.execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em) throws PersistenceException {
assertSame(em, manager);
return null;
}
}, true);
}
@Test
public void testExecuteJpaCallbackBooleanWithPrebound() {
template.setExposeNativeEntityManager(false);
template.setEntityManagerFactory(factory);
template.afterPropertiesSet();
TransactionSynchronizationManager.bindResource(factory, new EntityManagerHolder(manager));
try {
template.execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em) throws PersistenceException {
assertSame(em, manager);
return null;
}
}, true);
}
finally {
TransactionSynchronizationManager.unbindResource(factory);
}
}
/*
* Test method for
* 'org.springframework.orm.jpa.JpaTemplate.createSharedEntityManager(EntityManager)'
*/
@Test
public void testCreateEntityManagerProxy() {
EntityManager proxy = template.createEntityManagerProxy(manager);
assertNotSame(manager, proxy);
assertFalse(manager.equals(proxy));
assertFalse(manager.hashCode() == proxy.hashCode());
// close call not propagated to the em
proxy.close();
proxy.clear();
verify(manager).clear();
}
/*
* Test method for 'org.springframework.orm.jpa.JpaTemplate.find(Class<T>,
* Object) <T>'
*/
@Test
public void testFindClassOfTObject() {
Integer result = new Integer(1);
Object id = new Object();
given(manager.find(Number.class, id)).willReturn(result);
assertSame(result, template.find(Number.class, id));
}
/*
* Test method for
* 'org.springframework.orm.jpa.JpaTemplate.getReference(Class<T>, Object)
* <T>'
*/
@Test
public void testGetReference() {
Integer reference = new Integer(1);
Object id = new Object();
given(manager.getReference(Number.class, id)).willReturn(reference);
assertSame(reference, template.getReference(Number.class, id));
}
/*
* Test method for
* 'org.springframework.orm.jpa.JpaTemplate.contains(Object)'
*/
@Test
public void testContains() {
boolean result = true;
Object entity = new Object();
given(manager.contains(entity)).willReturn(result);
assertSame(result, template.contains(entity));
}
/*
* Test method for 'org.springframework.orm.jpa.JpaTemplate.refresh(Object)'
*/
@Test
public void testRefresh() {
Object entity = new Object();
template.refresh(entity);
verify(manager).refresh(entity);
}
/*
* Test method for 'org.springframework.orm.jpa.JpaTemplate.persist(Object)'
*/
@Test
public void testPersist() {
Object entity = new Object();
template.persist(entity);
verify(manager).persist(entity);
}
/*
* Test method for 'org.springframework.orm.jpa.JpaTemplate.merge(T) <T>'
*/
@Test
public void testMerge() {
Object result = new Object();
Object entity = new Object();
given(manager.merge(entity)).willReturn(result);
assertSame(result, template.merge(entity));
}
/*
* Test method for 'org.springframework.orm.jpa.JpaTemplate.remove(Object)'
*/
@Test
public void testRemove() {
Object entity = new Object();
template.remove(entity);
verify(manager).remove(entity);
}
/*
* Test method for 'org.springframework.orm.jpa.JpaTemplate.flush()'
*/
@Test
public void testFlush() {
template.flush();
verify(manager).flush();
}
/*
* Test method for 'org.springframework.orm.jpa.JpaTemplate.find(String)'
*/
@Test
public void testFindString() {
String queryString = "some query";
Query query = mock(Query.class);
List result = new ArrayList();
given(manager.createQuery(queryString)).willReturn(query);
given(query.getResultList()).willReturn(result);
assertSame(result, template.find(queryString));
}
/*
* Test method for 'org.springframework.orm.jpa.JpaTemplate.find(String,
* Object...)'
*/
@Test
public void testFindStringObjectArray() {
String queryString = "some query";
Query query = mock(Query.class);
List result = new ArrayList();
Object param1 = new Object();
Object param2 = new Object();
Object[] params = new Object[] { param1, param2 };
given(manager.createQuery(queryString)).willReturn(query);
given(query.getResultList()).willReturn(result);
assertSame(result, template.find(queryString, params));
verify(query).setParameter(1, param1);
verify(query).setParameter(2, param2);
}
/*
* Test method for 'org.springframework.orm.jpa.JpaTemplate.find(String, Map<String,
* Object>)'
*/
@Test
public void testFindStringMapOfStringObject() {
String queryString = "some query";
Query query = mock(Query.class);
List result = new ArrayList();
Object param1 = new Object();
Object param2 = new Object();
Map<String, Object> params = new HashMap<String, Object>();
params.put("param1", param1);
params.put("param2", param2);
given(manager.createQuery(queryString)).willReturn(query);
given(query.getResultList()).willReturn(result);
assertSame(result, template.findByNamedParams(queryString, params));
verify(query).setParameter("param1", param1);
verify(query).setParameter("param2", param2);
}
/*
* Test method for
* 'org.springframework.orm.jpa.JpaTemplate.findByNamedQuery(String)'
*/
@Test
public void testFindByNamedQueryString() {
String queryName = "some query name";
Query query = mock(Query.class);
List result = new ArrayList();
given(manager.createNamedQuery(queryName)).willReturn(query);
given(query.getResultList()).willReturn(result);
assertSame(result, template.findByNamedQuery(queryName));
}
/*
* Test method for
* 'org.springframework.orm.jpa.JpaTemplate.findByNamedQuery(String,
* Object...)'
*/
@Test
public void testFindByNamedQueryStringObjectArray() {
String queryName = "some query name";
Query query = mock(Query.class);
List result = new ArrayList();
Object param1 = new Object();
Object param2 = new Object();
Object[] params = new Object[] { param1, param2 };
given(manager.createNamedQuery(queryName)).willReturn(query);
given(query.getResultList()).willReturn(result);
assertSame(result, template.findByNamedQuery(queryName, params));
verify(query).setParameter(1, param1);
verify(query).setParameter(2, param2);
}
/*
* Test method for
* 'org.springframework.orm.jpa.JpaTemplate.findByNamedQuery(String, Map<String,
* Object>)'
*/
@Test
public void testFindByNamedQueryStringMapOfStringObject() {
String queryName = "some query name";
Query query = mock(Query.class);
List result = new ArrayList();
Object param1 = new Object();
Object param2 = new Object();
Map<String, Object> params = new HashMap<String, Object>();
params.put("param1", param1);
params.put("param2", param2);
given(manager.createNamedQuery(queryName)).willReturn(query);
given(query.getResultList()).willReturn(result);
assertSame(result, template.findByNamedQueryAndNamedParams(queryName, params));
verify(query).setParameter("param1", param1);
verify(query).setParameter("param2", param2);
}
}

View File

@@ -58,8 +58,6 @@ public class JpaTransactionManagerTests {
private JpaTransactionManager transactionManager;
private JpaTemplate template;
private TransactionTemplate tt;
@@ -70,8 +68,6 @@ public class JpaTransactionManagerTests {
tx = mock(EntityTransaction.class);
transactionManager = new JpaTransactionManager(factory);
template = new JpaTemplate(factory);
template.afterPropertiesSet();
tt = new TransactionTemplate(transactionManager);
given(factory.createEntityManager()).willReturn(manager);
@@ -101,13 +97,8 @@ public class JpaTransactionManagerTests {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.hasResource(factory));
return template.execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em) {
em.flush();
return l;
}
});
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
return l;
}
});
assertSame(l, result);
@@ -137,13 +128,8 @@ public class JpaTransactionManagerTests {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.hasResource(factory));
return template.execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em) {
em.flush();
return l;
}
});
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
return l;
}
});
assertSame(l, result);
@@ -176,12 +162,8 @@ public class JpaTransactionManagerTests {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.hasResource(factory));
return template.execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em) {
throw new RuntimeException("some exception");
}
});
EntityManagerFactoryUtils.getTransactionalEntityManager(factory);
throw new RuntimeException("some exception");
}
});
fail("Should have propagated RuntimeException");
@@ -213,12 +195,8 @@ public class JpaTransactionManagerTests {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.hasResource(factory));
return template.execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em) {
throw new RuntimeException("some exception");
}
});
EntityManagerFactoryUtils.getTransactionalEntityManager(factory);
throw new RuntimeException("some exception");
}
});
fail("Should have propagated RuntimeException");
@@ -249,16 +227,10 @@ public class JpaTransactionManagerTests {
public Object doInTransaction(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.hasResource(factory));
Object res = template.execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em) {
em.flush();
return l;
}
});
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
status.setRollbackOnly();
return res;
return l;
}
});
@@ -288,14 +260,8 @@ public class JpaTransactionManagerTests {
return tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
return template.execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em) {
em.flush();
return l;
}
});
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
return l;
}
});
}
@@ -328,12 +294,8 @@ public class JpaTransactionManagerTests {
return tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
return template.execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em) {
throw new RuntimeException("exception");
}
});
EntityManagerFactoryUtils.getTransactionalEntityManager(factory);
throw new RuntimeException("some exception");
}
});
}
@@ -374,15 +336,7 @@ public class JpaTransactionManagerTests {
return tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
template.execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em2) {
em2.flush();
return l;
}
});
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
status.setRollbackOnly();
return null;
}
@@ -425,13 +379,8 @@ public class JpaTransactionManagerTests {
return tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
return template.execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em2) {
em2.flush();
return l;
}
});
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
return l;
}
});
}
@@ -464,25 +413,14 @@ public class JpaTransactionManagerTests {
Object result = tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
JpaTemplate template2 = new JpaTemplate(factory);
template2.execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em) throws PersistenceException {
return null;
}
});
EntityManagerFactoryUtils.getTransactionalEntityManager(factory);
assertTrue(TransactionSynchronizationManager.hasResource(factory));
return tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
return template.execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em2) {
em2.flush();
return l;
}
});
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
return l;
}
});
}
@@ -523,13 +461,8 @@ public class JpaTransactionManagerTests {
return tt2.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
return template.execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em2) {
em2.flush();
return l;
}
});
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
return l;
}
});
}
@@ -561,13 +494,7 @@ public class JpaTransactionManagerTests {
Object result = tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
JpaTemplate template2 = new JpaTemplate(factory);
template2.execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em) throws PersistenceException {
return null;
}
});
EntityManagerFactoryUtils.getTransactionalEntityManager(factory);
assertTrue(TransactionSynchronizationManager.hasResource(factory));
TransactionTemplate tt2 = new TransactionTemplate(transactionManager);
@@ -575,13 +502,8 @@ public class JpaTransactionManagerTests {
return tt2.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
return template.execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em2) {
em2.flush();
return l;
}
});
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
return l;
}
});
}
@@ -614,26 +536,15 @@ public class JpaTransactionManagerTests {
tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
template.execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em2) {
em2.flush();
return null;
}
});
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCompletion(int status) {
tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
return template.execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em2) {
em2.flush();
return null;
}
});
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
return null;
}
});
}
@@ -672,13 +583,8 @@ public class JpaTransactionManagerTests {
assertTrue(!TransactionSynchronizationManager.hasResource(factory));
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
assertTrue(!status.isNewTransaction());
return template.execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em) {
em.flush();
return l;
}
});
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
return l;
}
});
assertSame(l, result);
@@ -705,13 +611,7 @@ public class JpaTransactionManagerTests {
assertTrue(!TransactionSynchronizationManager.hasResource(factory));
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
assertTrue(!status.isNewTransaction());
template.execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em) {
em.flush();
return null;
}
});
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
status.setRollbackOnly();
return null;
}
@@ -741,12 +641,8 @@ public class JpaTransactionManagerTests {
public Object doInTransaction(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.hasResource(factory));
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
return template.execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em) {
return l;
}
});
EntityManagerFactoryUtils.getTransactionalEntityManager(factory);
return l;
}
});
assertSame(l, result);
@@ -778,12 +674,7 @@ public class JpaTransactionManagerTests {
public Object doInTransaction(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.hasResource(factory));
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
template.execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em) {
return null;
}
});
EntityManagerFactoryUtils.getTransactionalEntityManager(factory);
status.setRollbackOnly();
return null;
}
@@ -820,13 +711,8 @@ public class JpaTransactionManagerTests {
assertTrue(TransactionSynchronizationManager.hasResource(factory));
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
assertTrue(!status.isNewTransaction());
return template.execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em) {
em.flush();
return l;
}
});
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
return l;
}
});
assertSame(l, result);
@@ -858,13 +744,7 @@ public class JpaTransactionManagerTests {
assertTrue(TransactionSynchronizationManager.hasResource(factory));
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
assertTrue(!status.isNewTransaction());
template.execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em) {
em.flush();
return null;
}
});
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
status.setRollbackOnly();
return null;
}
@@ -900,13 +780,8 @@ public class JpaTransactionManagerTests {
public Object doInTransaction(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.hasResource(factory));
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
return template.execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em) {
em.flush();
return l;
}
});
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
return l;
}
});

View File

@@ -1,101 +0,0 @@
/*
* Copyright 2002-2013 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.
* 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.springframework.orm.jpa.support;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.junit.Test;
import org.springframework.orm.jpa.JpaTemplate;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
/**
* @author Costin Leau
* @author Phillip Webb
*/
public class JpaDaoSupportTests {
@Test
public void testJpaDaoSupportWithEntityManager() throws Exception {
EntityManager entityManager = mock(EntityManager.class);
final List test = new ArrayList();
JpaDaoSupport dao = new JpaDaoSupport() {
@Override
protected void initDao() {
test.add("test");
}
};
dao.setEntityManager(entityManager);
dao.afterPropertiesSet();
assertNotNull("jpa template not created", dao.getJpaTemplate());
assertEquals("incorrect entity manager", entityManager, dao.getJpaTemplate().getEntityManager());
assertEquals("initDao not called", test.size(), 1);
}
@Test
public void testJpaDaoSupportWithEntityManagerFactory() throws Exception {
EntityManagerFactory entityManagerFactory = mock(EntityManagerFactory.class);
final List test = new ArrayList();
JpaDaoSupport dao = new JpaDaoSupport() {
@Override
protected void initDao() {
test.add("test");
}
};
dao.setEntityManagerFactory(entityManagerFactory);
dao.afterPropertiesSet();
assertNotNull("jpa template not created", dao.getJpaTemplate());
assertEquals("incorrect entity manager factory", entityManagerFactory,
dao.getJpaTemplate().getEntityManagerFactory());
assertEquals("initDao not called", test.size(), 1);
}
@Test
public void testJpaDaoSupportWithJpaTemplate() throws Exception {
JpaTemplate template = new JpaTemplate();
final List test = new ArrayList();
JpaDaoSupport dao = new JpaDaoSupport() {
@Override
protected void initDao() {
test.add("test");
}
};
dao.setJpaTemplate(template);
dao.afterPropertiesSet();
assertNotNull("jpa template not created", dao.getJpaTemplate());
assertEquals("incorrect JpaTemplate", template, dao.getJpaTemplate());
assertEquals("initDao not called", test.size(), 1);
}
@Test
public void testInvalidJpaTemplate() throws Exception {
JpaDaoSupport dao = new JpaDaoSupport() {
};
try {
dao.afterPropertiesSet();
fail("expected exception");
}
catch (IllegalArgumentException iae) {
// okay
}
}
}

View File

@@ -19,7 +19,6 @@ package org.springframework.orm.jpa.support;
import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.servlet.FilterChain;
@@ -30,13 +29,13 @@ import javax.servlet.ServletResponse;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.mock.web.test.MockFilterConfig;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
import org.springframework.mock.web.test.MockServletContext;
import org.springframework.mock.web.test.PassThroughFilterChain;
import org.springframework.orm.jpa.JpaTemplate;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.ServletWebRequest;
@@ -60,17 +59,12 @@ public class OpenEntityManagerInViewTests {
private EntityManagerFactory factory;
private JpaTemplate template;
@Before
public void setUp() throws Exception {
factory = mock(EntityManagerFactory.class);
manager = mock(EntityManager.class);
template = new JpaTemplate(factory);
template.afterPropertiesSet();
given(factory.createEntityManager()).willReturn(manager);
}