DATACOUCH-53 - protection against NPE on CouchbaseCache when putting null as a value

This commit is contained in:
Andrzej Wislowski
2013-12-03 23:18:27 +01:00
committed by Michael Nitschinger
parent 7d79d79e28
commit b11fc6c53c
2 changed files with 23 additions and 2 deletions

View File

@@ -88,8 +88,12 @@ public class CouchbaseCache implements Cache {
* @param value the Object to store.
*/
public final void put(final Object key, final Object value) {
String documentId = key.toString();
client.set(documentId, 0, value);
if (value != null) {
String documentId = key.toString();
client.set(documentId, 0, value);
} else {
evict(key);
}
}
/**

View File

@@ -97,4 +97,21 @@ public class CouchbaseCacheTests {
assertNull(result);
}
/**
* Putting into cache on the same key not null value, and then null value,
* results in null object
*/
@Test
public void testSettingNullAndGetting() {
CouchbaseCache cache = new CouchbaseCache(cacheName, client);
String key = "couchbase-cache-test";
String value = "Hello World!";
cache.put(key, value);
cache.put(key, null);
assertNull(cache.get(key));
}
}