You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2020/09/16 14:08:06 UTC
[shardingsphere] branch master updated: Revise #7488 (#7494)
This is an automated email from the ASF dual-hosted git repository.
zhangyonglun 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 fe11a3e Revise #7488 (#7494)
fe11a3e is described below
commit fe11a3e96b06f0b8801af7803fba21b039625566
Author: Liang Zhang <te...@163.com>
AuthorDate: Wed Sep 16 22:07:50 2020 +0800
Revise #7488 (#7494)
* Refactor createSchemaContextMap
* For code format
---
.../proxy/backend/context/ProxyContextTest.java | 29 ++++++++--------------
1 file changed, 11 insertions(+), 18 deletions(-)
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/context/ProxyContextTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/context/ProxyContextTest.java
index 6efbc55..9ac1f63 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/context/ProxyContextTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/context/ProxyContextTest.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.proxy.backend.context;
-import com.google.common.collect.Lists;
import org.apache.shardingsphere.infra.auth.Authentication;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.context.SchemaContext;
@@ -34,7 +33,8 @@ import javax.sql.DataSource;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.HashMap;
-import java.util.List;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
@@ -77,15 +77,14 @@ public final class ProxyContextTest {
SchemaContext schemaContext = mock(SchemaContext.class);
ShardingSphereSchema shardingSphereSchema = mock(ShardingSphereSchema.class);
RuntimeContext runtimeContext = mock(RuntimeContext.class);
- when(shardingSphereSchema.getDataSources()).thenReturn(new HashMap<String, DataSource>());
+ when(shardingSphereSchema.getDataSources()).thenReturn(Collections.emptyMap());
when(schemaContext.getName()).thenReturn("schema");
when(schemaContext.getSchema()).thenReturn(shardingSphereSchema);
when(schemaContext.getRuntimeContext()).thenReturn(runtimeContext);
Map<String, SchemaContext> schemaContextsMap = Collections.singletonMap("schema", schemaContext);
Field schemaContexts = ProxyContext.getInstance().getClass().getDeclaredField("schemaContexts");
schemaContexts.setAccessible(true);
- schemaContexts.set(ProxyContext.getInstance(),
- new StandardSchemaContexts(schemaContextsMap, new Authentication(), new ConfigurationProperties(new Properties()), new MySQLDatabaseType()));
+ schemaContexts.set(ProxyContext.getInstance(), new StandardSchemaContexts(schemaContextsMap, new Authentication(), new ConfigurationProperties(new Properties()), new MySQLDatabaseType()));
boolean exists1 = ProxyContext.getInstance().schemaExists("schema");
assertThat(true, is(exists1));
boolean exists2 = ProxyContext.getInstance().schemaExists("schema_2");
@@ -97,15 +96,14 @@ public final class ProxyContextTest {
SchemaContext schemaContext = mock(SchemaContext.class);
ShardingSphereSchema shardingSphereSchema = mock(ShardingSphereSchema.class);
RuntimeContext runtimeContext = mock(RuntimeContext.class);
- when(shardingSphereSchema.getDataSources()).thenReturn(new HashMap<String, DataSource>());
+ when(shardingSphereSchema.getDataSources()).thenReturn(Collections.emptyMap());
when(schemaContext.getName()).thenReturn("schema");
when(schemaContext.getSchema()).thenReturn(shardingSphereSchema);
when(schemaContext.getRuntimeContext()).thenReturn(runtimeContext);
Map<String, SchemaContext> schemaContextsMap = Collections.singletonMap("schema", schemaContext);
Field schemaContexts = ProxyContext.getInstance().getClass().getDeclaredField("schemaContexts");
schemaContexts.setAccessible(true);
- schemaContexts.set(ProxyContext.getInstance(),
- new StandardSchemaContexts(schemaContextsMap, new Authentication(), new ConfigurationProperties(new Properties()), new MySQLDatabaseType()));
+ schemaContexts.set(ProxyContext.getInstance(), new StandardSchemaContexts(schemaContextsMap, new Authentication(), new ConfigurationProperties(new Properties()), new MySQLDatabaseType()));
assertThat(null, is(ProxyContext.getInstance().getSchema(null)));
assertThat(null, is(ProxyContext.getInstance().getSchema("")));
assertThat(null, is(ProxyContext.getInstance().getSchema("schema1")));
@@ -114,19 +112,15 @@ public final class ProxyContextTest {
@Test
public void assertGetAllSchemaNames() throws NoSuchFieldException, IllegalAccessException {
- Map<String, SchemaContext> schemaContextsMap = getSchemaContextMap();
+ Map<String, SchemaContext> schemaContextsMap = createSchemaContextMap();
Field schemaContexts = ProxyContext.getInstance().getClass().getDeclaredField("schemaContexts");
schemaContexts.setAccessible(true);
- schemaContexts.set(ProxyContext.getInstance(),
- new StandardSchemaContexts(schemaContextsMap, new Authentication(), new ConfigurationProperties(new Properties()), new MySQLDatabaseType()));
- List<String> keyList = Lists.newArrayList(schemaContextsMap.keySet());
- for (int i = 0; i < keyList.size(); i++) {
- assertThat(keyList.get(i), is(ProxyContext.getInstance().getAllSchemaNames().get(i)));
- }
+ schemaContexts.set(ProxyContext.getInstance(), new StandardSchemaContexts(schemaContextsMap, new Authentication(), new ConfigurationProperties(new Properties()), new MySQLDatabaseType()));
+ assertThat(new LinkedHashSet<>(ProxyContext.getInstance().getAllSchemaNames()), is(schemaContextsMap.keySet()));
}
- private Map<String, SchemaContext> getSchemaContextMap() {
- Map<String, SchemaContext> result = new HashMap<>(10);
+ private Map<String, SchemaContext> createSchemaContextMap() {
+ Map<String, SchemaContext> result = new LinkedHashMap<>(10, 1);
for (int i = 0; i < 10; i++) {
result.put(String.format(SCHEMA_PATTERN, i), mock(SchemaContext.class));
}
@@ -143,5 +137,4 @@ public final class ProxyContextTest {
when(schemaContext.getRuntimeContext()).thenReturn(runtimeContext);
return Collections.singletonMap("schema", schemaContext);
}
-
}