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/08/04 09:37:46 UTC

[shardingsphere] branch master updated: Add test case for ProxySchemaContexts.getDataSourceSample (#6610)

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

zhangliang 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 0e734bc  Add test case for ProxySchemaContexts.getDataSourceSample (#6610)
0e734bc is described below

commit 0e734bc4d217097349f10739cb43b33a3684e288
Author: Andrew <41...@users.noreply.github.com>
AuthorDate: Tue Aug 4 17:37:33 2020 +0800

    Add test case for ProxySchemaContexts.getDataSourceSample (#6610)
    
    * Add test case for ProxySchemaContexts.getDataSourceSample
    
    * Remove useless blank line
---
 .../backend/schema/ProxySchemaContextsTest.java    | 75 ++++++++++++++++++++++
 1 file changed, 75 insertions(+)

diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/schema/ProxySchemaContextsTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/schema/ProxySchemaContextsTest.java
new file mode 100644
index 0000000..bd82fc1
--- /dev/null
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/schema/ProxySchemaContextsTest.java
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.proxy.backend.schema;
+
+import org.apache.shardingsphere.infra.auth.Authentication;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
+import org.apache.shardingsphere.kernel.context.SchemaContext;
+import org.apache.shardingsphere.kernel.context.StandardSchemaContexts;
+import org.apache.shardingsphere.kernel.context.runtime.RuntimeContext;
+import org.apache.shardingsphere.kernel.context.schema.ShardingSphereSchema;
+import org.apache.shardingsphere.proxy.backend.communication.jdbc.datasource.MockDataSource;
+import org.junit.Before;
+import org.junit.Test;
+
+import javax.sql.DataSource;
+import java.lang.reflect.Field;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class ProxySchemaContextsTest {
+    
+    private Map<String, DataSource> mockDataSourceMap;
+    
+    @Before
+    public void setUp() {
+        mockDataSourceMap = new HashMap<>(2, 1);
+        mockDataSourceMap.put("ds_1", new MockDataSource());
+        mockDataSourceMap.put("ds_2", new MockDataSource());
+    }
+    
+    @Test
+    public void assertGetDataSourceSample() throws NoSuchFieldException, IllegalAccessException {
+        assertThat(ProxySchemaContexts.getInstance().getDataSourceSample(), is(Optional.empty()));
+        Field schemaContexts = ProxySchemaContexts.getInstance().getClass().getDeclaredField("schemaContexts");
+        schemaContexts.setAccessible(true);
+        schemaContexts.set(ProxySchemaContexts.getInstance(), new StandardSchemaContexts(getSchemaContextMap(), new Authentication(), new ConfigurationProperties(new Properties())));
+        Optional<DataSource> actual = ProxySchemaContexts.getInstance().getDataSourceSample();
+        assertThat(actual, is(Optional.of(mockDataSourceMap.entrySet().iterator().next().getValue())));
+        
+    }
+    
+    private Map<String, SchemaContext> getSchemaContextMap() {
+        SchemaContext schemaContext = mock(SchemaContext.class);
+        ShardingSphereSchema shardingSphereSchema = mock(ShardingSphereSchema.class);
+        RuntimeContext runtimeContext = mock(RuntimeContext.class);
+        when(shardingSphereSchema.getDataSources()).thenReturn(mockDataSourceMap);
+        when(schemaContext.getName()).thenReturn("schema");
+        when(schemaContext.getSchema()).thenReturn(shardingSphereSchema);
+        when(schemaContext.getRuntimeContext()).thenReturn(runtimeContext);
+        return Collections.singletonMap("schema", schemaContext);
+    }
+}