IN PROGRESS - BATCH-710: updated tests to use SpringJUnit4ClassRunner and SimpleJdbcTemplate
This commit is contained in:
@@ -1,36 +1,44 @@
|
||||
package org.springframework.batch.item.database;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.StatelessSession;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.ItemStream;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* Tests for {@link HibernateCursorItemReader} using {@link StatelessSession}.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class HibernateCursorProjectionItemReaderIntegrationTests extends AbstractTransactionalDataSourceSpringContextTests {
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = "data-source-context.xml")
|
||||
public class HibernateCursorProjectionItemReaderIntegrationTests {
|
||||
|
||||
protected ItemReader<?> reader;
|
||||
|
||||
protected ExecutionContext executionContext;
|
||||
|
||||
protected String[] getConfigLocations() {
|
||||
return new String[] { "org/springframework/batch/item/database/data-source-context.xml" };
|
||||
}
|
||||
@Autowired
|
||||
protected DataSource dataSource;
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.test.AbstractTransactionalSpringContextTests#onSetUpInTransaction()
|
||||
*/
|
||||
protected void onSetUpInTransaction() throws Exception {
|
||||
super.onSetUpInTransaction();
|
||||
@Before
|
||||
public void onSetUpInTransaction() throws Exception {
|
||||
reader = createItemReader();
|
||||
executionContext = new ExecutionContext();
|
||||
}
|
||||
@@ -38,7 +46,7 @@ public class HibernateCursorProjectionItemReaderIntegrationTests extends Abstrac
|
||||
|
||||
protected ItemReader<?> createItemReader() throws Exception {
|
||||
LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
|
||||
factoryBean.setDataSource(super.getJdbcTemplate().getDataSource());
|
||||
factoryBean.setDataSource(dataSource);
|
||||
factoryBean.setMappingLocations(new Resource[] { new ClassPathResource("Foo.hbm.xml", getClass()) });
|
||||
factoryBean.afterPropertiesSet();
|
||||
|
||||
@@ -55,11 +63,12 @@ public class HibernateCursorProjectionItemReaderIntegrationTests extends Abstrac
|
||||
return inputSource;
|
||||
}
|
||||
|
||||
public void testNormalProcessing() throws Exception {
|
||||
@Transactional @Test
|
||||
public void testNormalProcessing() throws Exception {
|
||||
((InitializingBean) reader).afterPropertiesSet();
|
||||
((ItemStream) reader).open(new ExecutionContext());
|
||||
Object[] foo1 = (Object[]) reader.read();
|
||||
assertEquals(new Integer(1), foo1[0]);
|
||||
assertEquals(1, foo1[0]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,32 +3,48 @@
|
||||
*/
|
||||
package org.springframework.batch.item.database.support;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
import org.junit.Before;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* @author Lucas Ward
|
||||
*
|
||||
*/
|
||||
public class MultipleColumnJdbcKeyGeneratorIntegrationTests extends AbstractTransactionalDataSourceSpringContextTests {
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = "/org/springframework/batch/item/database/data-source-context.xml")
|
||||
public class MultipleColumnJdbcKeyGeneratorIntegrationTests {
|
||||
|
||||
MultipleColumnJdbcKeyCollector<Map<?,?>> keyStrategy;
|
||||
|
||||
ExecutionContext executionContext;
|
||||
|
||||
protected String[] getConfigLocations(){
|
||||
return new String[] { "org/springframework/batch/item/database/data-source-context.xml"};
|
||||
|
||||
private SimpleJdbcTemplate simpleJdbcTemplate;
|
||||
|
||||
@Autowired
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
protected void onSetUpBeforeTransaction() throws Exception {
|
||||
super.onSetUpBeforeTransaction();
|
||||
|
||||
keyStrategy = new MultipleColumnJdbcKeyCollector<Map<?,?>>(getJdbcTemplate(),
|
||||
@Before
|
||||
public void onSetUpBeforeTransaction() throws Exception {
|
||||
|
||||
keyStrategy = new MultipleColumnJdbcKeyCollector<Map<?,?>>(simpleJdbcTemplate.getJdbcOperations(),
|
||||
"SELECT ID, VALUE from T_FOOS order by ID");
|
||||
|
||||
keyStrategy.setRestartSql("SELECT ID, VALUE from T_FOOS where ID > ? and VALUE > ? order by ID");
|
||||
@@ -36,17 +52,19 @@ public class MultipleColumnJdbcKeyGeneratorIntegrationTests extends AbstractTran
|
||||
executionContext = new ExecutionContext();
|
||||
}
|
||||
|
||||
@Transactional @Test
|
||||
public void testRetrieveKeys(){
|
||||
|
||||
List<Map<?,?>> keys = keyStrategy.retrieveKeys(executionContext);
|
||||
|
||||
for (int i = 0; i < keys.size(); i++) {
|
||||
Map<?,?> id = keys.get(i);
|
||||
assertEquals(id.get("ID"), new Long(i + 1));
|
||||
assertEquals(id.get("VALUE"), new Integer(i + 1));
|
||||
assertEquals(i + 1L, id.get("ID"));
|
||||
assertEquals(i + 1, id.get("VALUE"));
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional @Test
|
||||
public void testRestoreKeys(){
|
||||
|
||||
Map<String, String> keyMap = new LinkedHashMap<String, String>();
|
||||
@@ -54,17 +72,18 @@ public class MultipleColumnJdbcKeyGeneratorIntegrationTests extends AbstractTran
|
||||
keyMap.put("VALUE", "3");
|
||||
executionContext.put(ClassUtils.getShortName(MultipleColumnJdbcKeyCollector.class)+ ".current.key", keyMap);
|
||||
|
||||
List<Map<?,?>> keys = keyStrategy.retrieveKeys(executionContext);
|
||||
List<Map<?, ?>> keys = keyStrategy.retrieveKeys(executionContext);
|
||||
|
||||
assertEquals(2, keys.size());
|
||||
Map<?,?> key = keys.get(0);
|
||||
assertEquals(new Long(4), key.get("ID"));
|
||||
assertEquals(new Integer(4), key.get("VALUE"));
|
||||
assertEquals(4L, key.get("ID"));
|
||||
assertEquals(4, key.get("VALUE"));
|
||||
key = keys.get(1);
|
||||
assertEquals(new Long(5), key.get("ID"));
|
||||
assertEquals(new Integer(5), key.get("VALUE"));
|
||||
assertEquals(5L, key.get("ID"));
|
||||
assertEquals(5, key.get("VALUE"));
|
||||
}
|
||||
|
||||
@Transactional @Test
|
||||
public void testGetNullKeyAsStreamContext(){
|
||||
|
||||
try{
|
||||
@@ -74,4 +93,5 @@ public class MultipleColumnJdbcKeyGeneratorIntegrationTests extends AbstractTran
|
||||
//expected
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,31 +1,46 @@
|
||||
package org.springframework.batch.item.database.support;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Lucas Ward
|
||||
*
|
||||
*/
|
||||
public class SingleColumnJdbcKeyGeneratorIntegrationTests extends AbstractTransactionalDataSourceSpringContextTests {
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = "/org/springframework/batch/item/database/data-source-context.xml")
|
||||
public class SingleColumnJdbcKeyGeneratorIntegrationTests {
|
||||
|
||||
SingleColumnJdbcKeyCollector<Long> keyStrategy;
|
||||
|
||||
ExecutionContext executionContext;
|
||||
|
||||
protected String[] getConfigLocations(){
|
||||
return new String[] { "org/springframework/batch/item/database/data-source-context.xml"};
|
||||
private SimpleJdbcTemplate simpleJdbcTemplate;
|
||||
|
||||
@Autowired
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
|
||||
protected void onSetUpBeforeTransaction() throws Exception {
|
||||
super.onSetUpBeforeTransaction();
|
||||
|
||||
keyStrategy = new SingleColumnJdbcKeyCollector<Long>(getJdbcTemplate(),
|
||||
@Before
|
||||
public void onSetUpBeforeTransaction() throws Exception {
|
||||
|
||||
keyStrategy = new SingleColumnJdbcKeyCollector<Long>(simpleJdbcTemplate.getJdbcOperations(),
|
||||
"SELECT ID from T_FOOS order by ID");
|
||||
|
||||
keyStrategy.setRestartSql("SELECT ID from T_FOOS where ID > ? order by ID");
|
||||
@@ -33,43 +48,47 @@ public class SingleColumnJdbcKeyGeneratorIntegrationTests extends AbstractTransa
|
||||
executionContext = new ExecutionContext();
|
||||
}
|
||||
|
||||
@Transactional @Test
|
||||
public void testRetrieveKeys(){
|
||||
|
||||
List<Long> keys = keyStrategy.retrieveKeys(new ExecutionContext());
|
||||
|
||||
for (int i = 0; i < keys.size(); i++) {
|
||||
Long id = (Long)keys.get(i);
|
||||
Long id = keys.get(i);
|
||||
assertEquals(new Long(i + 1), id);
|
||||
}
|
||||
for (int i = 0; i < keys.size(); i++) {
|
||||
System.out.println(keys.get(i));
|
||||
for (Long key : keys) {
|
||||
System.out.println(key);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Transactional @Test
|
||||
public void testRestoreKeys(){
|
||||
|
||||
keyStrategy.updateContext(new Long(3), executionContext);
|
||||
keyStrategy.updateContext(3L, executionContext);
|
||||
|
||||
List<Long> keys = keyStrategy.retrieveKeys(executionContext);
|
||||
|
||||
assertEquals(2, keys.size());
|
||||
assertEquals(new Long(4), keys.get(0));
|
||||
assertEquals(new Long(5), keys.get(1));
|
||||
|
||||
for (int i = 0; i < keys.size(); i++) {
|
||||
System.out.println(keys.get(i));
|
||||
|
||||
for (Long key : keys) {
|
||||
System.out.println(key);
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional @Test
|
||||
public void testGetKeyAsStreamContext(){
|
||||
|
||||
keyStrategy.updateContext(new Long(3), executionContext);
|
||||
keyStrategy.updateContext(3L, executionContext);
|
||||
|
||||
assertEquals(1, executionContext.size());
|
||||
assertEquals(new Long(3), executionContext.get(ClassUtils.getShortName(SingleColumnJdbcKeyCollector.class) + ".key"));
|
||||
assertEquals(3L, executionContext.get(ClassUtils.getShortName(SingleColumnJdbcKeyCollector.class) + ".key"));
|
||||
}
|
||||
|
||||
@Transactional @Test
|
||||
public void testGetNullKeyAsStreamContext(){
|
||||
|
||||
try{
|
||||
@@ -80,6 +99,7 @@ public class SingleColumnJdbcKeyGeneratorIntegrationTests extends AbstractTransa
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional @Test
|
||||
public void testRestoreKeysFromNull(){
|
||||
|
||||
try{
|
||||
|
||||
Reference in New Issue
Block a user