Skip to content

Commit

Permalink
Fix ClassCastException in RunIdIncrementer
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fmbenhassine committed Nov 9, 2020
1 parent 9d2e6c6 commit f2c1296
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
*/
Expand All @@ -41,14 +47,27 @@ public void setKey(String key) {

/**
* 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();
}

}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -24,6 +24,7 @@
/**
* @author Dave Syer
* @author Michael Minella
* @author Mahmoud Ben Hassine
*
*/
public class RunIdIncrementerTests {
Expand Down Expand Up @@ -51,4 +52,25 @@ public void testGetNextNamed() {
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());
}

}

0 comments on commit f2c1296

Please sign in to comment.