From e7e1bc49a019490a09bf6c3e86a5412c9b413c92 Mon Sep 17 00:00:00 2001 From: jingyuntian Date: Fri, 23 Mar 2018 13:37:06 +0800 Subject: [PATCH] add a UT for ReplicationUtils.contains() --- hbase-replication/pom.xml | 6 + .../hadoop/hbase/replication/ReplicationUtils.java | 2 +- .../hbase/replication/TestReplicationUtil.java | 220 +++++++++++++++++++++ 3 files changed, 227 insertions(+), 1 deletion(-) create mode 100644 hbase-replication/src/test/java/org/apache/hadoop/hbase/replication/TestReplicationUtil.java diff --git a/hbase-replication/pom.xml b/hbase-replication/pom.xml index d05c60e..38678bf 100644 --- a/hbase-replication/pom.xml +++ b/hbase-replication/pom.xml @@ -138,6 +138,12 @@ junit test + + org.mockito + mockito-core + ${mockito-core.version} + test + diff --git a/hbase-replication/src/main/java/org/apache/hadoop/hbase/replication/ReplicationUtils.java b/hbase-replication/src/main/java/org/apache/hadoop/hbase/replication/ReplicationUtils.java index e2479e0..625322f 100644 --- a/hbase-replication/src/main/java/org/apache/hadoop/hbase/replication/ReplicationUtils.java +++ b/hbase-replication/src/main/java/org/apache/hadoop/hbase/replication/ReplicationUtils.java @@ -147,7 +147,7 @@ public final class ReplicationUtils { if (excludeNamespaces != null && excludeNamespaces.contains(namespace)) { return false; } - Map> excludedTableCFs = peerConfig.getTableCFsMap(); + Map> excludedTableCFs = peerConfig.getExcludeTableCFsMap(); // trap here, must check existence first since HashMap allows null value. if (excludedTableCFs == null || !excludedTableCFs.containsKey(tableName)) { return true; diff --git a/hbase-replication/src/test/java/org/apache/hadoop/hbase/replication/TestReplicationUtil.java b/hbase-replication/src/test/java/org/apache/hadoop/hbase/replication/TestReplicationUtil.java new file mode 100644 index 0000000..fd597c3 --- /dev/null +++ b/hbase-replication/src/test/java/org/apache/hadoop/hbase/replication/TestReplicationUtil.java @@ -0,0 +1,220 @@ +package org.apache.hadoop.hbase.replication; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.testclassification.ReplicationTests; +import org.apache.hadoop.hbase.testclassification.SmallTests; +import org.junit.Assert; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; + + +/** + * Created by tianjingyun on 18-3-23. + */ +@Category({ ReplicationTests.class, SmallTests.class }) +public class TestReplicationUtil { + + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestReplicationUtil.class); + + static TableName a = TableName.valueOf("replication", "testA"); + static TableName b = TableName.valueOf("replication", "testB"); + + @Test + public void testContains() { + ReplicationPeer peer = mock(ReplicationPeer.class); + ReplicationPeerConfig peerConfig = mock(ReplicationPeerConfig.class); + + // 1. replication_all flag is false, no namespaces and table-cfs config + when(peerConfig.replicateAllUserTables()).thenReturn(false); + when(peerConfig.getNamespaces()).thenReturn(null); + when(peerConfig.getTableCFsMap()).thenReturn(null); + when(peer.getPeerConfig()).thenReturn(peerConfig); + Assert.assertFalse(ReplicationUtils.contains(peerConfig, a)); + + // 2. replication_all flag is true, no namespaces and table-cfs config + when(peerConfig.replicateAllUserTables()).thenReturn(true); + when(peerConfig.getNamespaces()).thenReturn(null); + when(peerConfig.getTableCFsMap()).thenReturn(null); + when(peer.getPeerConfig()).thenReturn(peerConfig); + Assert.assertTrue(ReplicationUtils.contains(peerConfig, a)); + + // 3. replicate_all flag is false, and only config table-cfs in peer + // empty map + Map> tableCfs = new HashMap<>(); + when(peerConfig.replicateAllUserTables()).thenReturn(false); + when(peerConfig.getTableCFsMap()).thenReturn(tableCfs); + when(peer.getPeerConfig()).thenReturn(peerConfig); + Assert.assertFalse(ReplicationUtils.contains(peerConfig, a)); + + // table testB + tableCfs = new HashMap<>(); + tableCfs.put(b, null); + when(peerConfig.replicateAllUserTables()).thenReturn(false); + when(peerConfig.getTableCFsMap()).thenReturn(tableCfs); + when(peer.getPeerConfig()).thenReturn(peerConfig); + Assert.assertFalse(ReplicationUtils.contains(peerConfig, a)); + + // table testA + tableCfs = new HashMap<>(); + tableCfs.put(a, null); + when(peerConfig.replicateAllUserTables()).thenReturn(false); + when(peerConfig.getTableCFsMap()).thenReturn(tableCfs); + when(peer.getPeerConfig()).thenReturn(peerConfig); + Assert.assertTrue(ReplicationUtils.contains(peerConfig, a)); + + // 4. replication_all flag is false, and only config namespace in peer + when(peerConfig.getTableCFsMap()).thenReturn(null); + // empty set + Set namespaces = new HashSet<>(); + when(peerConfig.replicateAllUserTables()).thenReturn(false); + when(peerConfig.getNamespaces()).thenReturn(namespaces); + when(peer.getPeerConfig()).thenReturn(peerConfig); + Assert.assertFalse(ReplicationUtils.contains(peerConfig, a)); + + // namespace default + namespaces = new HashSet<>(); + namespaces.add("default"); + when(peerConfig.replicateAllUserTables()).thenReturn(false); + when(peerConfig.getNamespaces()).thenReturn(namespaces); + when(peer.getPeerConfig()).thenReturn(peerConfig); + Assert.assertFalse(ReplicationUtils.contains(peerConfig, a)); + + // namespace replication + namespaces = new HashSet<>(); + namespaces.add("replication"); + when(peerConfig.replicateAllUserTables()).thenReturn(false); + when(peerConfig.getNamespaces()).thenReturn(namespaces); + when(peer.getPeerConfig()).thenReturn(peerConfig); + Assert.assertTrue(ReplicationUtils.contains(peerConfig, a)); + + // 5. replicate_all flag is false, and config namespaces and table-cfs both + // Namespaces config doesn't conflict with table-cfs config + namespaces = new HashSet<>(); + tableCfs = new HashMap<>(); + namespaces.add("replication"); + tableCfs.put(a, null); + when(peerConfig.replicateAllUserTables()).thenReturn(false); + when(peerConfig.getNamespaces()).thenReturn(namespaces); + when(peerConfig.getTableCFsMap()).thenReturn(tableCfs); + when(peer.getPeerConfig()).thenReturn(peerConfig); + Assert.assertTrue(ReplicationUtils.contains(peerConfig, a)); + + // Namespaces config conflicts with table-cfs config + namespaces = new HashSet<>(); + tableCfs = new HashMap<>(); + namespaces.add("default"); + tableCfs.put(a, null); + when(peerConfig.replicateAllUserTables()).thenReturn(false); + when(peerConfig.getNamespaces()).thenReturn(namespaces); + when(peerConfig.getTableCFsMap()).thenReturn(tableCfs); + when(peer.getPeerConfig()).thenReturn(peerConfig); + Assert.assertTrue(ReplicationUtils.contains(peerConfig, a)); + + namespaces = new HashSet<>(); + tableCfs = new HashMap<>(); + namespaces.add("replication"); + tableCfs.put(b, null); + when(peerConfig.replicateAllUserTables()).thenReturn(false); + when(peerConfig.getNamespaces()).thenReturn(namespaces); + when(peerConfig.getTableCFsMap()).thenReturn(tableCfs); + when(peer.getPeerConfig()).thenReturn(peerConfig); + Assert.assertTrue(ReplicationUtils.contains(peerConfig, a)); + + // 6. replicate_all flag is true, and config in excludedTableCfs + when(peerConfig.getExcludeNamespaces()).thenReturn(null); + // empty map + tableCfs = new HashMap<>(); + when(peerConfig.replicateAllUserTables()).thenReturn(true); + when(peerConfig.getExcludeTableCFsMap()).thenReturn(tableCfs); + when(peer.getPeerConfig()).thenReturn(peerConfig); + Assert.assertTrue(ReplicationUtils.contains(peerConfig, a)); + + // table testB + tableCfs = new HashMap<>(); + tableCfs.put(b, null); + when(peerConfig.replicateAllUserTables()).thenReturn(true); + when(peerConfig.getExcludeTableCFsMap()).thenReturn(tableCfs); + when(peer.getPeerConfig()).thenReturn(peerConfig); + Assert.assertTrue(ReplicationUtils.contains(peerConfig, a)); + + // table testA + tableCfs = new HashMap<>(); + tableCfs.put(a, null); + when(peerConfig.replicateAllUserTables()).thenReturn(true); + when(peerConfig.getExcludeTableCFsMap()).thenReturn(tableCfs); + when(peer.getPeerConfig()).thenReturn(peerConfig); + Assert.assertFalse(ReplicationUtils.contains(peerConfig, a)); + + // 7. replicate_all flag is true, and config in excludeNamespaces + when(peerConfig.getExcludeTableCFsMap()).thenReturn(null); + // empty set + namespaces = new HashSet<>(); + when(peerConfig.replicateAllUserTables()).thenReturn(true); + when(peerConfig.getExcludeNamespaces()).thenReturn(namespaces); + when(peer.getPeerConfig()).thenReturn(peerConfig); + Assert.assertTrue(ReplicationUtils.contains(peerConfig, a)); + + // namespace default + namespaces = new HashSet<>(); + namespaces.add("default"); + when(peerConfig.replicateAllUserTables()).thenReturn(true); + when(peerConfig.getExcludeNamespaces()).thenReturn(namespaces); + when(peer.getPeerConfig()).thenReturn(peerConfig); + Assert.assertTrue(ReplicationUtils.contains(peerConfig, a)); + + // namespace replication + namespaces = new HashSet<>(); + namespaces.add("replication"); + when(peerConfig.replicateAllUserTables()).thenReturn(true); + when(peerConfig.getExcludeNamespaces()).thenReturn(namespaces); + when(peer.getPeerConfig()).thenReturn(peerConfig); + Assert.assertFalse(ReplicationUtils.contains(peerConfig, a)); + + // 8. replicate_all flag is false, and config excludeNamespaces and excludedTableCfs both + // Namespaces config doesn't conflict with table-cfs config + namespaces = new HashSet<>(); + tableCfs = new HashMap<>(); + namespaces.add("replication"); + tableCfs.put(a, null); + when(peerConfig.replicateAllUserTables()).thenReturn(true); + when(peerConfig.getExcludeNamespaces()).thenReturn(namespaces); + when(peerConfig.getExcludeTableCFsMap()).thenReturn(tableCfs); + when(peer.getPeerConfig()).thenReturn(peerConfig); + Assert.assertFalse(ReplicationUtils.contains(peerConfig, a)); + + // Namespaces config conflicts with table-cfs config + namespaces = new HashSet<>(); + tableCfs = new HashMap<>(); + namespaces.add("default"); + tableCfs.put(a, null); + when(peerConfig.replicateAllUserTables()).thenReturn(true); + when(peerConfig.getExcludeNamespaces()).thenReturn(namespaces); + when(peerConfig.getExcludeTableCFsMap()).thenReturn(tableCfs); + when(peer.getPeerConfig()).thenReturn(peerConfig); + Assert.assertFalse(ReplicationUtils.contains(peerConfig, a)); + + namespaces = new HashSet<>(); + tableCfs = new HashMap<>(); + namespaces.add("replication"); + tableCfs.put(b, null); + when(peerConfig.replicateAllUserTables()).thenReturn(true); + when(peerConfig.getExcludeNamespaces()).thenReturn(namespaces); + when(peerConfig.getExcludeTableCFsMap()).thenReturn(tableCfs); + when(peer.getPeerConfig()).thenReturn(peerConfig); + Assert.assertFalse(ReplicationUtils.contains(peerConfig, a)); + + } +} -- 2.7.4