From 7e0728f3593ff0d71e95113f8cab7881a02e27fc Mon Sep 17 00:00:00 2001 From: MOOOOOSER Date: Thu, 7 Sep 2023 13:23:14 +0200 Subject: [PATCH 1/5] moved ManagedCacheSpec to kotlin file --- .../sirius/kernel/cache/ManagedCacheTest.kt | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 src/test/kotlin/sirius/kernel/cache/ManagedCacheTest.kt diff --git a/src/test/kotlin/sirius/kernel/cache/ManagedCacheTest.kt b/src/test/kotlin/sirius/kernel/cache/ManagedCacheTest.kt new file mode 100644 index 00000000..847224af --- /dev/null +++ b/src/test/kotlin/sirius/kernel/cache/ManagedCacheTest.kt @@ -0,0 +1,113 @@ +package sirius.kernel.cache + +/* + * Made with all the love in the world + * by scireum in Remshalden, Germany + * + * Copyright by scireum GmbH + * http://www.scireum.de - info@scireum.de + */ + +package sirius.kernel.cache + +import org.junit.jupiter.api.Tag +import sirius.kernel.commons.Strings +import sirius.kernel.commons.Tuple +import sirius.kernel.commons.Wait + +import java.util.function.BiFunction +import java.util.function.BiPredicate +import java.util.function.Function +import java.util.function.Predicate + +@Tag("nightly") +class ManagedCacheSpec extends BaseSpecification { + + def "test run eviction removes old entries"() { + given: + def cache = new ManagedCache("test-cache", null, null) + when: + cache.put("key1", "value1") + cache.put("key2", "value2") + Wait.millis(1001) + cache.put("key3", "value3") + cache.put("key4", "value4") + then: + cache.getSize() == 4 + cache.runEviction() + cache.getSize() == 2 + cache.get("key3") == "value3" + cache.get("key4") == "value4" + } + + def "optional value computer works"() { + given: + def valueComputer = { key -> + if (Strings.isEmpty(key)) { + return Optional.empty() + } + if (key.startsWith("empty")) { + return Optional.empty() + } else { + return Optional.of(key.toUpperCase()) + } + } + def cache = new ManagedCache("test-cache", OptionalValueComputer.of(valueComputer), null) + expect: + cache.get("") == null + cache.getOptional("key") == Optional.of("KEY") + cache.getOptional("empty_key") == Optional.empty() + } + + def "removeAll works as expected"() { + given: + ManagedCache> cache = new ManagedCache("test-cache", null, null) + cache.addRemover("FIRST", + { key, entry -> Strings.areEqual(key, entry.getValue().getFirst()) }) + cache.addRemover("SECOND", + { key, entry -> Strings.areEqual(key, entry.getValue().getSecond()) }) + + when: + cache.put("A", Tuple.create("0", "0")) + cache.put("B", Tuple.create("1", "2")) + cache.put("C", Tuple.create("2", "1")) + cache.put("D", Tuple.create("3", "3")) + and: "Remove all entries where the first is a '1' and then all where the second is a '1'" + cache.removeAll("FIRST", "1") + cache.removeAll("SECOND", "1") + then: "Ensure that the correct entries were removed and others remained in cache" + cache.get("A") != null + cache.get("B") == null + cache.get("C") == null + cache.get("D") != null + } + + def "remover builder works as expected"() { + given: + ManagedCache> cache = new ManagedCache("test-cache", null, null) + cache.addRemover("FILTER"). + filter({ selector, entry -> (entry.getKey() != selector) } as BiPredicate). + map({ entry -> entry.getValue() } as Function). + map({ tuple -> tuple.getFirst() } as Function). + map({ selector, value -> value + selector } as BiFunction). + removeIf({ x -> x.size() > 5 } as Predicate) + cache.addValueBasedRemover("REMOVE_ALWAYS"). + removeAlways({ selector, tuple -> tuple.getSecond() == selector } as BiPredicate). + removeIf({ false } as Predicate) + when: + cache.put("A", Tuple.create("gets ignored, ", "because the key is equal to the selector")) + cache.put("B", Tuple.create("gets ", "removed, because it's too long")) + cache.put("C", Tuple.create("does", " not get removed")) + cache.put("D", Tuple.create("B", "A")) + cache.put("E", Tuple.create("B", "C")) + and: + cache.removeAll("FILTER", "A") + cache.removeAll("REMOVE_ALWAYS", "C") + then: + cache.get("A") != null + cache.get("B") == null + cache.get("C") != null + cache.get("D") != null + cache.get("E") == null + } +} From a4d80cce84a54858191fdd1a7481e5c506c2ba6e Mon Sep 17 00:00:00 2001 From: MOOOOOSER Date: Fri, 8 Sep 2023 11:15:33 +0200 Subject: [PATCH 2/5] adapted Kotlin Syntax --- .../sirius/kernel/cache/ManagedCacheTest.kt | 160 ++++++++++-------- 1 file changed, 94 insertions(+), 66 deletions(-) diff --git a/src/test/kotlin/sirius/kernel/cache/ManagedCacheTest.kt b/src/test/kotlin/sirius/kernel/cache/ManagedCacheTest.kt index 847224af..e8da8c2c 100644 --- a/src/test/kotlin/sirius/kernel/cache/ManagedCacheTest.kt +++ b/src/test/kotlin/sirius/kernel/cache/ManagedCacheTest.kt @@ -1,5 +1,3 @@ -package sirius.kernel.cache - /* * Made with all the love in the world * by scireum in Remshalden, Germany @@ -10,104 +8,134 @@ package sirius.kernel.cache package sirius.kernel.cache -import org.junit.jupiter.api.Tag +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import sirius.kernel.SiriusExtension import sirius.kernel.commons.Strings import sirius.kernel.commons.Tuple import sirius.kernel.commons.Wait +import java.util.* +import kotlin.test.assertEquals +import kotlin.test.assertNotEquals -import java.util.function.BiFunction -import java.util.function.BiPredicate -import java.util.function.Function -import java.util.function.Predicate - -@Tag("nightly") -class ManagedCacheSpec extends BaseSpecification { +/** + * Tests the [AdvancedDateParser] class. + */ +@ExtendWith(SiriusExtension::class) +class ManagedCacheTest { - def "test run eviction removes old entries"() { - given: - def cache = new ManagedCache("test-cache", null, null) - when: + @Test + fun `test run eviction removes old entries`() { + val cache: ManagedCache = ManagedCache("test-cache", null, null) cache.put("key1", "value1") cache.put("key2", "value2") Wait.millis(1001) cache.put("key3", "value3") cache.put("key4", "value4") - then: - cache.getSize() == 4 + assertEquals(4, cache.getSize()) cache.runEviction() - cache.getSize() == 2 - cache.get("key3") == "value3" - cache.get("key4") == "value4" + assertEquals(2, cache.getSize()) + assertEquals("value3", cache.get("key3")) + assertEquals("value4", cache.get("key4")) } - def "optional value computer works"() { - given: - def valueComputer = { key -> - if (Strings.isEmpty(key)) { - return Optional.empty() - } - if (key.startsWith("empty")) { - return Optional.empty() + @Test + fun `optional value computer works`() { + val valueComputer = { key: String -> + if (key.isEmpty() || key.startsWith("empty")) { + Optional.empty() } else { - return Optional.of(key.toUpperCase()) + Optional.of(key.toUpperCase()) } } - def cache = new ManagedCache("test-cache", OptionalValueComputer.of(valueComputer), null) - expect: - cache.get("") == null - cache.getOptional("key") == Optional.of("KEY") - cache.getOptional("empty_key") == Optional.empty() + val cache = ManagedCache("test-cache", OptionalValueComputer.of(valueComputer), null) + assertEquals(null, cache.get("")) + assertEquals(Optional.of("KEY"), cache.getOptional("key")) + assertEquals(Optional.empty(), cache.getOptional("empty_key")) + } - def "removeAll works as expected"() { - given: - ManagedCache> cache = new ManagedCache("test-cache", null, null) + @Test + fun `removeAll works as expected`() { + val cache: ManagedCache> = ManagedCache("test-cache", null, null) cache.addRemover("FIRST", - { key, entry -> Strings.areEqual(key, entry.getValue().getFirst()) }) + { key, entry -> + Strings.areEqual(key, entry.getValue()?.getFirst()) + }) cache.addRemover("SECOND", - { key, entry -> Strings.areEqual(key, entry.getValue().getSecond()) }) - - when: + { key, entry -> + Strings.areEqual(key, entry.getValue()?.getSecond()) + }) cache.put("A", Tuple.create("0", "0")) cache.put("B", Tuple.create("1", "2")) cache.put("C", Tuple.create("2", "1")) cache.put("D", Tuple.create("3", "3")) - and: "Remove all entries where the first is a '1' and then all where the second is a '1'" + `Remove all entries where the first is a '1' and then all where the second is a '1'`(cache) + `Ensure that the correct entries were removed and others remained in cache`(cache) + } + + @DisplayName("Remove all entries where the first is a '1' and then all where the second is a '1'") + private fun `Remove all entries where the first is a '1' and then all where the second is a '1'`(cache: ManagedCache>) { cache.removeAll("FIRST", "1") cache.removeAll("SECOND", "1") - then: "Ensure that the correct entries were removed and others remained in cache" - cache.get("A") != null - cache.get("B") == null - cache.get("C") == null - cache.get("D") != null } - def "remover builder works as expected"() { - given: - ManagedCache> cache = new ManagedCache("test-cache", null, null) - cache.addRemover("FILTER"). - filter({ selector, entry -> (entry.getKey() != selector) } as BiPredicate). - map({ entry -> entry.getValue() } as Function). - map({ tuple -> tuple.getFirst() } as Function). - map({ selector, value -> value + selector } as BiFunction). - removeIf({ x -> x.size() > 5 } as Predicate) - cache.addValueBasedRemover("REMOVE_ALWAYS"). - removeAlways({ selector, tuple -> tuple.getSecond() == selector } as BiPredicate). - removeIf({ false } as Predicate) - when: + @DisplayName("Ensure that the correct entries were removed and others remained in cache") + private fun `Ensure that the correct entries were removed and others remained in cache`(cache: ManagedCache>) { + assertNotEquals(null, cache.get("A")) + assertEquals(null, cache.get("B")) + assertEquals(null, cache.get("C")) + assertNotEquals(null, cache.get("D")) + } + + @Test + fun `remover builder works as expected`() { + val cache: ManagedCache> = ManagedCache("test-cache", null, null) cache.put("A", Tuple.create("gets ignored, ", "because the key is equal to the selector")) cache.put("B", Tuple.create("gets ", "removed, because it's too long")) cache.put("C", Tuple.create("does", " not get removed")) cache.put("D", Tuple.create("B", "A")) cache.put("E", Tuple.create("B", "C")) - and: + + // defines a remover, that allows to define a key value, which should not be removed + cache.addRemover("FILTER") + .filter { selector: String, entry: CacheEntry> -> + (entry.getKey() != selector) + } + // get tuple consisting of two strings + .map { entry: CacheEntry?> -> + entry.getValue() + } + .map { tuple: Tuple? -> + // get first value of tuple (which is a string) + tuple?.first + }.map( + // add the selector (key of the cache entry) and the first value of the string + // for the cache value B this would be: "B" + "gets " = "Bgets " + { selector, value -> + value + selector + } + ).removeIf( + // if the resulting string, f.E. "Bgets " is larger than 5, remove entry + { x -> + x.length > 5 + } + ) + + cache.addValueBasedRemover("REMOVE_ALWAYS").removeAlways({ selector, tuple -> + tuple.getSecond() == selector + }).removeIf({ tuple -> + false + }) + cache.removeAll("FILTER", "A") cache.removeAll("REMOVE_ALWAYS", "C") - then: - cache.get("A") != null - cache.get("B") == null - cache.get("C") != null - cache.get("D") != null - cache.get("E") == null + + assertNotEquals(null, cache.get("A")) + assertEquals(null, cache.get("B")) + assertNotEquals(null, cache.get("C")) + assertNotEquals(null, cache.get("D")) + assertEquals(null, cache.get("E")) } } From 1881460becd9a4829eea09bd46b257ffc9452590 Mon Sep 17 00:00:00 2001 From: MOOOOOSER Date: Fri, 8 Sep 2023 11:15:52 +0200 Subject: [PATCH 3/5] deleted ManagedCacheSpec.groovy --- .../kernel/cache/ManagedCacheSpec.groovy | 112 ------------------ 1 file changed, 112 deletions(-) delete mode 100644 src/test/java/sirius/kernel/cache/ManagedCacheSpec.groovy diff --git a/src/test/java/sirius/kernel/cache/ManagedCacheSpec.groovy b/src/test/java/sirius/kernel/cache/ManagedCacheSpec.groovy deleted file mode 100644 index ba9873bf..00000000 --- a/src/test/java/sirius/kernel/cache/ManagedCacheSpec.groovy +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Made with all the love in the world - * by scireum in Remshalden, Germany - * - * Copyright by scireum GmbH - * http://www.scireum.de - info@scireum.de - */ - -package sirius.kernel.cache - -import org.junit.jupiter.api.Tag -import sirius.kernel.BaseSpecification -import sirius.kernel.commons.Strings -import sirius.kernel.commons.Tuple -import sirius.kernel.commons.Wait - -import java.util.function.BiFunction -import java.util.function.BiPredicate -import java.util.function.Function -import java.util.function.Predicate - -@Tag("nightly") -class ManagedCacheSpec extends BaseSpecification { - - def "test run eviction removes old entries"() { - given: - def cache = new ManagedCache("test-cache", null, null) - when: - cache.put("key1", "value1") - cache.put("key2", "value2") - Wait.millis(1001) - cache.put("key3", "value3") - cache.put("key4", "value4") - then: - cache.getSize() == 4 - cache.runEviction() - cache.getSize() == 2 - cache.get("key3") == "value3" - cache.get("key4") == "value4" - } - - def "optional value computer works"() { - given: - def valueComputer = { key -> - if (Strings.isEmpty(key)) { - return Optional.empty() - } - if (key.startsWith("empty")) { - return Optional.empty() - } else { - return Optional.of(key.toUpperCase()) - } - } - def cache = new ManagedCache("test-cache", OptionalValueComputer.of(valueComputer), null) - expect: - cache.get("") == null - cache.getOptional("key") == Optional.of("KEY") - cache.getOptional("empty_key") == Optional.empty() - } - - def "removeAll works as expected"() { - given: - ManagedCache> cache = new ManagedCache("test-cache", null, null) - cache.addRemover("FIRST", - { key, entry -> Strings.areEqual(key, entry.getValue().getFirst()) }) - cache.addRemover("SECOND", - { key, entry -> Strings.areEqual(key, entry.getValue().getSecond()) }) - - when: - cache.put("A", Tuple.create("0", "0")) - cache.put("B", Tuple.create("1", "2")) - cache.put("C", Tuple.create("2", "1")) - cache.put("D", Tuple.create("3", "3")) - and: "Remove all entries where the first is a '1' and then all where the second is a '1'" - cache.removeAll("FIRST", "1") - cache.removeAll("SECOND", "1") - then: "Ensure that the correct entries were removed and others remained in cache" - cache.get("A") != null - cache.get("B") == null - cache.get("C") == null - cache.get("D") != null - } - - def "remover builder works as expected"() { - given: - ManagedCache> cache = new ManagedCache("test-cache", null, null) - cache.addRemover("FILTER"). - filter({ selector, entry -> (entry.getKey() != selector) } as BiPredicate). - map({ entry -> entry.getValue() } as Function). - map({ tuple -> tuple.getFirst() } as Function). - map({ selector, value -> value + selector } as BiFunction). - removeIf({ x -> x.size() > 5 } as Predicate) - cache.addValueBasedRemover("REMOVE_ALWAYS"). - removeAlways({ selector, tuple -> tuple.getSecond() == selector } as BiPredicate). - removeIf({ false } as Predicate) - when: - cache.put("A", Tuple.create("gets ignored, ", "because the key is equal to the selector")) - cache.put("B", Tuple.create("gets ", "removed, because it's too long")) - cache.put("C", Tuple.create("does", " not get removed")) - cache.put("D", Tuple.create("B", "A")) - cache.put("E", Tuple.create("B", "C")) - and: - cache.removeAll("FILTER", "A") - cache.removeAll("REMOVE_ALWAYS", "C") - then: - cache.get("A") != null - cache.get("B") == null - cache.get("C") != null - cache.get("D") != null - cache.get("E") == null - } -} From dceecd7e8154596cae68a6e8395a19a80f70833f Mon Sep 17 00:00:00 2001 From: MOOOOOSER Date: Fri, 8 Sep 2023 13:53:48 +0200 Subject: [PATCH 4/5] split last complex test into two and simplifyed by using ManagedCache --- .../sirius/kernel/cache/ManagedCacheTest.kt | 77 +++++++++++-------- 1 file changed, 44 insertions(+), 33 deletions(-) diff --git a/src/test/kotlin/sirius/kernel/cache/ManagedCacheTest.kt b/src/test/kotlin/sirius/kernel/cache/ManagedCacheTest.kt index e8da8c2c..056b617d 100644 --- a/src/test/kotlin/sirius/kernel/cache/ManagedCacheTest.kt +++ b/src/test/kotlin/sirius/kernel/cache/ManagedCacheTest.kt @@ -8,9 +8,9 @@ package sirius.kernel.cache -import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test import org.junit.jupiter.api.extension.ExtendWith +import sirius.kernel.NightlyTest import sirius.kernel.SiriusExtension import sirius.kernel.commons.Strings import sirius.kernel.commons.Tuple @@ -22,7 +22,9 @@ import kotlin.test.assertNotEquals /** * Tests the [AdvancedDateParser] class. */ + @ExtendWith(SiriusExtension::class) +@NightlyTest class ManagedCacheTest { @Test @@ -71,18 +73,12 @@ class ManagedCacheTest { cache.put("B", Tuple.create("1", "2")) cache.put("C", Tuple.create("2", "1")) cache.put("D", Tuple.create("3", "3")) - `Remove all entries where the first is a '1' and then all where the second is a '1'`(cache) - `Ensure that the correct entries were removed and others remained in cache`(cache) - } - @DisplayName("Remove all entries where the first is a '1' and then all where the second is a '1'") - private fun `Remove all entries where the first is a '1' and then all where the second is a '1'`(cache: ManagedCache>) { + //Remove all entries where the first is a '1' and then all where the second is a '1' cache.removeAll("FIRST", "1") cache.removeAll("SECOND", "1") - } - @DisplayName("Ensure that the correct entries were removed and others remained in cache") - private fun `Ensure that the correct entries were removed and others remained in cache`(cache: ManagedCache>) { + //Ensure that the correct entries were removed and others remained in cache assertNotEquals(null, cache.get("A")) assertEquals(null, cache.get("B")) assertEquals(null, cache.get("C")) @@ -91,51 +87,66 @@ class ManagedCacheTest { @Test fun `remover builder works as expected`() { - val cache: ManagedCache> = ManagedCache("test-cache", null, null) - cache.put("A", Tuple.create("gets ignored, ", "because the key is equal to the selector")) - cache.put("B", Tuple.create("gets ", "removed, because it's too long")) - cache.put("C", Tuple.create("does", " not get removed")) - cache.put("D", Tuple.create("B", "A")) - cache.put("E", Tuple.create("B", "C")) + val cache: ManagedCache = ManagedCache("test-cache", null, null) + cache.put("A", "1") + cache.put("B", "12") + cache.put("C", "123") + cache.put("D", "1234") + cache.put("E", "12345") // defines a remover, that allows to define a key value, which should not be removed cache.addRemover("FILTER") - .filter { selector: String, entry: CacheEntry> -> + .filter { selector, entry -> (entry.getKey() != selector) } - // get tuple consisting of two strings - .map { entry: CacheEntry?> -> + // get value of managed cache + .map { entry -> entry.getValue() } - .map { tuple: Tuple? -> - // get first value of tuple (which is a string) - tuple?.first - }.map( - // add the selector (key of the cache entry) and the first value of the string - // for the cache value B this would be: "B" + "gets " = "Bgets " + .map( + // add key + value of the cache Entry, for example: "E" + 12345 { selector, value -> value + selector } ).removeIf( - // if the resulting string, f.E. "Bgets " is larger than 5, remove entry + // if the resulting string, f.E. "E12345" is larger than 5, remove entry { x -> x.length > 5 } ) - cache.addValueBasedRemover("REMOVE_ALWAYS").removeAlways({ selector, tuple -> - tuple.getSecond() == selector - }).removeIf({ tuple -> - false - }) - cache.removeAll("FILTER", "A") - cache.removeAll("REMOVE_ALWAYS", "C") assertNotEquals(null, cache.get("A")) - assertEquals(null, cache.get("B")) + assertNotEquals(null, cache.get("B")) assertNotEquals(null, cache.get("C")) assertNotEquals(null, cache.get("D")) assertEquals(null, cache.get("E")) } + + @Test + fun `valueBasedRemover works as expected`() { + val cache: ManagedCache> = ManagedCache("test-cache", null, null) + cache.put("Key1", Tuple.create("1, ", "A")) + cache.put("Key2", Tuple.create("2 ", "B")) + cache.put("Key3", Tuple.create("3", "C")) + cache.put("Key4", Tuple.create("4", "D")) + cache.put("Key5", Tuple.create("5", "E")) + + cache.addValueBasedRemover("REMOVE_ALWAYS") + .removeAlways({ selector, tuple -> + tuple.getSecond() == selector + }).removeIf({ tuple -> + false + }) + + cache.removeAll("REMOVE_ALWAYS", "C") + cache.removeAll("REMOVE_ALWAYS", "B") + + assertNotEquals(null, cache.get("Key1")) + assertEquals(null, cache.get("Key2")) + assertEquals(null, cache.get("Key3")) + assertNotEquals(null, cache.get("Key4")) + assertNotEquals(null, cache.get("Key5")) + } } From 0c2610af1ae9b322f84adeff7caf3898878354ea Mon Sep 17 00:00:00 2001 From: MOOOOOSER Date: Fri, 8 Sep 2023 14:13:22 +0200 Subject: [PATCH 5/5] minor formatting --- .../sirius/kernel/cache/ManagedCacheTest.kt | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/test/kotlin/sirius/kernel/cache/ManagedCacheTest.kt b/src/test/kotlin/sirius/kernel/cache/ManagedCacheTest.kt index 056b617d..d9a408a7 100644 --- a/src/test/kotlin/sirius/kernel/cache/ManagedCacheTest.kt +++ b/src/test/kotlin/sirius/kernel/cache/ManagedCacheTest.kt @@ -96,13 +96,17 @@ class ManagedCacheTest { // defines a remover, that allows to define a key value, which should not be removed cache.addRemover("FILTER") - .filter { selector, entry -> - (entry.getKey() != selector) - } - // get value of managed cache - .map { entry -> - entry.getValue() - } + .filter( + { selector, entry -> + entry.getKey() != selector + } + ) + .map( + // get value of managed cache + { entry -> + entry.getValue() + } + ) .map( // add key + value of the cache Entry, for example: "E" + 12345 { selector, value ->