Skip to content

Commit

Permalink
Improve error messages in JobParametersBuilder
Browse files Browse the repository at this point in the history
Resolves #4581
  • Loading branch information
fmbenhassine committed Apr 30, 2024
1 parent a1ce07e commit 21ea4d4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public JobParametersBuilder addString(String key, @NonNull String parameter) {
* @return a reference to this object.
*/
public JobParametersBuilder addString(String key, @NonNull String parameter, boolean identifying) {
Assert.notNull(parameter, "Value for parameter '" + key + "' must not be null");
this.parameterMap.put(key, new JobParameter<>(parameter, String.class, identifying));
return this;
}
Expand All @@ -128,6 +129,7 @@ public JobParametersBuilder addDate(String key, @NonNull Date parameter) {
* @return a reference to this object.
*/
public JobParametersBuilder addDate(String key, @NonNull Date parameter, boolean identifying) {
Assert.notNull(parameter, "Value for parameter '" + key + "' must not be null");
this.parameterMap.put(key, new JobParameter<>(parameter, Date.class, identifying));
return this;
}
Expand All @@ -151,6 +153,7 @@ public JobParametersBuilder addLocalDate(String key, @NonNull LocalDate paramete
* @return a reference to this object.
*/
public JobParametersBuilder addLocalDate(String key, @NonNull LocalDate parameter, boolean identifying) {
Assert.notNull(parameter, "Value for parameter '" + key + "' must not be null");
this.parameterMap.put(key, new JobParameter<>(parameter, LocalDate.class, identifying));
return this;
}
Expand All @@ -174,6 +177,7 @@ public JobParametersBuilder addLocalTime(String key, @NonNull LocalTime paramete
* @return a reference to this object.
*/
public JobParametersBuilder addLocalTime(String key, @NonNull LocalTime parameter, boolean identifying) {
Assert.notNull(parameter, "Value for parameter '" + key + "' must not be null");
this.parameterMap.put(key, new JobParameter<>(parameter, LocalTime.class, identifying));
return this;
}
Expand All @@ -197,6 +201,7 @@ public JobParametersBuilder addLocalDateTime(String key, @NonNull LocalDateTime
* @return a reference to this object.
*/
public JobParametersBuilder addLocalDateTime(String key, @NonNull LocalDateTime parameter, boolean identifying) {
Assert.notNull(parameter, "Value for parameter '" + key + "' must not be null");
this.parameterMap.put(key, new JobParameter<>(parameter, LocalDateTime.class, identifying));
return this;
}
Expand All @@ -220,6 +225,7 @@ public JobParametersBuilder addLong(String key, @NonNull Long parameter) {
* @return a reference to this object.
*/
public JobParametersBuilder addLong(String key, @NonNull Long parameter, boolean identifying) {
Assert.notNull(parameter, "Value for parameter '" + key + "' must not be null");
this.parameterMap.put(key, new JobParameter<>(parameter, Long.class, identifying));
return this;
}
Expand All @@ -243,6 +249,7 @@ public JobParametersBuilder addDouble(String key, @NonNull Double parameter) {
* @return a reference to this object.
*/
public JobParametersBuilder addDouble(String key, @NonNull Double parameter, boolean identifying) {
Assert.notNull(parameter, "Value for parameter '" + key + "' must not be null");
this.parameterMap.put(key, new JobParameter<>(parameter, Double.class, identifying));
return this;
}
Expand Down Expand Up @@ -285,27 +292,28 @@ public JobParametersBuilder addJobParameter(String key, JobParameter<?> jobParam
/**
* Add a job parameter.
* @param name the name of the parameter
* @param value the value of the parameter
* @param value the value of the parameter. Must not be {@code null}.
* @param type the type of the parameter
* @param identifying true if the parameter is identifying. false otherwise
* @return a reference to this object.
* @param <T> the type of the parameter
* @since 5.0
*/
public <T> JobParametersBuilder addJobParameter(String name, T value, Class<T> type, boolean identifying) {
public <T> JobParametersBuilder addJobParameter(String name, @NonNull T value, Class<T> type, boolean identifying) {
Assert.notNull(value, "Value for parameter '" + name + "' must not be null");
return addJobParameter(name, new JobParameter<>(value, type, identifying));
}

/**
* Add an identifying job parameter.
* @param name the name of the parameter
* @param value the value of the parameter
* @param value the value of the parameter. Must not be {@code null}.
* @param type the type of the parameter
* @return a reference to this object.
* @param <T> the type of the parameter
* @since 5.0
*/
public <T> JobParametersBuilder addJobParameter(String name, T value, Class<T> type) {
public <T> JobParametersBuilder addJobParameter(String name, @NonNull T value, Class<T> type) {
return addJobParameter(name, value, type, true);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2008-2023 the original author or authors.
* Copyright 2008-2024 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 @@ -22,6 +22,7 @@
import java.util.Map;
import java.util.Set;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -85,6 +86,13 @@ void testAddingExistingJobParameters() {
assertEquals(finalParams.getString("baz"), "quix");
}

@Test
void testAddingNullJobParameters() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> new JobParametersBuilder().addString("foo", null).toJobParameters());
Assertions.assertEquals("Value for parameter 'foo' must not be null", exception.getMessage());
}

@Test
void testNonIdentifyingParameters() {
this.parametersBuilder.addDate("SCHEDULE_DATE", date, false);
Expand Down

0 comments on commit 21ea4d4

Please sign in to comment.