You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by me...@apache.org on 2021/09/03 22:29:25 UTC

[shardingsphere] branch master updated: Fix curatorZookeeperRepositoryTest and code style. (#12203)

This is an automated email from the ASF dual-hosted git repository.

menghaoran pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 5c9bd5c  Fix curatorZookeeperRepositoryTest and code style. (#12203)
5c9bd5c is described below

commit 5c9bd5ce6bd4794ed6eb01547d38a5cf9046384c
Author: Guocheng Tang <to...@qq.com>
AuthorDate: Sat Sep 4 06:28:38 2021 +0800

    Fix curatorZookeeperRepositoryTest and code style. (#12203)
    
    * Fix CuratorZookeeperRepositoryTest.
    
    * Fix code style.
---
 ...orithmProviderConfigurationYamlSwapperTest.java |  5 ++-
 .../DatabaseDiscoveryRuleQueryResultSetTest.java   |  4 +--
 .../zookeeper/CuratorZookeeperRepository.java      |  2 +-
 .../zookeeper/CuratorZookeeperRepositoryTest.java  | 40 ++++++++++++++++------
 .../asserts/segment/distsql/SQLSegmentAssert.java  |  6 ++--
 .../asserts/segment/distsql/TableRuleAssert.java   | 10 +++---
 6 files changed, 43 insertions(+), 24 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/yaml/swapper/DatabaseDiscoveryRuleAlgorithmProviderConfigurationYamlSwapperTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/yaml/swapper/DatabaseDiscoveryRuleAlgorithmProviderConfigurationYamlSwapperTest.java
index 132999e..64bcd5b 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/yaml/swapper/DatabaseDiscoveryRuleAlgorithmProviderConfigurationYamlSwapperTest.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/yaml/swapper/DatabaseDiscoveryRuleAlgorithmProviderConfigurationYamlSwapperTest.java
@@ -21,9 +21,8 @@ import com.google.common.collect.ImmutableMap;
 import org.apache.shardingsphere.dbdiscovery.algorithm.config.AlgorithmProvidedDatabaseDiscoveryRuleConfiguration;
 import org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryDataSourceRuleConfiguration;
 import org.apache.shardingsphere.dbdiscovery.constant.DatabaseDiscoveryOrder;
-import org.apache.shardingsphere.dbdiscovery.yaml.config.YamlDatabaseDiscoveryRuleConfiguration;
 import org.apache.shardingsphere.dbdiscovery.mgr.MGRDatabaseDiscoveryType;
-import org.hamcrest.CoreMatchers;
+import org.apache.shardingsphere.dbdiscovery.yaml.config.YamlDatabaseDiscoveryRuleConfiguration;
 import org.junit.Test;
 
 import java.util.Collections;
@@ -69,7 +68,7 @@ public final class DatabaseDiscoveryRuleAlgorithmProviderConfigurationYamlSwappe
     
     @Test
     public void assertGetOrder() {
-        assertThat(swapper.getOrder(), CoreMatchers.is(DatabaseDiscoveryOrder.ALGORITHM_PROVIDER_ORDER));
+        assertThat(swapper.getOrder(), is(DatabaseDiscoveryOrder.ALGORITHM_PROVIDER_ORDER));
     }
     
     private YamlDatabaseDiscoveryRuleConfiguration createYamlHARuleConfiguration() {
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/DatabaseDiscoveryRuleQueryResultSetTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/DatabaseDiscoveryRule [...]
index bd4ffe4..09229d1 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/DatabaseDiscoveryRuleQueryResultSetTest.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/DatabaseDiscoveryRuleQueryResultSetTest.java
@@ -24,7 +24,6 @@ import org.apache.shardingsphere.infra.config.RuleConfiguration;
 import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
 import org.apache.shardingsphere.infra.distsql.query.DistSQLResultSet;
 import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import org.hamcrest.CoreMatchers;
 import org.junit.Test;
 
 import java.util.Arrays;
@@ -34,6 +33,7 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.Properties;
 
+import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
@@ -49,7 +49,7 @@ public final class DatabaseDiscoveryRuleQueryResultSetTest {
         DistSQLResultSet resultSet = new DatabaseDiscoveryRuleQueryResultSet();
         resultSet.init(metaData, mock(ShowDatabaseDiscoveryRulesStatement.class));
         Collection<Object> actual = resultSet.getRowData();
-        assertThat(actual.size(), CoreMatchers.is(4));
+        assertThat(actual.size(), is(4));
         assertTrue(actual.contains("ms_group"));
         assertTrue(actual.contains("ds_0,ds_1"));
         assertTrue(actual.contains("MGR"));
diff --git a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-provider/shardingsphere-cluster-mode-repository-zookeeper-curator/src/main/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/CuratorZookeeperRepository.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-prov [...]
index 5785203..6fbfb19 100644
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-provider/shardingsphere-cluster-mode-repository-zookeeper-curator/src/main/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/CuratorZookeeperRepository.java
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-provider/shardingsphere-cluster-mode-repository-zookeeper-curator/src/main/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/CuratorZookeeperRepository.java
@@ -247,7 +247,7 @@ public final class CuratorZookeeperRepository implements ClusterPersistRepositor
                         listener.onChange(new DataChangedEvent(treeCacheListener.getData().getPath(), 
                                 new String(treeCacheListener.getData().getData(), StandardCharsets.UTF_8), changedType));
                     }
-                }).afterInitialized().build();
+                }).build();
             cache.listenable().addListener(curatorCacheListener);
         }
     }
diff --git a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-provider/shardingsphere-cluster-mode-repository-zookeeper-curator/src/test/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/CuratorZookeeperRepositoryTest.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository- [...]
index 2c99f56..8eb603c 100644
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-provider/shardingsphere-cluster-mode-repository-zookeeper-curator/src/test/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/CuratorZookeeperRepositoryTest.java
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-provider/shardingsphere-cluster-mode-repository-zookeeper-curator/src/test/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/CuratorZookeeperRepositoryTest.java
@@ -23,13 +23,18 @@ import org.apache.curator.RetryPolicy;
 import org.apache.curator.framework.CuratorFramework;
 import org.apache.curator.framework.CuratorFrameworkFactory.Builder;
 import org.apache.curator.framework.api.ACLProvider;
+import org.apache.curator.framework.api.AddWatchBuilder;
+import org.apache.curator.framework.api.AddWatchBuilder2;
+import org.apache.curator.framework.api.BackgroundCallback;
 import org.apache.curator.framework.api.BackgroundVersionable;
 import org.apache.curator.framework.api.CreateBuilder;
 import org.apache.curator.framework.api.DeleteBuilder;
 import org.apache.curator.framework.api.ExistsBuilder;
 import org.apache.curator.framework.api.GetChildrenBuilder;
+import org.apache.curator.framework.api.Pathable;
 import org.apache.curator.framework.api.ProtectACLCreateModeStatPathAndBytesable;
 import org.apache.curator.framework.api.SetDataBuilder;
+import org.apache.curator.framework.api.WatchableBase;
 import org.apache.curator.framework.api.WatchesBuilder;
 import org.apache.curator.framework.listen.Listenable;
 import org.apache.curator.framework.recipes.cache.ChildData;
@@ -41,11 +46,11 @@ import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositor
 import org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEvent;
 import org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEvent.Type;
 import org.apache.shardingsphere.mode.repository.cluster.zookeeper.props.ZookeeperPropertyKey;
+import org.apache.zookeeper.AddWatchMode;
 import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.Watcher;
 import org.apache.zookeeper.data.Stat;
-import org.hamcrest.CoreMatchers;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.AdditionalAnswers;
@@ -128,6 +133,21 @@ public final class CuratorZookeeperRepositoryTest {
     @Mock
     private WatchesBuilder watchesBuilder;
     
+    @Mock
+    private AddWatchBuilder addWatchBuilder;
+    
+    @Mock
+    private AddWatchBuilder2 addWatchBuilder2;
+    
+    @Mock
+    private WatchableBase watchableBase;
+    
+    @Mock
+    private Pathable pathable;
+    
+    @Mock
+    private CuratorCacheListener curatorCacheListener;
+    
     @Before
     @SneakyThrows
     public void init() {
@@ -216,8 +236,6 @@ public final class CuratorZookeeperRepositoryTest {
     
     @Test
     @SneakyThrows
-    @Ignore
-    // TODO fix me
     public void assertWatchUpdatedChangedType() {
         mockCache();
         ChildData oldData = new ChildData("/test/children_updated/1", null, "value1".getBytes());
@@ -227,14 +245,12 @@ public final class CuratorZookeeperRepositoryTest {
         REPOSITORY.watch("/test/children_updated/1", settableFuture::set);
         DataChangedEvent dataChangedEvent = settableFuture.get();
         assertNotNull(dataChangedEvent);
-        assertThat(dataChangedEvent.getType(), CoreMatchers.is(Type.UPDATED));
+        assertThat(dataChangedEvent.getType(), is(Type.UPDATED));
         assertThat(dataChangedEvent.getKey(), is("/test/children_updated/1"));
         assertThat(dataChangedEvent.getValue(), is("value2"));
     }
     
     @Test
-    @Ignore
-    // TODO fix me
     public void assertWatchDeletedChangedType() throws Exception {
         mockCache();
         ChildData oldData = new ChildData("/test/children_deleted/5", null, "value5".getBytes());
@@ -251,8 +267,6 @@ public final class CuratorZookeeperRepositoryTest {
     
     @Test
     @SneakyThrows
-    @Ignore
-    // TODO fix me
     public void assertWatchAddedChangedType() {
         mockCache();
         ChildData data = new ChildData("/test/children_added/4", null, "value4".getBytes());
@@ -266,11 +280,17 @@ public final class CuratorZookeeperRepositoryTest {
         assertThat(dataChangedEvent.getValue(), is("value4"));
     }
     
-    private void mockCache() throws NoSuchFieldException, IllegalAccessException {
+    private void mockCache() throws Exception {
         Field cachesFiled = CuratorZookeeperRepository.class.getDeclaredField("caches");
         cachesFiled.setAccessible(true);
         cachesFiled.set(REPOSITORY, caches);
         when(caches.get(anyString())).thenReturn(curatorCache);
+        when(client.getConnectionStateListenable()).thenReturn(listenerListenable);
+        when(client.watchers()).thenReturn(watchesBuilder);
+        when(watchesBuilder.add()).thenReturn(addWatchBuilder);
+        when(addWatchBuilder.withMode(any(AddWatchMode.class))).thenReturn(addWatchBuilder2);
+        when(addWatchBuilder2.inBackground(any(BackgroundCallback.class))).thenReturn(watchableBase);
+        when(watchableBase.usingWatcher(any(Watcher.class))).thenReturn(pathable);
         when(curatorCache.listenable()).thenReturn(listenable);
     }
     
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/segment/distsql/SQLSegmentAssert.java b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/segment/distsql/SQLSegmentAssert.java
index 57c05b5..0bf8896 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/segment/distsql/SQLSegmentAssert.java
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/segment/distsql/SQLSegmentAssert.java
@@ -22,8 +22,8 @@ import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.SQLSegment;
 import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.SQLCaseAssertContext;
 import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.segment.ExpectedSQLSegment;
-import org.hamcrest.CoreMatchers;
 
+import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
 
 /**
@@ -40,7 +40,7 @@ public final class SQLSegmentAssert {
      * @param expected expected SQL segment
      */
     public static void assertIs(final SQLCaseAssertContext assertContext, final SQLSegment actual, final ExpectedSQLSegment expected) {
-        assertThat(assertContext.getText(String.format("`%s`'s start index assertion error: ", actual.getClass().getSimpleName())), actual.getStartIndex(), CoreMatchers.is(expected.getStartIndex()));
-        assertThat(assertContext.getText(String.format("`%s`'s start index assertion error: ", actual.getClass().getSimpleName())), actual.getStopIndex(), CoreMatchers.is(expected.getStopIndex()));
+        assertThat(assertContext.getText(String.format("`%s`'s start index assertion error: ", actual.getClass().getSimpleName())), actual.getStartIndex(), is(expected.getStartIndex()));
+        assertThat(assertContext.getText(String.format("`%s`'s start index assertion error: ", actual.getClass().getSimpleName())), actual.getStopIndex(), is(expected.getStopIndex()));
     }
 }
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/segment/distsql/TableRuleAssert.java b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/segment/distsql/TableRuleAssert.java
index 81955eb..9093f50 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/segment/distsql/TableRuleAssert.java
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/segment/distsql/TableRuleAssert.java
@@ -22,8 +22,8 @@ import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.sharding.distsql.parser.segment.TableRuleSegment;
 import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.SQLCaseAssertContext;
 import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.segment.impl.distsql.ExpectedTableRule;
-import org.hamcrest.CoreMatchers;
 
+import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertThat;
@@ -47,13 +47,13 @@ public final class TableRuleAssert {
         } else {
             assertNotNull(assertContext.getText("Actual table rule should exist."), actual);
             assertThat(assertContext.getText(String.format("`%s`'s table rule segment assertion error: ", actual.getClass().getSimpleName())),
-                    actual.getLogicTable(), CoreMatchers.is(expected.getName()));
+                    actual.getLogicTable(), is(expected.getName()));
             assertThat(assertContext.getText(String.format("`%s`'s table rule segment assertion error: ", actual.getClass().getSimpleName())),
-                    actual.getDataSources(), CoreMatchers.is(expected.getDataSources()));
+                    actual.getDataSources(), is(expected.getDataSources()));
             assertThat(assertContext.getText(String.format("`%s`'s table rule segment assertion error: ", actual.getClass().getSimpleName())),
-                    actual.getTableStrategyColumn(), CoreMatchers.is(expected.getTableStrategyColumn()));
+                    actual.getTableStrategyColumn(), is(expected.getTableStrategyColumn()));
             assertThat(assertContext.getText(String.format("`%s`'s table rule segment assertion error: ", actual.getClass().getSimpleName())),
-                    actual.getKeyGenerateStrategyColumn(), CoreMatchers.is(expected.getKeyGenerateStrategyColumn()));
+                    actual.getKeyGenerateStrategyColumn(), is(expected.getKeyGenerateStrategyColumn()));
             AlgorithmAssert.assertIs(assertContext, actual.getTableStrategy(), expected.getTableStrategy());
             AlgorithmAssert.assertIs(assertContext, actual.getKeyGenerateStrategy(), expected.getKeyGenerateStrategy());
         }