Skip to content

Commit f2c1296

Browse files
committed
Fix ClassCastException in RunIdIncrementer
Before this commit, the RunIdIncrementer was failing with a ClassCastException if the run.id parameter is not passed as a Long. This commit makes the RunIdIncrementer more liberal in what it accepts by trying to parse the parameter to a Long. Resolves #3799
1 parent 9d2e6c6 commit f2c1296

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/RunIdIncrementer.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2018 the original author or authors.
2+
* Copyright 2006-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,12 +15,18 @@
1515
*/
1616
package org.springframework.batch.core.launch.support;
1717

18+
import org.springframework.batch.core.JobParameter;
1819
import org.springframework.batch.core.JobParameters;
1920
import org.springframework.batch.core.JobParametersBuilder;
2021
import org.springframework.batch.core.JobParametersIncrementer;
2122
import org.springframework.lang.Nullable;
2223

2324
/**
25+
* This incrementer increments a "run.id" parameter of type {@link Long}
26+
* from the given job parameters. If the parameter does not exist, it will
27+
* be initialized to 1. The parameter name can be configured using
28+
* {@link #setKey(String)}.
29+
*
2430
* @author Dave Syer
2531
* @author Mahmoud Ben Hassine
2632
*/
@@ -41,14 +47,27 @@ public void setKey(String key) {
4147

4248
/**
4349
* Increment the run.id parameter (starting with 1).
50+
*
51+
* @param parameters the previous job parameters
52+
* @return the next job parameters with an incremented (or initialized) run.id
53+
* @throws IllegalArgumentException if the previous value of run.id is invalid
4454
*/
4555
@Override
4656
public JobParameters getNext(@Nullable JobParameters parameters) {
4757

4858
JobParameters params = (parameters == null) ? new JobParameters() : parameters;
49-
50-
long id = params.getLong(key, new Long(0)) + 1;
51-
return new JobParametersBuilder(params).addLong(key, id).toJobParameters();
59+
JobParameter runIdParameter = params.getParameters().get(this.key);
60+
long id = 1;
61+
if (runIdParameter != null) {
62+
try {
63+
id = Long.parseLong(runIdParameter.getValue().toString()) + 1;
64+
}
65+
catch (NumberFormatException exception) {
66+
throw new IllegalArgumentException("Invalid value for parameter "
67+
+ this.key, exception);
68+
}
69+
}
70+
return new JobParametersBuilder(params).addLong(this.key, id).toJobParameters();
5271
}
5372

5473
}

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2013 the original author or authors.
2+
* Copyright 2006-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
/**
2525
* @author Dave Syer
2626
* @author Michael Minella
27+
* @author Mahmoud Ben Hassine
2728
*
2829
*/
2930
public class RunIdIncrementerTests {
@@ -51,4 +52,25 @@ public void testGetNextNamed() {
5152
assertEquals(1, next.getLong("foo").intValue());
5253
}
5354

55+
@Test
56+
public void testGetNextWhenRunIdIsString() {
57+
// given
58+
JobParameters parameters = new JobParametersBuilder()
59+
.addString("run.id", "5")
60+
.toJobParameters();
61+
62+
// when
63+
JobParameters next = this.incrementer.getNext(parameters);
64+
65+
// then
66+
assertEquals(Long.valueOf(6), next.getLong("run.id"));
67+
}
68+
69+
@Test(expected = IllegalArgumentException.class)
70+
public void testGetNextWhenRunIdIsInvalidString() {
71+
this.incrementer.getNext(new JobParametersBuilder()
72+
.addString("run.id", "foo")
73+
.toJobParameters());
74+
}
75+
5476
}

0 commit comments

Comments
 (0)