From: Juergen H. <jho...@us...> - 2008-10-20 22:10:42
|
Update of /cvsroot/springframework/spring/tiger/test/org/springframework/orm/jpa In directory fdv4jf1.ch3.sourceforge.com:/tmp/cvs-serv12809/tiger/test/org/springframework/orm/jpa Modified Files: AbstractContainerEntityManagerFactoryIntegrationTests.java Log Message: @PersistenceContext of type TRANSACTION allows returned Query objects to be parameterized and executed as well Index: AbstractContainerEntityManagerFactoryIntegrationTests.java =================================================================== RCS file: /cvsroot/springframework/spring/tiger/test/org/springframework/orm/jpa/AbstractContainerEntityManagerFactoryIntegrationTests.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** AbstractContainerEntityManagerFactoryIntegrationTests.java 14 Jul 2008 12:48:28 -0000 1.8 --- AbstractContainerEntityManagerFactoryIntegrationTests.java 20 Oct 2008 22:10:36 -0000 1.9 *************** *** 22,25 **** --- 22,27 ---- import javax.persistence.EntityManager; import javax.persistence.EntityNotFoundException; + import javax.persistence.FlushModeType; + import javax.persistence.NoResultException; import javax.persistence.Query; *************** *** 30,34 **** import org.springframework.test.annotation.Repeat; import org.springframework.test.annotation.Timed; - import org.springframework.transaction.annotation.Transactional; /** --- 32,35 ---- *************** *** 41,45 **** public abstract class AbstractContainerEntityManagerFactoryIntegrationTests extends AbstractEntityManagerFactoryIntegrationTests { ! @NotTransactional public void testEntityManagerFactoryImplementsEntityManagerFactoryInfo() { --- 42,46 ---- public abstract class AbstractContainerEntityManagerFactoryIntegrationTests extends AbstractEntityManagerFactoryIntegrationTests { ! @NotTransactional public void testEntityManagerFactoryImplementsEntityManagerFactoryInfo() { *************** *** 57,66 **** 0, countRowsInTable("person")); } ! @Repeat(5) public void testJdbcTx1() throws Exception { testJdbcTx2(); } ! @Timed(millis=273) public void testJdbcTx2() throws InterruptedException { --- 58,67 ---- 0, countRowsInTable("person")); } ! @Repeat(5) public void testJdbcTx1() throws Exception { testJdbcTx2(); } ! @Timed(millis=273) public void testJdbcTx2() throws InterruptedException { *************** *** 70,75 **** executeSqlScript("/sql/insertPerson.sql", false); } ! ! @Transactional(readOnly=true) public void testEntityManagerProxyIsProxy() { assertTrue(Proxy.isProxyClass(sharedEntityManager.getClass())); --- 71,76 ---- executeSqlScript("/sql/insertPerson.sql", false); } ! ! //@NotTransactional public void testEntityManagerProxyIsProxy() { assertTrue(Proxy.isProxyClass(sharedEntityManager.getClass())); *************** *** 88,92 **** query.executeUpdate(); } ! @ExpectedException(EntityNotFoundException.class) public void testGetReferenceWhenNoRow() { --- 89,93 ---- query.executeUpdate(); } ! @ExpectedException(EntityNotFoundException.class) public void testGetReferenceWhenNoRow() { *************** *** 191,194 **** --- 192,257 ---- List<Person> people = q.getResultList(); assertEquals(0, people.size()); + try { + assertNull(q.getSingleResult()); + fail("Should have thrown NoResultException"); + } + catch (NoResultException ex) { + // expected + } + } + + @NotTransactional + public void testQueryNoPersonsNotTransactional() { + EntityManager em = entityManagerFactory.createEntityManager(); + Query q = em.createQuery("select p from Person as p"); + List<Person> people = q.getResultList(); + assertEquals(0, people.size()); + try { + assertNull(q.getSingleResult()); + fail("Should have thrown NoResultException"); + } + catch (NoResultException ex) { + // expected + } + } + + public void testQueryNoPersonsShared() { + EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(entityManagerFactory); + Query q = em.createQuery("select p from Person as p"); + q.setFlushMode(FlushModeType.AUTO); + List<Person> people = q.getResultList(); + try { + assertNull(q.getSingleResult()); + fail("Should have thrown NoResultException"); + } + catch (NoResultException ex) { + // expected + } + } + + @NotTransactional + public void testQueryNoPersonsSharedNotTransactional() { + EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(entityManagerFactory); + Query q = em.createQuery("select p from Person as p"); + q.setFlushMode(FlushModeType.AUTO); + List<Person> people = q.getResultList(); + assertEquals(0, people.size()); + try { + assertNull(q.getSingleResult()); + fail("Should have thrown IllegalStateException"); + } + catch (Exception ex) { + // IllegalStateException expected, but PersistenceException thrown by Hibernate + assertTrue(ex.getMessage().indexOf("closed") != -1); + } + q = em.createQuery("select p from Person as p"); + q.setFlushMode(FlushModeType.AUTO); + try { + assertNull(q.getSingleResult()); + fail("Should have thrown NoResultException"); + } + catch (NoResultException ex) { + // expected + } } |