Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

BATCH-1488: added common tests with Derby stored proc

  • Loading branch information...
commit c98a6f3b818bdf0096e5850b52987510d491813f 1 parent 96e6286
trisberg authored
View
2  spring-batch-infrastructure/.springBeans
@@ -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/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/item/database/StoredProcedureItemReaderTests-context.xml</config>
+ <config>src/test/resources/org/springframework/batch/item/database/stored-procedure-context.xml</config>
</configs>
<configSets>
</configSets>
View
1  ...-infrastructure/src/test/java/org/springframework/batch/item/AbstractItemReaderTests.java
@@ -56,6 +56,7 @@ public void testRead() throws Exception {
@Test
public void testEmptyInput() throws Exception {
pointToEmptyInput(tested);
+ Foo x = tested.read();
assertNull(tested.read());
}
View
4 ...cture/src/test/java/org/springframework/batch/item/AbstractItemStreamItemReaderTests.java
@@ -52,6 +52,8 @@ public void testRestart() throws Exception {
testedAsStream().update(executionContext);
+ testedAsStream().close();
+
// create new input source
tested = getItemReader();
@@ -82,6 +84,8 @@ public void testResetAndRestart() throws Exception {
Foo foo3 = tested.read();
assertEquals(3, foo3.getValue());
+ testedAsStream().close();
+
// create new input source
tested = getItemReader();
View
13 ...va/org/springframework/batch/item/database/AbstractDatabaseItemStreamItemReaderTests.java
@@ -16,11 +16,11 @@
public abstract class AbstractDatabaseItemStreamItemReaderTests extends AbstractItemStreamItemReaderTests {
- private ClassPathXmlApplicationContext ctx;
+ protected ClassPathXmlApplicationContext ctx;
@Before
public void setUp() throws Exception {
- ctx = new ClassPathXmlApplicationContext("org/springframework/batch/item/database/data-source-context.xml");
+ initializeContext();
super.setUp();
}
@@ -30,6 +30,14 @@ public void tearDown() throws Exception {
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
public void testReadToExhaustion() throws Exception {
ItemReader<Foo> reader = getItemReader();
@@ -40,6 +48,7 @@ public void testReadToExhaustion() throws Exception {
while (count++<100 && item!=null) {
item = reader.read();
}
+ ((ItemStream) reader).close();
assertEquals(7, count);
}
View
77 ...st/java/org/springframework/batch/item/database/StoredProcedureItemReaderCommonTests.java
@@ -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();
+ }
+
+}
View
4 ...abase/StoredProcedureItemReaderTests.java → ...dProcedureItemReaderIntegrationTests.java
@@ -7,8 +7,8 @@
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
-@ContextConfiguration(locations = "StoredProcedureItemReaderTests-context.xml")
-public class StoredProcedureItemReaderTests
+@ContextConfiguration(locations = "stored-procedure-context.xml")
+public class StoredProcedureItemReaderIntegrationTests
extends AbstractDataSourceItemReaderIntegrationTests {
@Override
View
8 spring-batch-infrastructure/src/test/java/test/jdbc/proc/derby/TestProcedures.java
@@ -23,4 +23,12 @@ public static void readFoos(ResultSet[] rs) throws SQLException {
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();
+ }
}
View
1  ...ture/src/test/resources/org/springframework/batch/item/database/drop-foo-schema-derby.sql
@@ -1,2 +1,3 @@
DROP PROCEDURE read_foos;
+DROP PROCEDURE read_some_foos;
DROP TABLE T_FOOS;
View
7 ...ture/src/test/resources/org/springframework/batch/item/database/init-foo-schema-derby.sql
@@ -18,3 +18,10 @@ CREATE PROCEDURE read_foos ()
READS SQL DATA
DYNAMIC RESULT SETS 1
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';
View
0  ...toredProcedureItemReaderTests-context.xml → ...tem/database/stored-procedure-context.xml
File renamed without changes
Please sign in to comment.
Something went wrong with that request. Please try again.