From 2ae57f85dbb4d8062df5969831d2a0129142807a Mon Sep 17 00:00:00 2001 From: trisberg Date: Mon, 20 Oct 2008 20:35:41 +0000 Subject: [PATCH] BATCH-762: added unit test --- ...treamExecutionContextStringSerializer.java | 2 +- .../core/repository/dao/ComplexObject.java | 70 ++++++++++++++++++ ...ExecutionContextStringSerializerTests.java | 71 +++++++++++++++++++ 3 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/ComplexObject.java create mode 100644 spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/XStreamExecutionContextStringSerializerTests.java diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/XStreamExecutionContextStringSerializer.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/XStreamExecutionContextStringSerializer.java index f79d89451..95def0938 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/XStreamExecutionContextStringSerializer.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/XStreamExecutionContextStringSerializer.java @@ -60,7 +60,7 @@ public class XStreamExecutionContextStringSerializer implements ExecutionContext init(); } - public void init() throws Exception { + public synchronized void init() throws Exception { if (hierarchicalStreamDriver == null) { this.hierarchicalStreamDriver = new JettisonMappedXmlDriver(); } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/ComplexObject.java b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/ComplexObject.java new file mode 100644 index 000000000..085d0cb18 --- /dev/null +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/ComplexObject.java @@ -0,0 +1,70 @@ +package org.springframework.batch.core.repository.dao; + +import java.math.BigDecimal; +import java.util.Map; + +/** + * @author trisberg + */ +public class ComplexObject { + private String name; + private BigDecimal number; + private ComplexObject obj; + private Map map; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public BigDecimal getNumber() { + return number; + } + + public void setNumber(BigDecimal number) { + this.number = number; + } + + public ComplexObject getObj() { + return obj; + } + + public void setObj(ComplexObject obj) { + this.obj = obj; + } + + public Map getMap() { + return map; + } + + public void setMap(Map map) { + this.map = map; + } + + + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ComplexObject that = (ComplexObject) o; + + if (map != null ? !map.equals(that.map) : that.map != null) return false; + if (name != null ? !name.equals(that.name) : that.name != null) return false; + if (number != null ? !number.equals(that.number) : that.number != null) return false; + if (obj != null ? !obj.equals(that.obj) : that.obj != null) return false; + + return true; + } + + public int hashCode() { + int result; + result = (name != null ? name.hashCode() : 0); + result = 31 * result + (number != null ? number.hashCode() : 0); + result = 31 * result + (obj != null ? obj.hashCode() : 0); + result = 31 * result + (map != null ? map.hashCode() : 0); + return result; + } +} diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/XStreamExecutionContextStringSerializerTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/XStreamExecutionContextStringSerializerTests.java new file mode 100644 index 000000000..200ac9d9b --- /dev/null +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/XStreamExecutionContextStringSerializerTests.java @@ -0,0 +1,71 @@ +package org.springframework.batch.core.repository.dao; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.junit.Before; + +import java.util.Map; +import java.util.HashMap; +import java.util.Date; +import java.math.BigDecimal; + +/** + * @author trisberg + */ +public class XStreamExecutionContextStringSerializerTests { + + ExecutionContextStringSerializer serializer; + + @Before + public void onSetUp() throws Exception { + serializer = new XStreamExecutionContextStringSerializer(); + ((XStreamExecutionContextStringSerializer)serializer).afterPropertiesSet(); + } + + @Test + public void testSerializeAMap() { + Map m1 = new HashMap(); + m1.put("object1", Long.valueOf(12345L)); + m1.put("object2", "OBJECT TWO"); + m1.put("object3", new Date(1234567L)); + m1.put("object4", new Double(1234567.1234D)); + + String s = serializer.serialize(m1); + + Map m2 = serializer.deserialize(s); + + compareContexts(m1, m2); + } + + @Test + public void testComplexObject() { + Map m1 = new HashMap(); + ComplexObject o1 = new ComplexObject(); + o1.setName("Test"); + Map m = new HashMap(); + m.put("object1", Long.valueOf(12345L)); + m.put("object2", "OBJECT TWO"); + o1.setMap(m); + o1.setNumber(new BigDecimal("12345.67")); + ComplexObject o2 = new ComplexObject(); + o2.setName("Inner Object"); + o2.setMap(m); + o2.setNumber(new BigDecimal("98765.43")); + o1.setObj(o2); + m1.put("co", o1); + + String s = serializer.serialize(m1); + + Map m2 = serializer.deserialize(s); + + compareContexts(m1, m2); + } + + private void compareContexts(Map m1, Map m2) { + for (String key : m1.keySet()) { + assertEquals("Bad key/value for " + key, m1.get(key), m2.get(key)); + } + } + +}