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
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2006-2018 the original author or authors.
|
||||
* Copyright 2006-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,12 +15,18 @@
|
||||
*/
|
||||
package org.springframework.batch.core.launch.support;
|
||||
|
||||
import org.springframework.batch.core.JobParameter;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.JobParametersIncrementer;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* This incrementer increments a "run.id" parameter of type {@link Long}
|
||||
* from the given job parameters. If the parameter does not exist, it will
|
||||
* be initialized to 1. The parameter name can be configured using
|
||||
* {@link #setKey(String)}.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Mahmoud Ben Hassine
|
||||
*/
|
||||
@@ -41,14 +47,27 @@ public class RunIdIncrementer implements JobParametersIncrementer {
|
||||
|
||||
/**
|
||||
* Increment the run.id parameter (starting with 1).
|
||||
*
|
||||
* @param parameters the previous job parameters
|
||||
* @return the next job parameters with an incremented (or initialized) run.id
|
||||
* @throws IllegalArgumentException if the previous value of run.id is invalid
|
||||
*/
|
||||
@Override
|
||||
public JobParameters getNext(@Nullable JobParameters parameters) {
|
||||
|
||||
JobParameters params = (parameters == null) ? new JobParameters() : parameters;
|
||||
|
||||
long id = params.getLong(key, new Long(0)) + 1;
|
||||
return new JobParametersBuilder(params).addLong(key, id).toJobParameters();
|
||||
JobParameter runIdParameter = params.getParameters().get(this.key);
|
||||
long id = 1;
|
||||
if (runIdParameter != null) {
|
||||
try {
|
||||
id = Long.parseLong(runIdParameter.getValue().toString()) + 1;
|
||||
}
|
||||
catch (NumberFormatException exception) {
|
||||
throw new IllegalArgumentException("Invalid value for parameter "
|
||||
+ this.key, exception);
|
||||
}
|
||||
}
|
||||
return new JobParametersBuilder(params).addLong(this.key, id).toJobParameters();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2006-2013 the original author or authors.
|
||||
* Copyright 2006-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -24,6 +24,7 @@ import org.springframework.batch.core.JobParametersBuilder;
|
||||
/**
|
||||
* @author Dave Syer
|
||||
* @author Michael Minella
|
||||
* @author Mahmoud Ben Hassine
|
||||
*
|
||||
*/
|
||||
public class RunIdIncrementerTests {
|
||||
@@ -51,4 +52,25 @@ public class RunIdIncrementerTests {
|
||||
assertEquals(1, next.getLong("foo").intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNextWhenRunIdIsString() {
|
||||
// given
|
||||
JobParameters parameters = new JobParametersBuilder()
|
||||
.addString("run.id", "5")
|
||||
.toJobParameters();
|
||||
|
||||
// when
|
||||
JobParameters next = this.incrementer.getNext(parameters);
|
||||
|
||||
// then
|
||||
assertEquals(Long.valueOf(6), next.getLong("run.id"));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testGetNextWhenRunIdIsInvalidString() {
|
||||
this.incrementer.getNext(new JobParametersBuilder()
|
||||
.addString("run.id", "foo")
|
||||
.toJobParameters());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user