You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by "sandynz (via GitHub)" <gi...@apache.org> on 2023/05/29 01:57:47 UTC

[GitHub] [shardingsphere] sandynz commented on a diff in pull request #25920: test added for JDBCContext

sandynz commented on code in PR #25920:
URL: https://github.com/apache/shardingsphere/pull/25920#discussion_r1208757768


##########
jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/context/JDBCContextTest.java:
##########
@@ -0,0 +1,53 @@
+package org.apache.shardingsphere.driver.jdbc.context;
+
+import org.apache.shardingsphere.driver.state.circuit.datasource.CircuitBreakerDataSource;
+import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.datasource.DataSourceChangedEvent;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import javax.sql.DataSource;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+
+@ExtendWith(MockitoExtension.class)
+class JDBCContextTest {
+
+    public static final String TEST_DB = "test_db";
+
+    @Mock
+    private DataSourceChangedEvent event;
+
+    @Test
+    void assertNullCachedDbMetadataWithEmptyDatasource() throws Exception {
+        JDBCContext jdbcContext = new JDBCContext(new HashMap<>());
+        assertThat(jdbcContext.getCachedDatabaseMetaData(), nullValue());
+    }
+
+    @Test
+    void assertNotNullCashedDbMetadataWith() throws SQLException {
+        Map<String, DataSource> dataSourceMap = getStringDataSourceMap();
+        JDBCContext jdbcContext = new JDBCContext(dataSourceMap);
+        assertThat(jdbcContext.getCachedDatabaseMetaData(), notNullValue());
+    }
+    @Test
+    void assetNullMetadataAfterRefreshingExisting() throws SQLException {
+        Map<String, DataSource> stringDataSourceMap = getStringDataSourceMap();
+        JDBCContext jdbcContext = new JDBCContext(stringDataSourceMap);
+        jdbcContext.refreshCachedDatabaseMetaData(event);
+        assertThat(jdbcContext.getCachedDatabaseMetaData(), nullValue());
+    }
+
+    private static Map<String, DataSource> getStringDataSourceMap() {
+        CircuitBreakerDataSource dataSource = new CircuitBreakerDataSource();
+        Map<String, DataSource> dataSourceMap = new HashMap<>();
+        dataSourceMap.put(TEST_DB, dataSource);
+        return dataSourceMap;

Review Comment:
   `dataSourceMap` name could be `result`



##########
jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/context/JDBCContextTest.java:
##########
@@ -0,0 +1,53 @@
+package org.apache.shardingsphere.driver.jdbc.context;
+
+import org.apache.shardingsphere.driver.state.circuit.datasource.CircuitBreakerDataSource;
+import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.datasource.DataSourceChangedEvent;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import javax.sql.DataSource;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+
+@ExtendWith(MockitoExtension.class)
+class JDBCContextTest {
+
+    public static final String TEST_DB = "test_db";
+
+    @Mock
+    private DataSourceChangedEvent event;
+
+    @Test
+    void assertNullCachedDbMetadataWithEmptyDatasource() throws Exception {
+        JDBCContext jdbcContext = new JDBCContext(new HashMap<>());
+        assertThat(jdbcContext.getCachedDatabaseMetaData(), nullValue());

Review Comment:
   1, `jdbcContext` name could be `actual`.
   
   2, assertThat nullValue, could be assertNull.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org