From 17dd5bb3f46b188d2d0c087c07c27a44840bca01 Mon Sep 17 00:00:00 2001 From: lucasward Date: Fri, 3 Apr 2009 02:53:52 +0000 Subject: [PATCH] BATCH-1150: Modified the StepExecutionPreparedStatementSetter to no longer implement StepExecutionListener. Also changed the name to ListPreparedStatementSetter. --- ....java => ListPreparedStatementSetter.java} | 24 ++----- ...aderPreparedStatementIntegrationTests.java | 40 ++++------- ... => ListPreparedStatementSetterTests.java} | 69 +++++-------------- 3 files changed, 38 insertions(+), 95 deletions(-) rename spring-batch-core/src/main/java/org/springframework/batch/core/resource/{StepExecutionPreparedStatementSetter.java => ListPreparedStatementSetter.java} (69%) rename spring-batch-core/src/test/java/org/springframework/batch/core/resource/{StepExecutionPreparedStatementSetterTests.java => ListPreparedStatementSetterTests.java} (62%) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/resource/StepExecutionPreparedStatementSetter.java b/spring-batch-core/src/main/java/org/springframework/batch/core/resource/ListPreparedStatementSetter.java similarity index 69% rename from spring-batch-core/src/main/java/org/springframework/batch/core/resource/StepExecutionPreparedStatementSetter.java rename to spring-batch-core/src/main/java/org/springframework/batch/core/resource/ListPreparedStatementSetter.java index 1def151b6..0643ce898 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/resource/StepExecutionPreparedStatementSetter.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/resource/ListPreparedStatementSetter.java @@ -18,13 +18,9 @@ package org.springframework.batch.core.resource; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.List; -import java.util.Map; -import org.springframework.batch.core.JobParameter; import org.springframework.batch.core.JobParameters; -import org.springframework.batch.core.StepExecution; import org.springframework.batch.core.StepExecutionListener; -import org.springframework.batch.core.listener.StepExecutionListenerSupport; import org.springframework.beans.factory.InitializingBean; import org.springframework.jdbc.core.PreparedStatementSetter; import org.springframework.jdbc.core.SqlTypeValue; @@ -39,35 +35,23 @@ import org.springframework.util.Assert; * @author Lucas Ward * */ -public class StepExecutionPreparedStatementSetter extends StepExecutionListenerSupport implements +public class ListPreparedStatementSetter implements PreparedStatementSetter, InitializingBean { - private List parameterKeys; - - private JobParameters jobParameters; + private List parameterKeys; public void setValues(PreparedStatement ps) throws SQLException { - Map parameters = jobParameters.getParameters(); for (int i = 0; i < parameterKeys.size(); i++) { - JobParameter arg = parameters.get(parameterKeys.get(i)); - if (arg == null) { - throw new IllegalStateException("No job parameter found for with key of: [" + parameterKeys.get(i) - + "]"); - } - StatementCreatorUtils.setParameterValue(ps, i + 1, SqlTypeValue.TYPE_UNKNOWN, arg.getValue()); + StatementCreatorUtils.setParameterValue(ps, i + 1, SqlTypeValue.TYPE_UNKNOWN, parameterKeys.get(i)); } } - public void beforeStep(StepExecution stepExecution) { - this.jobParameters = stepExecution.getJobParameters(); - } - /** * The parameter names that will be pulled from the {@link JobParameters}. * It is assumed that their order in the List is the order of the parameters * in the PreparedStatement. */ - public void setParameterKeys(List parameterKeys) { + public void setParameters(List parameterKeys) { this.parameterKeys = parameterKeys; } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/resource/JdbcCursorItemReaderPreparedStatementIntegrationTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/resource/JdbcCursorItemReaderPreparedStatementIntegrationTests.java index 262babefa..3dd4507db 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/resource/JdbcCursorItemReaderPreparedStatementIntegrationTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/resource/JdbcCursorItemReaderPreparedStatementIntegrationTests.java @@ -1,26 +1,22 @@ package org.springframework.batch.core.resource; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import java.util.ArrayList; import java.util.List; -import org.springframework.batch.core.JobExecution; -import org.springframework.batch.core.JobInstance; -import org.springframework.batch.core.JobParameters; -import org.springframework.batch.core.JobParametersBuilder; -import org.springframework.batch.core.StepExecution; -import org.springframework.batch.item.ExecutionContext; -import org.springframework.batch.item.database.JdbcCursorItemReader; -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 javax.sql.DataSource; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; - -import javax.sql.DataSource; +import org.springframework.batch.item.ExecutionContext; +import org.springframework.batch.item.database.JdbcCursorItemReader; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.transaction.annotation.Transactional; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "/org/springframework/batch/core/repository/dao/data-source-context.xml") @@ -49,17 +45,11 @@ public class JdbcCursorItemReaderPreparedStatementIntegrationTests { itemReader.setMaxRows(100); itemReader.setQueryTimeout(1000); itemReader.setSaveState(true); - StepExecutionPreparedStatementSetter pss = new StepExecutionPreparedStatementSetter(); - JobParameters jobParameters = new JobParametersBuilder().addLong("begin.id", 1L).addLong("end.id", 4L).toJobParameters(); - JobInstance jobInstance = new JobInstance(1L, jobParameters, "simpleJob"); - JobExecution jobExecution = new JobExecution(jobInstance, (long) 2); - StepExecution stepExecution = new StepExecution("taskletStep", jobExecution, 3L); - pss.beforeStep(stepExecution); - - List parameterNames = new ArrayList(); - parameterNames.add("begin.id"); - parameterNames.add("end.id"); - pss.setParameterKeys(parameterNames); + ListPreparedStatementSetter pss = new ListPreparedStatementSetter(); + List parameters = new ArrayList(); + parameters.add(1L); + parameters.add(4L); + pss.setParameters(parameters); itemReader.setPreparedStatementSetter(pss); } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/resource/StepExecutionPreparedStatementSetterTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/resource/ListPreparedStatementSetterTests.java similarity index 62% rename from spring-batch-core/src/test/java/org/springframework/batch/core/resource/StepExecutionPreparedStatementSetterTests.java rename to spring-batch-core/src/test/java/org/springframework/batch/core/resource/ListPreparedStatementSetterTests.java index 0d3b2658b..20bfbf299 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/resource/StepExecutionPreparedStatementSetterTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/resource/ListPreparedStatementSetterTests.java @@ -15,29 +15,26 @@ */ package org.springframework.batch.core.resource; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; -import static org.junit.Assert.*; +import javax.sql.DataSource; -import org.springframework.batch.core.JobExecution; -import org.springframework.batch.core.JobInstance; -import org.springframework.batch.core.JobParameters; -import org.springframework.batch.core.JobParametersBuilder; -import org.springframework.batch.core.StepExecution; -import org.springframework.jdbc.core.RowCallbackHandler; -import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.transaction.annotation.Transactional; -import org.springframework.beans.factory.annotation.Autowired; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; - -import javax.sql.DataSource; +import org.springframework.batch.core.StepExecution; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.RowCallbackHandler; +import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.transaction.annotation.Transactional; /** * @author Lucas Ward @@ -45,9 +42,9 @@ import javax.sql.DataSource; */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "/org/springframework/batch/core/repository/dao/data-source-context.xml") -public class StepExecutionPreparedStatementSetterTests { +public class ListPreparedStatementSetterTests { - StepExecutionPreparedStatementSetter pss; + ListPreparedStatementSetter pss; StepExecution stepExecution; @@ -61,21 +58,16 @@ public class StepExecutionPreparedStatementSetterTests { @Before public void onSetUpInTransaction() throws Exception { - pss = new StepExecutionPreparedStatementSetter(); - JobParameters jobParameters = new JobParametersBuilder().addLong("begin.id", 1L).addLong("end.id", 4L).toJobParameters(); - JobInstance jobInstance = new JobInstance(1L, jobParameters, "simpleJob"); - JobExecution jobExecution = new JobExecution(jobInstance, 2L); - stepExecution = new StepExecution("taskletStep", jobExecution, 3L); - pss.beforeStep(stepExecution); + pss = new ListPreparedStatementSetter(); + List parameters = new ArrayList(); + parameters.add(1L); + parameters.add(4L); + pss.setParameters(parameters); } @Transactional @Test public void testSetValues(){ - List parameterNames = new ArrayList(); - parameterNames.add("begin.id"); - parameterNames.add("end.id"); - pss.setParameterKeys(parameterNames); final List results = new ArrayList(); simpleJdbcTemplate.getJdbcOperations().query( "SELECT NAME from T_FOOS where ID > ? and ID < ?", @@ -93,6 +85,7 @@ public class StepExecutionPreparedStatementSetterTests { @Transactional @Test public void testAfterPropertiesSet() throws Exception{ try{ + pss.setParameters(null); pss.afterPropertiesSet(); fail(); } @@ -100,28 +93,4 @@ public class StepExecutionPreparedStatementSetterTests { //expected } } - - @Transactional @Test - public void testNonExistentProperties(){ - - List parameterNames = new ArrayList(); - parameterNames.add("badParameter"); - parameterNames.add("end.id"); - pss.setParameterKeys(parameterNames); - - try{ - simpleJdbcTemplate.getJdbcOperations().query( - "SELECT NAME from T_FOOS where ID > ? and ID < ?", - pss, - new RowCallbackHandler(){ - public void processRow(ResultSet rs) throws SQLException { - fail(); - }}); - - fail(); - }catch(IllegalStateException ex){ - //expected - } - - } }