From d707ffd08335cfb912ca8c03eee309fef3b0483e Mon Sep 17 00:00:00 2001 From: Carlos Ruiz Date: Fri, 26 Apr 2024 16:30:06 +0200 Subject: [PATCH] IDEMPIERE-6079 fix NPE caused by library ben-manes/caffeine (#2336) --- .../src/org/compiere/util/CCache.java | 6 ++++++ .../org/idempiere/test/performance/CacheTest.java | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/org.adempiere.base/src/org/compiere/util/CCache.java b/org.adempiere.base/src/org/compiere/util/CCache.java index 0e2c390d2b..9f96eb83ab 100644 --- a/org.adempiere.base/src/org/compiere/util/CCache.java +++ b/org.adempiere.base/src/org/compiere/util/CCache.java @@ -369,6 +369,8 @@ public void clear() @Override public boolean containsKey(Object key) { + if (key == null) + return false; return cache.containsKey(key) || nullList.contains(key); } // containsKey @@ -378,6 +380,8 @@ public boolean containsKey(Object key) @Override public boolean containsValue(Object value) { + if (value == null) + return false; return cache.containsValue(value); } // containsValue @@ -397,6 +401,8 @@ public Set> entrySet() @Override public V get(Object key) { + if (key == null) + return null; V v = cache.get(key); if (v == null) if (nullList.contains(key)) diff --git a/org.idempiere.test/src/org/idempiere/test/performance/CacheTest.java b/org.idempiere.test/src/org/idempiere/test/performance/CacheTest.java index 5c44c57ac7..de2269ed61 100644 --- a/org.idempiere.test/src/org/idempiere/test/performance/CacheTest.java +++ b/org.idempiere.test/src/org/idempiere/test/performance/CacheTest.java @@ -24,6 +24,7 @@ **********************************************************************/ package org.idempiere.test.performance; +import static org.assertj.core.api.Assertions.assertThatNoException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -946,4 +947,16 @@ public void testExpire() { CacheMgt.get().unregister(cache); } + + @Test + public void testNullKey() { + CCache testCache = new CCache(null, "Test_Cache", 10, 60, false); + assertThatNoException().isThrownBy(() -> testCache.get(null)); + assertThatNoException().isThrownBy(() -> testCache.containsKey(null)); + assertThatNoException().isThrownBy(() -> testCache.containsValue(null)); + assertFalse(testCache.containsValue(null)); + testCache.put("TestNull", null); + assertFalse(testCache.containsValue(null)); // still false because null is an unknown value + } + }