Skip to content

Commit 84aca22

Browse files
lmcreanGoogle Java Core Libraries
authored andcommitted
Add test for putIfAbsent method to verify replacement of null values.
This new test ensures that when a null value is present, the `putIfAbsent` method replaces it with a new value. The test checks both the return value and the updated state of the map. Fixes #6217 Fixes #7987 RELNOTES=n/a PiperOrigin-RevId: 807473633
1 parent fe8f6f8 commit 84aca22

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

android/guava-testlib/src/com/google/common/collect/testing/testers/MapPutIfAbsentTester.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,16 @@ public void testPut_nullValueSupported() {
126126
getMap().putIfAbsent(nullValueEntry.getKey(), nullValueEntry.getValue()));
127127
expectAdded(nullValueEntry);
128128
}
129+
130+
@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
131+
@CollectionSize.Require(absent = ZERO)
132+
public void testPutIfAbsent_replacesNullValue() {
133+
initMapWithNullValue();
134+
135+
// putIfAbsent should replace the null value with the new value
136+
assertNull(
137+
"putIfAbsent(existingKeyWithNullValue, value) should return null",
138+
getMap().putIfAbsent(getKeyForNullValue(), v3()));
139+
assertEquals("Map should now contain the new value", v3(), getMap().get(getKeyForNullValue()));
140+
}
129141
}

guava-testlib/src/com/google/common/collect/testing/testers/MapPutIfAbsentTester.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,16 @@ public void testPut_nullValueSupported() {
125125
getMap().putIfAbsent(nullValueEntry.getKey(), nullValueEntry.getValue()));
126126
expectAdded(nullValueEntry);
127127
}
128+
129+
@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
130+
@CollectionSize.Require(absent = ZERO)
131+
public void testPutIfAbsent_replacesNullValue() {
132+
initMapWithNullValue();
133+
134+
// putIfAbsent should replace the null value with the new value
135+
assertNull(
136+
"putIfAbsent(existingKeyWithNullValue, value) should return null",
137+
getMap().putIfAbsent(getKeyForNullValue(), v3()));
138+
assertEquals("Map should now contain the new value", v3(), getMap().get(getKeyForNullValue()));
139+
}
128140
}

0 commit comments

Comments
 (0)