Fix NPE in JobParameters.toProperties on null parameter value

Issue #834
This commit is contained in:
Taeik Lim
2021-03-11 02:32:56 +09:00
committed by Mahmoud Ben Hassine
parent 44034afe7e
commit f33fa4d261
2 changed files with 19 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2018 the original author or authors.
* Copyright 2006-2021 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.
@@ -20,6 +20,7 @@ import java.io.Serializable;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import org.springframework.lang.Nullable;
@@ -38,6 +39,7 @@ import org.springframework.lang.Nullable;
* @author Lucas Ward
* @author Michael Minella
* @author Mahmoud Ben Hassine
* @author Taeik Lim
* @since 1.0
*/
@SuppressWarnings("serial")
@@ -269,8 +271,8 @@ public class JobParameters implements Serializable {
Properties props = new Properties();
for (Map.Entry<String, JobParameter> param : parameters.entrySet()) {
if(param.getValue() != null) {
props.put(param.getKey(), param.getValue().toString());
if (param.getValue() != null) {
props.put(param.getKey(), Objects.toString(param.getValue().toString(), ""));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2018 the original author or authors.
* Copyright 2008-2021 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.
@@ -25,6 +25,7 @@ import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import org.junit.Before;
import org.junit.Test;
@@ -35,6 +36,7 @@ import org.springframework.util.SerializationUtils;
* @author Dave Syer
* @author Michael Minella
* @author Mahmoud Ben Hassine
* @author Taeik Lim
*
*/
public class JobParametersTests {
@@ -228,4 +230,15 @@ public class JobParametersTests {
public void testDateReturnsNullWhenKeyDoesntExit(){
assertNull(new JobParameters().getDate("keythatdoesntexist"));
}
@Test
public void testToPropertiesWithNullValue() {
Map<String, JobParameter> parameterMap = new HashMap<>();
Long value = null;
parameterMap.put("nullkey", new JobParameter(value));
JobParameters jobParameters = new JobParameters(parameterMap);
Properties properties = jobParameters.toProperties();
assertEquals("", properties.get("nullkey"));
}
}