Skip to content

Commit 8b96e46

Browse files
author
dsyer
committed
BATCH-1579: probe a bit with some unit tests, but not finding any issues
1 parent 0771bcb commit 8b96e46

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed

spring-batch-core/src/test/java/org/springframework/batch/core/job/SimpleJobTests.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.springframework.batch.core.JobInstance;
4444
import org.springframework.batch.core.JobInterruptedException;
4545
import org.springframework.batch.core.JobParameters;
46+
import org.springframework.batch.core.JobParametersBuilder;
4647
import org.springframework.batch.core.Step;
4748
import org.springframework.batch.core.StepExecution;
4849
import org.springframework.batch.core.UnexpectedJobExecutionException;
@@ -387,6 +388,29 @@ public void testRestart() throws Exception {
387388
assertFalse(step2.passedInStepContext.isEmpty());
388389
}
389390

391+
@Test
392+
public void testRestartWithNullParameter() throws Exception {
393+
394+
JobParameters jobParameters = new JobParametersBuilder().addString("foo", null).toJobParameters();
395+
jobExecution = jobRepository.createJobExecution(job.getName(), jobParameters);
396+
jobInstance = jobExecution.getJobInstance();
397+
398+
step1.setAllowStartIfComplete(true);
399+
final RuntimeException exception = new RuntimeException("Foo!");
400+
step2.setProcessException(exception);
401+
402+
job.execute(jobExecution);
403+
Throwable e = jobExecution.getAllFailureExceptions().get(0);
404+
assertSame(exception, e);
405+
406+
jobExecution = jobRepository.createJobExecution(job.getName(), jobParameters);
407+
job.execute(jobExecution);
408+
e = jobExecution.getAllFailureExceptions().get(0);
409+
assertSame(exception, e);
410+
assertTrue(step1.passedInStepContext.isEmpty());
411+
assertFalse(step2.passedInStepContext.isEmpty());
412+
}
413+
390414
@Test
391415
public void testInterruptWithListener() throws Exception {
392416
step1.setProcessException(new JobInterruptedException("job interrupted!"));
@@ -408,7 +432,7 @@ public void testInterruptWithListener() throws Exception {
408432
* Execution context should be restored on restart.
409433
*/
410434
@Test
411-
public void testRestartScenario() throws Exception {
435+
public void testRestartAndExecutionContextRestored() throws Exception {
412436

413437
job.setRestartable(true);
414438

spring-batch-core/src/test/java/org/springframework/batch/core/launch/SimpleJobLauncherTests.java

+28-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void testRun() throws Exception {
7979
run(ExitStatus.COMPLETED);
8080
}
8181

82-
@Test(expected=JobParametersInvalidException.class)
82+
@Test(expected = JobParametersInvalidException.class)
8383
public void testRunWithValidator() throws Exception {
8484

8585
job.setJobParametersValidator(new DefaultJobParametersValidator(new String[] { "missing-and-required" },
@@ -98,6 +98,32 @@ public void testRunWithValidator() throws Exception {
9898

9999
}
100100

101+
@Test
102+
public void testRunRestartableJobInstanceTwice() throws Exception {
103+
job = new JobSupport("foo") {
104+
@Override
105+
public boolean isRestartable() {
106+
return true;
107+
}
108+
109+
@Override
110+
public void execute(JobExecution execution) {
111+
execution.setExitStatus(ExitStatus.COMPLETED);
112+
return;
113+
}
114+
};
115+
116+
testRun();
117+
reset(jobRepository);
118+
expect(jobRepository.getLastJobExecution(job.getName(), jobParameters)).andReturn(
119+
new JobExecution(new JobInstance(1L, jobParameters, job.getName())));
120+
expect(jobRepository.createJobExecution(job.getName(), jobParameters)).andReturn(
121+
new JobExecution(new JobInstance(1L, jobParameters, job.getName())));
122+
replay(jobRepository);
123+
jobLauncher.run(job, jobParameters);
124+
verify(jobRepository);
125+
}
126+
101127
/*
102128
* Non-restartable JobInstance can be run only once - attempt to run
103129
* existing non-restartable JobInstance causes error.
@@ -231,7 +257,7 @@ public void testInitialiseWithRepository() throws Exception {
231257
jobLauncher.setJobRepository(jobRepository);
232258
jobLauncher.afterPropertiesSet(); // no error
233259
}
234-
260+
235261
private void run(ExitStatus exitStatus) throws Exception {
236262
JobExecution jobExecution = new JobExecution(null, null);
237263

spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/AbstractJobInstanceDaoTests.java

+25-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public abstract class AbstractJobInstanceDaoTests extends AbstractTransactionalJ
2222

2323
private static final long DATE = 777;
2424

25-
protected JobInstanceDao dao = new MapJobInstanceDao();
25+
protected JobInstanceDao dao;
2626

2727
private String fooJob = "foo";
2828

@@ -61,6 +61,29 @@ public void testCreateAndRetrieve() throws Exception {
6161
assertEquals(new Date(DATE), retrievedParams.getDate("dateKey"));
6262
}
6363

64+
/*
65+
* Create and retrieve a job instance.
66+
*/
67+
@Transactional
68+
@Test
69+
public void testCreateAndRetrieveWithNullParameter() throws Exception {
70+
71+
JobParameters jobParameters = new JobParametersBuilder().addString("foo", null).toJobParameters();
72+
73+
JobInstance fooInstance = dao.createJobInstance(fooJob, jobParameters);
74+
assertNotNull(fooInstance.getId());
75+
assertEquals(fooJob, fooInstance.getJobName());
76+
assertEquals(jobParameters, fooInstance.getJobParameters());
77+
78+
JobInstance retrievedInstance = dao.getJobInstance(fooJob, jobParameters);
79+
JobParameters retrievedParams = retrievedInstance.getJobParameters();
80+
assertEquals(fooInstance, retrievedInstance);
81+
assertEquals(fooJob, retrievedInstance.getJobName());
82+
assertEquals(jobParameters, retrievedParams);
83+
84+
assertEquals(null, retrievedParams.getString("foo"));
85+
}
86+
6487
/*
6588
* Create and retrieve a job instance.
6689
*/
@@ -211,7 +234,7 @@ public void testCreationAddsVersion() {
211234
}
212235

213236
public void testGetJobInstanceByExecutionId() {
214-
237+
// TODO: test this (or maybe the method isn't needed or has wrong signature)
215238
}
216239

217240
}

0 commit comments

Comments
 (0)