Skip to content

Commit

Permalink
BATCH-1488: added common tests with Derby stored proc
Browse files Browse the repository at this point in the history
  • Loading branch information
trisberg committed Feb 1, 2010
1 parent 96e6286 commit c98a6f3
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 5 deletions.
2 changes: 1 addition & 1 deletion spring-batch-infrastructure/.springBeans
Expand Up @@ -19,7 +19,7 @@
<config>src/test/resources/org/springframework/batch/item/database/JpaPagingItemReaderParameterTests-context.xml</config> <config>src/test/resources/org/springframework/batch/item/database/JpaPagingItemReaderParameterTests-context.xml</config>
<config>src/test/resources/org/springframework/batch/item/adapter/pe-delegating-item-writer.xml</config> <config>src/test/resources/org/springframework/batch/item/adapter/pe-delegating-item-writer.xml</config>
<config>src/test/resources/org/springframework/batch/retry/interceptor/retry-transaction-test.xml</config> <config>src/test/resources/org/springframework/batch/retry/interceptor/retry-transaction-test.xml</config>
<config>src/test/resources/org/springframework/batch/item/database/StoredProcedureItemReaderTests-context.xml</config> <config>src/test/resources/org/springframework/batch/item/database/stored-procedure-context.xml</config>
</configs> </configs>
<configSets> <configSets>
</configSets> </configSets>
Expand Down
Expand Up @@ -56,6 +56,7 @@ public void testRead() throws Exception {
@Test @Test
public void testEmptyInput() throws Exception { public void testEmptyInput() throws Exception {
pointToEmptyInput(tested); pointToEmptyInput(tested);
Foo x = tested.read();
assertNull(tested.read()); assertNull(tested.read());
} }


Expand Down
Expand Up @@ -52,6 +52,8 @@ public void testRestart() throws Exception {


testedAsStream().update(executionContext); testedAsStream().update(executionContext);


testedAsStream().close();

// create new input source // create new input source
tested = getItemReader(); tested = getItemReader();


Expand Down Expand Up @@ -82,6 +84,8 @@ public void testResetAndRestart() throws Exception {
Foo foo3 = tested.read(); Foo foo3 = tested.read();
assertEquals(3, foo3.getValue()); assertEquals(3, foo3.getValue());


testedAsStream().close();

// create new input source // create new input source
tested = getItemReader(); tested = getItemReader();


Expand Down
Expand Up @@ -16,11 +16,11 @@


public abstract class AbstractDatabaseItemStreamItemReaderTests extends AbstractItemStreamItemReaderTests { public abstract class AbstractDatabaseItemStreamItemReaderTests extends AbstractItemStreamItemReaderTests {


private ClassPathXmlApplicationContext ctx; protected ClassPathXmlApplicationContext ctx;


@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
ctx = new ClassPathXmlApplicationContext("org/springframework/batch/item/database/data-source-context.xml"); initializeContext();
super.setUp(); super.setUp();
} }


Expand All @@ -30,6 +30,14 @@ public void tearDown() throws Exception {
ctx.close(); ctx.close();
} }


/**
* Sub-classes can override this and create their own context.
* @throws Exception
*/
protected void initializeContext() throws Exception {
ctx = new ClassPathXmlApplicationContext("org/springframework/batch/item/database/data-source-context.xml");
}

@Test @Test
public void testReadToExhaustion() throws Exception { public void testReadToExhaustion() throws Exception {
ItemReader<Foo> reader = getItemReader(); ItemReader<Foo> reader = getItemReader();
Expand All @@ -40,6 +48,7 @@ public void testReadToExhaustion() throws Exception {
while (count++<100 && item!=null) { while (count++<100 && item!=null) {
item = reader.read(); item = reader.read();
} }
((ItemStream) reader).close();
assertEquals(7, count); assertEquals(7, count);
} }


Expand Down
@@ -0,0 +1,77 @@
package org.springframework.batch.item.database;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.hsqldb.Types;
import org.junit.Test;
import org.junit.internal.runners.JUnit4ClassRunner;
import org.junit.runner.RunWith;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ReaderNotOpenException;
import org.springframework.batch.item.sample.Foo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.SqlParameter;

@RunWith(JUnit4ClassRunner.class)
public class StoredProcedureItemReaderCommonTests extends AbstractDatabaseItemStreamItemReaderTests {

protected ItemReader<Foo> getItemReader() throws Exception {
StoredProcedureItemReader<Foo> result = new StoredProcedureItemReader<Foo>();
result.setDataSource(getDataSource());
result.setProcedureName("read_foos");
result.setRowMapper(new FooRowMapper());
result.setVerifyCursorPosition(false);
result.afterPropertiesSet();
return result;
}

protected void initializeContext() throws Exception {
ctx = new ClassPathXmlApplicationContext("org/springframework/batch/item/database/stored-procedure-context.xml");
}

@Test
public void testRestartWithDriverSupportsAbsolute() throws Exception {
testedAsStream().close();
tested = getItemReader();
((StoredProcedureItemReader<Foo>) tested).setDriverSupportsAbsolute(true);
testedAsStream().open(executionContext);
testedAsStream().close();
testedAsStream().open(executionContext);
testRestart();
}

protected void pointToEmptyInput(ItemReader<Foo> tested) throws Exception {
StoredProcedureItemReader<Foo> reader = (StoredProcedureItemReader<Foo>) tested;
reader.close();
reader.setDataSource(getDataSource());
reader.setProcedureName("read_some_foos");
reader.setParameters(
new SqlParameter[] {
new SqlParameter("from_id", Types.NUMERIC),
new SqlParameter("to_id", Types.NUMERIC)
});
reader.setPreparedStatementSetter(
new PreparedStatementSetter() {
public void setValues(PreparedStatement ps)
throws SQLException {
ps.setInt(1, 1000);
ps.setInt(2, 1001);
}
});
reader.setRowMapper(new FooRowMapper());
reader.setVerifyCursorPosition(false);
reader.afterPropertiesSet();
reader.open(new ExecutionContext());
}

@Test(expected=ReaderNotOpenException.class)
public void testReadBeforeOpen() throws Exception {
testedAsStream().close();
tested = getItemReader();
tested.read();
}

}
Expand Up @@ -7,8 +7,8 @@
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "StoredProcedureItemReaderTests-context.xml") @ContextConfiguration(locations = "stored-procedure-context.xml")
public class StoredProcedureItemReaderTests public class StoredProcedureItemReaderIntegrationTests
extends AbstractDataSourceItemReaderIntegrationTests { extends AbstractDataSourceItemReaderIntegrationTests {


@Override @Override
Expand Down
Expand Up @@ -23,4 +23,12 @@ public static void readFoos(ResultSet[] rs) throws SQLException {
rs[0] = ps1.executeQuery(); rs[0] = ps1.executeQuery();
} }


public static void readSomeFoos(int fromId, int toId, ResultSet[] rs) throws SQLException {
String SQL = "SELECT id, name, value FROM T_FOOS WHERE id between ? and ?";
Connection conn = DriverManager.getConnection("jdbc:default:connection");
PreparedStatement ps2 = conn.prepareStatement(SQL);
ps2.setInt(1, fromId);
ps2.setInt(2, toId);
rs[0] = ps2.executeQuery();
}
} }
@@ -1,2 +1,3 @@
DROP PROCEDURE read_foos; DROP PROCEDURE read_foos;
DROP PROCEDURE read_some_foos;
DROP TABLE T_FOOS; DROP TABLE T_FOOS;
Expand Up @@ -18,3 +18,10 @@ CREATE PROCEDURE read_foos ()
READS SQL DATA READS SQL DATA
DYNAMIC RESULT SETS 1 DYNAMIC RESULT SETS 1
EXTERNAL NAME 'test.jdbc.proc.derby.TestProcedures.readFoos'; EXTERNAL NAME 'test.jdbc.proc.derby.TestProcedures.readFoos';

CREATE PROCEDURE read_some_foos (from_id INTEGER, to_id INTEGER)
PARAMETER STYLE JAVA
LANGUAGE JAVA
READS SQL DATA
DYNAMIC RESULT SETS 1
EXTERNAL NAME 'test.jdbc.proc.derby.TestProcedures.readSomeFoos';

0 comments on commit c98a6f3

Please sign in to comment.