diff --git a/oo-memoized/memoized-chm/src/test/java/com/pragmaticobjects/oo/memoized/chm/MemoryCHMTest.java b/oo-memoized/memoized-chm/src/test/java/com/pragmaticobjects/oo/memoized/chm/MemoryCHMTest.java index 73ab6b7e..ce55971b 100644 --- a/oo-memoized/memoized-chm/src/test/java/com/pragmaticobjects/oo/memoized/chm/MemoryCHMTest.java +++ b/oo-memoized/memoized-chm/src/test/java/com/pragmaticobjects/oo/memoized/chm/MemoryCHMTest.java @@ -27,24 +27,72 @@ import com.pragmaticobjects.oo.memoized.core.AssertCallTimes; +import com.pragmaticobjects.oo.memoized.core.AssertDisposal; +import com.pragmaticobjects.oo.memoized.core.MemoizedCallable; import com.pragmaticobjects.oo.tests.TestCase; import com.pragmaticobjects.oo.tests.junit5.TestsSuite; +import io.vavr.collection.List; +import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; +class TestCallable implements MemoizedCallable { + private final int id; + + public TestCallable(int id) { + this.id = id; + } + + @Override + public final Integer call() { + return id; + } +} + + class MemoryCHMTest extends TestsSuite { public MemoryCHMTest() { super( - new TestCase( - "Check at most once call", - new AssertCallTimes( - new MemoryCHM( - new ConcurrentHashMap<>() - ), - 10, - 1 - ) + new TestCase( + "Check at most once call", + new AssertCallTimes( + new MemoryCHM( + new ConcurrentHashMap<>() + ), + 10, + 1 + ) + ), + new TestCase( + "Check disposal of existing value", + new AssertDisposal( + new MemoryCHM( + new ConcurrentHashMap<>() + ), + List.of( + new TestCallable(1), + new TestCallable(2), + new TestCallable(3) + ), + new TestCallable(1), + Optional.of(1) + ) + ), + new TestCase( + "Check disposal of inexisting value", + new AssertDisposal( + new MemoryCHM( + new ConcurrentHashMap<>() + ), + List.of( + new TestCallable(1), + new TestCallable(2), + new TestCallable(3) + ), + new TestCallable(4), + Optional.empty() ) + ) ); } } diff --git a/oo-memoized/memoized-core/src/main/java/com/pragmaticobjects/oo/memoized/core/AssertCallTimes.java b/oo-memoized/memoized-core/src/main/java/com/pragmaticobjects/oo/memoized/core/AssertCallTimes.java index 13144f10..4d6ff981 100644 --- a/oo-memoized/memoized-core/src/main/java/com/pragmaticobjects/oo/memoized/core/AssertCallTimes.java +++ b/oo-memoized/memoized-core/src/main/java/com/pragmaticobjects/oo/memoized/core/AssertCallTimes.java @@ -30,22 +30,6 @@ import java.util.concurrent.atomic.AtomicInteger; -// NOTE: equals, hashCode and toString are implicitly generated -// by equivalence-maven-plugin -class TestCallable implements MemoizedCallable { - private final AtomicInteger counter; - - public TestCallable(AtomicInteger counter) { - this.counter = counter; - } - - @Override - public final Object call() { - counter.incrementAndGet(); - return null; - } -} - public class AssertCallTimes implements Assertion { private final Memory memory; private final int callNums; diff --git a/oo-memoized/memoized-core/src/main/java/com/pragmaticobjects/oo/memoized/core/AssertDisposal.java b/oo-memoized/memoized-core/src/main/java/com/pragmaticobjects/oo/memoized/core/AssertDisposal.java new file mode 100644 index 00000000..697676df --- /dev/null +++ b/oo-memoized/memoized-core/src/main/java/com/pragmaticobjects/oo/memoized/core/AssertDisposal.java @@ -0,0 +1,53 @@ +/*- + * =========================================================================== + * memoized-core + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * Copyright (C) 2019 - 2022 Kapralov Sergey + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * ============================================================================ + */ +package com.pragmaticobjects.oo.memoized.core; + +import com.pragmaticobjects.oo.tests.Assertion; +import io.vavr.collection.List; +import org.assertj.core.api.Assertions; + +import java.util.Optional; + +public class AssertDisposal implements Assertion { + private final Memory memoryUnderTest; + private final List> preparationCallables; + private final MemoizedCallable callableToDispose; + private final Optional expectedDisposedValue; + + public AssertDisposal(Memory memoryUnderTest, List> preparationCallables, MemoizedCallable callableToDispose, Optional expectedDisposedValue) { + this.memoryUnderTest = memoryUnderTest; + this.preparationCallables = preparationCallables; + this.callableToDispose = callableToDispose; + this.expectedDisposedValue = expectedDisposedValue; + } + + @Override + public final void check() throws Exception { + preparationCallables.forEach(memoryUnderTest::memoized); + Optional disposedValue = memoryUnderTest.dispose(callableToDispose); + Assertions.assertThat(disposedValue).isEqualTo(expectedDisposedValue); + } +} diff --git a/oo-memoized/memoized-core/src/main/java/com/pragmaticobjects/oo/memoized/core/TestCallable.java b/oo-memoized/memoized-core/src/main/java/com/pragmaticobjects/oo/memoized/core/TestCallable.java new file mode 100644 index 00000000..071241ac --- /dev/null +++ b/oo-memoized/memoized-core/src/main/java/com/pragmaticobjects/oo/memoized/core/TestCallable.java @@ -0,0 +1,44 @@ +/*- + * =========================================================================== + * memoized-core + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * Copyright (C) 2019 - 2022 Kapralov Sergey + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * ============================================================================ + */ +package com.pragmaticobjects.oo.memoized.core; + +import java.util.concurrent.atomic.AtomicInteger; + +// NOTE: equals, hashCode and toString are implicitly generated +// by equivalence-maven-plugin +class TestCallable implements MemoizedCallable { + private final AtomicInteger counter; + + public TestCallable(AtomicInteger counter) { + this.counter = counter; + } + + @Override + public final Object call() { + counter.incrementAndGet(); + return null; + } +} diff --git a/oo-memoized/memoized-core/src/test/java/com/pragmaticobjects/oo/memoized/core/AssertCallTimesTest.java b/oo-memoized/memoized-core/src/test/java/com/pragmaticobjects/oo/memoized/core/AssertCallTimesTest.java index 549c6fe8..05bf2da8 100644 --- a/oo-memoized/memoized-core/src/test/java/com/pragmaticobjects/oo/memoized/core/AssertCallTimesTest.java +++ b/oo-memoized/memoized-core/src/test/java/com/pragmaticobjects/oo/memoized/core/AssertCallTimesTest.java @@ -31,26 +31,6 @@ import java.util.Optional; -/** - * Test {@link Memory} implementation that memoises nothing - */ -class BluntMemory implements Memory { - @Override - public final T memoized(MemoizedCallable callable) { - return callable.call(); - } - - @Override - public final Optional dispose(MemoizedCallable callable) { - return Optional.empty(); - } - - @Override - public final void clean() { - // Do Nothing - } -} - public class AssertCallTimesTest extends TestsSuite { public AssertCallTimesTest() { diff --git a/oo-memoized/memoized-core/src/test/java/com/pragmaticobjects/oo/memoized/core/AssertDisposalTest.java b/oo-memoized/memoized-core/src/test/java/com/pragmaticobjects/oo/memoized/core/AssertDisposalTest.java new file mode 100644 index 00000000..9547cb0e --- /dev/null +++ b/oo-memoized/memoized-core/src/test/java/com/pragmaticobjects/oo/memoized/core/AssertDisposalTest.java @@ -0,0 +1,54 @@ +/*- + * =========================================================================== + * memoized-core + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * Copyright (C) 2019 - 2022 Kapralov Sergey + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * ============================================================================ + */ +package com.pragmaticobjects.oo.memoized.core; + +import com.pragmaticobjects.oo.tests.AssertAssertionPasses; +import com.pragmaticobjects.oo.tests.TestCase; +import com.pragmaticobjects.oo.tests.junit5.TestsSuite; +import io.vavr.collection.List; + +import java.util.Optional; +import java.util.concurrent.atomic.AtomicInteger; + +public class AssertDisposalTest extends TestsSuite { + public AssertDisposalTest() { + super( + new TestCase( + "blunt disposal", + new AssertAssertionPasses( + new AssertDisposal( + new BluntMemory(), + List.empty(), + new TestCallable( + new AtomicInteger() + ), + Optional.empty() + ) + ) + ) + ); + } +} diff --git a/oo-memoized/memoized-core/src/test/java/com/pragmaticobjects/oo/memoized/core/BluntMemory.java b/oo-memoized/memoized-core/src/test/java/com/pragmaticobjects/oo/memoized/core/BluntMemory.java new file mode 100644 index 00000000..aa72d8fb --- /dev/null +++ b/oo-memoized/memoized-core/src/test/java/com/pragmaticobjects/oo/memoized/core/BluntMemory.java @@ -0,0 +1,48 @@ +/*- + * =========================================================================== + * memoized-core + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * Copyright (C) 2019 - 2022 Kapralov Sergey + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * ============================================================================ + */ +package com.pragmaticobjects.oo.memoized.core; + +import java.util.Optional; + +/** + * Test {@link Memory} implementation that memoises nothing + */ +class BluntMemory implements Memory { + @Override + public final T memoized(MemoizedCallable callable) { + return callable.call(); + } + + @Override + public final Optional dispose(MemoizedCallable callable) { + return Optional.empty(); + } + + @Override + public final void clean() { + // Do Nothing + } +}