diff --git a/spring-webflow/src/test/java/org/springframework/webflow/persistence/HibernateFlowExecutionListenerTests.java b/spring-webflow/src/test/java/org/springframework/webflow/persistence/HibernateFlowExecutionListenerTests.java index a483bc1c..d531044d 100644 --- a/spring-webflow/src/test/java/org/springframework/webflow/persistence/HibernateFlowExecutionListenerTests.java +++ b/spring-webflow/src/test/java/org/springframework/webflow/persistence/HibernateFlowExecutionListenerTests.java @@ -17,27 +17,22 @@ package org.springframework.webflow.persistence; import javax.sql.DataSource; -import junit.framework.TestCase; - import org.hibernate.Hibernate; import org.hibernate.Session; -import org.hibernate.SessionFactory; import org.springframework.core.io.ClassPathResource; -import org.springframework.core.io.Resource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.jdbc.datasource.init.DataSourceInitializer; import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; -import org.springframework.orm.hibernate4.HibernateCallback; -import org.springframework.orm.hibernate4.HibernateTemplate; -import org.springframework.orm.hibernate4.HibernateTransactionManager; -import org.springframework.orm.hibernate4.LocalSessionFactoryBean; import org.springframework.transaction.support.TransactionSynchronizationManager; import org.springframework.webflow.engine.EndState; import org.springframework.webflow.execution.FlowExecutionException; +import org.springframework.webflow.persistence.HibernateHandler.SessionCallback; import org.springframework.webflow.test.MockFlowSession; import org.springframework.webflow.test.MockRequestContext; +import junit.framework.TestCase; + /** * Tests for {@link HibernateFlowExecutionListener} * @@ -45,23 +40,18 @@ import org.springframework.webflow.test.MockRequestContext; */ public class HibernateFlowExecutionListenerTests extends TestCase { - private SessionFactory sessionFactory; + private HibernateHandler hibernate; private JdbcTemplate jdbcTemplate; - private HibernateTemplate hibernateTemplate; - private HibernateFlowExecutionListener hibernateListener; protected void setUp() throws Exception { DataSource dataSource = getDataSource(); populateDataBase(dataSource); jdbcTemplate = new JdbcTemplate(dataSource); - sessionFactory = getSessionFactory(dataSource); - hibernateTemplate = new HibernateTemplate(sessionFactory); - hibernateTemplate.setCheckWriteOperations(false); - HibernateTransactionManager tm = new HibernateTransactionManager(sessionFactory); - hibernateListener = new HibernateFlowExecutionListener(sessionFactory, tm); + hibernate = HibernateHandlerFactory.create(dataSource); + hibernateListener = new HibernateFlowExecutionListener(hibernate.getSessionFactory(), hibernate.getTransactionManager()); } public void testSameSession() { @@ -82,10 +72,10 @@ public class HibernateFlowExecutionListenerTests extends TestCase { hibernateListener.resuming(context); assertSessionBound(); - hibernateTemplate.executeWithNativeSession(new HibernateCallback() { - public Object doInHibernate(Session session) { + hibernate.templateExecuteWithNativeSession(new SessionCallback() { + @Override + public void doWithSession(Session session) { assertSame("Should have been original instance", hibSession, session); - return null; } }); hibernateListener.paused(context); @@ -109,7 +99,7 @@ public class HibernateFlowExecutionListenerTests extends TestCase { assertSessionBound(); TestBean bean = new TestBean("Keith Donald"); - hibernateTemplate.save(bean); + hibernate.templateSave(bean); assertEquals("Table should still only have one row", 1, (int)jdbcTemplate.queryForObject("select count(*) from T_BEAN", Integer.class)); EndState endState = new EndState(flowSession.getDefinitionInternal(), "success"); @@ -132,14 +122,14 @@ public class HibernateFlowExecutionListenerTests extends TestCase { assertSessionBound(); TestBean bean1 = new TestBean("Keith Donald"); - hibernateTemplate.save(bean1); + hibernate.templateSave(bean1); assertEquals("Table should still only have one row", 1, (int)jdbcTemplate.queryForObject("select count(*) from T_BEAN", Integer.class)); hibernateListener.paused(context); assertSessionNotBound(); hibernateListener.resuming(context); TestBean bean2 = new TestBean("Keith Donald"); - hibernateTemplate.save(bean2); + hibernate.templateSave(bean2); assertEquals("Table should still only have one row", 1, (int)jdbcTemplate.queryForObject("select count(*) from T_BEAN", Integer.class)); assertSessionBound(); @@ -164,7 +154,7 @@ public class HibernateFlowExecutionListenerTests extends TestCase { assertSessionBound(); TestBean bean = new TestBean("Keith Donald"); - hibernateTemplate.save(bean); + hibernate.templateSave(bean); assertEquals("Table should still only have one row", 1, (int)jdbcTemplate.queryForObject("select count(*) from T_BEAN", Integer.class)); EndState endState = new EndState(flowSession.getDefinitionInternal(), "cancel"); @@ -205,7 +195,7 @@ public class HibernateFlowExecutionListenerTests extends TestCase { assertSessionBound(); TestBean bean1 = new TestBean("Keith Donald"); - hibernateTemplate.save(bean1); + hibernate.templateSave(bean1); assertEquals("Table should still only have one row", 1, (int)jdbcTemplate.queryForObject("select count(*) from T_BEAN", Integer.class)); hibernateListener.exceptionThrown(context, new FlowExecutionException("bla", "bla", "bla")); assertEquals("Table should still only have one row", 1, (int)jdbcTemplate.queryForObject("select count(*) from T_BEAN", Integer.class)); @@ -231,7 +221,7 @@ public class HibernateFlowExecutionListenerTests extends TestCase { context.setActiveSession(flowSession); assertSessionBound(); - TestBean bean = hibernateTemplate.get(TestBean.class, Long.valueOf(0)); + TestBean bean = hibernate.templateGet(TestBean.class, Long.valueOf(0)); assertFalse("addresses should not be initialized", Hibernate.isInitialized(bean.getAddresses())); hibernateListener.paused(context); assertFalse("addresses should not be initialized", Hibernate.isInitialized(bean.getAddresses())); @@ -250,29 +240,19 @@ public class HibernateFlowExecutionListenerTests extends TestCase { private void populateDataBase(DataSource dataSource) throws Exception { ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(); - databasePopulator.addScript(new ClassPathResource("test-data.sql", this.getClass())); + databasePopulator.addScript(new ClassPathResource("test-data.sql", getClass())); DataSourceInitializer initializer = new DataSourceInitializer(); initializer.setDataSource(dataSource); initializer.setDatabasePopulator(databasePopulator); initializer.afterPropertiesSet(); } - private SessionFactory getSessionFactory(DataSource dataSource) throws Exception { - LocalSessionFactoryBean factory = new LocalSessionFactoryBean(); - factory.setDataSource(dataSource); - factory.setMappingLocations(new Resource[] { - new ClassPathResource("org/springframework/webflow/persistence/TestBean.hbm.xml"), - new ClassPathResource("org/springframework/webflow/persistence/TestAddress.hbm.xml") }); - factory.afterPropertiesSet(); - return factory.getObject(); - } - private void assertSessionNotBound() { - assertNull(TransactionSynchronizationManager.getResource(sessionFactory)); + assertNull(TransactionSynchronizationManager.getResource(hibernate.getSessionFactory())); } private void assertSessionBound() { - assertNotNull(TransactionSynchronizationManager.getResource(sessionFactory)); + assertNotNull(TransactionSynchronizationManager.getResource(hibernate.getSessionFactory())); } } diff --git a/spring-webflow/src/test/java/org/springframework/webflow/persistence/HibernateHandler.java b/spring-webflow/src/test/java/org/springframework/webflow/persistence/HibernateHandler.java new file mode 100644 index 00000000..eb8592d5 --- /dev/null +++ b/spring-webflow/src/test/java/org/springframework/webflow/persistence/HibernateHandler.java @@ -0,0 +1,42 @@ +/* + * Copyright 2004-2016 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.webflow.persistence; + +import java.io.Serializable; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.springframework.transaction.PlatformTransactionManager; + +interface HibernateHandler { + + void templateSave(Object entity); + + T templateGet(Class entityClass, Serializable id); + + PlatformTransactionManager getTransactionManager(); + + SessionFactory getSessionFactory(); + + void templateExecuteWithNativeSession(SessionCallback callback); + + interface SessionCallback { + + void doWithSession(Session session); + + } + +} diff --git a/spring-webflow/src/test/java/org/springframework/webflow/persistence/HibernateHandlerFactory.java b/spring-webflow/src/test/java/org/springframework/webflow/persistence/HibernateHandlerFactory.java new file mode 100644 index 00000000..07fd7d89 --- /dev/null +++ b/spring-webflow/src/test/java/org/springframework/webflow/persistence/HibernateHandlerFactory.java @@ -0,0 +1,152 @@ +/* + * Copyright 2004-2016 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.webflow.persistence; + +import java.io.Serializable; + +import javax.sql.DataSource; + +import org.hibernate.HibernateException; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.orm.hibernate4.HibernateCallback; +import org.springframework.orm.hibernate4.HibernateTemplate; +import org.springframework.orm.hibernate4.HibernateTransactionManager; +import org.springframework.orm.hibernate4.LocalSessionFactoryBean; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.util.ClassUtils; + +public class HibernateHandlerFactory { + + static HibernateHandler create(DataSource dataSource) throws Exception { + if (ClassUtils.isPresent("org.hibernate.engine.transaction.spi.TransactionContext", + HibernateHandlerFactory.class.getClassLoader())) { + return new Hibernate4Handler(dataSource); + } + return new Hibernate5Handler(dataSource); + } + + private static class Hibernate4Handler implements HibernateHandler { + + private final HibernateTemplate template; + + private final PlatformTransactionManager tranasactionManager; + + private final SessionFactory sessionFactory; + + private Hibernate4Handler(DataSource dataSource) throws Exception { + sessionFactory = createSessionFactory(dataSource); + template = new HibernateTemplate(sessionFactory); + template.setCheckWriteOperations(false); + tranasactionManager = new HibernateTransactionManager(sessionFactory); + } + + public void templateSave(Object entity) { + template.save(entity); + } + + public T templateGet(Class entityClass, Serializable id) { + return template.get(entityClass, id); + } + + public void templateExecuteWithNativeSession(final SessionCallback callback) { + template.executeWithNativeSession(new HibernateCallback() { + + @Override + public Void doInHibernate(Session session) throws HibernateException { + callback.doWithSession(session); + return null; + } + }); + } + + public PlatformTransactionManager getTransactionManager() { + return tranasactionManager; + } + + public SessionFactory getSessionFactory() { + return sessionFactory; + } + + private static SessionFactory createSessionFactory(DataSource dataSource) throws Exception { + LocalSessionFactoryBean factory = new LocalSessionFactoryBean(); + factory.setDataSource(dataSource); + factory.setMappingLocations(new Resource[] { + new ClassPathResource("org/springframework/webflow/persistence/TestBean.hbm.xml"), + new ClassPathResource("org/springframework/webflow/persistence/TestAddress.hbm.xml") }); + factory.afterPropertiesSet(); + return factory.getObject(); + } + + } + + private static class Hibernate5Handler implements HibernateHandler { + + private final org.springframework.orm.hibernate5.HibernateTemplate template; + + private final PlatformTransactionManager tranasactionManager; + + private final SessionFactory sessionFactory; + + private Hibernate5Handler(DataSource dataSource) throws Exception { + sessionFactory = getSessionFactory(dataSource); + template = new org.springframework.orm.hibernate5.HibernateTemplate(sessionFactory); + template.setCheckWriteOperations(false); + tranasactionManager = new org.springframework.orm.hibernate5.HibernateTransactionManager(sessionFactory); + } + + public void templateSave(Object entity) { + template.save(entity); + } + + public T templateGet(Class entityClass, Serializable id) { + return template.get(entityClass, id); + } + + public void templateExecuteWithNativeSession(final SessionCallback callback) { + template.executeWithNativeSession(new org.springframework.orm.hibernate5.HibernateCallback() { + + @Override + public Void doInHibernate(Session session) throws HibernateException { + callback.doWithSession(session); + return null; + } + }); + } + + public PlatformTransactionManager getTransactionManager() { + return tranasactionManager; + } + + public SessionFactory getSessionFactory() { + return sessionFactory; + } + + private SessionFactory getSessionFactory(DataSource dataSource) throws Exception { + org.springframework.orm.hibernate5.LocalSessionFactoryBean factory = new org.springframework.orm.hibernate5.LocalSessionFactoryBean(); + factory.setDataSource(dataSource); + factory.setMappingLocations(new Resource[] { + new ClassPathResource("org/springframework/webflow/persistence/TestBean.hbm.xml"), + new ClassPathResource("org/springframework/webflow/persistence/TestAddress.hbm.xml") }); + factory.afterPropertiesSet(); + return factory.getObject(); + } + + } + +} diff --git a/spring-webflow/src/test/java/org/springframework/webflow/persistence/HibernatePersistenceContextPropagationTests.java b/spring-webflow/src/test/java/org/springframework/webflow/persistence/HibernatePersistenceContextPropagationTests.java index 3b45ed13..b5b0a002 100644 --- a/spring-webflow/src/test/java/org/springframework/webflow/persistence/HibernatePersistenceContextPropagationTests.java +++ b/spring-webflow/src/test/java/org/springframework/webflow/persistence/HibernatePersistenceContextPropagationTests.java @@ -2,20 +2,12 @@ package org.springframework.webflow.persistence; import javax.sql.DataSource; -import org.hibernate.SessionFactory; -import org.springframework.core.io.ClassPathResource; -import org.springframework.core.io.Resource; -import org.springframework.orm.hibernate4.HibernateTemplate; -import org.springframework.orm.hibernate4.HibernateTransactionManager; -import org.springframework.orm.hibernate4.LocalSessionFactoryBean; import org.springframework.transaction.support.TransactionSynchronizationManager; import org.springframework.webflow.execution.FlowExecutionListener; public class HibernatePersistenceContextPropagationTests extends AbstractPersistenceContextPropagationTests { - private SessionFactory sessionFactory; - - private HibernateTemplate hibernateTemplate; + private HibernateHandler hibernate; private HibernateFlowExecutionListener executionListener; @@ -23,11 +15,8 @@ public class HibernatePersistenceContextPropagationTests extends AbstractPersist @Override protected void setUpResources(DataSource dataSource) throws Exception { - sessionFactory = getSessionFactory(dataSource); - hibernateTemplate = new HibernateTemplate(sessionFactory); - hibernateTemplate.setCheckWriteOperations(false); - HibernateTransactionManager tm = new HibernateTransactionManager(sessionFactory); - executionListener = new HibernateFlowExecutionListener(sessionFactory, tm); + hibernate = HibernateHandlerFactory.create(dataSource); + executionListener = new HibernateFlowExecutionListener(hibernate.getSessionFactory(), hibernate.getTransactionManager()); rowCount = 1; } @@ -38,18 +27,18 @@ public class HibernatePersistenceContextPropagationTests extends AbstractPersist @Override protected void assertSessionNotBound() { - assertNull(TransactionSynchronizationManager.getResource(sessionFactory)); + assertNull(TransactionSynchronizationManager.getResource(hibernate.getSessionFactory())); } @Override protected void assertSessionBound() { - assertNotNull(TransactionSynchronizationManager.getResource(sessionFactory)); + assertNotNull(TransactionSynchronizationManager.getResource(hibernate.getSessionFactory())); } @Override protected void assertCommitState(boolean insertRow, boolean isCommited) { if (insertRow) { - hibernateTemplate.save(new TestBean(rowCount++, "Keith Donald")); + hibernate.templateSave(new TestBean(rowCount++, "Keith Donald")); } if (!isCommited) { assertEquals("Nothing should be committed yet", 1, @@ -60,15 +49,4 @@ public class HibernatePersistenceContextPropagationTests extends AbstractPersist } } - @SuppressWarnings("cast") - private SessionFactory getSessionFactory(DataSource dataSource) throws Exception { - LocalSessionFactoryBean factory = new LocalSessionFactoryBean(); - factory.setDataSource(dataSource); - factory.setMappingLocations(new Resource[] { - new ClassPathResource("org/springframework/webflow/persistence/TestBean.hbm.xml"), - new ClassPathResource("org/springframework/webflow/persistence/TestAddress.hbm.xml") }); - factory.afterPropertiesSet(); - return (SessionFactory) factory.getObject(); - } - }