This is an automated email from the ASF dual-hosted git repository.

gtchaboussie pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/trunk by this push:
     new ffdda02834 Improved: Rewrites the test class UtilCacheTest 
(OFBIZ-13311)
ffdda02834 is described below

commit ffdda02834948c51bc0434642f0715960e59f099
Author: Gaetan <[email protected]>
AuthorDate: Wed Jan 7 17:00:21 2026 +0100

    Improved: Rewrites the test class UtilCacheTest (OFBIZ-13311)
    
    Rewrote the UtilCacheTests to a more readable fashion, with tools in a 
dedicated class.
    Also removes the GenericTestCaseBase, that was used only in the
    UtilCacheTest.
    Deleted a part of the testExpire test method, that was based on the
    useAllMemory method, which is highly OS or hardware dependant making
    unit test dependant as well.
    
    Thanks: Nicolas for the help in analyzing and Jacopo and Jacques for the 
review.
---
 build.gradle                                       |   2 +
 .../ofbiz/base/test/GenericTestCaseBase.java       | 418 -------------------
 .../apache/ofbiz/base/util/UtilCacheTest.groovy    | 270 ++++++++++++
 .../ofbiz/base/util/tool/UtilCacheTestTools.groovy | 139 +++++++
 .../ofbiz/base/util/cache/UtilCacheTests.java      | 458 ---------------------
 5 files changed, 411 insertions(+), 876 deletions(-)

diff --git a/build.gradle b/build.gradle
index c7d141bf2b..9610468c58 100644
--- a/build.gradle
+++ b/build.gradle
@@ -307,6 +307,8 @@ sourceSets {
             include 
'org/apache/ofbiz/base/util/string/FlexibleStringExpanderBaseCodeTests.groovy'
             include 'org/apache/ofbiz/base/util/FileUtilTests.groovy'
             include 'org/apache/ofbiz/service/test/ServicePurgeTest.groovy'
+            include 'org/apache/ofbiz/base/util/UtilCacheTest.groovy'
+            include 'org/apache/ofbiz/base/util/tool/UtilCacheTestTools.groovy'
             include 'org.apache.ofbiz.base.test.SimpleTests.groovy'
         }
         resources {
diff --git 
a/framework/base/src/main/java/org/apache/ofbiz/base/test/GenericTestCaseBase.java
 
b/framework/base/src/main/java/org/apache/ofbiz/base/test/GenericTestCaseBase.java
deleted file mode 100644
index fe61bf3c5f..0000000000
--- 
a/framework/base/src/main/java/org/apache/ofbiz/base/test/GenericTestCaseBase.java
+++ /dev/null
@@ -1,418 +0,0 @@
-/*******************************************************************************
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- 
*******************************************************************************/
-package org.apache.ofbiz.base.test;
-
-import java.lang.reflect.Array;
-import java.lang.reflect.Constructor;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.LinkedHashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.Future;
-
-import junit.framework.TestCase;
-
-// This class can not use any other ofbiz helper methods, because it
-// may be used to test those helper methods.
-public abstract class GenericTestCaseBase extends TestCase {
-    protected GenericTestCaseBase(String name) {
-        super(name);
-    }
-
-    public static void useAllMemory() throws Exception {
-        List<long[]> dummy = new LinkedList<>();
-        try {
-            do {
-                dummy.add(new long[1048576]);
-            } while (true);
-        } catch (OutOfMemoryError e) {
-            System.gc();
-            Thread.sleep(100);
-        }
-    }
-
-    public static void assertStaticHelperClass(Class<?> clz) throws Exception {
-        Constructor<?>[] constructors = clz.getDeclaredConstructors();
-        assertEquals(clz.getName() + " constructor count", 1, 
constructors.length);
-        assertEquals(clz.getName() + " private declared constructor", 1 << 
Constructor.DECLARED, constructors[0].getModifiers()
-                & ~(1 << Constructor.PUBLIC) & (1 << Constructor.DECLARED));
-        constructors[0].setAccessible(true);
-        constructors[0].newInstance();
-    }
-
-    public static void assertComparison(String label, int wanted, int result) {
-        if (wanted == 0) {
-            assertEquals(label, wanted, result);
-        } else {
-            assertEquals(label, wanted, result / Math.abs(result));
-        }
-    }
-
-    public static <V, E extends Exception> void assertFuture(String label, 
Future<V> future, V wanted, boolean interruptable, Class<E> thrownClass,
-                                                             String 
thrownMessage) {
-        try {
-            assertEquals(label + ": future return", wanted, future.get());
-        } catch (InterruptedException e) {
-            assertTrue(label + ": expected interruption", interruptable);
-        } catch (ExecutionException e) {
-            assertNotNull(label + ": expecting an exception", thrownClass);
-            Throwable caught = e.getCause();
-            assertNotNull(label + ": captured exception", caught);
-            assertEquals(label + ": correct thrown class", thrownClass, 
caught.getClass());
-            if (thrownMessage != null) {
-                assertEquals(label + ": exception message", thrownMessage, 
caught.getMessage());
-            }
-        }
-    }
-
-    public static void assertNotEquals(Object wanted, Object got) {
-        assertNotEquals(null, wanted, got);
-    }
-
-    public static void assertNotEquals(String msg, Object wanted, Object got) {
-        if (wanted == null) {
-            if (got != null) {
-                return;
-            }
-            failEquals(msg, wanted, got);
-        } else if (wanted.equals(got)) {
-            failEquals(msg, wanted, got);
-        }
-    }
-
-    private static void failEquals(String msg, Object wanted, Object got) {
-        StringBuilder sb = new StringBuilder();
-        if (msg != null) {
-            sb.append(msg).append(' ');
-        }
-        sb.append(" expected value: ").append(wanted);
-        sb.append(" actual value: ").append(got);
-        fail(sb.toString());
-    }
-
-    public static <T> void assertEquals(List<T> wanted, Object got) {
-        assertEquals(null, wanted, got);
-    }
-
-    public static <T> void assertEquals(String msg, List<T> wanted, Object 
got) {
-        msg = msg == null ? "" : msg + ' ';
-        assertNotNull(msg + "expected a value", got);
-        if (got.getClass().isArray()) {
-            assertEqualsListArray(msg, wanted, got);
-            return;
-        }
-        if (!(got instanceof Collection<?>)) {
-            fail(msg + "expected a collection, got a " + got.getClass());
-        }
-        Iterator<T> leftIt = wanted.iterator();
-        Iterator<?> rightIt = ((Collection<?>) got).iterator();
-        int i = 0;
-        while (leftIt.hasNext() && rightIt.hasNext()) {
-            T left = leftIt.next();
-            Object right = rightIt.next();
-            assertEquals(msg + "item " + i, left, right);
-            i++;
-        }
-        assertFalse(msg + "not enough items", leftIt.hasNext());
-        assertFalse(msg + "too many items", rightIt.hasNext());
-    }
-
-    public static <T> void assertEquals(Collection<T> wanted, Object got) {
-        assertEquals(null, wanted, got);
-    }
-
-    public static <T> void assertEquals(String msg, Collection<T> wanted, 
Object got) {
-        if (wanted instanceof List<?> || wanted instanceof Set<?>) {
-            // list.equals(list) and set.equals(set), see docs for 
Collection.equals
-            if (got instanceof Set<?>) {
-                fail("Not a collection, is a set");
-            }
-            if (got instanceof List<?>) {
-                fail("Not a collection, is a list");
-            }
-        }
-        if (wanted.equals(got)) {
-            return;
-        }
-        if (!(got instanceof Collection<?>)) {
-            fail(msg + "not a collection");
-        }
-        // Need to check the reverse, wanted may not implement equals,
-        // which is the case for HashMap.values()
-        if (got.equals(wanted)) {
-            return;
-        }
-        msg = msg == null ? "" : msg + ' ';
-        assertNotNull(msg + "expected a value", got);
-        List<T> list = new ArrayList<>(wanted);
-        Iterator<?> rightIt = ((Collection<?>) got).iterator();
-        OUTER:
-        while (rightIt.hasNext()) {
-            Object right = rightIt.next();
-            for (int i = 0; i < list.size(); i++) {
-                T left = list.get(i);
-                if (left == null) {
-                    if (right == null) {
-                        list.remove(i);
-                        continue OUTER;
-                    }
-                } else if (left.equals(right)) {
-                    list.remove(i);
-                    continue OUTER;
-                }
-            }
-            fail(msg + "couldn't find " + right);
-        }
-        if (!list.isEmpty()) {
-            fail(msg + "not enough items: " + list);
-        }
-    }
-
-    public static <T> void assertEquals(Set<T> wanted, Object got) {
-        assertEquals(null, wanted, got);
-    }
-
-    public static <T> void assertEquals(String msg, Set<T> wanted, Object got) 
{
-        if (wanted.equals(got)) {
-            return;
-        }
-        if (!(got instanceof Set<?>)) {
-            fail(msg + "not a set");
-        }
-        // Need to check the reverse, wanted may not implement equals,
-        // which is the case for HashMap.values()
-        if (got.equals(wanted)) {
-            return;
-        }
-        msg = msg == null ? "" : msg + ' ';
-        assertNotNull(msg + "expected a value", got);
-        Set<T> wantedSet = new HashSet<>(wanted);
-        Iterator<?> rightIt = ((Set<?>) got).iterator();
-        while (rightIt.hasNext()) {
-            Object right = rightIt.next();
-            if (wantedSet.contains(right)) {
-                wantedSet.remove(right);
-            } else {
-                fail(msg + "couldn't find " + right);
-            }
-        }
-        if (!wantedSet.isEmpty()) {
-            fail(msg + "not enough items: " + wantedSet);
-        }
-    }
-
-    private static void assertEqualsArrayArray(String msg, Object wanted, 
Object got) {
-        int i = 0;
-        while (i < Array.getLength(wanted) && i < Array.getLength(got)) {
-            Object left = Array.get(wanted, i);
-            Object right = Array.get(got, i);
-            assertEquals(msg + "item " + i, left, right);
-            i++;
-        }
-        assertFalse(msg + "not enough items", i < Array.getLength(wanted));
-        assertFalse(msg + "too many items", i < Array.getLength(got));
-    }
-
-    private static <T> void assertEqualsArrayList(String msg, Object wanted, 
List<T> got) {
-        Iterator<T> rightIt = got.iterator();
-        int i = 0;
-        while (i < Array.getLength(wanted) && rightIt.hasNext()) {
-            Object left = Array.get(wanted, i);
-            T right = rightIt.next();
-            assertEquals(msg + "item " + i, left, right);
-            i++;
-        }
-        assertFalse(msg + "too enough items", i < Array.getLength(wanted));
-        assertFalse(msg + "not many items", rightIt.hasNext());
-    }
-
-    private static <T> void assertEqualsListArray(String msg, List<T> wanted, 
Object got) {
-        Iterator<T> leftIt = wanted.iterator();
-        int i = 0;
-        while (leftIt.hasNext() && i < Array.getLength(got)) {
-            T left = leftIt.next();
-            Object right = Array.get(got, i);
-            assertEquals(msg + "item " + i, left, right);
-            i++;
-        }
-        assertFalse(msg + "not enough items", leftIt.hasNext());
-        assertFalse(msg + "too many items", i < Array.getLength(got));
-    }
-
-    public static <V, I extends Iterable<V>> void assertEqualsIterable(String 
label, List<? extends V> wanted, I got) {
-        assertEqualsIterable(label, wanted, 0, got, 0);
-    }
-
-    public static <V, I extends Iterable<V>> void assertEqualsIterable(String 
label, List<? extends V> wanted, int wantedExtra, I got, int gotExtra) {
-        Iterator<? extends V> wantedIt = wanted.iterator();
-        Iterator<V> gotIt = got.iterator();
-        while (wantedIt.hasNext() && gotIt.hasNext()) {
-            assertEquals(label + ":iterate", wantedIt.next(), gotIt.next());
-        }
-        while (wantedExtra > 0) {
-            assertTrue(label + ":wanted-extra(" + wantedExtra + ")", 
wantedIt.hasNext());
-            wantedExtra--;
-        }
-        assertFalse(label + ":wanted-done", wantedIt.hasNext());
-        while (gotExtra > 0) {
-            assertTrue(label + ":got-extra(" + gotExtra + ")", 
gotIt.hasNext());
-            gotExtra--;
-        }
-        assertFalse(label + ":got-done", gotIt.hasNext());
-    }
-
-    public static <V, I extends Iterable<V>> void assertEqualsIterable(String 
label, List<? extends V> wanted, List<? extends V> wantedExtra,
-                                                                       I got, 
List<? extends V> gotExtra) {
-        assertEqualsIterable(label, wanted, wantedExtra, false, got, gotExtra, 
false);
-    }
-
-    public static <V, I extends Iterable<V>> void assertEqualsIterable(String 
label, List<? extends V> wanted, List<? extends V> wantedExtra,
-                                                                       boolean 
removeWanted, I got, List<? extends V> gotExtra, boolean removeGot) {
-        Iterator<? extends V> wantedIt = wanted.iterator();
-        Iterator<V> gotIt = got.iterator();
-        while (wantedIt.hasNext() && gotIt.hasNext()) {
-            assertEquals(label + ":iterate", wantedIt.next(), gotIt.next());
-        }
-        while (!wantedExtra.isEmpty()) {
-            assertTrue(label + ":wanted-extra(" + wantedExtra + ")-hasNext", 
wantedIt.hasNext());
-            assertEquals(label + ":wanted-extra(" + wantedExtra + ")", 
wantedExtra.remove(0), wantedIt.next());
-            if (removeWanted) {
-                wantedIt.remove();
-            }
-        }
-        assertFalse(label + ":wanted-done", wantedIt.hasNext());
-        while (!gotExtra.isEmpty()) {
-            assertTrue(label + ":got-extra(" + gotExtra + ")-hasNext", 
gotIt.hasNext());
-            assertEquals(label + ":got-extra(" + gotExtra + ")", 
gotExtra.remove(0), gotIt.next());
-            if (removeGot) {
-                gotIt.remove();
-            }
-        }
-        assertFalse(label + ":got-done", gotIt.hasNext());
-    }
-
-    public static <T> void assertEquals(Map<T, ?> wanted, Object got) {
-        assertEquals(null, wanted, got);
-    }
-
-    public static <T> void assertEquals(String msg, Map<T, ?> wanted, Object 
got) {
-        msg = msg == null ? "" : msg + ' ';
-        assertNotNull(msg + "expected a value", got);
-        if (!(got instanceof Map<?, ?>)) {
-            fail(msg + "expected a map");
-        }
-        Map<?, ?> gotMap = (Map<?, ?>) got;
-        if (!got.equals(wanted)) {
-            Set<T> leftKeys = new LinkedHashSet<>(wanted.keySet());
-            Set<Object> rightKeys = new HashSet<>(gotMap.keySet());
-            for (T key: leftKeys) {
-                assertTrue(msg + "got key(" + key + ")", 
rightKeys.remove(key));
-                assertEquals(msg + "key(" + key + ") value", wanted.get(key), 
gotMap.get(key));
-            }
-            assertTrue(msg + "extra entries", rightKeys.isEmpty());
-        }
-    }
-
-    public static void assertEquals(String msg, String wanted, String got) {
-        TestCase.assertEquals(msg, wanted, got);
-    }
-
-    public static void assertEquals(Object wanted, Object got) {
-        assertEquals(null, wanted, got);
-    }
-
-    public static void assertEquals(String msg, Object wanted, Object got) {
-        if (wanted instanceof List) {
-            assertEquals(msg, (List<?>) wanted, got);
-        } else if (wanted instanceof Map) {
-            assertEquals(msg, (Map<?, ?>) wanted, got);
-        } else if (wanted == null) {
-            TestCase.assertEquals(msg, wanted, got);
-        } else if (wanted instanceof Set) {
-            assertEquals(msg, (Set<?>) wanted, got);
-        } else if (wanted instanceof Collection) {
-            assertEquals(msg, (Collection<?>) wanted, got);
-        } else if (wanted.getClass().isArray()) {
-            if (got == null) {
-                TestCase.assertEquals(msg, wanted, got);
-            } else if (got.getClass().isArray()) {
-                assertEqualsArrayArray(msg, wanted, got);
-            } else if (got instanceof List) {
-                assertEqualsArrayList(msg, wanted, (List<?>) got);
-            } else {
-                TestCase.assertEquals(msg, wanted, got);
-            }
-        } else {
-            TestCase.assertEquals(msg, wanted, got);
-        }
-    }
-
-    public static <T> List<T> list(T value) {
-        List<T> list = new ArrayList<>(1);
-        list.add(value);
-        return list;
-    }
-
-    @SafeVarargs
-    public static <T> List<T> list(T... list) {
-        return new ArrayList<>(Arrays.asList(list));
-    }
-
-    public static <T> Set<T> set(T value) {
-        Set<T> set = new HashSet<>(1);
-        set.add(value);
-        return set;
-    }
-
-    @SafeVarargs
-    public static <T> Set<T> set(T... list) {
-        return new HashSet<>(Arrays.asList(list));
-    }
-
-    public static <T> Set<T> set(Iterable<T> iterable) {
-        return set(iterable.iterator());
-    }
-
-    public static <T> Set<T> set(Iterator<T> it) {
-        Set<T> set = new HashSet<>();
-        while (it.hasNext()) {
-            T item = it.next();
-            set.add(item);
-        }
-        return set;
-    }
-
-    @SuppressWarnings("unchecked")
-    public static <K, V> Map<K, V> map(Object... list) {
-        assertEquals("list has even number of elements", 0, list.length % 2);
-        Map<K, V> map = new LinkedHashMap<>();
-        for (int i = 0; i < list.length; i += 2) {
-            map.put((K) list[i], (V) list[i + 1]);
-        }
-        return map;
-    }
-}
diff --git 
a/framework/base/src/test/groovy/org/apache/ofbiz/base/util/UtilCacheTest.groovy
 
b/framework/base/src/test/groovy/org/apache/ofbiz/base/util/UtilCacheTest.groovy
new file mode 100644
index 0000000000..6f50cd204b
--- /dev/null
+++ 
b/framework/base/src/test/groovy/org/apache/ofbiz/base/util/UtilCacheTest.groovy
@@ -0,0 +1,270 @@
+package org.apache.ofbiz.base.util
+
+import static org.apache.ofbiz.base.util.tool.UtilCacheTestTools.createListener
+
+import org.apache.ofbiz.base.util.cache.UtilCache
+import org.apache.ofbiz.base.util.tool.UtilCacheTestTools.Listener
+import org.apache.ofbiz.service.testtools.OFBizTestCase
+import org.junit.Test
+import org.junit.jupiter.api.BeforeAll
+
+// codenarc-disable JUnitLostTest
+class UtilCacheTest extends OFBizTestCase {
+
+    UtilCacheTest(String name) {
+        super(name)
+    }
+
+    @BeforeAll
+    static void clearCaches() { // codenarc-disable UnusedPrivateMethod
+        UtilCache.clearAllCaches()
+    }
+
+    static void populateEmptyCache(String name, UtilCache cacheToPopulate, 
Integer rowsToAdd, Map controlMap,
+                                   Listener controlListener = null) {
+        assert cacheToPopulate.isEmpty()
+        assert controlMap.isEmpty()
+        for (int row in 1..rowsToAdd) {
+            String key = "${row}KEY_$name"
+            String value = "${row}VAL_$name"
+            if (controlListener) {
+                controlListener.noteKeyAddition(cacheToPopulate, key, value)
+            }
+            cacheToPopulate.put(key, value)
+            controlMap.put(key, value)
+        }
+        assert cacheToPopulate.size() == rowsToAdd
+        assert controlMap.keySet() == cacheToPopulate.getCacheLineKeys()
+        assert cacheToPopulate.values().containsAll(controlMap.values())
+    }
+
+    static void doUtilCacheCreateTest(UtilCache myCache, Integer sizeLimit, 
Integer maxInMemory, Long expireTime,
+                                      Boolean useSoftReference) {
+        if (sizeLimit) {
+            assert sizeLimit.intValue() == myCache.sizeLimit
+        }
+        if (maxInMemory) {
+            assert maxInMemory.intValue() == myCache.maxInMemory
+        }
+        if (expireTime) {
+            assert expireTime.longValue() == myCache.expireTime
+        }
+        if (useSoftReference) {
+            assert useSoftReference == myCache.getUseSoftReference()
+        }
+        assert myCache.isEmpty()
+        assert Collections.emptySet() == myCache.cacheLineKeys
+        assert Collections.emptyList() == myCache.values()
+        assert myCache === UtilCache.findCache(myCache.name)
+        assert myCache !== UtilCache.createUtilCache()
+    }
+
+    static <K, V> void doSingleKeyInSingleCacheTest(UtilCache myCache, K 
myKey, V myValue) {
+        assert !myCache.isEmpty()
+        assert myCache.size() == 1
+        assert myCache.containsKey(myKey)
+        assert UtilCache.validKey(myCache.getName(), myKey)
+        assert myValue == myCache.get(myKey)
+    }
+
+    static <K> void doKeyNotInCacheTest(UtilCache myCache, K myKey) {
+        assert !myCache.containsKey(myKey)
+        assert !UtilCache.validKey(myCache.getName(), myKey)
+        assert myCache.get(myKey) == null
+        assert myCache.remove(myKey) == null
+    }
+
+    // codenarc-disable JUnitTestMethodWithoutAssert
+    @Test
+    void testCreateUtilCache() {
+        doUtilCacheCreateTest(UtilCache.createUtilCache(), null, null, null, 
null)
+        doUtilCacheCreateTest(UtilCache.createUtilCache(name), null, null, 
null, null)
+        doUtilCacheCreateTest(UtilCache.createUtilCache(name, false), null, 
null, null, Boolean.FALSE)
+        doUtilCacheCreateTest(UtilCache.createUtilCache(name, true), null, 
null, null, Boolean.TRUE)
+        doUtilCacheCreateTest(UtilCache.createUtilCache(5, 15000), 5, null, 
15000L, null)
+        doUtilCacheCreateTest(UtilCache.createUtilCache(name, 6, 16000), 6, 
null, 16000L, null)
+        doUtilCacheCreateTest(UtilCache.createUtilCache(name, 7, 17000, 
false), 7, null, 17000L, Boolean.FALSE)
+        doUtilCacheCreateTest(UtilCache.createUtilCache(name, 8, 18000, true), 
8, null, 18000L, Boolean.TRUE)
+        doUtilCacheCreateTest(UtilCache.createUtilCache(name, 9, 5, 19000, 
false), 9, 5, 19000L, Boolean.FALSE)
+        doUtilCacheCreateTest(UtilCache.createUtilCache(name, 10, 6, 20000, 
false), 10, 6, 20000L, Boolean.FALSE)
+        doUtilCacheCreateTest(UtilCache.createUtilCache(name, 11, 7, 21000, 
false, 'a', 'b'), 11, 7, 21000L, Boolean.FALSE)
+        doUtilCacheCreateTest(UtilCache.createUtilCache(name, 12, 8, 22000, 
false, 'c', 'd'), 12, 8, 22000L, Boolean.FALSE)
+    }
+    // codenarc-enable JUnitTestMethodWithoutAssert
+
+    @Test
+    void testCacheGetterOnCreation() {
+        UtilCache myCache = UtilCache.createUtilCache(name, 5, 0, 0, false)
+        assert UtilCache.getUtilCacheTableKeySet().contains(name)
+        assert myCache == UtilCache.findCache(name)
+        assert myCache == UtilCache.getOrCreateUtilCache(
+                name, myCache.sizeLimit, myCache.maxInMemory, 
myCache.expireTime, myCache.useSoftReference)
+    }
+
+    @Test
+    void testCacheCreateEntry() {
+        UtilCache myCache = UtilCache.createUtilCache(name, 5, 0, 0, false)
+        Listener myCacheListener = createListener(myCache)
+        Listener controlListener = new Listener()
+        String key = "KEY_$name"
+        String value = "VAL_$name"
+
+        controlListener.noteKeyAddition(myCache, key, value)
+        Object objectInCache = myCache.put(key, value)
+        assert objectInCache == null
+        doSingleKeyInSingleCacheTest myCache, key, value
+        assert myCacheListener == controlListener
+    }
+
+    @Test
+    void testCacheCreateEntryWithNullKey() {
+        UtilCache myCache = UtilCache.createUtilCache(name, 5, 0, 0, false)
+        Listener myCacheListener = createListener(myCache)
+        Listener controlListener = new Listener()
+        String value = "VAL_$name"
+
+        controlListener.noteKeyAddition(myCache, null, value)
+        Object objectInCache = myCache.put(null, value)
+        assert objectInCache == null
+        doSingleKeyInSingleCacheTest myCache, null, value
+        assert myCacheListener == controlListener
+    }
+
+    @Test
+    void testCacheUpdateEntry() {
+        UtilCache myCache = UtilCache.createUtilCache(name, 5, 0, 0, false)
+        Listener myCacheListener = createListener(myCache)
+        Listener controlListener = new Listener()
+        String key = "KEY_$name"
+        String value1 = "VAL1_$name"
+        String value2 = "VAL2_$name"
+
+        controlListener.noteKeyAddition(myCache, key, value1)
+        Object objectInCache = myCache.put(key, value1)
+        assert objectInCache == null
+        doSingleKeyInSingleCacheTest myCache, key, value1
+
+        controlListener.noteKeyUpdate(myCache, key, value2, value1)
+        objectInCache = myCache.put(key, value2)
+        assert objectInCache == value1
+        doSingleKeyInSingleCacheTest myCache, key, value2
+        assert myCacheListener == controlListener
+    }
+
+    @Test
+    void testRemoveCacheEntry() {
+        UtilCache myCache = UtilCache.createUtilCache(name, 5, 0, 0, false)
+        Listener myCacheListener = createListener(myCache)
+        Listener controlListener = new Listener()
+        String key = "KEY_$name"
+        String value = "VAL_$name"
+
+        controlListener.noteKeyAddition(myCache, key, value)
+        Object objectInCache = myCache.put(key, value)
+        assert objectInCache == null
+        doSingleKeyInSingleCacheTest myCache, key, value
+
+        controlListener.noteKeyRemoval(myCache, key, value)
+        objectInCache = myCache.remove(key)
+        assert objectInCache == value
+        doKeyNotInCacheTest myCache, key
+        assert myCacheListener == controlListener
+    }
+
+    @Test
+    void testSetExpireCache() {
+        UtilCache myCache = UtilCache.createUtilCache(name, 5, 0, 0, false)
+        Listener myCacheListener = createListener(myCache)
+        Listener controlListener = new Listener()
+        Map controlMap = [:]
+        myCache.setExpireTime(100)
+        populateEmptyCache(name, myCache, 5, controlMap, controlListener)
+        controlMap.forEach { k, v -> controlListener.noteKeyRemoval(myCache, 
k, v) }
+        try {
+            sleep(200)
+        } catch (InterruptedException e) {
+            fail('Failed to pause process during tests execution')
+        }
+        assert myCache.getCacheLineKeys().isEmpty()
+        assert myCache.values().isEmpty()
+        assert myCacheListener == controlListener
+    }
+
+    @Test
+    void testChangeMemorySize() {
+        int size = 5
+        UtilCache<String, Serializable> myCache = 
UtilCache.createUtilCache(name, size, size, 0, false)
+        Map controlMap = [:]
+        populateEmptyCache(name, myCache, 5, controlMap)
+
+        myCache.setMaxInMemory(2)
+        assert myCache.size() == 2
+        controlMap.keySet().retainAll(myCache.getCacheLineKeys())
+        assert controlMap.keySet() == myCache.getCacheLineKeys()
+        assert myCache.values().containsAll(controlMap.values())
+
+        myCache.setMaxInMemory(0)
+        assert controlMap.keySet() == myCache.getCacheLineKeys()
+        assert myCache.values().containsAll(controlMap.values())
+
+        myCache.setMaxInMemory(size)
+        assert controlMap.keySet() == myCache.getCacheLineKeys()
+        assert myCache.values().containsAll(controlMap.values())
+    }
+
+    @Test
+    void testPutIfAbsent() {
+        UtilCache<String, String> myCache = UtilCache.createUtilCache(name, 1, 
1, 0, false)
+        Listener myCacheListener = createListener(myCache)
+        Listener controlListener = new Listener()
+        String key = "KEY_$name"
+        String value1 = "VAL1_$name"
+        String value2 = "VAL2_$name"
+
+        controlListener.noteKeyAddition(myCache, key, value1)
+        Object oldObject = myCache.putIfAbsent(key, value1)
+        assert oldObject == null
+        doSingleKeyInSingleCacheTest(myCache, key, value1)
+
+        oldObject = myCache.putIfAbsent(key, value2)
+        assert oldObject == value1
+        doSingleKeyInSingleCacheTest(myCache, key, value1)
+        assert myCacheListener == controlListener
+    }
+
+    @Test
+    void testPutIfAbsentAndGet() {
+        UtilCache<String, String> myCache = UtilCache.createUtilCache(name, 1, 
1, 0, false)
+        Listener myCacheListener = createListener(myCache)
+        Listener controlListener = new Listener()
+        String key1 = "KEY1_$name"
+        String value1 = "VAL1_$name"
+
+        Object inCache = myCache.get(key1)
+        assert inCache == null
+        controlListener.noteKeyAddition(myCache, key1, value1)
+        inCache = myCache.putIfAbsentAndGet(key1, value1)
+        assert inCache == value1
+        doSingleKeyInSingleCacheTest myCache, key1, value1
+
+        inCache = myCache.putIfAbsentAndGet(key1, 'newValue')
+        assert inCache == value1
+        doSingleKeyInSingleCacheTest myCache, key1, value1
+
+        String key2 = "KEY2_$name"
+        String value2 = new String('anotherValue') // codenarc-disable 
UnnecessaryStringInstantiation
+        String value2Bis = new String('anotherValue') // codenarc-disable 
UnnecessaryStringInstantiation
+        assert value2 == value2Bis
+        assert value2 !== value2Bis
+
+        controlListener.noteKeyAddition(myCache, key2, value2)
+        inCache = myCache.putIfAbsentAndGet(key2, value2)
+        assert inCache === value2
+        inCache = myCache.putIfAbsentAndGet(key2, value2Bis)
+        assert inCache !== value2Bis
+        assert inCache === value2
+        doSingleKeyInSingleCacheTest myCache, key2, value2
+        assert controlListener == myCacheListener
+    }
+
+}
diff --git 
a/framework/base/src/test/groovy/org/apache/ofbiz/base/util/tool/UtilCacheTestTools.groovy
 
b/framework/base/src/test/groovy/org/apache/ofbiz/base/util/tool/UtilCacheTestTools.groovy
new file mode 100644
index 0000000000..123cedee05
--- /dev/null
+++ 
b/framework/base/src/test/groovy/org/apache/ofbiz/base/util/tool/UtilCacheTestTools.groovy
@@ -0,0 +1,139 @@
+package org.apache.ofbiz.base.util.tool
+
+import org.apache.ofbiz.base.util.UtilObject
+import org.apache.ofbiz.base.util.cache.CacheListener
+import org.apache.ofbiz.base.util.cache.UtilCache
+
+class UtilCacheTestTools {
+
+    static <K, V> Listener<K, V> createListener(UtilCache<K, V> cache) {
+        Listener<K, V> listener = new Listener<>()
+        cache.addListener(listener)
+        return listener
+    }
+
+    static class Change {
+
+        private int count = 1
+
+        int getCount() {
+            return count
+        }
+
+        void incCount() {
+            count += 1
+        }
+
+    }
+
+    protected static final class Removal<V> extends Change {
+
+        private final V oldValue
+
+        int hashCode() {
+            return UtilObject.doHashCode(oldValue)
+        }
+
+        boolean equals(Object o) {
+            if (o instanceof Removal) {
+                return Objects.equals(oldValue, (o as Removal).oldValue)
+            }
+            return false
+        }
+
+        private Removal(V oldValue) {
+            this.oldValue = oldValue
+        }
+
+    }
+
+    protected static final class Addition<V> extends Change {
+
+        private final V newValue
+
+        int hashCode() {
+            return UtilObject.doHashCode(newValue)
+        }
+
+        boolean equals(Object o) {
+            if (o instanceof Addition) {
+                return Objects.equals(newValue, (o as Addition).newValue)
+            }
+            return false
+        }
+
+        private Addition(V newValue) {
+            this.newValue = newValue
+        }
+
+    }
+
+    protected static final class Update<V> extends Change {
+
+        private final V newValue
+        private final V oldValue
+
+        int hashCode() {
+            return UtilObject.doHashCode(newValue) ^ 
UtilObject.doHashCode(oldValue)
+        }
+
+        boolean equals(Object o) {
+            if (o instanceof Update) {
+                if (!Objects.equals(newValue, (o as Update).newValue)) {
+                    return false
+                }
+                return Objects.equals(oldValue, (o as Update).oldValue)
+            }
+            return false
+        }
+
+        private Update(V newValue, V oldValue) {
+            this.newValue = newValue
+            this.oldValue = oldValue
+        }
+
+    }
+
+    public static final class Listener<K, V> implements CacheListener<K, V> {
+
+        private final Map<K, Set<Change>> changeMap = [:]
+
+        // codenarc-disable UnusedMethodParameter, SynchronizedMethod
+        synchronized void noteKeyRemoval(UtilCache<K, V> cache, K key, V 
oldValue) {
+            add(key, new Removal<>(oldValue))
+        }
+
+        synchronized void noteKeyAddition(UtilCache<K, V> cache, K key, V 
newValue) {
+            add(key, new Addition<>(newValue))
+        }
+
+        synchronized void noteKeyUpdate(UtilCache<K, V> cache, K key, V 
newValue, V oldValue) {
+            add(key, new Update<>(newValue, oldValue))
+        }
+        // codenarc-enable UnusedMethodParameter, SynchronizedMethod
+
+        boolean equals(Object o) {
+            if (!(o instanceof Listener)) {
+                return false
+            }
+            return changeMap == (o as Listener).changeMap
+        }
+
+        int hashCode() {
+            return super.hashCode()
+        }
+
+        private void add(K key, Change change) {
+            Set<Change> changeSet = changeMap.computeIfAbsent(key, k -> [] as 
Set)
+            for (Change checkChange : changeSet) {
+                if (checkChange == change) {
+                    checkChange.incCount()
+                    return
+                }
+            }
+            changeSet.add(change)
+        }
+
+    }
+
+}
diff --git 
a/framework/base/src/test/java/org/apache/ofbiz/base/util/cache/UtilCacheTests.java
 
b/framework/base/src/test/java/org/apache/ofbiz/base/util/cache/UtilCacheTests.java
deleted file mode 100644
index 077256b95c..0000000000
--- 
a/framework/base/src/test/java/org/apache/ofbiz/base/util/cache/UtilCacheTests.java
+++ /dev/null
@@ -1,458 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.ofbiz.base.util.cache;
-
-import static org.apache.ofbiz.base.test.GenericTestCaseBase.useAllMemory;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.containsInAnyOrder;
-import static org.hamcrest.Matchers.greaterThan;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotSame;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertTrue;
-
-import java.io.Serializable;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Objects;
-import java.util.Set;
-
-import org.apache.ofbiz.base.util.UtilMisc;
-import org.apache.ofbiz.base.util.UtilObject;
-import org.junit.Test;
-
-@SuppressWarnings("serial")
-public class UtilCacheTests implements Serializable {
-    abstract static class Change {
-        private int count = 1;
-
-        public int getCount() {
-            return count;
-        }
-
-        public void incCount() {
-            count += 1;
-        }
-    }
-
-    protected static final class Removal<V> extends Change {
-        private final V oldValue;
-
-        protected Removal(V oldValue) {
-            this.oldValue = oldValue;
-        }
-
-        @Override
-        public int hashCode() {
-            return UtilObject.doHashCode(oldValue);
-        }
-
-        @Override
-        public boolean equals(Object o) {
-            if (o instanceof Removal<?>) {
-                Removal<?> other = (Removal<?>) o;
-                return Objects.equals(oldValue, other.oldValue);
-            }
-            return false;
-        }
-    }
-
-    protected static final class Addition<V> extends Change {
-        private final V newValue;
-
-        protected Addition(V newValue) {
-            this.newValue = newValue;
-        }
-
-        @Override
-        public int hashCode() {
-            return UtilObject.doHashCode(newValue);
-        }
-
-        @Override
-        public boolean equals(Object o) {
-            if (o instanceof Addition<?>) {
-                Addition<?> other = (Addition<?>) o;
-                return Objects.equals(newValue, other.newValue);
-            }
-            return false;
-        }
-    }
-
-    protected static final class Update<V> extends Change {
-        private final V newValue;
-        private final V oldValue;
-
-        protected Update(V newValue, V oldValue) {
-            this.newValue = newValue;
-            this.oldValue = oldValue;
-        }
-
-        @Override
-        public int hashCode() {
-            return UtilObject.doHashCode(newValue) ^ 
UtilObject.doHashCode(oldValue);
-        }
-
-        @Override
-        public boolean equals(Object o) {
-            if (o instanceof Update<?>) {
-                Update<?> other = (Update<?>) o;
-                if (!Objects.equals(newValue, other.newValue)) {
-                    return false;
-                }
-                return Objects.equals(oldValue, other.oldValue);
-            }
-            return false;
-        }
-    }
-
-    private static final class Listener<K, V> implements CacheListener<K, V> {
-        private Map<K, Set<Change>> changeMap = new HashMap<>();
-
-        private void add(K key, Change change) {
-            Set<Change> changeSet = changeMap.get(key);
-            if (changeSet == null) {
-                changeSet = new HashSet<>();
-                changeMap.put(key, changeSet);
-            }
-            for (Change checkChange: changeSet) {
-                if (checkChange.equals(change)) {
-                    checkChange.incCount();
-                    return;
-                }
-            }
-            changeSet.add(change);
-        }
-
-        @Override
-        public synchronized void noteKeyRemoval(UtilCache<K, V> cache, K key, 
V oldValue) {
-            add(key, new Removal<>(oldValue));
-        }
-
-        @Override
-        public synchronized void noteKeyAddition(UtilCache<K, V> cache, K key, 
V newValue) {
-            add(key, new Addition<>(newValue));
-        }
-
-        @Override
-        public synchronized void noteKeyUpdate(UtilCache<K, V> cache, K key, V 
newValue, V oldValue) {
-            add(key, new Update<>(newValue, oldValue));
-        }
-
-        @Override
-        public boolean equals(Object o) {
-            if (!(o instanceof Listener)) {
-                return false;
-            }
-            Listener<?, ?> other = (Listener<?, ?>) o;
-            return changeMap.equals(other.changeMap);
-        }
-
-        @Override
-        public int hashCode() {
-            return super.hashCode();
-        }
-    }
-
-    private static <K, V> Listener<K, V> createListener(UtilCache<K, V> cache) 
{
-        Listener<K, V> listener = new Listener<>();
-        cache.addListener(listener);
-        return listener;
-    }
-
-    private <K, V> UtilCache<K, V> createUtilCache(int sizeLimit, int 
maxInMemory, long ttl, boolean useSoftReference) {
-        return UtilCache.createUtilCache(getClass().getName(), sizeLimit, 
maxInMemory, ttl, useSoftReference);
-    }
-
-    private static <K, V> void assertUtilCacheSettings(UtilCache<K, V> cache, 
Integer sizeLimit, Integer maxInMemory,
-            Long expireTime, Boolean useSoftReference) {
-        if (sizeLimit != null) {
-            assertEquals(cache.getName() + ":sizeLimit", sizeLimit.intValue(), 
cache.getSizeLimit());
-        }
-        if (maxInMemory != null) {
-            assertEquals(cache.getName() + ":maxInMemory", 
maxInMemory.intValue(), cache.getMaxInMemory());
-        }
-        if (expireTime != null) {
-            assertEquals(cache.getName() + ":expireTime", 
expireTime.longValue(), cache.getExpireTime());
-        }
-        if (useSoftReference != null) {
-            assertEquals(cache.getName() + ":useSoftReference", 
useSoftReference.booleanValue(),
-                    cache.getUseSoftReference());
-        }
-        assertEquals("initial empty", true, cache.isEmpty());
-        assertEquals("empty keys", Collections.emptySet(), 
cache.getCacheLineKeys());
-        assertEquals("empty values", Collections.emptyList(), cache.values());
-        assertSame("find cache", cache, UtilCache.findCache(cache.getName()));
-        assertNotSame("new cache", cache, UtilCache.createUtilCache());
-    }
-
-    @Test
-    public void testCreateUtilCache() {
-        String name = getClass().getName();
-        assertUtilCacheSettings(UtilCache.createUtilCache(), null, null, null, 
null);
-        assertUtilCacheSettings(UtilCache.createUtilCache(name), null, null, 
null, null);
-        assertUtilCacheSettings(UtilCache.createUtilCache(name, false), null, 
null, null, Boolean.FALSE);
-        assertUtilCacheSettings(UtilCache.createUtilCache(name, true), null, 
null, null, Boolean.TRUE);
-        assertUtilCacheSettings(UtilCache.createUtilCache(5, 15000), 5, null, 
15000L, null);
-        assertUtilCacheSettings(UtilCache.createUtilCache(name, 6, 16000), 6, 
null, 16000L, null);
-        assertUtilCacheSettings(UtilCache.createUtilCache(name, 7, 17000, 
false), 7, null, 17000L, Boolean.FALSE);
-        assertUtilCacheSettings(UtilCache.createUtilCache(name, 8, 18000, 
true), 8, null, 18000L, Boolean.TRUE);
-        assertUtilCacheSettings(UtilCache.createUtilCache(name, 9, 5, 19000, 
false), 9, 5, 19000L, Boolean.FALSE);
-        assertUtilCacheSettings(UtilCache.createUtilCache(name, 10, 6, 20000, 
false), 10, 6, 20000L, Boolean.FALSE);
-        assertUtilCacheSettings(
-                UtilCache.createUtilCache(name, 11, 7, 21000, false, "a", 
"b"), 11, 7, 21000L, Boolean.FALSE);
-        assertUtilCacheSettings(
-                UtilCache.createUtilCache(name, 12, 8, 22000, false, "c", 
"d"), 12, 8, 22000L, Boolean.FALSE);
-    }
-
-    public static <K, V> void assertKey(String label, UtilCache<K, V> cache, K 
key, V value, V other, int size,
-            Map<K, V> map) {
-        assertNull(label + ":get-empty", cache.get(key));
-        assertFalse(label + ":containsKey-empty", cache.containsKey(key));
-        V oldValue = cache.put(key, other);
-        assertTrue(label + ":containsKey-class", cache.containsKey(key));
-        assertEquals(label + ":get-class", other, cache.get(key));
-        assertNull(label + ":oldValue-class", oldValue);
-        assertEquals(label + ":size-class", size, cache.size());
-        oldValue = cache.put(key, value);
-        assertTrue(label + ":containsKey-value", cache.containsKey(key));
-        assertEquals(label + ":get-value", value, cache.get(key));
-        assertEquals(label + ":oldValue-value", other, oldValue);
-        assertEquals(label + ":size-value", size, cache.size());
-        map.put(key, value);
-        assertEquals(label + ":map-keys", map.keySet(), 
cache.getCacheLineKeys());
-        assertThat(label + ":map-values", cache.values(), 
containsInAnyOrder(map.values().toArray()));
-    }
-
-    private static <K, V> void assertHasSingleKey(UtilCache<K, V> cache, K 
key, V value) {
-        assertFalse("is-empty", cache.isEmpty());
-        assertEquals("size", 1, cache.size());
-        assertTrue("found", cache.containsKey(key));
-        assertTrue("validKey", UtilCache.validKey(cache.getName(), key));
-        assertFalse("validKey", UtilCache.validKey(":::" + cache.getName(), 
key));
-        assertEquals("get", value, cache.get(key));
-        assertEquals("keys", new HashSet<>(UtilMisc.toList(key)), 
cache.getCacheLineKeys());
-        assertEquals("values", UtilMisc.toList(value), cache.values());
-    }
-
-    private static <K, V> void assertNoSingleKey(UtilCache<K, V> cache, K key) 
{
-        assertFalse("not-found", cache.containsKey(key));
-        assertFalse("validKey", UtilCache.validKey(cache.getName(), key));
-        assertNull("no-get", cache.get(key));
-        assertNull("remove", cache.remove(key));
-        assertTrue("is-empty", cache.isEmpty());
-        assertEquals("size", 0, cache.size());
-        assertEquals("keys", Collections.emptySet(), cache.getCacheLineKeys());
-        assertEquals("values", Collections.emptyList(), cache.values());
-    }
-
-    private static void basicTest(UtilCache<String, String> cache) throws 
Exception {
-        Listener<String, String> gotListener = createListener(cache);
-        Listener<String, String> wantedListener = new Listener<>();
-        for (int i = 0; i < 2; i++) {
-            assertTrue("UtilCacheTable.keySet", 
UtilCache.getUtilCacheTableKeySet().contains(cache.getName()));
-            assertSame("UtilCache.findCache", cache, 
UtilCache.findCache(cache.getName()));
-            assertSame("UtilCache.getOrCreateUtilCache", cache, 
UtilCache.getOrCreateUtilCache(cache.getName(),
-                    cache.getSizeLimit(), cache.getMaxInMemory(), 
cache.getExpireTime(), cache.getUseSoftReference()));
-
-            assertNoSingleKey(cache, "one");
-            long origByteSize = cache.getSizeInBytes();
-
-            wantedListener.noteKeyAddition(cache, null, "null");
-            assertNull("put", cache.put(null, "null"));
-            assertHasSingleKey(cache, null, "null");
-            long nullByteSize = cache.getSizeInBytes();
-            assertThat(nullByteSize, greaterThan(origByteSize));
-
-            wantedListener.noteKeyRemoval(cache, null, "null");
-            assertEquals("remove", "null", cache.remove(null));
-            assertNoSingleKey(cache, null);
-
-            wantedListener.noteKeyAddition(cache, "one", "uno");
-            assertNull("put", cache.put("one", "uno"));
-            assertHasSingleKey(cache, "one", "uno");
-            long unoByteSize = cache.getSizeInBytes();
-            assertThat(unoByteSize, greaterThan(origByteSize));
-
-            wantedListener.noteKeyUpdate(cache, "one", "single", "uno");
-            assertEquals("replace", "uno", cache.put("one", "single"));
-            assertHasSingleKey(cache, "one", "single");
-            long singleByteSize = cache.getSizeInBytes();
-            assertThat(singleByteSize, greaterThan(origByteSize));
-            assertThat(singleByteSize, greaterThan(unoByteSize));
-
-            wantedListener.noteKeyRemoval(cache, "one", "single");
-            assertEquals("remove", "single", cache.remove("one"));
-            assertNoSingleKey(cache, "one");
-            assertEquals("byteSize", origByteSize, cache.getSizeInBytes());
-
-            wantedListener.noteKeyAddition(cache, "one", "uno");
-            assertNull("put", cache.put("one", "uno"));
-            assertHasSingleKey(cache, "one", "uno");
-
-            wantedListener.noteKeyUpdate(cache, "one", "only", "uno");
-            assertEquals("replace", "uno", cache.put("one", "only"));
-            assertHasSingleKey(cache, "one", "only");
-
-            wantedListener.noteKeyRemoval(cache, "one", "only");
-            cache.erase();
-            assertNoSingleKey(cache, "one");
-            assertEquals("byteSize", origByteSize, cache.getSizeInBytes());
-
-            cache.setExpireTime(100);
-            wantedListener.noteKeyAddition(cache, "one", "uno");
-            assertNull("put", cache.put("one", "uno"));
-            assertHasSingleKey(cache, "one", "uno");
-
-            wantedListener.noteKeyRemoval(cache, "one", "uno");
-            Thread.sleep(200);
-            assertNoSingleKey(cache, "one");
-        }
-
-        assertEquals("get-miss", 10, cache.getMissCountNotFound());
-        assertEquals("get-miss-total", 10, cache.getMissCountTotal());
-        assertEquals("get-hit", 12, cache.getHitCount());
-        assertEquals("remove-hit", 6, cache.getRemoveHitCount());
-        assertEquals("remove-miss", 10, cache.getRemoveMissCount());
-        cache.removeListener(gotListener);
-        assertEquals("listener", wantedListener, gotListener);
-        UtilCache.clearCache(cache.getName());
-        UtilCache.clearCache(":::" + cache.getName());
-    }
-
-    @Test
-    public void testSimple() throws Exception {
-        UtilCache<String, String> cache = createUtilCache(5, 0, 0, false);
-        basicTest(cache);
-    }
-
-    @Test
-    public void testPutIfAbsent() throws Exception {
-        UtilCache<String, String> cache = createUtilCache(5, 5, 2000, false);
-        Listener<String, String> gotListener = createListener(cache);
-        Listener<String, String> wantedListener = new Listener<>();
-        wantedListener.noteKeyAddition(cache, "two", "dos");
-        assertNull("putIfAbsent", cache.putIfAbsent("two", "dos"));
-        assertHasSingleKey(cache, "two", "dos");
-        assertEquals("putIfAbsent", "dos", cache.putIfAbsent("two", "double"));
-        assertHasSingleKey(cache, "two", "dos");
-        cache.removeListener(gotListener);
-        assertEquals("listener", wantedListener, gotListener);
-    }
-
-    @Test
-    public void testPutIfAbsentAndGet() throws Exception {
-        UtilCache<String, String> cache = createUtilCache(5, 5, 2000, false);
-        Listener<String, String> gotListener = createListener(cache);
-        Listener<String, String> wantedListener = new Listener<>();
-        wantedListener.noteKeyAddition(cache, "key", "value");
-        wantedListener.noteKeyAddition(cache, "anotherKey", "anotherValue");
-        assertNull("no-get", cache.get("key"));
-        assertEquals("putIfAbsentAndGet", "value", 
cache.putIfAbsentAndGet("key", "value"));
-        assertHasSingleKey(cache, "key", "value");
-        assertEquals("putIfAbsentAndGet", "value", 
cache.putIfAbsentAndGet("key", "newValue"));
-        assertHasSingleKey(cache, "key", "value");
-        String anotherValueAddedToCache = new String("anotherValue");
-        String anotherValueNotAddedToCache = new String("anotherValue");
-        assertEquals(anotherValueAddedToCache, anotherValueNotAddedToCache);
-        assertNotSame(anotherValueAddedToCache, anotherValueNotAddedToCache);
-        String cachedValue = cache.putIfAbsentAndGet("anotherKey", 
anotherValueAddedToCache);
-        assertSame(cachedValue, anotherValueAddedToCache);
-        cachedValue = cache.putIfAbsentAndGet("anotherKey", 
anotherValueNotAddedToCache);
-        assertNotSame(cachedValue, anotherValueNotAddedToCache);
-        assertSame(cachedValue, anotherValueAddedToCache);
-        cache.removeListener(gotListener);
-        assertEquals("listener", wantedListener, gotListener);
-    }
-
-    @Test
-    public void testChangeMemSize() throws Exception {
-        int size = 5;
-        long ttl = 2000;
-        UtilCache<String, Serializable> cache = createUtilCache(size, size, 
ttl, false);
-        Map<String, Serializable> map = new HashMap<>();
-        assertKeyLoop(size, cache, map);
-        cache.setMaxInMemory(2);
-        assertEquals("cache.size", 2, cache.size());
-        map.keySet().retainAll(cache.getCacheLineKeys());
-        assertEquals("map-keys", map.keySet(), cache.getCacheLineKeys());
-        assertThat("map-values", cache.values(), 
containsInAnyOrder(map.values().toArray()));
-        cache.setMaxInMemory(0);
-        assertEquals("map-keys", map.keySet(), cache.getCacheLineKeys());
-        assertThat("map-values", cache.values(), 
containsInAnyOrder(map.values().toArray()));
-        for (int i = size * 2; i < size * 3; i++) {
-            String s = Integer.toString(i);
-            assertKey(s, cache, s, new String(s), new String(":" + s), i - 
size * 2 + 3, map);
-        }
-        cache.setMaxInMemory(0);
-        assertEquals("map-keys", map.keySet(), cache.getCacheLineKeys());
-        assertThat("map-values", cache.values(), 
containsInAnyOrder(map.values().toArray()));
-        cache.setMaxInMemory(size);
-        for (int i = 0; i < size * 2; i++) {
-            map.remove(Integer.toString(i));
-        }
-        // Can't compare the contents of these collections, as setting LRU 
after not
-        // having one, means the items that get evicted are essentially random.
-        assertEquals("map-keys", map.keySet().size(), 
cache.getCacheLineKeys().size());
-        assertEquals("map-values", map.values().size(), cache.values().size());
-    }
-
-    private static void expireTest(UtilCache<String, Serializable> cache, int 
size, long ttl) throws Exception {
-        Map<String, Serializable> map = new HashMap<>();
-        assertKeyLoop(size, cache, map);
-        Thread.sleep(ttl + 500);
-        map.clear();
-        for (int i = 0; i < size; i++) {
-            String s = Integer.toString(i);
-            assertNull("no-key(" + s + ")", cache.get(s));
-        }
-        assertEquals("map-keys", map.keySet(), cache.getCacheLineKeys());
-        assertThat("map-values", cache.values(), 
containsInAnyOrder(map.values().toArray()));
-        assertKeyLoop(size, cache, map);
-        assertEquals("map-keys", map.keySet(), cache.getCacheLineKeys());
-        assertThat("map-values", cache.values(), 
containsInAnyOrder(map.values().toArray()));
-    }
-
-    private static void assertKeyLoop(int size, UtilCache<String, 
Serializable> cache, Map<String, Serializable> map) {
-        for (int i = 0; i < size; i++) {
-            String s = Integer.toString(i);
-            assertKey(s, cache, s, new String(s), new String(":" + s), i + 1, 
map);
-        }
-    }
-
-    @Test
-    public void testExpire() throws Exception {
-        UtilCache<String, Serializable> cache = createUtilCache(5, 5, 2000, 
false);
-        expireTest(cache, 5, 2000);
-        long start = System.currentTimeMillis();
-        useAllMemory();
-        long end = System.currentTimeMillis();
-        long ttl = end - start + 1000;
-        cache = createUtilCache(1, 1, ttl, true);
-        expireTest(cache, 1, ttl);
-        assertFalse("not empty", cache.isEmpty());
-        useAllMemory();
-        assertNull("not-key(0)", cache.get("0"));
-        assertTrue("empty", cache.isEmpty());
-    }
-}

Reply via email to