Skip to content

Commit 4dee9cb

Browse files
committed
ConcurrentMapCache.putIfAbsent properly supports nulls
Issue: SPR-13458
1 parent f3b7e9f commit 4dee9cb

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCache.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -124,7 +124,7 @@ public void put(Object key, Object value) {
124124

125125
@Override
126126
public ValueWrapper putIfAbsent(Object key, Object value) {
127-
Object existing = this.store.putIfAbsent(key, value);
127+
Object existing = this.store.putIfAbsent(key, toStoreValue(value));
128128
return toWrapper(existing);
129129
}
130130

@@ -169,6 +169,7 @@ private ValueWrapper toWrapper(Object value) {
169169
return (value != null ? new SimpleValueWrapper(fromStoreValue(value)) : null);
170170
}
171171

172+
172173
@SuppressWarnings("serial")
173174
private static class NullHolder implements Serializable {
174175
}

spring-context/src/test/java/org/springframework/cache/concurrent/ConcurrentMapCacheManagerTests.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,6 +50,18 @@ public void testDynamicMode() {
5050
assertEquals(2, cache1.get("key2").get());
5151
cache1.put("key3", null);
5252
assertNull(cache1.get("key3").get());
53+
cache1.put("key3", null);
54+
assertNull(cache1.get("key3").get());
55+
cache1.evict("key3");
56+
assertNull(cache1.get("key3"));
57+
58+
assertEquals("value1", cache1.putIfAbsent("key1", "value1x").get());
59+
assertEquals("value1", cache1.get("key1").get());
60+
assertEquals(2, cache1.putIfAbsent("key2", 2.1).get());
61+
assertNull(cache1.putIfAbsent("key3", null));
62+
assertNull(cache1.get("key3").get());
63+
assertNull(cache1.putIfAbsent("key3", null).get());
64+
assertNull(cache1.get("key3").get());
5365
cache1.evict("key3");
5466
assertNull(cache1.get("key3"));
5567
}

0 commit comments

Comments
 (0)