BATCH-910: A new job has been added to showcase the JobParametersIncrementer usage of JobOperator, and the non-sequential step was updated to use a file name coming from JobParameters.

This commit is contained in:
lucasward
2008-11-11 20:46:54 +00:00
parent 647d9d7439
commit 12c4fb0e59
15 changed files with 410 additions and 141 deletions

View File

@@ -0,0 +1,105 @@
package org.springframework.batch.sample;
import static org.junit.Assert.*;
import java.util.Map;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.launch.JobOperator;
import org.springframework.batch.core.launch.JobParametersNotFoundException;
import org.springframework.batch.core.launch.NoSuchJobException;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.jdbc.SimpleJdbcTestUtils;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/incrementer-job-launcher-context.xml" })
public class IncrementerJobFunctionalTests {
private SimpleJdbcTemplate simpleJdbcTemplate;
@Autowired
private JobOperator jobOperator;
/**
* This test calls the same job twice. However, using a job incrementer, the
* second launching is a separate job instance.<br>
* <br>
* Conditions:
* <ul>
* <li>Two flat files, each containing 20 player records
* <li>Job is started twice, using the job incrementer to chose the input
* file.
* </ul>
* Expected Results:
* <ul>
* <li>First run completes with 20 players in the database
* <li>Second run completes with 40 players in the database.
* </ul>
*/
@Test
public void testWithSkips() throws Exception {
simpleJdbcTemplate.update("DELETE from PLAYERS");
long id1 = this.launchJob();
Map<String, Object> execution1 = this.getJobExecution(id1);
assertEquals("COMPLETED", execution1.get("STATUS"));
assertEquals(20, this.countPlayers());
long id2 = this.launchJob();
Map<String, Object> execution2 = this.getJobExecution(id2);
assertEquals("COMPLETED", execution2.get("STATUS"));
assertEquals(40, this.countPlayers());
assertTrue(id1 != id2);
assertTrue(!execution1.get("JOB_INSTANCE_ID").equals(execution2.get("JOB_INSTANCE_ID")));
}
private Map<String, Object> getJobExecution(long jobExecutionId) {
return simpleJdbcTemplate.queryForMap("SELECT * from BATCH_JOB_EXECUTION where JOB_EXECUTION_ID = ?",
jobExecutionId);
}
private int countPlayers() {
return SimpleJdbcTestUtils.countRowsInTable(simpleJdbcTemplate, "PLAYERS");
}
/**
* Launch the entire job, including all steps, in order.
*
* @return JobExecution, so that the test may validate the exit status
*/
public long launchJob() {
try {
return this.jobOperator.startNextInstance("incrementerJob");
}
catch (NoSuchJobException e) {
throw new RuntimeException(e);
}
catch (JobExecutionAlreadyRunningException e) {
throw new RuntimeException(e);
}
catch (JobParametersNotFoundException e) {
throw new RuntimeException(e);
}
catch (JobRestartException e) {
throw new RuntimeException(e);
}
catch (JobInstanceAlreadyCompleteException e) {
throw new RuntimeException(e);
}
}
@Autowired
public void setDataSource(DataSource dataSource) {
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
}

View File

@@ -1,40 +1,10 @@
package org.springframework.batch.sample;
import static org.junit.Assert.assertEquals;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.test.AbstractJobTests;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.jdbc.SimpleJdbcTestUtils;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/nonSequentialDecisionJob.xml" })
public class NonSequentialDecisionJobFunctionalTests extends AbstractJobTests {
private SimpleJdbcTemplate simpleJdbcTemplate;
@Test
public void testWithSkips() throws Exception {
simpleJdbcTemplate.update("DELETE from ERROR_LOG");
simpleJdbcTemplate.update("DELETE from PLAYER_SUMMARY");
simpleJdbcTemplate.update("DELETE from PLAYERS");
simpleJdbcTemplate.update("DELETE from GAMES");
assertEquals(BatchStatus.COMPLETED, this.launchJob().getStatus());
assertEquals(1, SimpleJdbcTestUtils.countRowsInTable(simpleJdbcTemplate, "ERROR_LOG"));
assertEquals(9, SimpleJdbcTestUtils.countRowsInTable(simpleJdbcTemplate, "PLAYER_SUMMARY"));
}
@Autowired
public void setDataSource(DataSource dataSource) {
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
public class NonSequentialDecisionJobFunctionalTests extends NonSequentialJobFunctionalTestsBase {
}

View File

@@ -1,41 +1,10 @@
package org.springframework.batch.sample;
import static org.junit.Assert.assertEquals;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.test.AbstractJobTests;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.jdbc.SimpleJdbcTestUtils;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/nonSequentialJob.xml" })
public class NonSequentialJobFunctionalTests extends AbstractJobTests {
private SimpleJdbcTemplate simpleJdbcTemplate;
@Test
public void testWithSkips() throws Exception {
simpleJdbcTemplate.update("DELETE from ERROR_LOG");
simpleJdbcTemplate.update("DELETE from PLAYER_SUMMARY");
simpleJdbcTemplate.update("DELETE from PLAYERS");
simpleJdbcTemplate.update("DELETE from GAMES");
assertEquals(BatchStatus.COMPLETED, this.launchJob().getStatus());
assertEquals(1, SimpleJdbcTestUtils.countRowsInTable(simpleJdbcTemplate, "ERROR_LOG"));
assertEquals(9, SimpleJdbcTestUtils.countRowsInTable(simpleJdbcTemplate, "PLAYER_SUMMARY"));
}
@Autowired
public void setDataSource(DataSource dataSource) {
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
public class NonSequentialJobFunctionalTests extends NonSequentialJobFunctionalTestsBase {
}

View File

@@ -0,0 +1,89 @@
package org.springframework.batch.sample;
import static org.junit.Assert.assertEquals;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.junit.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.test.AbstractJobTests;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.jdbc.SimpleJdbcTestUtils;
public abstract class NonSequentialJobFunctionalTestsBase extends AbstractJobTests {
private SimpleJdbcTemplate simpleJdbcTemplate;
/**
* This test processes a file that contains bad records. Those records will
* skip. The step execution listener will detect that skips have occurred,
* and return an exit status that directs the flow job to the error logging
* step. The error logging step will log an error. <br>
* <br>
* Conditions:
* <ul>
* <li>Flat file containing 20 player records, 5 are invalid
* <li>Skipping is allowed
* </ul>
* Expected Results:
* <ul>
* <li>15 player records written to the database
* <li>1 error logged to the database
* </ul>
*/
@Test
public void testWithSkips() throws Exception {
launchTest("player-containsBadRecords.csv");
assertEquals(1, SimpleJdbcTestUtils.countRowsInTable(simpleJdbcTemplate, "ERROR_LOG"));
assertEquals(15, SimpleJdbcTestUtils.countRowsInTable(simpleJdbcTemplate, "PLAYERS"));
}
/**
* This test processes a file that contains all valid record. The step
* execution listener will detect that NO skips have occurred, and return an
* exit status that direct the flow job to bypass the error logging step.<br>
* <br>
* Conditions:
* <ul>
* <li>Flat file containing 20 player records, all are valid
* <li>Skipping is allowed
* </ul>
* Expected Results:
* <ul>
* <li>20 player records written to the database
* <li>NO errors logged to the database
* </ul>
*/
@Test
public void testWithoutSkips() throws Exception {
launchTest("player-small1.csv");
assertEquals(0, SimpleJdbcTestUtils.countRowsInTable(simpleJdbcTemplate, "ERROR_LOG"));
assertEquals(20, SimpleJdbcTestUtils.countRowsInTable(simpleJdbcTemplate, "PLAYERS"));
}
private void launchTest(String playerInputfile) throws Exception {
simpleJdbcTemplate.update("DELETE from ERROR_LOG");
simpleJdbcTemplate.update("DELETE from PLAYER_SUMMARY");
simpleJdbcTemplate.update("DELETE from PLAYERS");
simpleJdbcTemplate.update("DELETE from GAMES");
Map<String, JobParameter> parameters = new HashMap<String, JobParameter>();
parameters.put("timestamp", new JobParameter(new Date().getTime()));
parameters.put("player.file.name", new JobParameter(playerInputfile));
JobParameters jobParameters = new JobParameters(parameters);
assertEquals(BatchStatus.COMPLETED, this.launchJob(jobParameters).getStatus());
}
@Autowired
public void setDataSource(DataSource dataSource) {
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
}