From 17bb793f8c8cea60eca512ce4e2c5536aecb55af Mon Sep 17 00:00:00 2001 From: Ben Hale Date: Mon, 13 Aug 2007 15:50:58 +0000 Subject: [PATCH] RESOLVED - issue SWF-332: Test Lazy Loading behavior from views with Hibernate and JPA conversational persistence implementation http://opensource.atlassian.com/projects/spring/browse/SWF-332 --- .../HibernateFlowExecutionListenerTests.java | 31 ++++++++++++++++-- .../JpaFlowExecutionListenerTests.java | 26 +++++++++++++++ .../support/persistence/TestAddress.hbm.xml | 13 ++++++++ .../support/persistence/TestAddress.java | 25 +++++++++++++++ .../support/persistence/TestBean.hbm.xml | 4 +++ .../webflow/support/persistence/TestBean.java | 13 ++++++++ .../webflow/support/persistence/orm.xml | 32 +++++++++++++++---- 7 files changed, 135 insertions(+), 9 deletions(-) create mode 100644 spring-webflow/src/test/java/org/springframework/webflow/support/persistence/TestAddress.hbm.xml create mode 100644 spring-webflow/src/test/java/org/springframework/webflow/support/persistence/TestAddress.java diff --git a/spring-webflow/src/test/java/org/springframework/webflow/support/persistence/HibernateFlowExecutionListenerTests.java b/spring-webflow/src/test/java/org/springframework/webflow/support/persistence/HibernateFlowExecutionListenerTests.java index c4c824bc..0274a52e 100644 --- a/spring-webflow/src/test/java/org/springframework/webflow/support/persistence/HibernateFlowExecutionListenerTests.java +++ b/spring-webflow/src/test/java/org/springframework/webflow/support/persistence/HibernateFlowExecutionListenerTests.java @@ -22,6 +22,7 @@ import javax.sql.DataSource; import junit.framework.TestCase; +import org.hibernate.Hibernate; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; @@ -228,6 +229,22 @@ public class HibernateFlowExecutionListenerTests extends TestCase { assertSessionNotBound(); } + public void testLazyInitializedCollection() { + MockRequestContext context = new MockRequestContext(); + MockFlowSession flowSession = new MockFlowSession(); + flowSession.getDefinitionInternal().getAttributeMap().put("persistenceContext", "true"); + listener.sessionCreated(context, flowSession); + context.setActiveSession(flowSession); + assertSessionBound(); + + TestBean bean = (TestBean) hibernateTemplate.get(TestBean.class, Long.valueOf(0)); + assertFalse("addresses should not be initialized", Hibernate.isInitialized(bean.getAddresses())); + listener.paused(context, ViewSelection.NULL_VIEW); + assertFalse("addresses should not be initialized", Hibernate.isInitialized(bean.getAddresses())); + Hibernate.initialize(bean.getAddresses()); + assertTrue("addresses should be initialized", Hibernate.isInitialized(bean.getAddresses())); + } + private DataSource getDataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("org.hsqldb.jdbcDriver"); @@ -241,10 +258,19 @@ public class HibernateFlowExecutionListenerTests extends TestCase { Connection connection = null; try { connection = dataSource.getConnection(); + connection.createStatement().execute("drop table T_ADDRESS if exists;"); connection.createStatement().execute("drop table T_BEAN if exists;"); connection.createStatement().execute( "create table T_BEAN (ID integer primary key, NAME varchar(50) not null);"); + connection.createStatement().execute( + "create table T_ADDRESS (ID integer primary key, BEAN_ID integer, VALUE varchar(50) not null);"); + connection + .createStatement() + .execute( + "alter table T_ADDRESS add constraint FK_BEAN_ADDRESS foreign key (BEAN_ID) references T_BEAN(ID) on delete cascade"); connection.createStatement().execute("insert into T_BEAN (ID, NAME) values (0, 'Ben Hale');"); + connection.createStatement().execute( + "insert into T_ADDRESS (ID, BEAN_ID, VALUE) values (0, 0, 'Melbourne')"); } catch (SQLException e) { throw new RuntimeException("SQL exception occurred acquiring connection", e); } finally { @@ -260,8 +286,9 @@ public class HibernateFlowExecutionListenerTests extends TestCase { private SessionFactory getSessionFactory(DataSource dataSource) throws Exception { LocalSessionFactoryBean factory = new LocalSessionFactoryBean(); factory.setDataSource(dataSource); - factory.setMappingLocations(new Resource[] { new ClassPathResource( - "org/springframework/webflow/support/persistence/TestBean.hbm.xml") }); + factory.setMappingLocations(new Resource[] { + new ClassPathResource("org/springframework/webflow/support/persistence/TestBean.hbm.xml"), + new ClassPathResource("org/springframework/webflow/support/persistence/TestAddress.hbm.xml") }); factory.afterPropertiesSet(); return (SessionFactory) factory.getObject(); } diff --git a/spring-webflow/src/test/java/org/springframework/webflow/support/persistence/JpaFlowExecutionListenerTests.java b/spring-webflow/src/test/java/org/springframework/webflow/support/persistence/JpaFlowExecutionListenerTests.java index c96d2f65..551e64b4 100644 --- a/spring-webflow/src/test/java/org/springframework/webflow/support/persistence/JpaFlowExecutionListenerTests.java +++ b/spring-webflow/src/test/java/org/springframework/webflow/support/persistence/JpaFlowExecutionListenerTests.java @@ -8,6 +8,7 @@ import javax.sql.DataSource; import junit.framework.TestCase; +import org.hibernate.Hibernate; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.orm.jpa.JpaTemplate; @@ -108,6 +109,22 @@ public class JpaFlowExecutionListenerTests extends TestCase { } + public void testLazilyInitalizedCollection() { + MockRequestContext context = new MockRequestContext(); + MockFlowSession flowSession = new MockFlowSession(); + flowSession.getDefinitionInternal().getAttributeMap().put("persistenceContext", "true"); + jpaListener.sessionCreated(context, flowSession); + context.setActiveSession(flowSession); + assertSessionBound(); + + TestBean bean = (TestBean) jpaTemplate.getReference(TestBean.class, Long.valueOf(0)); + assertFalse("addresses should not be initialized", Hibernate.isInitialized(bean.getAddresses())); + jpaListener.paused(context, ViewSelection.NULL_VIEW); + assertFalse("addresses should not be initialized", Hibernate.isInitialized(bean.getAddresses())); + Hibernate.initialize(bean.getAddresses()); + assertTrue("addresses should be initialized", Hibernate.isInitialized(bean.getAddresses())); + } + private DataSource getDataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("org.hsqldb.jdbcDriver"); @@ -121,10 +138,19 @@ public class JpaFlowExecutionListenerTests extends TestCase { Connection connection = null; try { connection = dataSource.getConnection(); + connection.createStatement().execute("drop table T_ADDRESS if exists;"); connection.createStatement().execute("drop table T_BEAN if exists;"); connection.createStatement().execute( "create table T_BEAN (ID integer primary key, NAME varchar(50) not null);"); + connection.createStatement().execute( + "create table T_ADDRESS (ID integer primary key, BEAN_ID integer, VALUE varchar(50) not null);"); + connection + .createStatement() + .execute( + "alter table T_ADDRESS add constraint FK_BEAN_ADDRESS foreign key (BEAN_ID) references T_BEAN(ID) on delete cascade"); connection.createStatement().execute("insert into T_BEAN (ID, NAME) values (0, 'Ben Hale');"); + connection.createStatement().execute( + "insert into T_ADDRESS (ID, BEAN_ID, VALUE) values (0, 0, 'Melbourne')"); } catch (SQLException e) { throw new RuntimeException("SQL exception occurred acquiring connection", e); } finally { diff --git a/spring-webflow/src/test/java/org/springframework/webflow/support/persistence/TestAddress.hbm.xml b/spring-webflow/src/test/java/org/springframework/webflow/support/persistence/TestAddress.hbm.xml new file mode 100644 index 00000000..f89463ae --- /dev/null +++ b/spring-webflow/src/test/java/org/springframework/webflow/support/persistence/TestAddress.hbm.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/spring-webflow/src/test/java/org/springframework/webflow/support/persistence/TestAddress.java b/spring-webflow/src/test/java/org/springframework/webflow/support/persistence/TestAddress.java new file mode 100644 index 00000000..982bd43d --- /dev/null +++ b/spring-webflow/src/test/java/org/springframework/webflow/support/persistence/TestAddress.java @@ -0,0 +1,25 @@ +package org.springframework.webflow.support.persistence; + +public class TestAddress { + + private Long entityId; + + private String value; + + public Long getEntityId() { + return entityId; + } + + public void setEntityId(Long entityId) { + this.entityId = entityId; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + +} diff --git a/spring-webflow/src/test/java/org/springframework/webflow/support/persistence/TestBean.hbm.xml b/spring-webflow/src/test/java/org/springframework/webflow/support/persistence/TestBean.hbm.xml index be154398..e4ba7c12 100644 --- a/spring-webflow/src/test/java/org/springframework/webflow/support/persistence/TestBean.hbm.xml +++ b/spring-webflow/src/test/java/org/springframework/webflow/support/persistence/TestBean.hbm.xml @@ -9,5 +9,9 @@ + + + + \ No newline at end of file diff --git a/spring-webflow/src/test/java/org/springframework/webflow/support/persistence/TestBean.java b/spring-webflow/src/test/java/org/springframework/webflow/support/persistence/TestBean.java index 083b571a..16d73e0e 100644 --- a/spring-webflow/src/test/java/org/springframework/webflow/support/persistence/TestBean.java +++ b/spring-webflow/src/test/java/org/springframework/webflow/support/persistence/TestBean.java @@ -15,12 +15,17 @@ */ package org.springframework.webflow.support.persistence; +import java.util.HashSet; +import java.util.Set; + public class TestBean { private Long entityId; private String name; + private Set addresses = new HashSet(); + public TestBean() { } @@ -49,4 +54,12 @@ public class TestBean { public String getName() { return name; } + + public Set getAddresses() { + return addresses; + } + + public void setAddresses(Set addresses) { + this.addresses = addresses; + } } diff --git a/spring-webflow/src/test/java/org/springframework/webflow/support/persistence/orm.xml b/spring-webflow/src/test/java/org/springframework/webflow/support/persistence/orm.xml index 2d16749b..008b25d9 100644 --- a/spring-webflow/src/test/java/org/springframework/webflow/support/persistence/orm.xml +++ b/spring-webflow/src/test/java/org/springframework/webflow/support/persistence/orm.xml @@ -1,17 +1,35 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd" + version="1.0"> + + org.springframework.webflow.support.persistence + - - + +
- + - + + + + + + + + + +
+ + + + + +