Re-org database item reader tests
This commit is contained in:
@@ -26,7 +26,6 @@ import javax.persistence.EntityTransaction;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.database.support.AbstractJpaQueryProvider;
|
||||
import org.springframework.dao.DataAccessResourceFailureException;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
@@ -135,7 +134,7 @@ public class JpaPagingItemReader<T> extends AbstractPagingItemReader<T> {
|
||||
}
|
||||
// making sure that the appropriate (JPA) query provider is set
|
||||
else {
|
||||
Assert.isTrue(queryProvider instanceof AbstractJpaQueryProvider, "JPA query provider must be set");
|
||||
Assert.isTrue(queryProvider != null, "JPA query provider must be set");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,20 +1,25 @@
|
||||
package org.springframework.batch.item.database;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.ItemStream;
|
||||
import org.springframework.batch.item.sample.Foo;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.transaction.AfterTransaction;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Common scenarios for testing {@link ItemReader} implementations which read
|
||||
@@ -23,6 +28,8 @@ import javax.sql.DataSource;
|
||||
* @author Lucas Ward
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = "data-source-context.xml")
|
||||
public abstract class AbstractDataSourceItemReaderIntegrationTests {
|
||||
|
||||
protected ItemReader<Foo> reader;
|
||||
@@ -38,10 +45,6 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests {
|
||||
|
||||
protected abstract ItemReader<Foo> createItemReader() throws Exception;
|
||||
|
||||
// protected String[] getConfigLocations() {
|
||||
// return new String[] { "org/springframework/batch/item/database/data-source-context.xml" };
|
||||
// }
|
||||
|
||||
@Before
|
||||
public void onSetUpInTransaction() throws Exception {
|
||||
reader = createItemReader();
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package org.springframework.batch.item.database;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.StatelessSession;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.sample.Foo;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
|
||||
|
||||
/**
|
||||
* Tests for {@link HibernateCursorItemReader} using {@link StatelessSession}.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public abstract class AbstractHibernateCursorItemReaderIntegrationTests extends
|
||||
AbstractDataSourceItemReaderIntegrationTests {
|
||||
|
||||
protected ItemReader<Foo> createItemReader() throws Exception {
|
||||
|
||||
LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
|
||||
factoryBean.setDataSource(dataSource);
|
||||
factoryBean.setMappingLocations(new Resource[] { new ClassPathResource("Foo.hbm.xml", getClass()) });
|
||||
customizeSessionFactory(factoryBean);
|
||||
factoryBean.afterPropertiesSet();
|
||||
|
||||
SessionFactory sessionFactory = (SessionFactory) factoryBean.getObject();
|
||||
|
||||
HibernateCursorItemReader<Foo> hibernateReader = new HibernateCursorItemReader<Foo>();
|
||||
setQuery(hibernateReader);
|
||||
hibernateReader.setSessionFactory(sessionFactory);
|
||||
hibernateReader.setUseStatelessSession(isUseStatelessSession());
|
||||
hibernateReader.afterPropertiesSet();
|
||||
hibernateReader.setSaveState(true);
|
||||
|
||||
return hibernateReader;
|
||||
|
||||
}
|
||||
|
||||
protected void customizeSessionFactory(LocalSessionFactoryBean factoryBean) {
|
||||
}
|
||||
|
||||
protected void setQuery(HibernateCursorItemReader<?> reader) throws Exception {
|
||||
reader.setQueryString("from Foo");
|
||||
}
|
||||
|
||||
protected boolean isUseStatelessSession() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,55 +1,18 @@
|
||||
package org.springframework.batch.item.database;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.StatelessSession;
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.sample.Foo;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
* Tests for {@link HibernateCursorItemReader} using {@link StatelessSession}.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = "data-source-context.xml")
|
||||
public class HibernateCursorItemReaderIntegrationTests extends AbstractDataSourceItemReaderIntegrationTests {
|
||||
|
||||
protected ItemReader<Foo> createItemReader() throws Exception {
|
||||
LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
|
||||
factoryBean.setDataSource(dataSource);
|
||||
factoryBean.setMappingLocations(new Resource[] { new ClassPathResource("Foo.hbm.xml", getClass()) });
|
||||
factoryBean.afterPropertiesSet();
|
||||
|
||||
SessionFactory sessionFactory = (SessionFactory) factoryBean.getObject();
|
||||
|
||||
|
||||
HibernateCursorItemReader<Foo> hibernateReader = new HibernateCursorItemReader<Foo>();
|
||||
setQuery(hibernateReader);
|
||||
hibernateReader.setSessionFactory(sessionFactory);
|
||||
hibernateReader.setUseStatelessSession(isUseStatelessSession());
|
||||
hibernateReader.afterPropertiesSet();
|
||||
hibernateReader.setSaveState(true);
|
||||
|
||||
return hibernateReader;
|
||||
}
|
||||
|
||||
protected void setQuery(HibernateCursorItemReader<?> reader) {
|
||||
reader.setQueryString("from Foo");
|
||||
}
|
||||
|
||||
protected boolean isUseStatelessSession() {
|
||||
return true;
|
||||
}
|
||||
public class HibernateCursorItemReaderIntegrationTests extends AbstractHibernateCursorItemReaderIntegrationTests {
|
||||
|
||||
/**
|
||||
* Exception scenario.
|
||||
|
||||
@@ -3,7 +3,7 @@ package org.springframework.batch.item.database;
|
||||
/**
|
||||
* Tests {@link HibernateCursorItemReader} configured with named query.
|
||||
*/
|
||||
public class HibernateCursorItemReaderNamedQueryIntegrationTests extends HibernateCursorItemReaderIntegrationTests {
|
||||
public class HibernateCursorItemReaderNamedQueryIntegrationTests extends AbstractHibernateCursorItemReaderIntegrationTests {
|
||||
|
||||
@Override
|
||||
protected void setQuery(HibernateCursorItemReader<?> reader) {
|
||||
|
||||
@@ -1,31 +1,18 @@
|
||||
package org.springframework.batch.item.database;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.database.support.HibernateNativeQueryProvider;
|
||||
import org.springframework.batch.item.sample.Foo;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
|
||||
|
||||
/**
|
||||
* @author Anatoly Polinsky
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class HibernateCursorItemReaderNativeQueryIntegrationTests extends HibernateCursorItemReaderIntegrationTests {
|
||||
public class HibernateCursorItemReaderNativeQueryIntegrationTests extends AbstractHibernateCursorItemReaderIntegrationTests {
|
||||
|
||||
@Override
|
||||
protected void setQuery(HibernateCursorItemReader<?> hibernateReader) throws Exception {
|
||||
|
||||
public ItemReader<Foo> createItemReader() throws Exception {
|
||||
LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
|
||||
factoryBean.setDataSource(dataSource);
|
||||
factoryBean.setMappingLocations(new Resource[] { new ClassPathResource("Foo.hbm.xml", getClass()) });
|
||||
factoryBean.afterPropertiesSet();
|
||||
|
||||
SessionFactory sessionFactory = (SessionFactory) factoryBean.getObject();
|
||||
|
||||
|
||||
HibernateCursorItemReader<Foo> hibernateReader = new HibernateCursorItemReader<Foo>();
|
||||
|
||||
String nativeQuery = "select * from T_FOOS";
|
||||
String nativeQuery = "select * from T_FOOS";
|
||||
|
||||
//creating a native query provider as it would be created in configuration
|
||||
HibernateNativeQueryProvider<Foo> queryProvider =
|
||||
@@ -37,11 +24,5 @@ public class HibernateCursorItemReaderNativeQueryIntegrationTests extends Hibern
|
||||
|
||||
hibernateReader.setQueryProvider(queryProvider);
|
||||
|
||||
//hibernateReader.setUseStatelessSession(isUseStatelessSession());
|
||||
hibernateReader.setSessionFactory(sessionFactory);
|
||||
hibernateReader.setSaveState(true);
|
||||
hibernateReader.afterPropertiesSet();
|
||||
|
||||
return hibernateReader;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,16 +2,7 @@ package org.springframework.batch.item.database;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.StatelessSession;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.sample.Foo;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* Tests for {@link HibernateCursorItemReader} using {@link StatelessSession}.
|
||||
@@ -19,36 +10,12 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
* @author Robert Kasanicky
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = "data-source-context.xml")
|
||||
public class HibernateCursorItemReaderParametersIntegrationTests extends AbstractDataSourceItemReaderIntegrationTests {
|
||||
public class HibernateCursorItemReaderParametersIntegrationTests extends
|
||||
AbstractHibernateCursorItemReaderIntegrationTests {
|
||||
|
||||
protected ItemReader<Foo> createItemReader() throws Exception {
|
||||
LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
|
||||
factoryBean.setDataSource(dataSource);
|
||||
factoryBean.setMappingLocations(new Resource[] { new ClassPathResource("Foo.hbm.xml", getClass()) });
|
||||
factoryBean.afterPropertiesSet();
|
||||
|
||||
SessionFactory sessionFactory = (SessionFactory) factoryBean.getObject();
|
||||
|
||||
|
||||
HibernateCursorItemReader<Foo> hibernateReader = new HibernateCursorItemReader<Foo>();
|
||||
setQuery(hibernateReader);
|
||||
hibernateReader.setSessionFactory(sessionFactory);
|
||||
hibernateReader.setUseStatelessSession(isUseStatelessSession());
|
||||
hibernateReader.afterPropertiesSet();
|
||||
hibernateReader.setSaveState(true);
|
||||
|
||||
return hibernateReader;
|
||||
}
|
||||
|
||||
protected void setQuery(HibernateCursorItemReader<?> reader) {
|
||||
reader.setQueryString("from Foo where name like :name");
|
||||
reader.setParameterValues(Collections.singletonMap("name", (Object)"bar%"));
|
||||
}
|
||||
|
||||
protected boolean isUseStatelessSession() {
|
||||
return true;
|
||||
reader.setParameterValues(Collections.singletonMap("name", (Object) "bar%"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
package org.springframework.batch.item.database;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.createNiceMock;
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
|
||||
import org.hibernate.Query;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.classic.Session;
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.sample.Foo;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Tests for {@link HibernateCursorItemReader} using standard hibernate {@link Session}.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = "data-source-context.xml")
|
||||
public class HibernateCursorItemReaderStatefulIntegrationTests extends HibernateCursorItemReaderIntegrationTests {
|
||||
public class HibernateCursorItemReaderStatefulIntegrationTests extends AbstractHibernateCursorItemReaderIntegrationTests {
|
||||
|
||||
protected boolean isUseStatelessSession() {
|
||||
return false;
|
||||
@@ -26,7 +26,7 @@ public class HibernateCursorItemReaderStatefulIntegrationTests extends Hibernate
|
||||
|
||||
//Ensure close is called on the stateful session correctly.
|
||||
@Test
|
||||
public void testStatfulClose(){
|
||||
public void testStatefulClose(){
|
||||
|
||||
SessionFactory sessionFactory = createMock(SessionFactory.class);
|
||||
Session session = createMock(Session.class);
|
||||
|
||||
@@ -49,7 +49,7 @@ public class HibernateCursorProjectionItemReaderIntegrationTests {
|
||||
reader.open(new ExecutionContext());
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testMultipleItemsInProjection() throws Exception {
|
||||
HibernateCursorItemReader<Object[]> reader = new HibernateCursorItemReader<Object[]>();
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package org.springframework.batch.item.database;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.sample.Foo;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
/**
|
||||
* Tests for {@link JdbcCursorItemReader}
|
||||
@@ -12,7 +11,6 @@ import org.junit.runner.RunWith;
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = "data-source-context.xml")
|
||||
public class JdbcCursorItemReaderIntegrationTests extends AbstractDataSourceItemReaderIntegrationTests {
|
||||
|
||||
protected ItemReader<Foo> createItemReader() throws Exception {
|
||||
|
||||
@@ -1,23 +1,18 @@
|
||||
package org.springframework.batch.item.database;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.batch.item.sample.Foo;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.database.support.HsqlPagingQueryProvider;
|
||||
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.database.support.HsqlPagingQueryProvider;
|
||||
import org.springframework.batch.item.sample.Foo;
|
||||
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
|
||||
|
||||
/**
|
||||
* Tests for {@link JpaPagingItemReader}.
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = "data-source-context.xml")
|
||||
public class JdbcPagingItemReaderIntegrationTests extends AbstractDataSourceItemReaderIntegrationTests {
|
||||
|
||||
protected ItemReader<Foo> createItemReader() throws Exception {
|
||||
|
||||
@@ -2,23 +2,18 @@ package org.springframework.batch.item.database;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.batch.item.sample.Foo;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.sample.Foo;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
|
||||
/**
|
||||
* Tests for {@link org.springframework.batch.item.database.JpaPagingItemReader}.
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = "data-source-context.xml")
|
||||
public class JpaPagingItemReaderIntegrationTests extends AbstractDataSourceItemReaderIntegrationTests {
|
||||
|
||||
protected ItemReader<Foo> createItemReader() throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user