From d64d7bbd015d0e27d8e136cd7fd90f94c7ea19b0 Mon Sep 17 00:00:00 2001 From: TiA4f8R <74829229+TiA4f8R@users.noreply.github.com> Date: Sun, 3 Apr 2022 20:54:35 +0200 Subject: [PATCH] Move ManifestCreatorCache tests to a separate class and remove override of equals and hashCode methods in ManifestCreatorCache These methods don't need to be overriden, as they are not excepted to be used in collections. Also improve the toString method of this class, which contains also now clearFactor and maximumSize attributes and for each operations. --- .../extractor/utils/ManifestCreatorCache.java | 56 +++------------ .../utils/ManifestCreatorCacheTest.java | 69 +++++++++++++++++++ .../newpipe/extractor/utils/UtilsTest.java | 48 ------------- 3 files changed, 77 insertions(+), 96 deletions(-) create mode 100644 extractor/src/test/java/org/schabi/newpipe/extractor/utils/ManifestCreatorCacheTest.java diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/ManifestCreatorCache.java b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/ManifestCreatorCache.java index 8e885f7cf..e2084f1d4 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/ManifestCreatorCache.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/ManifestCreatorCache.java @@ -1,10 +1,10 @@ package org.schabi.newpipe.extractor.utils; +import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.io.Serializable; import java.util.ArrayList; import java.util.Map; -import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; /** @@ -225,50 +225,11 @@ public final class ManifestCreatorCache manifestCreatorCache = - (ManifestCreatorCache) obj; - return maximumSize == manifestCreatorCache.maximumSize - && Double.compare(manifestCreatorCache.clearFactor, clearFactor) == 0 - && concurrentHashMap.equals(manifestCreatorCache.concurrentHashMap); - } - - /** - * Returns a hash code of the current {@code ManifestCreatorCache}, using its - * {@link #maximumSize maximum size}, {@link #clearFactor clear factor} and - * {@link #concurrentHashMap internal concurrent hash map} used as a cache. - * - * @return a hash code of the current {@code ManifestCreatorCache} - */ - @Override - public int hashCode() { - return Objects.hash(maximumSize, clearFactor, concurrentHashMap); - } - - /** - * Returns a string version of the {@link ConcurrentHashMap} used internally as the cache. - * - * @return the string version of the {@link ConcurrentHashMap} used internally as the cache - */ + @Nonnull @Override public String toString() { - return concurrentHashMap.toString(); + return "ManifestCreatorCache[clearFactor=" + clearFactor + ", maximumSize=" + maximumSize + + ", concurrentHashMap=" + concurrentHashMap + "]"; } /** @@ -285,17 +246,16 @@ public final class ManifestCreatorCache>> entriesToRemove = new ArrayList<>(); - for (final Map.Entry> entry : concurrentHashMap.entrySet()) { + concurrentHashMap.entrySet().forEach(entry -> { final Pair value = entry.getValue(); if (value.getFirst() < difference) { entriesToRemove.add(entry); } else { value.setFirst(value.getFirst() - difference); } - } + }); - for (final Map.Entry> entry : entriesToRemove) { - concurrentHashMap.remove(entry.getKey(), entry.getValue()); - } + entriesToRemove.forEach(entry -> concurrentHashMap.remove(entry.getKey(), + entry.getValue())); } } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/utils/ManifestCreatorCacheTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/utils/ManifestCreatorCacheTest.java new file mode 100644 index 000000000..7d3fa5b65 --- /dev/null +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/utils/ManifestCreatorCacheTest.java @@ -0,0 +1,69 @@ +package org.schabi.newpipe.extractor.utils; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class ManifestCreatorCacheTest { + @Test + void basicMaximumSizeAndResetTest() { + final ManifestCreatorCache cache = new ManifestCreatorCache<>(); + + // 30 elements set -> cache resized to 23 -> 5 new elements set to the cache -> 28 + cache.setMaximumSize(30); + setCacheContent(cache); + assertEquals(28, cache.size(), + "Wrong cache size with default clear factor and 30 as the maximum size"); + cache.reset(); + + assertEquals(0, cache.size(), + "The cache has been not cleared after a reset call (wrong cache size)"); + assertEquals(ManifestCreatorCache.DEFAULT_MAXIMUM_SIZE, cache.getMaximumSize(), + "Wrong maximum size after cache reset"); + assertEquals(ManifestCreatorCache.DEFAULT_CLEAR_FACTOR, cache.getClearFactor(), + "Wrong clear factor after cache reset"); + } + + @Test + void maximumSizeAndClearFactorSettersAndResettersTest() { + final ManifestCreatorCache cache = new ManifestCreatorCache<>(); + cache.setMaximumSize(20); + cache.setClearFactor(0.5); + + setCacheContent(cache); + // 30 elements set -> cache resized to 10 -> 5 new elements set to the cache -> 15 + assertEquals(15, cache.size(), + "Wrong cache size with 0.5 as the clear factor and 20 as the maximum size"); + + // Clear factor and maximum size getters tests + assertEquals(0.5, cache.getClearFactor(), + "Wrong clear factor gotten from clear factor getter"); + assertEquals(20, cache.getMaximumSize(), + "Wrong maximum cache size gotten from maximum size getter"); + + // Resetters tests + cache.resetMaximumSize(); + assertEquals(ManifestCreatorCache.DEFAULT_MAXIMUM_SIZE, cache.getMaximumSize(), + "Wrong maximum cache size gotten from maximum size getter after maximum size " + + "resetter call"); + + cache.resetClearFactor(); + assertEquals(ManifestCreatorCache.DEFAULT_CLEAR_FACTOR, cache.getClearFactor(), + "Wrong clear factor gotten from clear factor getter after clear factor resetter " + + "call"); + } + + private static void setCacheContent(final ManifestCreatorCache cache) { + int i = 0; + while (i < 26) { + cache.put(String.valueOf((char) ('a' + i)), "V"); + ++i; + } + + i = 0; + while (i < 9) { + cache.put("a" + (char) ('a' + i), "V"); + ++i; + } + } +} diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/utils/UtilsTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/utils/UtilsTest.java index 3d835dce2..2dd787b88 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/utils/UtilsTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/utils/UtilsTest.java @@ -47,52 +47,4 @@ class UtilsTest { assertEquals("https://www.youtube.com/watch?v=Hu80uDzh8RY&url=hello", Utils.followGoogleRedirectIfNeeded("https://www.youtube.com/watch?v=Hu80uDzh8RY&url=hello")); } - - @Test - void dashManifestCreatorCacheTest() { - final ManifestCreatorCache cache = new ManifestCreatorCache<>(); - cache.setMaximumSize(30); - setCacheContent(cache); - // 30 elements set -> cache resized to 23 -> 5 new elements set to the cache -> 28 - assertEquals(28, cache.size(), - "Wrong cache size with default clear factor and 30 as the maximum size"); - - cache.reset(); - cache.setMaximumSize(20); - cache.setClearFactor(0.5); - - setCacheContent(cache); - // 30 elements set -> cache resized to 10 -> 5 new elements set to the cache -> 15 - assertEquals(15, cache.size(), - "Wrong cache size with 0.5 as the clear factor and 20 as the maximum size"); - - // Clear factor and maximum size getters tests - assertEquals(0.5, cache.getClearFactor(), - "Wrong clear factor gotten from clear factor getter"); - assertEquals(20, cache.getMaximumSize(), - "Wrong maximum cache size gotten from maximum size getter"); - - // Resetters tests - cache.resetMaximumSize(); - assertEquals(ManifestCreatorCache.DEFAULT_MAXIMUM_SIZE, cache.getMaximumSize(), - "Wrong maximum cache size gotten from maximum size getter after maximum size reset"); - - cache.resetClearFactor(); - assertEquals(ManifestCreatorCache.DEFAULT_CLEAR_FACTOR, cache.getClearFactor(), - "Wrong clear factor gotten from clear factor getter after clear factor reset"); - } - - private void setCacheContent(@Nonnull final ManifestCreatorCache cache) { - int i = 0; - while (i < 26) { - cache.put(Character.toString((char) (97 + i)), "V"); - ++i; - } - - i = 0; - while (i < 9) { - cache.put("a" + (char) (97 + i), "V"); - ++i; - } - } }