From 9bff1f8754502d5a226c2ccd0e513fc6d013c8ab Mon Sep 17 00:00:00 2001 From: leventov Date: Wed, 27 May 2015 14:24:28 +0100 Subject: [PATCH] More Listeners tests --- .../net/openhft/chronicle/map/GuavaTest.java | 64 +++++++++-- .../openhft/chronicle/map/ListenersTest.java | 103 +++++++++++++++++- 2 files changed, 152 insertions(+), 15 deletions(-) diff --git a/src/test/java/net/openhft/chronicle/map/GuavaTest.java b/src/test/java/net/openhft/chronicle/map/GuavaTest.java index 6eb87ad7c..8d2eeb3eb 100644 --- a/src/test/java/net/openhft/chronicle/map/GuavaTest.java +++ b/src/test/java/net/openhft/chronicle/map/GuavaTest.java @@ -26,26 +26,42 @@ import com.google.common.collect.testing.features.CollectionSize; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; -import org.junit.Ignore; +import net.openhft.chronicle.hash.Value; +import org.jetbrains.annotations.NotNull; +import org.junit.Assert; +import java.util.HashMap; import java.util.List; import java.util.Map; +import static com.google.common.collect.testing.MapTestSuiteBuilder.using; import static com.google.common.collect.testing.features.MapFeature.*; public class GuavaTest extends TestCase { public static Test suite() { - TestSuite hhmTests = MapTestSuiteBuilder.using(new HHMTestGenerator()) - .named("HugeHashMap Guava tests") - .withFeatures(GENERAL_PURPOSE) + MapTestSuiteBuilder chmSuite = using(new CHMTestGenerator()); + configureSuite(chmSuite); + TestSuite chmTests = chmSuite.named("Guava tests of Chronicle Map").createTestSuite(); + + MapTestSuiteBuilder backed = using(new BackedUpMapGenerator()); + configureSuite(backed); + TestSuite backedTests = backed + .named("Guava tests tests of Chronicle Map, backed with HashMap") + .createTestSuite(); + + TestSuite tests = new TestSuite(); + tests.addTest(chmTests); + // TODO + //tests.addTest(backedTests); + return tests; + } + + private static void configureSuite(MapTestSuiteBuilder suite) { + suite.withFeatures(GENERAL_PURPOSE) .withFeatures(CollectionSize.ANY) .withFeatures(CollectionFeature.REMOVE_OPERATIONS) - .withFeatures(RESTRICTS_KEYS, RESTRICTS_VALUES) - .createTestSuite(); - TestSuite tests = new TestSuite(); - tests.addTest(hhmTests); - return tests; + .withFeatures(RESTRICTS_KEYS, RESTRICTS_VALUES); } static abstract class TestGenerator @@ -109,10 +125,36 @@ public class GuavaTest extends TestCase { return builder.create(); } } - - static class HHMTestGenerator extends CHMTestGenerator { + + static class BackedUpMapGenerator extends CHMTestGenerator { + @Override Map newMap() { + Map m = new HashMap<>(); + builder.entryOperations(new MapEntryOperations() { + @Override + public Void remove(@NotNull MapEntry entry) { + Assert.assertEquals(m, entry.context().map()); + m.remove(entry.key().get().toString()); + return MapEntryOperations.super.remove(entry); + } + + @Override + public Void replaceValue(@NotNull MapEntry entry, + Value newValue) { + Assert.assertEquals(m, entry.context().map()); + m.put(entry.key().get().toString(), newValue.get().toString()); + return MapEntryOperations.super.replaceValue(entry, newValue); + } + + @Override + public Void insert(@NotNull MapAbsentEntry absentEntry, + Value value) { + Assert.assertEquals(m, absentEntry.context().map()); + m.put(absentEntry.absentKey().get().toString(), value.get().toString()); + return MapEntryOperations.super.insert(absentEntry, value); + } + }); return builder.create(); } } diff --git a/src/test/java/net/openhft/chronicle/map/ListenersTest.java b/src/test/java/net/openhft/chronicle/map/ListenersTest.java index d7b786522..523ec480c 100644 --- a/src/test/java/net/openhft/chronicle/map/ListenersTest.java +++ b/src/test/java/net/openhft/chronicle/map/ListenersTest.java @@ -1,6 +1,7 @@ package net.openhft.chronicle.map; import net.openhft.chronicle.hash.Value; +import net.openhft.chronicle.map.impl.NullReturnValue; import org.jetbrains.annotations.NotNull; import org.junit.Test; @@ -8,8 +9,7 @@ import java.util.Iterator; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; +import static org.junit.Assert.*; public class ListenersTest { @@ -36,7 +36,7 @@ public class ListenersTest { } @Test - public void testRemove() { + public void testAnyRemove() { CountingEntryOperations removeCounting = new CountingEntryOperations<>(); ChronicleMap map = @@ -64,7 +64,7 @@ public class ListenersTest { } @Test - public void testPut() { + public void testAnyPut() { CountingEntryOperations putCounting = new CountingEntryOperations<>(); ChronicleMap map = @@ -85,4 +85,99 @@ public class ListenersTest { assertEquals(3, putCounting.replaceValueCount.get()); assertEquals(2, putCounting.insertCount.get()); } + + @Test + public void testContainsKey() { + AtomicInteger c = new AtomicInteger(); + ChronicleMap map = + ChronicleMapBuilder.of(Integer.class, Integer.class) + .entries(100) + .mapMethods(new MapMethods() { + @Override + public boolean containsKey(MapQueryContext q) { + if (q.queriedKey().get() == 2) + return false; + c.incrementAndGet(); + return MapMethods.super.containsKey(q); + } + }) + .create(); + + assertFalse(map.containsKey(1)); // 1 + map.put(1, 1); + assertTrue(map.containsKey(1)); // 2 + + map.put(2, 2); + assertFalse(map.containsKey(2)); + + assertEquals(2, c.get()); + } + + @Test + public void testGet() { + AtomicInteger c = new AtomicInteger(); + ChronicleMap map = + ChronicleMapBuilder.of(Integer.class, Integer.class) + .entries(100) + .mapMethods(new MapMethods() { + @Override + public void get(MapQueryContext q, + ReturnValue returnValue) { + if (q.queriedKey().get() == 2) { + returnValue.returnValue(q.wrapValueAsValue(42)); + return; + } + c.incrementAndGet(); + MapMethods.super.get(q, returnValue); + } + }) + .create(); + + assertNull(map.get(1)); // 1 + map.put(1, 1); + assertEquals(1, map.get(1).intValue()); // 2 + + map.put(2, 2); + assertEquals(42, map.get(2).intValue()); + + assertEquals(2, c.get()); + } + + @Test + public void testPut() { + AtomicInteger c = new AtomicInteger(); + ChronicleMap map = + ChronicleMapBuilder.of(Integer.class, Integer.class) + .entries(100) + .mapMethods(new MapMethods() { + @Override + public void put(MapQueryContext q, + Value value, + ReturnValue returnValue) { + if (q.queriedKey().get() == 2) { + MapMethods.super.put(q, value, NullReturnValue.get()); + return; + } + if (q.queriedKey().get() == 3) { + MapMethods.super.put(q, q.wrapValueAsValue(value.get() + 1), + returnValue); + return; + } + c.incrementAndGet(); + MapMethods.super.put(q, value, returnValue); + } + }) + .create(); + + assertNull(map.put(1, 1)); // 1 + assertEquals(1, map.put(1, 2).intValue()); // 2 + + assertNull(map.put(2, 1)); + assertNull(map.put(2, 2)); + + assertNull(map.put(3, 1)); + assertEquals(2, map.get(3).intValue()); + + assertEquals(2, c.get()); + } }