diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/BatchStatus.java b/spring-batch-core/src/main/java/org/springframework/batch/core/BatchStatus.java index 09fe63217..f276391be 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/BatchStatus.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/BatchStatus.java @@ -16,73 +16,14 @@ package org.springframework.batch.core; -import java.io.ObjectStreamException; -import java.io.Serializable; /** - * Typesafe enumeration representing the status of an artifact within the batch environment. See Effective Java - * Programming by Joshua Bloch for more details on the pattern used. - * - * A BatchStatus can be safely serialized, however, it should be noted that the pattern can break down if different - * class loaders load the enumeration. - * - * This class is immutable and therefore thread-safe. + * Enumeration representing the status of a an Execution. * * @author Lucas Ward - * @author Greg Kick */ -public class BatchStatus implements Serializable { +public enum BatchStatus { - private static final long serialVersionUID = 1634960297477743037L; - - private final String name; - - private BatchStatus(String name) { - this.name = name; - } - - private Object readResolve() throws ObjectStreamException { - return getStatus(name); - } - - public String toString() { - return name; - } - - public static final BatchStatus COMPLETED = new BatchStatus("COMPLETED"); - - public static final BatchStatus STARTED = new BatchStatus("STARTED"); - - public static final BatchStatus STARTING = new BatchStatus("STARTING"); - - public static final BatchStatus FAILED = new BatchStatus("FAILED"); - - public static final BatchStatus STOPPING = new BatchStatus("STOPPING"); - - public static final BatchStatus STOPPED = new BatchStatus("STOPPED"); - - public static final BatchStatus UNKNOWN = new BatchStatus("UNKNOWN"); - - private static final BatchStatus[] VALUES = { STARTING, STARTED, COMPLETED, FAILED, STOPPING, STOPPED, UNKNOWN }; - - /** - * Given a string representation of a status, return the appropriate BatchStatus. - * - * @param statusAsString string representation of a status - * @return a valid BatchStatus or null if the input is null - * @throws IllegalArgumentException if no status matches provided string. - */ - public static BatchStatus getStatus(String statusAsString) { - if (statusAsString == null) { - return null; - } - final String upperCaseStatusAsString = statusAsString.toUpperCase(); - for (int i = 0; i < VALUES.length; i++) { - if (VALUES[i].toString().equals(upperCaseStatusAsString)) { - return VALUES[i]; - } - } - throw new IllegalArgumentException("The string did not match a valid status."); - } + COMPLETED, STARTED, STARTING, FAILED, STOPPING, STOPPED, UNKNOWN; } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/JobParameter.java b/spring-batch-core/src/main/java/org/springframework/batch/core/JobParameter.java new file mode 100644 index 000000000..03d0bb1f7 --- /dev/null +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/JobParameter.java @@ -0,0 +1,112 @@ +/** + * + */ +package org.springframework.batch.core; + +import java.io.Serializable; +import java.util.Date; + +import org.apache.commons.lang.builder.HashCodeBuilder; + +/** + * Domain representation of a parameter to a batch job. Only the following types can be + * parameters: String, Long, Date, and Double. + * + * @author Lucas Ward + * @since 2.0 + * + */ +public class JobParameter implements Serializable{ + + private final Object parameter; + private final ParameterType parameterType; + + /** + * Construct a new JobParameter as a String. + */ + public JobParameter(String parameter){ + this.parameter = parameter; + parameterType = ParameterType.STRING; + } + + /** + * Construct a new JobParameter as a Long. + * + * @param parameter + */ + public JobParameter(Long parameter){ + this.parameter = parameter; + parameterType = ParameterType.LONG; + } + + /** + * Construct a new JobParameter as a Date. + * + * @param parameter + */ + public JobParameter(Date parameter) { + this.parameter = new Date(parameter.getTime()); + parameterType = ParameterType.DATE; + } + + /** + * Construct a new JobParameter as a Double. + * + * @param parameter + */ + public JobParameter(Double parameter){ + this.parameter = parameter; + parameterType = ParameterType.DOUBLE; + } + + /** + * @return the value contained within this JobParameter. + */ + public Object getValue(){ + + if(parameter.getClass().isInstance(Date.class)){ + return new Date(((Date)parameter).getTime()); + } + else{ + return parameter; + } + } + + /** + * @return a ParameterType representing the type of this parameter. + */ + public ParameterType getType(){ + return parameterType; + } + + @Override + public boolean equals(Object obj) { + if(obj instanceof JobParameter == false){ + return false; + } + + if(this == obj){ + return true; + } + + JobParameter rhs = (JobParameter)obj; + return this.parameter.equals(rhs.parameter); + } + + @Override + public String toString() { + return parameter.toString(); + } + + public int hashCode() { + return new HashCodeBuilder(7, 21).append(parameter).toHashCode(); + } + + /** + * Enumeration representing the type of a JobParameter. + */ + public enum ParameterType{ + + STRING, DATE, LONG, DOUBLE; + } +} diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/JobParameters.java b/spring-batch-core/src/main/java/org/springframework/batch/core/JobParameters.java index 8ed46858f..0041b25ae 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/JobParameters.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/JobParameters.java @@ -4,12 +4,9 @@ package org.springframework.batch.core; import java.io.Serializable; -import java.util.Collections; import java.util.Date; -import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; -import java.util.Map.Entry; import org.apache.commons.lang.builder.HashCodeBuilder; @@ -29,43 +26,42 @@ import org.apache.commons.lang.builder.HashCodeBuilder; */ public class JobParameters implements Serializable { - private final Map stringMap; - - private final Map longMap; - - private final Map doubleMap; - - private final Map dateMap; - - /** - * Default constructor. Creates a new empty JobRuntimeParameters. It should - * be noted that this constructor should only be used if an empty parameters - * is needed, since JobRuntimeParameters is immutable. - */ + private final Map parameters; + public JobParameters() { - this.stringMap = new LinkedHashMap(); - this.longMap = new LinkedHashMap(); - this.doubleMap = new LinkedHashMap(); - this.dateMap = new LinkedHashMap(); + this.parameters = new LinkedHashMap(); } - + + public JobParameters(Map parameters) { + this.parameters = new LinkedHashMap(parameters); + } + /** - * Create a new parameters object based upon the maps for each of the - * supported data types. See {@link JobParametersBuilder} for an easier way - * to create parameters. + * Typesafe Getter for the Long represented by the provided key. + * + * @param key The key to get a value for + * @return The Long value */ - public JobParameters(Map stringMap, Map longMap, Map doubleMap, - Map dateMap) { - super(); - - validateMap(stringMap, String.class); - validateMap(longMap, Long.class); - validateMap(doubleMap, Double.class); - validateMap(dateMap, Date.class); - this.stringMap = new LinkedHashMap(stringMap); - this.longMap = new LinkedHashMap(longMap); - this.doubleMap = new LinkedHashMap(doubleMap); - this.dateMap = copyDateMap(dateMap); + public long getLong(String key){ + return ((Long)parameters.get(key).getValue()).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 + * otherwise. + */ + public long getLong(String key, long defaultValue){ + if(parameters.containsKey(key)){ + return getLong(key); + } + else{ + return defaultValue; + } } /** @@ -74,40 +70,84 @@ public class JobParameters implements Serializable { * @param key The key to get a value for * @return The String value */ - public String getString(String key) { - return stringMap.get(key); + public String getString(String key){ + return parameters.get(key).toString(); } - + /** - * Typesafe Getter for the Long represented by the provided key. + * Typesafe Getter for the String represented by the provided key. If the + * key does not exist, the default value will be returned. * - * @param key The key to get a value for - * @return The Long value + * @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 + * otherwise. */ - public Long getLong(String key) { - return longMap.get(key); + public String getString(String key, String defaultValue){ + if(parameters.containsKey(key)){ + return getString(key); + } + else{ + return defaultValue; + } } - + /** * Typesafe Getter for the Long represented by the provided key. * * @param key The key to get a value for * @return The Double value */ - public Double getDouble(String key) { - return doubleMap.get(key); + public double getDouble(String key){ + return ((Double)parameters.get(key).getValue()).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 + * otherwise. + */ + public double getDouble(String key, double defaultValue){ + if(parameters.containsKey(key)){ + return getDouble(key); + } + else{ + return defaultValue; + } + } + /** * Typesafe Getter for the Date represented by the provided key. * * @param key The key to get a value for * @return The java.util.Date value */ - public Date getDate(String key) { - return dateMap.get(key); + public Date getDate(String key){ + return (Date)parameters.get(key).getValue(); } - + + /** + * 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 + * otherwise. + */ + public Date getDate(String key, Date defaultValue){ + if(parameters.containsKey(key)){ + return getDate(key); + } + else{ + return defaultValue; + } + } + /** * Get a map of all parameters, including string, long, and date. It should * be noted that a Collections$UnmodifiableMap is returned, ensuring @@ -115,119 +155,38 @@ public class JobParameters implements Serializable { * * @return an unmodifiable map containing all parameters. */ - public Map getParameters() { - Map tempMap = new LinkedHashMap(stringMap); - tempMap.putAll(longMap); - tempMap.putAll(doubleMap); - tempMap.putAll(dateMap); - return Collections.unmodifiableMap(tempMap); + public Map getParameters(){ + return new LinkedHashMap(parameters); } - - /** - * Get a map of only string parameters. - * - * @return String parameters. - */ - public Map getStringParameters() { - return Collections.unmodifiableMap(stringMap); - } - - /** - * Get a map of only Long parameters - * - * @return long parameters. - */ - public Map getLongParameters() { - return Collections.unmodifiableMap(longMap); - } - - /** - * Get a map of only Double parameters - * - * @return long parameters. - */ - public Map getDoubleParameters() { - return Collections.unmodifiableMap(doubleMap); - } - - /** - * Get a map of only Date parameters - * - * @return date parameters. - */ - public Map getDateParameters() { - return Collections.unmodifiableMap(dateMap); - } - + /** * @return true if the prameters is empty, false otherwise. */ - public boolean isEmpty() { - return (dateMap.isEmpty() && longMap.isEmpty() && doubleMap.isEmpty() && stringMap.isEmpty()); + public boolean isEmpty(){ + return parameters.isEmpty(); } - - /* - * Convenience method for validating that a the provided map only contains a - * particular type as a value, with only a String as a key. - */ - @SuppressWarnings("unchecked") - private void validateMap(Map map, Class type) { - - for (Iterator it = map.entrySet().iterator(); it.hasNext();) { - - Entry entry = (Entry) it.next(); - if (entry.getKey() instanceof String == false) { - throw new IllegalArgumentException("All parameter keys must be strings."); - } - if (entry.getValue().getClass() != type) { - throw new IllegalArgumentException("The values in this map must be of type:[" + type + "]."); - } - } - } - - /* - * Convenience method for copying Date values to ensure immutability. - */ - private Map copyDateMap(Map dateMap) { - Map tempMap = new LinkedHashMap(); - - for (Entry entry : dateMap.entrySet()) { - tempMap.put(entry.getKey(), new Date(entry.getValue().getTime())); - } - - return tempMap; - } - + @Override public boolean equals(Object obj) { - - if (obj instanceof JobParameters == false) { + if(obj instanceof JobParameters == false){ return false; } - - if (this == obj) { + + if(obj == this){ return true; } - - JobParameters parameters = (JobParameters) obj; - - // Since the type contained by each map is known, it's safe to call - // Map.equals() - if (getParameters().equals(parameters.getParameters())) { - return true; - } - else { - return false; - } + + JobParameters rhs = (JobParameters)obj; + return this.parameters.equals(rhs.parameters); } - + + @Override public int hashCode() { - return new HashCodeBuilder(7, 21).append(stringMap).append(longMap).append(doubleMap).append(dateMap) - .toHashCode(); + return new HashCodeBuilder(7, 21).append(parameters).toHashCode(); } - + + @Override public String toString() { - - return stringMap.toString() + longMap.toString() + doubleMap.toString() + dateMap.toString(); + return parameters.toString(); } } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/JobParametersBuilder.java b/spring-batch-core/src/main/java/org/springframework/batch/core/JobParametersBuilder.java index 493336316..0792b9f32 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/JobParametersBuilder.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/JobParametersBuilder.java @@ -23,23 +23,14 @@ import org.springframework.util.Assert; */ public class JobParametersBuilder { - private final Map stringMap; - - private final Map longMap; - - private final Map doubleMap; - - private final Map dateMap; + private final Map parameterMap; /** * Default constructor. Initializes the builder */ public JobParametersBuilder() { - this.stringMap = new LinkedHashMap(); - this.longMap = new LinkedHashMap(); - this.doubleMap = new LinkedHashMap(); - this.dateMap = new LinkedHashMap(); + this.parameterMap = new LinkedHashMap(); } /** @@ -51,7 +42,7 @@ public class JobParametersBuilder { */ public JobParametersBuilder addString(String key, String parameter) { Assert.notNull(parameter, "Parameter must not be null."); - stringMap.put(key, parameter); + parameterMap.put(key, new JobParameter(parameter)); return this; } @@ -64,7 +55,7 @@ public class JobParametersBuilder { */ public JobParametersBuilder addDate(String key, Date parameter) { Assert.notNull(parameter, "Parameter must not be null."); - dateMap.put(key, new Date(parameter.getTime())); + parameterMap.put(key, new JobParameter(parameter)); return this; } @@ -77,7 +68,7 @@ public class JobParametersBuilder { */ public JobParametersBuilder addLong(String key, Long parameter) { Assert.notNull(parameter, "Parameter must not be null."); - longMap.put(key, parameter); + parameterMap.put(key, new JobParameter(parameter)); return this; } @@ -90,7 +81,7 @@ public class JobParametersBuilder { */ public JobParametersBuilder addDouble(String key, Double parameter) { Assert.notNull(parameter, "Parameter must not be null."); - doubleMap.put(key, parameter); + parameterMap.put(key, new JobParameter(parameter)); return this; } @@ -98,9 +89,9 @@ public class JobParametersBuilder { * Conversion method that takes the current state of this builder and * returns it as a JobruntimeParameters object. * - * @return a valid JobRuntimeParameters object. + * @return a valid JobParameters object. */ public JobParameters toJobParameters() { - return new JobParameters(stringMap, longMap, doubleMap, dateMap); + return new JobParameters(parameterMap); } } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/JobParametersPrototype.java b/spring-batch-core/src/main/java/org/springframework/batch/core/JobParametersPrototype.java new file mode 100644 index 000000000..bfd6ed28d --- /dev/null +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/JobParametersPrototype.java @@ -0,0 +1,56 @@ +/** + * + */ +package org.springframework.batch.core; + +import java.util.Date; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Map.Entry; + +/** + * @author Lucas Ward + * + */ +public class JobParametersPrototype { + + private final Map parameters; + + + public JobParametersPrototype(Map parameters) { + + for(Entry entry : parameters.entrySet()){ + Object parameter = entry.getValue(); + String key = entry.getKey(); + Class type = parameter.getClass(); + if (!type.isInstance(String.class) || !type.isInstance(Long.class) || + !type.isInstance(Double.class) || !type.isInstance(Date.class)) { + throw new ClassCastException("Value for key=[" + key + "] is not of type: [ String, Long, Double, or Date], it is [" + + (parameter == null ? null : "(" + type + ")" + parameter) + "]"); + } + } + + this.parameters = new LinkedHashMap(parameters); + } + + public long getLong(String key){ + return ((Long)parameters.get(key)).longValue(); + } + + public String getString(String key){ + return parameters.get(key).toString(); + } + + public Double getDouble(String key){ + return ((Double)parameters.get(key)).doubleValue(); + } + + public Date getDate(String key){ + return (Date)parameters.get(key); + } + + public Map getParameters(){ + return new LinkedHashMap(parameters); + } + +} diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/converter/DefaultJobParametersConverter.java b/spring-batch-core/src/main/java/org/springframework/batch/core/converter/DefaultJobParametersConverter.java index 490ad194b..cc75bd907 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/converter/DefaultJobParametersConverter.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/converter/DefaultJobParametersConverter.java @@ -26,8 +26,10 @@ import java.util.Map; import java.util.Properties; import java.util.Map.Entry; +import org.springframework.batch.core.JobParameter; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.JobParametersBuilder; +import org.springframework.batch.core.JobParameter.ParameterType; import org.springframework.util.StringUtils; /** @@ -152,16 +154,17 @@ public class DefaultJobParametersConverter implements JobParametersConverter { return new Properties(); } - Map parameters = params.getParameters(); + Map parameters = params.getParameters(); Properties result = new Properties(); - for (Entry entry : parameters.entrySet()) { + for (Entry entry : parameters.entrySet()) { String key = entry.getKey(); - Object value = entry.getValue(); - if (value instanceof Date) { + JobParameter jobParameter = entry.getValue(); + Object value = jobParameter.getValue(); + if (jobParameter.getType() == ParameterType.DATE) { result.setProperty(key + DATE_TYPE, dateFormat.format(value)); } - else if (value instanceof Long) { + else if (jobParameter.getType() == ParameterType.LONG) { result.setProperty(key + LONG_TYPE, numberFormat.format(value)); } else { diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/ScheduledJobParametersFactory.java b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/ScheduledJobParametersFactory.java index 65bbb0670..eadc1c40f 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/ScheduledJobParametersFactory.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/ScheduledJobParametersFactory.java @@ -23,6 +23,7 @@ import java.util.Map; import java.util.Properties; import java.util.Map.Entry; +import org.springframework.batch.core.JobParameter; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.JobParametersBuilder; import org.springframework.batch.core.converter.JobParametersConverter; @@ -78,14 +79,15 @@ public class ScheduledJobParametersFactory implements JobParametersConverter { return new Properties(); } - Map parameters = params.getParameters(); + Map parameters = params.getParameters(); Properties result = new Properties(); - for (Entry entry : parameters.entrySet()) { + for (Entry entry : parameters.entrySet()) { String key = entry.getKey(); + JobParameter jobParameter = entry.getValue(); if (key.equals(SCHEDULE_DATE_KEY)) { - result.setProperty(key, dateFormat.format(entry.getValue())); + result.setProperty(key, dateFormat.format(jobParameter.getValue())); } else { - result.setProperty(key, "" + entry.getValue()); + result.setProperty(key, "" + jobParameter.getValue()); } } return result; diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobExecutionDao.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobExecutionDao.java index 35351254c..7cc636b1c 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobExecutionDao.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobExecutionDao.java @@ -221,7 +221,7 @@ public class JdbcJobExecutionDao extends AbstractJdbcBatchMetadataDao implements jobExecution.setId(new Long(rs.getLong(1))); jobExecution.setStartTime(rs.getTimestamp(2)); jobExecution.setEndTime(rs.getTimestamp(3)); - jobExecution.setStatus(BatchStatus.getStatus(rs.getString(4))); + jobExecution.setStatus(BatchStatus.valueOf(rs.getString(4))); jobExecution.setExitStatus(new ExitStatus("Y".equals(rs.getString(5)), rs.getString(6), rs.getString(7))); jobExecution.setCreateTime(rs.getDate(8)); jobExecution.setExecutionContext(findExecutionContext(jobExecution)); diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobInstanceDao.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobInstanceDao.java index 25909053d..dbfd02be7 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobInstanceDao.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobInstanceDao.java @@ -4,14 +4,15 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.sql.Types; -import java.util.Date; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.springframework.batch.core.Job; import org.springframework.batch.core.JobInstance; +import org.springframework.batch.core.JobParameter; import org.springframework.batch.core.JobParameters; +import org.springframework.batch.core.JobParameter.ParameterType; import org.springframework.beans.factory.InitializingBean; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer; @@ -79,9 +80,9 @@ public class JdbcJobInstanceDao extends AbstractJdbcBatchMetadataDao implements private String createJobKey(JobParameters jobParameters) { - Map props = jobParameters.getParameters(); + Map props = jobParameters.getParameters(); StringBuffer stringBuffer = new StringBuffer(); - for (Entry entry : props.entrySet()) { + for (Entry entry : props.entrySet()) { stringBuffer.append(entry.toString() + ";"); } @@ -95,22 +96,10 @@ public class JdbcJobInstanceDao extends AbstractJdbcBatchMetadataDao implements */ private void insertJobParameters(Long jobId, JobParameters jobParameters) { - for (Entry entry : jobParameters.getStringParameters().entrySet()) { - insertParameter(jobId, ParameterType.STRING, entry.getKey().toString(), entry.getValue()); + for(Entry entry : jobParameters.getParameters().entrySet()){ + JobParameter jobParameter = entry.getValue(); + insertParameter(jobId, jobParameter.getType(), entry.getKey(), jobParameter.getValue()); } - - for (Entry entry : jobParameters.getLongParameters().entrySet()) { - insertParameter(jobId, ParameterType.LONG, entry.getKey().toString(), entry.getValue()); - } - - for (Entry entry : jobParameters.getDoubleParameters().entrySet()) { - insertParameter(jobId, ParameterType.DOUBLE, entry.getKey().toString(), entry.getValue()); - } - - for (Entry entry : jobParameters.getDateParameters().entrySet()) { - insertParameter(jobId, ParameterType.DATE, entry.getKey().toString(), entry.getValue()); - } - } /** @@ -196,38 +185,4 @@ public class JdbcJobInstanceDao extends AbstractJdbcBatchMetadataDao implements super.afterPropertiesSet(); Assert.notNull(jobIncrementer); } - - private static class ParameterType { - - private final String type; - - private ParameterType(String type) { - this.type = type; - } - - public String toString() { - return type; - } - - public static final ParameterType STRING = new ParameterType("STRING"); - - public static final ParameterType DATE = new ParameterType("DATE"); - - public static final ParameterType LONG = new ParameterType("LONG"); - - public static final ParameterType DOUBLE = new ParameterType("DOUBLE"); - - private static final ParameterType[] VALUES = { STRING, DATE, LONG, DOUBLE }; - - public static ParameterType getType(String typeAsString) { - - for (int i = 0; i < VALUES.length; i++) { - if (VALUES[i].toString().equals(typeAsString)) { - return (ParameterType) VALUES[i]; - } - } - - return null; - } - } } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcStepExecutionDao.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcStepExecutionDao.java index a54be282a..bd15deae2 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcStepExecutionDao.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcStepExecutionDao.java @@ -221,7 +221,7 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implement StepExecution stepExecution = new StepExecution(step.getName(), jobExecution, new Long(rs.getLong(1))); stepExecution.setStartTime(rs.getTimestamp(3)); stepExecution.setEndTime(rs.getTimestamp(4)); - stepExecution.setStatus(BatchStatus.getStatus(rs.getString(5))); + stepExecution.setStatus(BatchStatus.valueOf(rs.getString(5))); stepExecution.setCommitCount(rs.getInt(6)); stepExecution.setItemCount(rs.getInt(7)); stepExecution.setExitStatus(new ExitStatus("Y".equals(rs.getString(8)), rs.getString(9), rs.getString(10))); diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/resource/StepExecutionPreparedStatementSetter.java b/spring-batch-core/src/main/java/org/springframework/batch/core/resource/StepExecutionPreparedStatementSetter.java index 03587e0b4..1def151b6 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/resource/StepExecutionPreparedStatementSetter.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/resource/StepExecutionPreparedStatementSetter.java @@ -20,6 +20,7 @@ import java.sql.SQLException; import java.util.List; import java.util.Map; +import org.springframework.batch.core.JobParameter; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.StepExecution; import org.springframework.batch.core.StepExecutionListener; @@ -46,14 +47,14 @@ public class StepExecutionPreparedStatementSetter extends StepExecutionListenerS private JobParameters jobParameters; public void setValues(PreparedStatement ps) throws SQLException { - Map parameters = jobParameters.getParameters(); + Map parameters = jobParameters.getParameters(); for (int i = 0; i < parameterKeys.size(); i++) { - Object arg = parameters.get(parameterKeys.get(i)); + JobParameter arg = parameters.get(parameterKeys.get(i)); if (arg == null) { throw new IllegalStateException("No job parameter found for with key of: [" + parameterKeys.get(i) + "]"); } - StatementCreatorUtils.setParameterValue(ps, i + 1, SqlTypeValue.TYPE_UNKNOWN, arg); + StatementCreatorUtils.setParameterValue(ps, i + 1, SqlTypeValue.TYPE_UNKNOWN, arg.getValue()); } } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/resource/StepExecutionSimpleCompletionPolicy.java b/spring-batch-core/src/main/java/org/springframework/batch/core/resource/StepExecutionSimpleCompletionPolicy.java index b3dd29846..6a974ed00 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/resource/StepExecutionSimpleCompletionPolicy.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/resource/StepExecutionSimpleCompletionPolicy.java @@ -64,9 +64,9 @@ public class StepExecutionSimpleCompletionPolicy extends StepExecutionListenerSu */ public void beforeStep(StepExecution stepExecution) { JobParameters jobParameters = stepExecution.getJobParameters(); - Assert.state(jobParameters.getLongParameters().containsKey(keyName), + Assert.state(jobParameters.getParameters().containsKey(keyName), "JobParameters do not contain Long parameter with key=[" + keyName + "]"); - delegate = new SimpleCompletionPolicy(jobParameters.getLong(keyName).intValue()); + delegate = new SimpleCompletionPolicy(new Long(jobParameters.getLong(keyName)).intValue()); } /** diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/BatchStatusTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/BatchStatusTests.java index 2e7345dc4..3022ecde8 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/BatchStatusTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/BatchStatusTests.java @@ -15,42 +15,39 @@ */ package org.springframework.batch.core; +import static org.junit.Assert.*; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; -import junit.framework.TestCase; +import org.junit.Test; /** * @author Dave Syer * */ -public class BatchStatusTests extends TestCase { +public class BatchStatusTests { /** * Test method for * {@link org.springframework.batch.core.BatchStatus#toString()}. */ + @Test public void testToString() { assertEquals("FAILED", BatchStatus.FAILED.toString()); } - /** - * Test method for - * {@link org.springframework.batch.core.BatchStatus#getStatus(java.lang.String)}. - */ + @Test public void testGetStatus() { - assertEquals(BatchStatus.FAILED, BatchStatus.getStatus(BatchStatus.FAILED.toString())); + assertEquals(BatchStatus.FAILED, BatchStatus.valueOf(BatchStatus.FAILED.toString())); } - /** - * Test method for - * {@link org.springframework.batch.core.BatchStatus#getStatus(java.lang.String)}. - */ + @Test public void testGetStatusWrongCode() { try { - BatchStatus.getStatus("foo"); + BatchStatus.valueOf("foo"); fail(); } catch (IllegalArgumentException ex) { @@ -58,14 +55,12 @@ public class BatchStatusTests extends TestCase { } } - /** - * Test method for - * {@link org.springframework.batch.core.BatchStatus#getStatus(java.lang.String)}. - */ + @Test(expected=NullPointerException.class) public void testGetStatusNullCode() { - assertNull(BatchStatus.getStatus(null)); + assertNull(BatchStatus.valueOf(null)); } + @Test public void testSerialization() throws Exception { ByteArrayOutputStream bout = new ByteArrayOutputStream(); diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/JobParameterTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/JobParameterTests.java new file mode 100644 index 000000000..408209e96 --- /dev/null +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/JobParameterTests.java @@ -0,0 +1,52 @@ +/** + * + */ +package org.springframework.batch.core; + +import static org.junit.Assert.*; + +import java.util.Date; + +import org.junit.Test; + +/** + * @author Lucas Ward + * + */ +public class JobParameterTests { + + JobParameter jobParameter; + + @Test + public void testStringParameter(){ + jobParameter = new JobParameter("test"); + assertEquals("test", jobParameter.getValue()); + } + + @Test + public void testLongParameter(){ + jobParameter = new JobParameter(1L); + assertEquals(1L, jobParameter.getValue()); + } + + @Test + public void testDoubleParameter(){ + jobParameter = new JobParameter(1.1); + assertEquals(1.1, jobParameter.getValue()); + } + + @Test + public void testDateParameter(){ + Date epoch = new Date(0L); + jobParameter = new JobParameter(epoch); + assertEquals(new Date(0L), jobParameter.getValue()); + } + + @Test + public void testEquals(){ + jobParameter = new JobParameter("test"); + JobParameter testParameter = new JobParameter("test"); + assertTrue(jobParameter.equals(testParameter)); + } + +} \ No newline at end of file diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/JobParametersBuilderTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/JobParametersBuilderTests.java index 8b2d654ed..132558a62 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/JobParametersBuilderTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/JobParametersBuilderTests.java @@ -24,7 +24,7 @@ public class JobParametersBuilderTests extends TestCase { parametersBuilder.addString("STRING", "string value"); JobParameters parameters = parametersBuilder.toJobParameters(); assertEquals(date, parameters.getDate("SCHEDULE_DATE")); - assertEquals(new Long(1), parameters.getLong("LONG")); + assertEquals(1L, parameters.getLong("LONG")); assertEquals("string value", parameters.getString("STRING")); } @@ -33,9 +33,9 @@ public class JobParametersBuilderTests extends TestCase { parametersBuilder.addLong("LONG", new Long(1)); parametersBuilder.addString("STRING", "string value"); Iterator parameters = parametersBuilder.toJobParameters().getParameters().keySet().iterator(); - assertEquals("STRING", parameters.next()); - assertEquals("LONG", parameters.next()); assertEquals("SCHEDULE_DATE", parameters.next()); + assertEquals("LONG", parameters.next()); + assertEquals("STRING", parameters.next()); } public void testOrderedStrings(){ diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/JobParametersTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/JobParametersTests.java index 44b3ae3e0..0ab95a8d4 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/JobParametersTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/JobParametersTests.java @@ -20,14 +20,6 @@ public class JobParametersTests extends TestCase { JobParameters parameters; - Map stringMap; - - Map longMap; - - Map dateMap; - - Map doubleMap; - Date date1 = new Date(4321431242L); Date date2 = new Date(7809089900L); @@ -39,118 +31,28 @@ public class JobParametersTests extends TestCase { private JobParameters getNewParameters() { - stringMap = new HashMap(); - stringMap.put("string.key1", "value1"); - stringMap.put("string.key2", "value2"); + Map parameterMap = new HashMap(); + parameterMap.put("string.key1", new JobParameter("value1")); + parameterMap.put("string.key2", new JobParameter("value2")); + parameterMap.put("long.key1", new JobParameter(1L)); + parameterMap.put("long.key2", new JobParameter(2L)); + parameterMap.put("double.key1", new JobParameter(1.1)); + parameterMap.put("double.key2", new JobParameter(2.2)); + parameterMap.put("date.key1", new JobParameter(date1)); + parameterMap.put("date.key2", new JobParameter(date2)); - longMap = new HashMap(); - longMap.put("long.key1", new Long(1)); - longMap.put("long.key2", new Long(2)); - - doubleMap = new HashMap(); - doubleMap.put("double.key1", new Double(1.1)); - doubleMap.put("double.key2", new Double(2.2)); - - dateMap = new HashMap(); - dateMap.put("date.key1", date1); - dateMap.put("date.key2", date2); - - return new JobParameters(stringMap, longMap, doubleMap, dateMap); + return new JobParameters(parameterMap); } - @SuppressWarnings("unchecked") - public void testBadLongKeyException() throws Exception { - - Map badLongMap = new HashMap(); - badLongMap.put(new Long(0), new Long(1)); - - try { - new JobParameters(stringMap, badLongMap, doubleMap, dateMap); - fail(); - } - catch (IllegalArgumentException ex) { - // expected - } - } - - @SuppressWarnings("unchecked") - public void testBadLongConstructorException() throws Exception { - - Map badLongMap = new HashMap(); - badLongMap.put("key", "bad long"); - - try { - new JobParameters(stringMap, badLongMap, doubleMap, dateMap); - fail(); - } - catch (IllegalArgumentException ex) { - // expected - } - } - - @SuppressWarnings("unchecked") - public void testBadDoubleConstructorException() throws Exception { - - Map badDoubleMap = new HashMap(); - badDoubleMap.put("key", "bad double"); - - try { - new JobParameters(stringMap, longMap, badDoubleMap, dateMap); - fail(); - } - catch (IllegalArgumentException ex) { - // expected - } - } - - @SuppressWarnings("unchecked") - public void testBadStringConstructorException() throws Exception { - - Map badMap = new HashMap(); - badMap.put("key", new Integer(2)); - - try { - new JobParameters(badMap, longMap, doubleMap, dateMap); - fail(); - } - catch (IllegalArgumentException ex) { - // expected - } - } - - @SuppressWarnings("unchecked") - public void testBadDateConstructorException() throws Exception { - - Map badMap = new HashMap(); - badMap.put("key", new java.sql.Date(System.currentTimeMillis())); - - try { - new JobParameters(stringMap, longMap, doubleMap, badMap); - fail(); - } - catch (IllegalArgumentException ex) { - // expected - } - } public void testGetString() { assertEquals("value1", parameters.getString("string.key1")); assertEquals("value2", parameters.getString("string.key2")); } - public void testGetStringParameters() { - assertEquals("value1", parameters.getStringParameters().get("string.key1")); - assertEquals("value2", parameters.getStringParameters().get("string.key2")); - } - public void testGetLong() { - assertEquals(new Long(1), parameters.getLong("long.key1")); - assertEquals(new Long(2), parameters.getLong("long.key2")); - } - - public void testGetLongParameters() { - assertEquals(new Long(1), parameters.getLongParameters().get("long.key1")); - assertEquals(new Long(2), parameters.getLongParameters().get("long.key2")); + assertEquals(1L, parameters.getLong("long.key1")); + assertEquals(2L, parameters.getLong("long.key2")); } public void testGetDouble() { @@ -158,21 +60,11 @@ public class JobParametersTests extends TestCase { assertEquals(new Double(2.2), parameters.getDouble("double.key2")); } - public void testGetDoubleParameters() { - assertEquals(new Double(1.1), parameters.getDoubleParameters().get("double.key1")); - assertEquals(new Double(2.2), parameters.getDoubleParameters().get("double.key2")); - } - public void testGetDate() { assertEquals(date1, parameters.getDate("date.key1")); assertEquals(date2, parameters.getDate("date.key2")); } - public void testGetDateParameters() { - assertEquals(date1, parameters.getDateParameters().get("date.key1")); - assertEquals(date2, parameters.getDateParameters().get("date.key2")); - } - public void testIsEmptyWhenEmpty() throws Exception { assertTrue(new JobParameters().isEmpty()); } @@ -204,35 +96,29 @@ public class JobParametersTests extends TestCase { public void testToStringOrder() { - Map props = parameters.getParameters(); + Map props = parameters.getParameters(); StringBuffer stringBuilder = new StringBuffer(); - for (Entry entry : props.entrySet()) { + for (Entry entry : props.entrySet()) { stringBuilder.append(entry.toString() + ";"); } String string1 = stringBuilder.toString(); - stringMap = new HashMap(); - stringMap.put("string.key2", "value2"); - stringMap.put("string.key1", "value1"); - - longMap = new HashMap(); - longMap.put("long.key2", new Long(2)); - longMap.put("long.key1", new Long(1)); - - doubleMap = new HashMap(); - doubleMap.put("double.key2", new Double(2.2)); - doubleMap.put("double.key1", new Double(1.1)); - - dateMap = new HashMap(); - dateMap.put("date.key2", date2); - dateMap.put("date.key1", date1); - - JobParameters testProps = new JobParameters(stringMap, longMap, doubleMap, dateMap); + Map parameterMap = new HashMap(); + parameterMap.put("string.key2", new JobParameter("value2")); + parameterMap.put("string.key1", new JobParameter("value1")); + parameterMap.put("long.key2", new JobParameter(2L)); + parameterMap.put("long.key1", new JobParameter(1L)); + parameterMap.put("double.key2", new JobParameter(2.2)); + parameterMap.put("double.key1", new JobParameter(1.1)); + parameterMap.put("date.key2", new JobParameter(date2)); + parameterMap.put("date.key1", new JobParameter(date1)); + + JobParameters testProps = new JobParameters(parameterMap); props = testProps.getParameters(); stringBuilder = new StringBuffer(); - for (Entry entry : props.entrySet()) { + for (Entry entry : props.entrySet()) { stringBuilder.append(entry.toString() + ";"); } String string2 = stringBuilder.toString(); diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/converter/DefaultJobParametersConverterTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/converter/DefaultJobParametersConverterTests.java index 0890ae471..2c3af6619 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/converter/DefaultJobParametersConverterTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/converter/DefaultJobParametersConverterTests.java @@ -49,7 +49,7 @@ public class DefaultJobParametersConverterTests extends TestCase { JobParameters props = factory.getJobParameters(StringUtils.splitArrayElementsIntoProperties(args, "=")); assertNotNull(props); assertEquals("myKey", props.getString("job.key")); - assertEquals(new Long(33243243L), props.getLong("vendor.id")); + assertEquals(33243243L, props.getLong("vendor.id")); Date date = dateFormat.parse("01/23/2008"); assertEquals(date, props.getDate("schedule.date")); } @@ -85,7 +85,7 @@ public class DefaultJobParametersConverterTests extends TestCase { factory.setNumberFormat(new DecimalFormat("#,###")); JobParameters props = factory.getJobParameters(StringUtils.splitArrayElementsIntoProperties(args, "=")); assertNotNull(props); - assertEquals(1000L, props.getLong("value").longValue()); + assertEquals(1000L, props.getLong("value")); } public void testGetParametersWithBogusLong() throws Exception { @@ -134,7 +134,7 @@ public class DefaultJobParametersConverterTests extends TestCase { JobParameters props = factory.getJobParameters(StringUtils.splitArrayElementsIntoProperties(args, "=")); assertNotNull(props); - assertEquals(1.38, props.getDouble("value").doubleValue(), Double.MIN_VALUE); + assertEquals(1.38, props.getDouble("value"), Double.MIN_VALUE); } public void testGetProperties() throws Exception { diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/AbstractJobInstanceDaoTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/AbstractJobInstanceDaoTests.java index 59282d607..697776e2e 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/AbstractJobInstanceDaoTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/AbstractJobInstanceDaoTests.java @@ -43,8 +43,8 @@ public abstract class AbstractJobInstanceDaoTests extends AbstractTransactionalD assertEquals(fooJob.getName(), retrievedInstance.getJobName()); assertEquals(fooParams, retrievedParams); - assertEquals(Long.MAX_VALUE, retrievedParams.getLong("longKey").longValue()); - assertEquals(Double.MAX_VALUE, retrievedParams.getDouble("doubleKey").doubleValue(), 0.001); + assertEquals(Long.MAX_VALUE, retrievedParams.getLong("longKey")); + assertEquals(Double.MAX_VALUE, retrievedParams.getDouble("doubleKey"), 0.001); assertEquals("stringValue", retrievedParams.getString("stringKey")); assertEquals(new Date(DATE), retrievedParams.getDate("dateKey")); } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryIntegrationTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryIntegrationTests.java index b45b522a9..b7198b0d3 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryIntegrationTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryIntegrationTests.java @@ -1,12 +1,11 @@ package org.springframework.batch.core.repository.support; import java.util.Date; -import java.util.HashMap; -import java.util.Map; import org.springframework.batch.core.BatchStatus; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParameters; +import org.springframework.batch.core.JobParametersBuilder; import org.springframework.batch.core.Step; import org.springframework.batch.core.StepExecution; import org.springframework.batch.core.job.JobSupport; @@ -47,27 +46,9 @@ public class SimpleJobRepositoryIntegrationTests extends AbstractTransactionalDa job.setRestartable(true); - Map stringParams = new HashMap() { - { - put("stringKey", "stringValue"); - } - }; - Map longParams = new HashMap() { - { - put("longKey", new Long(1)); - } - }; - Map doubleParams = new HashMap() { - { - put("doubleKey", new Double(1.1)); - } - }; - Map dateParams = new HashMap() { - { - put("dateKey", new Date(1)); - } - }; - JobParameters jobParams = new JobParameters(stringParams, longParams, doubleParams, dateParams); + JobParametersBuilder builder = new JobParametersBuilder(); + builder.addString("stringKey", "stringValue").addLong("longKey", 1L).addDouble("doubleKey", 1.1).addDate("dateKey", new Date(1L)); + JobParameters jobParams = builder.toJobParameters(); JobExecution firstExecution = jobRepository.createJobExecution(job, jobParams); firstExecution.setStartTime(new Date());