Skip to content

Commit

Permalink
Fix curatorZookeeperRepositoryTest and code style. (apache#12203)
Browse files Browse the repository at this point in the history
* Fix CuratorZookeeperRepositoryTest.

* Fix code style.
  • Loading branch information
totalo authored Sep 3, 2021
1 parent 9cc1ac0 commit 5c9bd5c
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
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;
Expand Down Expand Up @@ -69,7 +68,7 @@ public void assertGetRuleTagName() {

@Test
public void assertGetOrder() {
assertThat(swapper.getOrder(), CoreMatchers.is(DatabaseDiscoveryOrder.ALGORITHM_PROVIDER_ORDER));
assertThat(swapper.getOrder(), is(DatabaseDiscoveryOrder.ALGORITHM_PROVIDER_ORDER));
}

private YamlDatabaseDiscoveryRuleConfiguration createYamlHARuleConfiguration() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
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;
Expand All @@ -34,6 +33,7 @@
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;
Expand All @@ -49,7 +49,7 @@ public void assertGetRowData() {
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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public void watch(final String key, final DataChangedEventListener listener) {
listener.onChange(new DataChangedEvent(treeCacheListener.getData().getPath(),
new String(treeCacheListener.getData().getData(), StandardCharsets.UTF_8), changedType));
}
}).afterInitialized().build();
}).build();
cache.listenable().addListener(curatorCacheListener);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@
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;
Expand All @@ -41,11 +46,11 @@
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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -216,8 +236,6 @@ public void assertGetChildrenKeys() {

@Test
@SneakyThrows
@Ignore
// TODO fix me
public void assertWatchUpdatedChangedType() {
mockCache();
ChildData oldData = new ChildData("/test/children_updated/1", null, "value1".getBytes());
Expand All @@ -227,14 +245,12 @@ public void assertWatchUpdatedChangedType() {
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());
Expand All @@ -251,8 +267,6 @@ public void assertWatchDeletedChangedType() throws Exception {

@Test
@SneakyThrows
@Ignore
// TODO fix me
public void assertWatchAddedChangedType() {
mockCache();
ChildData data = new ChildData("/test/children_added/4", null, "value4".getBytes());
Expand All @@ -266,11 +280,17 @@ public void assertWatchAddedChangedType() {
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
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;

/**
Expand All @@ -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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
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;
Expand All @@ -47,13 +47,13 @@ public static void assertIs(final SQLCaseAssertContext assertContext, final Tabl
} 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());
}
Expand Down

0 comments on commit 5c9bd5c

Please sign in to comment.