You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by to...@apache.org on 2022/04/22 18:20:29 UTC

[shardingsphere] branch master updated: Refactor MasterSlaveDatabaseDiscoveryTypeTest (#17026)

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

totalo 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 d0699d7e913 Refactor MasterSlaveDatabaseDiscoveryTypeTest (#17026)
d0699d7e913 is described below

commit d0699d7e9132a4cf71423daf25deb1bbb9dd5ef3
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat Apr 23 02:20:23 2022 +0800

    Refactor MasterSlaveDatabaseDiscoveryTypeTest (#17026)
    
    * Rename MasterSlaveStatusDatabaseDiscoveryType
    
    * Rename MasterSlaveDatabaseDiscoveryType
    
    * Update docs
    
    * Move package
    
    * Refactor MasterSlaveDatabaseDiscoveryTypeTest
    
    * Refactor MasterSlaveDatabaseDiscoveryTypeTest
---
 .../MasterSlaveDatabaseDiscoveryTypeTest.java      | 30 ++++++++--------------
 1 file changed, 10 insertions(+), 20 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/masterslave/MasterSlaveDatabaseDiscoveryTypeTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/masterslave/MasterSlaveDatabaseDiscoveryTypeTest.java
index ada90a0dcac..abaef92a37e 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/masterslave/MasterSlaveDatabaseDiscoveryTypeTest.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/masterslave/MasterSlaveDatabaseDiscoveryTypeTest.java
@@ -20,11 +20,8 @@ package org.apache.shardingsphere.dbdiscovery.mysql.type.masterslave;
 import org.junit.Test;
 
 import javax.sql.DataSource;
-import java.sql.Connection;
-import java.sql.DatabaseMetaData;
 import java.sql.ResultSet;
 import java.sql.SQLException;
-import java.sql.Statement;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Optional;
@@ -32,6 +29,7 @@ import java.util.Optional;
 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;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
@@ -39,36 +37,28 @@ public final class MasterSlaveDatabaseDiscoveryTypeTest {
     
     @Test
     public void assertLoadHighlyAvailableStatus() throws SQLException {
-        MasterSlaveHighlyAvailableStatus actual = new MasterSlaveDatabaseDiscoveryType().loadHighlyAvailableStatus(getDataSource(true, 3306));
+        MasterSlaveHighlyAvailableStatus actual = new MasterSlaveDatabaseDiscoveryType().loadHighlyAvailableStatus(mockDataSource(3306));
         assertThat(actual.getPrimaryInstanceURL(), is("127.0.0.1:3306"));
     }
     
     @Test
     public void assertDeterminePrimaryDataSource() throws SQLException {
         Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1);
-        dataSourceMap.put("ds_0", getDataSource(false, 3306));
-        dataSourceMap.put("ds_1", getDataSource(true, 3307));
+        dataSourceMap.put("ds_0", mockDataSource(3306));
+        dataSourceMap.put("ds_1", mockDataSource(3307));
         Optional<String> actual = new MasterSlaveDatabaseDiscoveryType().determinePrimaryDataSource(dataSourceMap);
         assertTrue(actual.isPresent());
         assertThat(actual.get(), is("ds_0"));
     }
     
-    private DataSource getDataSource(final boolean slave, final int port) throws SQLException {
-        DataSource result = mock(DataSource.class);
-        Connection connection = mock(Connection.class);
-        when(result.getConnection()).thenReturn(connection);
-        Statement statement = mock(Statement.class);
-        when(connection.createStatement()).thenReturn(statement);
+    private DataSource mockDataSource(final int port) throws SQLException {
+        DataSource result = mock(DataSource.class, RETURNS_DEEP_STUBS);
         ResultSet resultSet = mock(ResultSet.class);
-        when(statement.executeQuery("SHOW SLAVE STATUS")).thenReturn(resultSet);
+        when(result.getConnection().createStatement().executeQuery("SHOW SLAVE STATUS")).thenReturn(resultSet);
         when(resultSet.next()).thenReturn(true, false);
-        if (slave) {
-            when(resultSet.getString("Master_Host")).thenReturn("127.0.0.1");
-            when(resultSet.getString("Master_Port")).thenReturn(Integer.toString(3306));
-        }
-        DatabaseMetaData databaseMetaData = mock(DatabaseMetaData.class);
-        when(connection.getMetaData()).thenReturn(databaseMetaData);
-        when(databaseMetaData.getURL()).thenReturn("jdbc:mysql://127.0.0.1:" + port + "/test?serverTimezone=UTC&useSSL=false");
+        when(resultSet.getString("Master_Host")).thenReturn("127.0.0.1");
+        when(resultSet.getString("Master_Port")).thenReturn(Integer.toString(3306));
+        when(result.getConnection().getMetaData().getURL()).thenReturn(String.format("jdbc:mysql://127.0.0.1:%s/test?serverTimezone=UTC&useSSL=false", port));
         return result;
     }
 }