From c98a6f3b818bdf0096e5850b52987510d491813f Mon Sep 17 00:00:00 2001 From: trisberg Date: Mon, 1 Feb 2010 20:54:04 +0000 Subject: [PATCH] BATCH-1488: added common tests with Derby stored proc --- spring-batch-infrastructure/.springBeans | 2 +- .../batch/item/AbstractItemReaderTests.java | 1 + .../AbstractItemStreamItemReaderTests.java | 4 + ...ractDatabaseItemStreamItemReaderTests.java | 13 +++- .../StoredProcedureItemReaderCommonTests.java | 77 +++++++++++++++++++ ...dProcedureItemReaderIntegrationTests.java} | 4 +- .../test/jdbc/proc/derby/TestProcedures.java | 8 ++ .../item/database/drop-foo-schema-derby.sql | 1 + .../item/database/init-foo-schema-derby.sql | 7 ++ ...ntext.xml => stored-procedure-context.xml} | 0 10 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/StoredProcedureItemReaderCommonTests.java rename spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/{StoredProcedureItemReaderTests.java => StoredProcedureItemReaderIntegrationTests.java} (85%) rename spring-batch-infrastructure/src/test/resources/org/springframework/batch/item/database/{StoredProcedureItemReaderTests-context.xml => stored-procedure-context.xml} (100%) diff --git a/spring-batch-infrastructure/.springBeans b/spring-batch-infrastructure/.springBeans index ef9bdc6176..5f547405dc 100644 --- a/spring-batch-infrastructure/.springBeans +++ b/spring-batch-infrastructure/.springBeans @@ -19,7 +19,7 @@ src/test/resources/org/springframework/batch/item/database/JpaPagingItemReaderParameterTests-context.xml src/test/resources/org/springframework/batch/item/adapter/pe-delegating-item-writer.xml src/test/resources/org/springframework/batch/retry/interceptor/retry-transaction-test.xml - src/test/resources/org/springframework/batch/item/database/StoredProcedureItemReaderTests-context.xml + src/test/resources/org/springframework/batch/item/database/stored-procedure-context.xml diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/AbstractItemReaderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/AbstractItemReaderTests.java index c6381babcd..5bfabf972e 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/AbstractItemReaderTests.java +++ b/spring-batch-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()); } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/AbstractItemStreamItemReaderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/AbstractItemStreamItemReaderTests.java index 56f1453866..6fb69b7cd1 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/AbstractItemStreamItemReaderTests.java +++ b/spring-batch-infrastructure/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(); diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/AbstractDatabaseItemStreamItemReaderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/AbstractDatabaseItemStreamItemReaderTests.java index 67bd642cb1..3510bceed6 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/AbstractDatabaseItemStreamItemReaderTests.java +++ b/spring-batch-infrastructure/src/test/java/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 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); } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/StoredProcedureItemReaderCommonTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/StoredProcedureItemReaderCommonTests.java new file mode 100644 index 0000000000..fc2805dbaa --- /dev/null +++ b/spring-batch-infrastructure/src/test/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 getItemReader() throws Exception { + StoredProcedureItemReader result = new StoredProcedureItemReader(); + 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) tested).setDriverSupportsAbsolute(true); + testedAsStream().open(executionContext); + testedAsStream().close(); + testedAsStream().open(executionContext); + testRestart(); + } + + protected void pointToEmptyInput(ItemReader tested) throws Exception { + StoredProcedureItemReader reader = (StoredProcedureItemReader) 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(); + } + +} diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/StoredProcedureItemReaderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/StoredProcedureItemReaderIntegrationTests.java similarity index 85% rename from spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/StoredProcedureItemReaderTests.java rename to spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/StoredProcedureItemReaderIntegrationTests.java index 7626262d5d..880c0d3bb1 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/StoredProcedureItemReaderTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/StoredProcedureItemReaderIntegrationTests.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 diff --git a/spring-batch-infrastructure/src/test/java/test/jdbc/proc/derby/TestProcedures.java b/spring-batch-infrastructure/src/test/java/test/jdbc/proc/derby/TestProcedures.java index 321d46549c..e3832d246d 100644 --- a/spring-batch-infrastructure/src/test/java/test/jdbc/proc/derby/TestProcedures.java +++ b/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(); + } } diff --git a/spring-batch-infrastructure/src/test/resources/org/springframework/batch/item/database/drop-foo-schema-derby.sql b/spring-batch-infrastructure/src/test/resources/org/springframework/batch/item/database/drop-foo-schema-derby.sql index 8b580c8055..a268005962 100644 --- a/spring-batch-infrastructure/src/test/resources/org/springframework/batch/item/database/drop-foo-schema-derby.sql +++ b/spring-batch-infrastructure/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; diff --git a/spring-batch-infrastructure/src/test/resources/org/springframework/batch/item/database/init-foo-schema-derby.sql b/spring-batch-infrastructure/src/test/resources/org/springframework/batch/item/database/init-foo-schema-derby.sql index 3cc5368b43..ae340bed10 100644 --- a/spring-batch-infrastructure/src/test/resources/org/springframework/batch/item/database/init-foo-schema-derby.sql +++ b/spring-batch-infrastructure/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'; diff --git a/spring-batch-infrastructure/src/test/resources/org/springframework/batch/item/database/StoredProcedureItemReaderTests-context.xml b/spring-batch-infrastructure/src/test/resources/org/springframework/batch/item/database/stored-procedure-context.xml similarity index 100% rename from spring-batch-infrastructure/src/test/resources/org/springframework/batch/item/database/StoredProcedureItemReaderTests-context.xml rename to spring-batch-infrastructure/src/test/resources/org/springframework/batch/item/database/stored-procedure-context.xml