diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ExecutionContext.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ExecutionContext.java index 71e56ce4d5..19317425b7 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ExecutionContext.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ExecutionContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2023 the original author or authors. + * Copyright 2006-2024 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. @@ -37,6 +37,7 @@ * @author Lucas Ward * @author Douglas Kaminsky * @author Mahmoud Ben Hassine + * @author Seokmun Heo */ public class ExecutionContext implements Serializable { @@ -124,11 +125,18 @@ public void putDouble(String key, double value) { public void put(String key, @Nullable Object value) { if (value != null) { Object result = this.map.put(key, value); - this.dirty = result == null || !result.equals(value); + boolean newDirty = result == null || !result.equals(value); + + if (!this.dirty) { + this.dirty = newDirty; + } } else { Object result = this.map.remove(key); - this.dirty = result != null; + + if (!this.dirty) { + this.dirty = result != null; + } } } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/ExecutionContextTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/ExecutionContextTests.java index fe4fd5bb76..7e9052624e 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/ExecutionContextTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/ExecutionContextTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2023 the original author or authors. + * Copyright 2006-2024 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. @@ -30,7 +30,7 @@ /** * @author Lucas Ward * @author Mahmoud Ben Hassine - * + * @author Seokmun Heo */ class ExecutionContextTests { @@ -88,11 +88,13 @@ void testNotDirtyWithDuplicate() { } @Test - void testNotDirtyWithRemoveMissing() { + void testDirtyWithRemoveMissing() { context.putString("1", "test"); assertTrue(context.isDirty()); context.putString("1", null); // remove an item that was present assertTrue(context.isDirty()); + + context.clearDirtyFlag(); context.putString("1", null); // remove a non-existent item assertFalse(context.isDirty()); } @@ -161,6 +163,15 @@ void testCopyConstructorNullInput() { assertTrue(context.isEmpty()); } + @Test + void testDirtyWithDuplicate() { + ExecutionContext context = new ExecutionContext(); + context.put("1", "testString1"); + assertTrue(context.isDirty()); + context.put("1", "testString1"); // put the same value + assertTrue(context.isDirty()); + } + /** * Value object for testing serialization */