Repository: ignite Updated Branches: refs/heads/master 4ee181ce4 -> 29d56aede
IGNITE-7881 Tests for using TreeMap or TreeSet as cache key. Signed-off-by: Andrey Gura <ag...@apache.org> Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/29d56aed Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/29d56aed Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/29d56aed Branch: refs/heads/master Commit: 29d56aedeae7c00ffe997530aeb7624d5863df5b Parents: 4ee181c Author: Ilya Kasnacheev <ilya.kasnach...@gmail.com> Authored: Mon Mar 12 15:19:10 2018 +0300 Committer: Andrey Gura <ag...@apache.org> Committed: Mon Mar 12 15:19:10 2018 +0300 ---------------------------------------------------------------------- .../internal/binary/BinaryTreeSelfTest.java | 273 +++++++++++++++---- 1 file changed, 226 insertions(+), 47 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/29d56aed/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryTreeSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryTreeSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryTreeSelfTest.java index 9481340..819e766 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryTreeSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryTreeSelfTest.java @@ -17,6 +17,11 @@ package org.apache.ignite.internal.binary; +import java.util.Collections; +import java.util.Comparator; +import java.util.TreeMap; +import java.util.TreeSet; +import javax.cache.Cache.Entry; import org.apache.ignite.IgniteCache; import org.apache.ignite.Ignition; import org.apache.ignite.binary.BinaryObject; @@ -29,11 +34,6 @@ import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder; import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; import org.jetbrains.annotations.NotNull; -import java.util.Collections; -import java.util.Comparator; -import java.util.TreeMap; -import java.util.TreeSet; - /** * Tests for TreeMap and TreeSet structures. */ @@ -71,8 +71,8 @@ public class BinaryTreeSelfTest extends GridCommonAbstractTest { * * @throws Exception If failed. */ - public void testTreeMapRegularNoComparator() throws Exception { - checkTreeMap(false, false); + public void testTreeMapAsValueRegularNoComparator() throws Exception { + checkTreeMapAsValue(false, false); } /** @@ -80,8 +80,8 @@ public class BinaryTreeSelfTest extends GridCommonAbstractTest { * * @throws Exception If failed. */ - public void testTreeMapRegularComparator() throws Exception { - checkTreeMap(false, true); + public void testTreeMapAsValueRegularComparator() throws Exception { + checkTreeMapAsValue(false, true); } /** @@ -89,8 +89,8 @@ public class BinaryTreeSelfTest extends GridCommonAbstractTest { * * @throws Exception If failed. */ - public void testTreeMapBinaryNoComparator() throws Exception { - checkTreeMap(true, false); + public void testTreeMapAsValueBinaryNoComparator() throws Exception { + checkTreeMapAsValue(true, false); } /** @@ -98,8 +98,26 @@ public class BinaryTreeSelfTest extends GridCommonAbstractTest { * * @throws Exception If failed. */ - public void testTreeMapBinaryComparator() throws Exception { - checkTreeMap(true, true); + public void testTreeMapAsValueBinaryComparator() throws Exception { + checkTreeMapAsValue(true, true); + } + + /** + * Test {@code TreeMap} data structure when used as key. + * + * @throws Exception If failed. + */ + public void testTreeMapAsKeyNoComparator() throws Exception { + checkTreeMapAsKey(false); + } + + /** + * Test {@code TreeMap} data structure with comparator when used as key. + * + * @throws Exception If failed. + */ + public void testTreeMapAsKeyComparator() throws Exception { + checkTreeMapAsKey(true); } /** @@ -110,22 +128,11 @@ public class BinaryTreeSelfTest extends GridCommonAbstractTest { * @throws Exception If failed. */ @SuppressWarnings("unchecked") - private void checkTreeMap(boolean useBinary, boolean useComparator) throws Exception { + private void checkTreeMapAsValue(boolean useBinary, boolean useComparator) throws Exception { // Populate map. TreeMap<TestKey, Integer> map; - if (useComparator) { - map = new TreeMap<>(new TestKeyComparator()); - - for (int i = 0; i < SIZE; i++) - map.put(key(false, i), i); - } - else { - map = new TreeMap<>(); - - for (int i = 0; i < SIZE; i++) - map.put(key(true, i), i); - } + map = testMap(useComparator); // Put and get value from cache. cache().put(KEY, map); @@ -147,6 +154,71 @@ public class BinaryTreeSelfTest extends GridCommonAbstractTest { assertNull(resMap.comparator()); assertEquals(map, resMap); + + cache().clear(); + } + + /** + * Check {@code TreeMap} data structure when used as key. + * + * @param useComparator Whether comparator should be used. + * @throws Exception If failed. + */ + @SuppressWarnings("unchecked") + private void checkTreeMapAsKey(boolean useComparator) throws Exception { + // Populate map. + TreeMap<TestKey, Integer> map; + + map = testMap(useComparator); + + // Put and get value from cache. + cache().put(map, KEY); + + TreeMap<TestKey, Integer> resMap = (TreeMap<TestKey, Integer>)((Entry)cache().iterator().next()).getKey(); + + // Ensure content is correct. + if (useComparator) + assert resMap.comparator() instanceof TestKeyComparator; + else + assertNull(resMap.comparator()); + + assertEquals(map, resMap); + + // Ensure value is correct. + Integer resSameMap = (Integer)cache().get(map); + + assertEquals((Object)KEY, resSameMap); + + Integer resIdenticalMap = (Integer)cache().get(testMap(useComparator)); + + assertEquals((Object)KEY, resIdenticalMap); + + // Ensure wrong comparator is not accepted. + Integer resDifferentComp = (Integer)cache().get(testMap(!useComparator)); + + assertEquals(null, resDifferentComp); + + cache().clear(); + } + + /** */ + private TreeMap<TestKey, Integer> testMap(boolean useComp) { + TreeMap<TestKey, Integer> map; + + if (useComp) { + map = new TreeMap<>(new TestKeyComparator()); + + for (int i = 0; i < SIZE; i++) + map.put(key(false, i), i); + } + else { + map = new TreeMap<>(); + + for (int i = 0; i < SIZE; i++) + map.put(key(true, i), i); + } + + return map; } /** @@ -154,8 +226,8 @@ public class BinaryTreeSelfTest extends GridCommonAbstractTest { * * @throws Exception If failed. */ - public void testTreeSetRegularNoComparator() throws Exception { - checkTreeSet(false, false); + public void testTreeSetAsValueRegularNoComparator() throws Exception { + checkTreeSetAsValue(false, false); } /** @@ -163,8 +235,8 @@ public class BinaryTreeSelfTest extends GridCommonAbstractTest { * * @throws Exception If failed. */ - public void testTreeSetRegularComparator() throws Exception { - checkTreeSet(false, true); + public void testTreeSetAsValueRegularComparator() throws Exception { + checkTreeSetAsValue(false, true); } /** @@ -172,8 +244,8 @@ public class BinaryTreeSelfTest extends GridCommonAbstractTest { * * @throws Exception If failed. */ - public void testTreeSetBinaryNoComparator() throws Exception { - checkTreeSet(true, false); + public void testTreeSetAsValueBinaryNoComparator() throws Exception { + checkTreeSetAsValue(true, false); } /** @@ -181,8 +253,26 @@ public class BinaryTreeSelfTest extends GridCommonAbstractTest { * * @throws Exception If failed. */ - public void testTreeSetBinaryComparator() throws Exception { - checkTreeSet(true, true); + public void testTreeSetAsValueBinaryComparator() throws Exception { + checkTreeSetAsValue(true, true); + } + + /** + * Test {@code TreeSet} data structure. + * + * @throws Exception If failed. + */ + public void testTreeSetAsKeyNoComparator() throws Exception { + checkTreeSetAsKey(false); + } + + /** + * Test {@code TreeSet} data structure with comparator. + * + * @throws Exception If failed. + */ + public void testTreeSetAsKeyComparator() throws Exception { + checkTreeSetAsKey(true); } /** @@ -193,22 +283,11 @@ public class BinaryTreeSelfTest extends GridCommonAbstractTest { * @throws Exception If failed. */ @SuppressWarnings("unchecked") - private void checkTreeSet(boolean useBinary, boolean useComparator) throws Exception { + private void checkTreeSetAsValue(boolean useBinary, boolean useComparator) throws Exception { // Populate set. TreeSet<TestKey> set; - if (useComparator) { - set = new TreeSet<>(new TestKeyComparator()); - - for (int i = 0; i < SIZE; i++) - set.add(key(false, i)); - } - else { - set = new TreeSet<>(); - - for (int i = 0; i < SIZE; i++) - set.add(key(true, i)); - } + set = testSet(useComparator); // Put and get value from cache. cache().put(KEY, set); @@ -230,6 +309,73 @@ public class BinaryTreeSelfTest extends GridCommonAbstractTest { assertNull(resSet.comparator()); assertEquals(set, resSet); + + cache().clear(); + } + + /** + * Check {@code TreeSet} data structure when used as key. + * + * @param useComp Whether comparator should be used. + * @throws Exception If failed. + */ + @SuppressWarnings("unchecked") + private void checkTreeSetAsKey(boolean useComp) throws Exception { + // Populate set. + TreeSet<TestKey> set; + + set = testSet(useComp); + + // Put and get value from cache. + cache().put(set, KEY); + + TreeSet<TestKey> resSet = (TreeSet<TestKey>)((Entry)cache().iterator().next()).getKey(); + + // Ensure content is correct. + if (useComp) + assert resSet.comparator() instanceof TestKeyComparator; + else + assertNull(resSet.comparator()); + + assertEquals(set, resSet); + + // Ensure value is correct. + Integer resSameMap = (Integer)cache().get(set); + + assertEquals((Object)KEY, resSameMap); + + Integer resIdenticalMap = (Integer)cache().get(testSet(useComp)); + + assertEquals((Object)KEY, resIdenticalMap); + + // Ensure wrong comparator is not accepted. + Integer resDifferentComp = (Integer)cache().get(testSet(!useComp)); + + assertEquals(null, resDifferentComp); + + assertEquals(set, resSet); + + cache().clear(); + } + + /** */ + private TreeSet<TestKey> testSet(boolean useComp) { + TreeSet<TestKey> set; + + if (useComp) { + set = new TreeSet<>(new TestKeyComparator()); + + for (int i = 0; i < SIZE; i++) + set.add(key(false, i)); + } + else { + set = new TreeSet<>(); + + for (int i = 0; i < SIZE; i++) + set.add(key(true, i)); + } + + return set; } /** @@ -308,6 +454,23 @@ public class BinaryTreeSelfTest extends GridCommonAbstractTest { public int id() { return id; } + + /** */ + @Override public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + + TestKey key = (TestKey)o; + + return id == key.id; + } + + /** */ + @Override public int hashCode() { + return id; + } } /** @@ -337,5 +500,21 @@ public class BinaryTreeSelfTest extends GridCommonAbstractTest { @Override public int compare(TestKey o1, TestKey o2) { return o1.id() - o2.id(); } + + /** */ + @Override public boolean equals(Object o) { + if (this == o) + return true; + + if (o == null || getClass() != o.getClass()) + return false; + + return true; + } + + /** */ + @Override public int hashCode() { + return 13; + } } }