diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/StreamContext.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/StreamContext.java index 3ca0affd7..fde18cac8 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/StreamContext.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/StreamContext.java @@ -24,7 +24,6 @@ import java.util.Set; import java.util.Map.Entry; import org.springframework.util.Assert; -import org.springframework.util.ClassUtils; /** * Value object representing a context for an {@link ItemStream}. It is @@ -76,9 +75,13 @@ public class StreamContext { return ((Long) readAndValidate(key, Long.class)).longValue(); } - - public Object get(String key) { - + + public double getDouble(String key){ + return ((Double)readAndValidate(key, Double.class)).doubleValue(); + } + + public Object get(String key){ + return map.get(key); } @@ -86,10 +89,10 @@ public class StreamContext { Object value = map.get(key); -// if (!type.isInstance(key)) { -// throw new ClassCastException("Value for key=[" + key + "] is not of type: [" + ClassUtils.getShortName(type) -// + "], it is [" + (value == null ? null : ClassUtils.getShortName(value.getClass())) + "]"); -// } + if (!type.isInstance(value)) { + throw new ClassCastException("Value for key=[" + key + "] is not of type: [" + type + + "], it is [" + (value == null ? null : value) + "]"); + } return value; } @@ -124,4 +127,19 @@ public class StreamContext { return props; } + + public boolean equals(Object obj) { + if(obj instanceof StreamContext == false){ + return false; + } + if(this == obj){ + return true; + } + StreamContext rhs = (StreamContext)obj; + return this.entrySet().equals(rhs.entrySet()); + } + + public int hashCode() { + return map.hashCode(); + } } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/StreamContextTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/StreamContextTests.java new file mode 100644 index 000000000..49b395003 --- /dev/null +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/StreamContextTests.java @@ -0,0 +1,87 @@ +/* + * Copyright 2006-2008 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.item; + +import junit.framework.TestCase; + +/** + * @author Lucas Ward + * + */ +public class StreamContextTests extends TestCase{ + + StreamContext context; + + protected void setUp() throws Exception { + super.setUp(); + + context = new StreamContext(); + } + + public void testNormalUsage(){ + + context.putString("1", "testString1"); + context.putString("2", "testString2"); + context.putLong("3", 3); + context.putDouble("4", 4.4); + + assertEquals("testString1", context.getString("1")); + assertEquals("testString2", context.getString("2")); + assertEquals(4.4, context.getDouble("4"), 0); + assertEquals(3, context.getLong("3")); + } + + public void testInvalidCast(){ + + context.putLong("1", 1); + try{ + context.getDouble("1"); + fail(); + } + catch(ClassCastException ex){ + //expected + } + } + + public void testIsEmpty(){ + assertTrue(context.isEmpty()); + context.put("1", new Object()); + assertFalse(context.isEmpty()); + } + + public void testDirtyFlag(){ + assertFalse(context.isDirty()); + context.put("1", new Object()); + assertTrue(context.isDirty()); + context.clearDirtyFlag(); + assertFalse(context.isDirty()); + } + + public void testContains(){ + context.put("1", "testString"); + assertTrue(context.containsKey("1")); + assertTrue(context.containsValue("testString")); + } + + public void testEquals(){ + context.put("1", "testString"); + StreamContext tempContext = new StreamContext(); + assertFalse(tempContext.equals(context)); + tempContext.put("1", "testString"); + assertTrue(tempContext.equals(context)); + } + +}