Fix NPE thrown by LRU Cache When attempting to remove a non-existing entry from the LRU cache, the underlying synchronized map will return 'null' rather than a Boolean. The attempt to unbox 'null' as a primitive boolean was causing an NPE to be thrown. Fix this by changing the return type of the remove() method to Boolean. Bug: Issue 14099 Change-Id: If4aca686ed0fe1e4ccc2ad58deb50694a343dc38
diff --git a/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/InMemoryLRU.java b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/InMemoryLRU.java index ac5183e..7f450d5 100644 --- a/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/InMemoryLRU.java +++ b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/InMemoryLRU.java
@@ -46,7 +46,13 @@ return LRUMap.containsKey(key); } - public boolean remove(K key) { + /** + * Remove a key from the map + * + * @param key element to remove from the map + * @return true when key was in the map, null otherwise + */ + public Boolean remove(K key) { return LRUMap.remove(key); }
diff --git a/src/test/java/com/googlesource/gerrit/modules/cache/chroniclemap/InMemoryLRUTest.java b/src/test/java/com/googlesource/gerrit/modules/cache/chroniclemap/InMemoryLRUTest.java index af27216..3d435c9 100644 --- a/src/test/java/com/googlesource/gerrit/modules/cache/chroniclemap/InMemoryLRUTest.java +++ b/src/test/java/com/googlesource/gerrit/modules/cache/chroniclemap/InMemoryLRUTest.java
@@ -42,4 +42,20 @@ assertThat(map.toArray()).asList().containsExactly("B", "C"); } + + @Test + public void remove_unexistingEntryShouldReturnNull() { + InMemoryLRU<Object> map = new InMemoryLRU<>(1); + + assertThat(map.remove("foo")).isNull(); + } + + @Test + public void remove_unexistingEntryShouldReturnTrue() { + InMemoryLRU<Object> map = new InMemoryLRU<>(1); + + map.add("foo"); + + assertThat(map.remove("foo")).isTrue(); + } }