diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/BatchStatus.java b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/BatchStatus.java index 75b2c8fd4..529c67867 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/BatchStatus.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/BatchStatus.java @@ -16,27 +16,39 @@ package org.springframework.batch.core.domain; +import java.io.Serializable; + /** - * Typesafe enumeration representating the status of an artifact within - * the batch container. See Effective Java Programming by Joshua Bloch - * for more details on the pattern used. + * 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. * * @author Lucas Ward - * + * @author Greg Kick */ -public class BatchStatus { - +public class BatchStatus implements Serializable{ + + private static final long serialVersionUID = 1634960297477743037L; + private final String name; private BatchStatus(String name) { this.name = name; } - public String toString(){ + private Object readResolve() throws java.io.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"); @@ -44,19 +56,25 @@ public class BatchStatus { public static final BatchStatus STARTING = new BatchStatus("STARTING"); public static final BatchStatus FAILED = new BatchStatus("FAILED"); - - public static final BatchStatus STOPPED = new BatchStatus("STOPPED"); - - private static final BatchStatus[] VALUES = {STARTING, STARTED, COMPLETED, FAILED, STOPPED}; - public static BatchStatus getStatus(String statusAsString){ - - for(int i = 0; i < VALUES.length; i++){ - if(VALUES[i].toString().equals(statusAsString)){ - return (BatchStatus)VALUES[i]; + public static final BatchStatus STOPPED = new BatchStatus("STOPPED"); + + private static final BatchStatus[] VALUES = { STARTING, STARTED, COMPLETED, FAILED, STOPPED }; + + /** + * Given a string representation of a status, return the appropriate BatchStatus. + * + * @param statusAsString: string representation of a status + * @return Valid BatchStatus + * @throws IllegalArgumentException if no status matches provided string. + */ + public static BatchStatus getStatus(String statusAsString) { + final String upperCaseStatusAsString = statusAsString.toUpperCase(); + for (int i = 0; i < VALUES.length; i++) { + if (VALUES[i].toString().equals(upperCaseStatusAsString)) { + return VALUES[i]; } } - - return null; + throw new IllegalArgumentException("The string did not match a valid status."); } } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/domain/BatchStatusTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/domain/BatchStatusTests.java index 8cb29f4cb..89719ab8d 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/domain/BatchStatusTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/domain/BatchStatusTests.java @@ -15,6 +15,11 @@ */ package org.springframework.batch.core.domain; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + import junit.framework.TestCase; /** @@ -41,6 +46,27 @@ public class BatchStatusTests extends TestCase { * Test method for {@link org.springframework.batch.core.domain.BatchStatus#getStatus(java.lang.String)}. */ public void testGetStatusWrongCode() { - assertEquals(null, BatchStatus.getStatus("foo")); + try{ + BatchStatus.getStatus("foo"); + fail(); + } + catch(IllegalArgumentException ex){ + //expected + } + } + + public void testSerialization() throws Exception{ + + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + ObjectOutputStream out = new ObjectOutputStream (bout); + + out.writeObject (BatchStatus.COMPLETED); + out.flush (); + + ByteArrayInputStream bin = new ByteArrayInputStream (bout.toByteArray()); + ObjectInputStream in = new ObjectInputStream(bin); + + BatchStatus status = (BatchStatus) in.readObject (); + assertEquals(BatchStatus.COMPLETED, status); } }