Merge pull request #139 from mminella/BATCH-1723
BATCH-1723: Updated JobParameters to return wrapper classes
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2006-2007 the original author or authors.
|
||||
* Copyright 2006-2013 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.
|
||||
@@ -33,45 +33,46 @@ import java.util.Map;
|
||||
* This class is immutable and therefore thread-safe.
|
||||
*
|
||||
* @author Lucas Ward
|
||||
* @author Michael Minella
|
||||
* @since 1.0
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class JobParameters implements Serializable {
|
||||
|
||||
private final Map<String,JobParameter> parameters;
|
||||
|
||||
|
||||
public JobParameters() {
|
||||
this.parameters = new LinkedHashMap<String, JobParameter>();
|
||||
}
|
||||
|
||||
|
||||
public JobParameters(Map<String,JobParameter> parameters) {
|
||||
this.parameters = new LinkedHashMap<String,JobParameter>(parameters);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Typesafe Getter for the Long represented by the provided key.
|
||||
*
|
||||
* @param key The key to get a value for
|
||||
* @return The <code>Long</code> value
|
||||
*/
|
||||
public long getLong(String key){
|
||||
public Long getLong(String key){
|
||||
if (!parameters.containsKey(key)) {
|
||||
return 0L;
|
||||
}
|
||||
Object value = parameters.get(key).getValue();
|
||||
return value==null ? 0L : ((Long)value).longValue();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Typesafe Getter for the Long represented by the provided key. If the
|
||||
* key does not exist, the default value will be returned.
|
||||
*
|
||||
* @param key to return the value for
|
||||
* @param defaultValue to return if the value doesn't exist
|
||||
* @return the parameter represented by the provided key, defaultValue
|
||||
* @return the parameter represented by the provided key, defaultValue
|
||||
* otherwise.
|
||||
*/
|
||||
public long getLong(String key, long defaultValue){
|
||||
public Long getLong(String key, long defaultValue){
|
||||
if(parameters.containsKey(key)){
|
||||
return getLong(key);
|
||||
}
|
||||
@@ -90,14 +91,14 @@ public class JobParameters implements Serializable {
|
||||
JobParameter value = parameters.get(key);
|
||||
return value==null ? null : value.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Typesafe Getter for the String represented by the provided key. If the
|
||||
* key does not exist, the default value will be returned.
|
||||
*
|
||||
* @param key to return the value for
|
||||
* @param defaultValue to return if the value doesn't exist
|
||||
* @return the parameter represented by the provided key, defaultValue
|
||||
* @return the parameter represented by the provided key, defaultValue
|
||||
* otherwise.
|
||||
*/
|
||||
public String getString(String key, String defaultValue){
|
||||
@@ -108,31 +109,31 @@ public class JobParameters implements Serializable {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Typesafe Getter for the Long represented by the provided key.
|
||||
*
|
||||
* @param key The key to get a value for
|
||||
* @return The <code>Double</code> value
|
||||
*/
|
||||
public double getDouble(String key){
|
||||
public Double getDouble(String key){
|
||||
if (!parameters.containsKey(key)) {
|
||||
return 0L;
|
||||
return 0.0;
|
||||
}
|
||||
Double value = (Double)parameters.get(key).getValue();
|
||||
return value==null ? 0.0 : value.doubleValue();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Typesafe Getter for the Double represented by the provided key. If the
|
||||
* key does not exist, the default value will be returned.
|
||||
*
|
||||
* @param key to return the value for
|
||||
* @param defaultValue to return if the value doesn't exist
|
||||
* @return the parameter represented by the provided key, defaultValue
|
||||
* @return the parameter represented by the provided key, defaultValue
|
||||
* otherwise.
|
||||
*/
|
||||
public double getDouble(String key, double defaultValue){
|
||||
public Double getDouble(String key, double defaultValue){
|
||||
if(parameters.containsKey(key)){
|
||||
return getDouble(key);
|
||||
}
|
||||
@@ -140,7 +141,7 @@ public class JobParameters implements Serializable {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Typesafe Getter for the Date represented by the provided key.
|
||||
*
|
||||
@@ -150,14 +151,14 @@ public class JobParameters implements Serializable {
|
||||
public Date getDate(String key){
|
||||
return this.getDate(key,null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Typesafe Getter for the Date represented by the provided key. If the
|
||||
* key does not exist, the default value will be returned.
|
||||
*
|
||||
* @param key to return the value for
|
||||
* @param defaultValue to return if the value doesn't exist
|
||||
* @return the parameter represented by the provided key, defaultValue
|
||||
* @return the parameter represented by the provided key, defaultValue
|
||||
* otherwise.
|
||||
*/
|
||||
public Date getDate(String key, Date defaultValue){
|
||||
@@ -168,42 +169,42 @@ public class JobParameters implements Serializable {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a map of all parameters, including string, long, and date.
|
||||
* Get a map of all parameters, including string, long, and date.
|
||||
*
|
||||
* @return an unmodifiable map containing all parameters.
|
||||
*/
|
||||
public Map<String, JobParameter> getParameters(){
|
||||
return new LinkedHashMap<String, JobParameter>(parameters);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return true if the parameters is empty, false otherwise.
|
||||
*/
|
||||
public boolean isEmpty(){
|
||||
return parameters.isEmpty();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if(obj instanceof JobParameters == false){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
if(obj == this){
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
JobParameters rhs = (JobParameters)obj;
|
||||
return this.parameters.equals(rhs.parameters);
|
||||
return this.parameters.equals(rhs.parameters);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return 17 + 23 * parameters.hashCode();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return parameters.toString();
|
||||
|
||||
@@ -76,7 +76,7 @@ public class StepExecutionSimpleCompletionPolicy extends StepExecutionListenerSu
|
||||
JobParameters jobParameters = stepExecution.getJobParameters();
|
||||
Assert.state(jobParameters.getParameters().containsKey(keyName),
|
||||
"JobParameters do not contain Long parameter with key=[" + keyName + "]");
|
||||
delegate = new SimpleCompletionPolicy((int) jobParameters.getLong(keyName));
|
||||
delegate = new SimpleCompletionPolicy(jobParameters.getLong(keyName).intValue());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.springframework.batch.core;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -14,6 +11,7 @@ import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Lucas Ward
|
||||
* @author Michael Minella
|
||||
*
|
||||
*/
|
||||
public class JobParametersBuilderTests {
|
||||
@@ -29,7 +27,7 @@ public class JobParametersBuilderTests {
|
||||
parametersBuilder.addString("STRING", "string value", false);
|
||||
JobParameters parameters = parametersBuilder.toJobParameters();
|
||||
assertEquals(date, parameters.getDate("SCHEDULE_DATE"));
|
||||
assertEquals(1L, parameters.getLong("LONG"));
|
||||
assertEquals(1L, parameters.getLong("LONG").longValue());
|
||||
assertEquals("string value", parameters.getString("STRING"));
|
||||
assertFalse(parameters.getParameters().get("SCHEDULE_DATE").isIdentifying());
|
||||
assertFalse(parameters.getParameters().get("LONG").isIdentifying());
|
||||
@@ -43,7 +41,7 @@ public class JobParametersBuilderTests {
|
||||
parametersBuilder.addString("STRING", "string value");
|
||||
JobParameters parameters = parametersBuilder.toJobParameters();
|
||||
assertEquals(date, parameters.getDate("SCHEDULE_DATE"));
|
||||
assertEquals(1L, parameters.getLong("LONG"));
|
||||
assertEquals(1L, parameters.getLong("LONG").longValue());
|
||||
assertEquals("string value", parameters.getString("STRING"));
|
||||
}
|
||||
|
||||
@@ -54,7 +52,7 @@ public class JobParametersBuilderTests {
|
||||
parametersBuilder.addString("STRING", null);
|
||||
JobParameters parameters = parametersBuilder.toJobParameters();
|
||||
assertEquals(null, parameters.getDate("SCHEDULE_DATE"));
|
||||
assertEquals(0L, parameters.getLong("LONG"));
|
||||
assertEquals(0L, parameters.getLong("LONG").longValue());
|
||||
assertEquals(null, parameters.getString("STRING"));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.springframework.batch.core;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -21,6 +18,7 @@ import org.springframework.batch.support.SerializationUtils;
|
||||
/**
|
||||
* @author Lucas Ward
|
||||
* @author Dave Syer
|
||||
* @author Michael Minella
|
||||
*
|
||||
*/
|
||||
public class JobParametersTests {
|
||||
@@ -66,8 +64,8 @@ public class JobParametersTests {
|
||||
|
||||
@Test
|
||||
public void testGetLong() {
|
||||
assertEquals(1L, parameters.getLong("long.key1"));
|
||||
assertEquals(2L, parameters.getLong("long.key2"));
|
||||
assertEquals(1L, parameters.getLong("long.key1").longValue());
|
||||
assertEquals(2L, parameters.getLong("long.key2").longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -91,12 +89,12 @@ public class JobParametersTests {
|
||||
@Test
|
||||
public void testGetEmptyLong() {
|
||||
parameters = new JobParameters(Collections.singletonMap("long1", new JobParameter((Long)null, true)));
|
||||
assertEquals(0L, parameters.getLong("long1"));
|
||||
assertEquals(0L, parameters.getLong("long1").longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetMissingLong() {
|
||||
assertEquals(0L, parameters.getLong("missing.long1"));
|
||||
assertEquals(0L, parameters.getLong("missing.long1").longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -197,7 +195,7 @@ public class JobParametersTests {
|
||||
|
||||
@Test
|
||||
public void testLongReturns0WhenKeyDoesntExit(){
|
||||
assertEquals(0L,new JobParameters().getLong("keythatdoesntexist"));
|
||||
assertEquals(0L,new JobParameters().getLong("keythatdoesntexist").longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -116,7 +116,7 @@ public class DefaultJobParametersConverterTests {
|
||||
JobParameters props = factory.getJobParameters(StringUtils.splitArrayElementsIntoProperties(args, "="));
|
||||
assertNotNull(props);
|
||||
assertEquals("myKey", props.getString("job.key"));
|
||||
assertEquals(33243243L, props.getLong("vendor.id"));
|
||||
assertEquals(33243243L, props.getLong("vendor.id").longValue());
|
||||
Date date = dateFormat.parse("01/23/2008");
|
||||
assertEquals(date, props.getDate("schedule.date"));
|
||||
}
|
||||
@@ -156,7 +156,7 @@ public class DefaultJobParametersConverterTests {
|
||||
factory.setNumberFormat(new DecimalFormat("#,###"));
|
||||
JobParameters props = factory.getJobParameters(StringUtils.splitArrayElementsIntoProperties(args, "="));
|
||||
assertNotNull(props);
|
||||
assertEquals(1000L, props.getLong("value"));
|
||||
assertEquals(1000L, props.getLong("value").longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -225,7 +225,7 @@ public class DefaultJobParametersConverterTests {
|
||||
JobParameters props = factory.getJobParameters(StringUtils.splitArrayElementsIntoProperties(args, "="));
|
||||
assertNotNull(props);
|
||||
assertEquals(1.23456, props.getDouble("value"), Double.MIN_VALUE);
|
||||
assertEquals(123456, props.getLong("long"));
|
||||
assertEquals(123456, props.getLong("long").longValue());
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2006-2010 the original author or authors.
|
||||
* Copyright 2006-2013 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,7 +15,7 @@
|
||||
*/
|
||||
package org.springframework.batch.core.launch.support;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
@@ -23,23 +23,24 @@ import org.springframework.batch.core.JobParametersBuilder;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
* @author Michael Minella
|
||||
*
|
||||
*/
|
||||
public class RunIdIncrementerTests {
|
||||
|
||||
|
||||
private RunIdIncrementer incrementer = new RunIdIncrementer();
|
||||
|
||||
@Test
|
||||
public void testGetNext() {
|
||||
JobParameters next = incrementer.getNext(null);
|
||||
assertEquals(1, next.getLong("run.id"));
|
||||
assertEquals(2, incrementer.getNext(next).getLong("run.id"));
|
||||
assertEquals(1, next.getLong("run.id").intValue());
|
||||
assertEquals(2, incrementer.getNext(next).getLong("run.id").intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNextAppends() {
|
||||
JobParameters next = incrementer.getNext(new JobParametersBuilder().addString("foo", "bar").toJobParameters());
|
||||
assertEquals(1, next.getLong("run.id"));
|
||||
assertEquals(1, next.getLong("run.id").intValue());
|
||||
assertEquals("bar", next.getString("foo"));
|
||||
}
|
||||
|
||||
@@ -47,7 +48,7 @@ public class RunIdIncrementerTests {
|
||||
public void testGetNextNamed() {
|
||||
incrementer.setKey("foo");
|
||||
JobParameters next = incrementer.getNext(null);
|
||||
assertEquals(1, next.getLong("foo"));
|
||||
assertEquals(1, next.getLong("foo").intValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user