/* * Copyright 2012-2018 Chronicle Map Contributors * * Licensed 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 net.openhft.chronicle.map; import com.google.common.collect.testing.MapTestSuiteBuilder; import com.google.common.collect.testing.SampleElements; import com.google.common.collect.testing.TestMapGenerator; import com.google.common.collect.testing.features.CollectionFeature; import com.google.common.collect.testing.features.CollectionSize; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import net.openhft.chronicle.core.Jvm; import net.openhft.chronicle.hash.Data; import org.jetbrains.annotations.NotNull; import org.junit.Assert; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Vector; 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() { MapTestSuiteBuilder chmSuite = using(new CHMTestGenerator()); configureSuite(chmSuite); TestSuite chmTests = chmSuite.named("Guava tests of Chronicle Map").createTestSuite(); // TODO: remove after https://github.com/OpenHFT/Chronicle-Map/issues/303 is fixed. { Vector collectionSizeSuites = Jvm.getValue(chmTests, "fTests"); assert collectionSizeSuites != null; for (TestSuite sizeSuite : collectionSizeSuites) { Vector testers = Jvm.getValue(sizeSuite, "fTests"); assert testers != null; testers.removeIf(tester -> tester.getName().contains("MapComputeIfAbsentTester") || tester.getName().contains("MapMergeTester") || tester.getName().contains("MapReplaceAllTester")); } } 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); } static abstract class TestGenerator implements TestMapGenerator { abstract Map newMap(); public String[] createKeyArray(int length) { return new String[length]; } @Override public String[] createValueArray(int length) { return new String[length]; } @Override public SampleElements> samples() { return SampleElements.mapEntries( new SampleElements<>("key1", "key2", "key3", "key4", "key5"), new SampleElements<>("val1", "val2", "val3", "val4", "val5") ); } @Override public Map create(Object... objects) { Map map = newMap(); for (Object obj : objects) { Map.Entry e = (Map.Entry) obj; map.put((String) e.getKey(), (String) e.getValue()); } return map; } @Override public Map.Entry[] createArray(int length) { //noinspection unchecked return new Map.Entry[length]; } @Override public Iterable> order( List> insertionOrder) { return insertionOrder; } } static class CHMTestGenerator extends TestGenerator { ChronicleMapBuilder builder = ChronicleMapBuilder.of(String.class, String.class) .entries(100) .averageKeySize(10).averageValueSize(10) .minSegments(2); @Override Map newMap() { return builder.create(); } } 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()); return MapEntryOperations.super.remove(entry); } @Override public Void replaceValue(@NotNull MapEntry entry, net.openhft.chronicle.hash.Data newValue) { Assert.assertEquals(m, entry.context().map()); m.put(entry.key().get(), newValue.get()); return MapEntryOperations.super.replaceValue(entry, newValue); } @Override public Void insert(@NotNull MapAbsentEntry absentEntry, Data value) { Assert.assertEquals(m, absentEntry.context().map()); m.put(absentEntry.absentKey().get(), value.get()); return MapEntryOperations.super.insert(absentEntry, value); } }); return builder.create(); } } }