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 2026/03/19 02:51:28 UTC
(shardingsphere) branch master updated: Add more test cases on CatalogSwitchableDataSourceTest (#38502)
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 e7b1f72d8b0 Add more test cases on CatalogSwitchableDataSourceTest (#38502)
e7b1f72d8b0 is described below
commit e7b1f72d8b08cd4eb984e3b082b9756e3d335e74
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Thu Mar 19 10:51:22 2026 +0800
Add more test cases on CatalogSwitchableDataSourceTest (#38502)
---
.../pool/CatalogSwitchableDataSourceTest.java | 112 +++++++++++++++++----
1 file changed, 93 insertions(+), 19 deletions(-)
diff --git a/infra/data-source-pool/core/src/test/java/org/apache/shardingsphere/infra/datasource/pool/CatalogSwitchableDataSourceTest.java b/infra/data-source-pool/core/src/test/java/org/apache/shardingsphere/infra/datasource/pool/CatalogSwitchableDataSourceTest.java
index 9af0113db68..09df2da8cc7 100644
--- a/infra/data-source-pool/core/src/test/java/org/apache/shardingsphere/infra/datasource/pool/CatalogSwitchableDataSourceTest.java
+++ b/infra/data-source-pool/core/src/test/java/org/apache/shardingsphere/infra/datasource/pool/CatalogSwitchableDataSourceTest.java
@@ -19,17 +19,31 @@ package org.apache.shardingsphere.infra.datasource.pool;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import javax.sql.DataSource;
+import java.io.PrintWriter;
+import java.io.StringWriter;
import java.sql.Connection;
import java.sql.SQLException;
+import java.util.stream.Stream;
+import java.util.logging.Logger;
-import static org.mockito.Mockito.never;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.withSettings;
+@SuppressWarnings("resource")
@ExtendWith(MockitoExtension.class)
class CatalogSwitchableDataSourceTest {
@@ -39,31 +53,91 @@ class CatalogSwitchableDataSourceTest {
@Mock
private Connection connection;
- @Test
- void assertGetConnectionWhenCatalogIsNull() throws SQLException {
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("getConnectionArguments")
+ void assertGetConnection(final String name, final String catalog, final String currentCatalog, final int expectedGetCatalogCount, final int expectedSetCatalogCount) throws SQLException {
when(dataSource.getConnection()).thenReturn(connection);
- CatalogSwitchableDataSource catalogSwitchableDataSource = new CatalogSwitchableDataSource(dataSource, null, "jdbc:mysql://localhost:3306/db");
- catalogSwitchableDataSource.getConnection();
- verify(connection, never()).setCatalog(null);
+ if (0 < expectedGetCatalogCount) {
+ when(connection.getCatalog()).thenReturn(currentCatalog);
+ }
+ Connection actualConnection = new CatalogSwitchableDataSource(dataSource, catalog, "jdbc:mysql://localhost:3306/db").getConnection();
+ assertThat(actualConnection, is(connection));
+ verify(connection, times(expectedSetCatalogCount)).setCatalog(catalog);
}
+ @SuppressWarnings("JDBCResourceOpenedButNotSafelyClosed")
@Test
- void assertGetConnectionWhenCatalogIsSame() throws SQLException {
- String catalog = "db";
+ void assertGetConnectionWithCredentials() throws SQLException {
when(dataSource.getConnection()).thenReturn(connection);
- when(connection.getCatalog()).thenReturn(catalog);
- CatalogSwitchableDataSource catalogSwitchableDataSource = new CatalogSwitchableDataSource(dataSource, catalog, "jdbc:mysql://localhost:3306/db");
- catalogSwitchableDataSource.getConnection();
- verify(connection, never()).setCatalog(catalog);
+ when(connection.getCatalog()).thenReturn("other_db");
+ Connection actualConnection = new CatalogSwitchableDataSource(dataSource, "db", "jdbc:mysql://localhost:3306/db").getConnection("foo_user", "foo_password");
+ assertThat(actualConnection, is(connection));
+ verify(dataSource).getConnection();
+ verify(connection).setCatalog("db");
}
@Test
- void assertGetConnectionWhenCatalogIsDifferent() throws SQLException {
- String catalog = "db";
- when(dataSource.getConnection()).thenReturn(connection);
- when(connection.getCatalog()).thenReturn("other_db");
- CatalogSwitchableDataSource catalogSwitchableDataSource = new CatalogSwitchableDataSource(dataSource, catalog, "jdbc:mysql://localhost:3306/db");
- catalogSwitchableDataSource.getConnection();
- verify(connection).setCatalog(catalog);
+ void assertGetLogWriter() throws SQLException {
+ PrintWriter expectedLogWriter = new PrintWriter(new StringWriter());
+ when(dataSource.getLogWriter()).thenReturn(expectedLogWriter);
+ assertThat(new CatalogSwitchableDataSource(dataSource, "db", "jdbc:mysql://localhost:3306/db").getLogWriter(), is(expectedLogWriter));
+ }
+
+ @Test
+ void assertSetLogWriter() throws SQLException {
+ PrintWriter logWriter = new PrintWriter(new StringWriter());
+ new CatalogSwitchableDataSource(dataSource, "db", "jdbc:mysql://localhost:3306/db").setLogWriter(logWriter);
+ verify(dataSource).setLogWriter(logWriter);
+ }
+
+ @Test
+ void assertSetLoginTimeout() throws SQLException {
+ new CatalogSwitchableDataSource(dataSource, "db", "jdbc:mysql://localhost:3306/db").setLoginTimeout(30);
+ verify(dataSource).setLoginTimeout(30);
+ }
+
+ @Test
+ void assertGetLoginTimeout() throws SQLException {
+ when(dataSource.getLoginTimeout()).thenReturn(30);
+ assertThat(new CatalogSwitchableDataSource(dataSource, "db", "jdbc:mysql://localhost:3306/db").getLoginTimeout(), is(30));
+ }
+
+ @Test
+ void assertGetParentLogger() throws SQLException {
+ Logger expectedLogger = Logger.getLogger("foo_logger");
+ when(dataSource.getParentLogger()).thenReturn(expectedLogger);
+ assertThat(new CatalogSwitchableDataSource(dataSource, "db", "jdbc:mysql://localhost:3306/db").getParentLogger(), is(expectedLogger));
+ }
+
+ @Test
+ void assertUnwrap() throws SQLException {
+ when(dataSource.unwrap(DataSource.class)).thenReturn(dataSource);
+ assertThat(new CatalogSwitchableDataSource(dataSource, "db", "jdbc:mysql://localhost:3306/db").unwrap(DataSource.class), is(dataSource));
+ }
+
+ @Test
+ void assertIsWrapperFor() throws SQLException {
+ when(dataSource.isWrapperFor(DataSource.class)).thenReturn(true);
+ assertTrue(new CatalogSwitchableDataSource(dataSource, "db", "jdbc:mysql://localhost:3306/db").isWrapperFor(DataSource.class));
+ }
+
+ @Test
+ void assertCloseWhenDataSourceIsAutoCloseable() throws Exception {
+ DataSource autoCloseableDataSource = mock(DataSource.class, withSettings().extraInterfaces(AutoCloseable.class));
+ new CatalogSwitchableDataSource(autoCloseableDataSource, "db", "jdbc:mysql://localhost:3306/db").close();
+ verify((AutoCloseable) autoCloseableDataSource).close();
+ }
+
+ @Test
+ void assertCloseWhenDataSourceIsNotAutoCloseable() throws Exception {
+ new CatalogSwitchableDataSource(dataSource, "db", "jdbc:mysql://localhost:3306/db").close();
+ verifyNoInteractions(dataSource);
+ }
+
+ private static Stream<Arguments> getConnectionArguments() {
+ return Stream.of(
+ Arguments.of("catalog is null", null, "other_db", 0, 0),
+ Arguments.of("catalog matches current catalog", "db", "db", 1, 0),
+ Arguments.of("catalog differs from current catalog", "db", "other_db", 1, 1));
}
}