Delegation support for JDBC 4.3 ConnectionBuilder and ShardingKeyBuilder

Also moves ShardingKeyProvider to datasource package and declares getSuperShardingKey as default method.

Closes gh-31795
See gh-31506
This commit is contained in:
Juergen Hoeller
2023-12-08 23:52:22 +01:00
parent e4e2224449
commit 69bc4e2828
7 changed files with 208 additions and 151 deletions

View File

@@ -1,3 +1,19 @@
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.jdbc.datasource;
import java.sql.Connection;
@@ -10,41 +26,58 @@ import javax.sql.DataSource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.ShardingKeyProvider;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.BDDMockito.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.RETURNS_DEEP_STUBS;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.mock;
import static org.mockito.BDDMockito.when;
/**
* Tests for {@link ShardingKeyDataSourceAdapter}.
*
* @author Mohamed Lahyane (Anir)
* @author Juergen Hoeller
* @since 6.1.2
*/
public class ShardingKeyDataSourceAdapterTests {
private final Connection connection = mock();
private final Connection shardConnection = mock();
private final DataSource dataSource = mock();
private final ConnectionBuilder connectionBuilder = mock(ConnectionBuilder.class, RETURNS_DEEP_STUBS);
private final ConnectionBuilder shardConnectionBuilder = mock(ConnectionBuilder.class, RETURNS_DEEP_STUBS);
private final ShardingKey shardingKey = mock();
private final ShardingKey superShardingKey = mock();
private final ShardingKeyProvider shardingKeyProvider = new ShardingKeyProvider() {
@Override
public ShardingKey getShardingKey() throws SQLException {
return shardingKey;
}
@Override
public ShardingKey getSuperShardingKey() throws SQLException {
return superShardingKey;
}
};
@BeforeEach
public void setUp() throws SQLException {
void setup() throws SQLException {
given(dataSource.createConnectionBuilder()).willReturn(connectionBuilder);
when(connectionBuilder.shardingKey(null).superShardingKey(null)).thenReturn(connectionBuilder);
when(connectionBuilder.shardingKey(shardingKey).superShardingKey(superShardingKey))
.thenReturn(shardConnectionBuilder);
}
@Test
public void testGetConnectionNoKeyProvider() throws SQLException {
void getConnectionNoKeyProvider() throws SQLException {
ShardingKeyDataSourceAdapter dataSourceAdapter = new ShardingKeyDataSourceAdapter(dataSource);
when(connectionBuilder.build()).thenReturn(connection);
@@ -53,11 +86,9 @@ public class ShardingKeyDataSourceAdapterTests {
}
@Test
public void testGetConnectionWithKeyProvider() throws SQLException {
ShardingKeyDataSourceAdapter dataSourceAdapter = new ShardingKeyDataSourceAdapter(
dataSource,
shardingKeyProvider);
void getConnectionWithKeyProvider() throws SQLException {
ShardingKeyDataSourceAdapter dataSourceAdapter =
new ShardingKeyDataSourceAdapter(dataSource, shardingKeyProvider);
when(shardConnectionBuilder.build()).thenReturn(shardConnection);
@@ -65,33 +96,28 @@ public class ShardingKeyDataSourceAdapterTests {
}
@Test
public void testGetConnectionWithCredentialsNoKeyProvider() throws SQLException {
void getConnectionWithCredentialsNoKeyProvider() throws SQLException {
ShardingKeyDataSourceAdapter dataSourceAdapter = new ShardingKeyDataSourceAdapter(dataSource);
String username = "Anir";
String password = "spring";
Connection userConnection = mock();
when(connectionBuilder.user(username).password(password).build()).thenReturn(connection);
when(connectionBuilder.user(username).password(password).build()).thenReturn(userConnection);
assertThat(dataSourceAdapter.getConnection(username, password)).isEqualTo(userConnection);
assertThat(dataSourceAdapter.getConnection(username, password)).isEqualTo(connection);
}
@Test
public void testGetConnectionWithCredentialsAndKeyProvider() throws SQLException {
ShardingKeyDataSourceAdapter dataSourceAdapter = new ShardingKeyDataSourceAdapter(
dataSource,
shardingKeyProvider);
void getConnectionWithCredentialsAndKeyProvider() throws SQLException {
ShardingKeyDataSourceAdapter dataSourceAdapter =
new ShardingKeyDataSourceAdapter(dataSource, shardingKeyProvider);
String username = "mbekraou";
String password = "jdbc";
Connection userWithKeyProviderConnection = mock();
when(shardConnectionBuilder.user(username).password(password).build()).thenReturn(connection);
when(shardConnectionBuilder.user(username).password(password).build())
.thenReturn(userWithKeyProviderConnection);
assertThat(dataSourceAdapter.getConnection(username, password)).isEqualTo(userWithKeyProviderConnection);
assertThat(dataSourceAdapter.getConnection(username, password)).isEqualTo(connection);
}
}
}