Skip to content

Commit 963142c

Browse files
author
dsyer
committed
BATCH-1566: fix dirty flag on ExecutionContext
1 parent 4b6df84 commit 963142c

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ExecutionContext.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,13 @@ public void putDouble(String key, double value) {
125125
public void put(String key, Object value) {
126126
if (value != null) {
127127
Assert.isInstanceOf(Serializable.class, value, "Value: [ " + value + "must be serializable.");
128-
map.put(key, value);
128+
Object result = map.put(key, value);
129+
dirty = result==null || result!=null && !result.equals(value);
129130
}
130131
else {
131-
map.remove(key);
132+
Object result = map.remove(key);
133+
dirty = result!=null;
132134
}
133-
dirty = true;
134135
}
135136

136137
/**

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/ExecutionContextTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,25 @@ public void testDirtyFlag() {
8989
assertFalse(context.isDirty());
9090
}
9191

92+
@Test
93+
public void testNotDirtyWithDuplicate() {
94+
context.putString("1", "test");
95+
assertTrue(context.isDirty());
96+
context.clearDirtyFlag();
97+
context.putString("1", "test");
98+
assertFalse(context.isDirty());
99+
}
100+
101+
@Test
102+
public void testNotDirtyWithRemoveMissing() {
103+
context.putString("1", "test");
104+
assertTrue(context.isDirty());
105+
context.putString("1", null); // remove an item that was present
106+
assertTrue(context.isDirty());
107+
context.putString("1", null); // remove a non-existent item
108+
assertFalse(context.isDirty());
109+
}
110+
92111
@Test
93112
public void testContains() {
94113
context.putString("1", "testString");

0 commit comments

Comments
 (0)