You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by yx...@apache.org on 2024/05/11 15:41:01 UTC
(shardingsphere) branch master updated: Refactor Fix code inspection for jdbc module (#31208)
This is an automated email from the ASF dual-hosted git repository.
yx9o 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 78454cc18c5 Refactor Fix code inspection for jdbc module (#31208)
78454cc18c5 is described below
commit 78454cc18c547c31ee7daaa6dc999ddf1338a34a
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat May 11 23:40:55 2024 +0800
Refactor Fix code inspection for jdbc module (#31208)
* Refactor Fix code inspection for jdbc module
* Refactor Fix code inspection for authority module
---
.../driver/jdbc/adapter/AbstractStatementAdapter.java | 2 +-
.../connection/DriverDatabaseConnectionManager.java | 2 +-
.../core/resultset/ShardingSphereResultSetUtils.java | 2 +-
.../circuit/resultset/CircuitBreakerResultSet.java | 12 ++++++------
.../api/ShardingSphereDataSourceFactoryTest.java | 8 ++++----
.../jdbc/adapter/ResultSetGetterAdapterTest.java | 8 ++++----
.../core/datasource/ShardingSphereDataSourceTest.java | 6 +++---
.../UnsupportedOperationPreparedStatementTest.java | 4 ++--
.../UnsupportedUpdateOperationResultSetTest.java | 16 ++++++++--------
.../driver/state/DriverStateContextTest.java | 6 ++----
.../database/DatabasePermittedPrivilegeProvider.java | 2 +-
.../update/AlterSQLTranslatorRuleExecutorTest.java | 18 ++++++------------
12 files changed, 39 insertions(+), 47 deletions(-)
diff --git a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractStatementAdapter.java b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractStatementAdapter.java
index cdfae0683ef..3b09e073bbd 100644
--- a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractStatementAdapter.java
+++ b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractStatementAdapter.java
@@ -171,7 +171,7 @@ public abstract class AbstractStatementAdapter extends AbstractUnsupportedOperat
}
private int accumulate() throws SQLException {
- long result = 0;
+ long result = 0L;
boolean hasResult = false;
for (Statement each : getRoutedStatements()) {
int updateCount = each.getUpdateCount();
diff --git a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java
index 1989b586f84..ee3ed656142 100644
--- a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java
+++ b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java
@@ -122,7 +122,7 @@ public final class DriverDatabaseConnectionManager implements OnlineDatabaseConn
private Map<String, DataSourcePoolProperties> createDataSourcePoolPropertiesMap(final Collection<InstanceMetaData> instances, final Collection<ShardingSphereUser> users,
final DataSourcePoolProperties propsSample, final String schema) {
- Map<String, DataSourcePoolProperties> result = new LinkedHashMap<>();
+ Map<String, DataSourcePoolProperties> result = new LinkedHashMap<>(instances.size(), 1F);
for (InstanceMetaData each : instances) {
result.put(each.getId(), createDataSourcePoolProperties((ProxyInstanceMetaData) each, users, propsSample, schema));
}
diff --git a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ShardingSphereResultSetUtils.java b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ShardingSphereResultSetUtils.java
index fa5a3989604..f7fb31dd922 100644
--- a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ShardingSphereResultSetUtils.java
+++ b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ShardingSphereResultSetUtils.java
@@ -50,7 +50,7 @@ public final class ShardingSphereResultSetUtils {
if (selectContainsEnhancedTable && hasSelectExpandProjections(sqlStatementContext)) {
return createColumnLabelAndIndexMapWithExpandProjections((SelectStatementContext) sqlStatementContext);
}
- Map<String, Integer> result = new CaseInsensitiveMap<>(resultSetMetaData.getColumnCount(), 1);
+ Map<String, Integer> result = new CaseInsensitiveMap<>(resultSetMetaData.getColumnCount(), 1F);
for (int columnIndex = resultSetMetaData.getColumnCount(); columnIndex > 0; columnIndex--) {
result.put(resultSetMetaData.getColumnLabel(columnIndex), columnIndex);
}
diff --git a/jdbc/src/main/java/org/apache/shardingsphere/driver/state/circuit/resultset/CircuitBreakerResultSet.java b/jdbc/src/main/java/org/apache/shardingsphere/driver/state/circuit/resultset/CircuitBreakerResultSet.java
index baa7819fac5..679d6500103 100644
--- a/jdbc/src/main/java/org/apache/shardingsphere/driver/state/circuit/resultset/CircuitBreakerResultSet.java
+++ b/jdbc/src/main/java/org/apache/shardingsphere/driver/state/circuit/resultset/CircuitBreakerResultSet.java
@@ -117,32 +117,32 @@ public final class CircuitBreakerResultSet extends AbstractUnsupportedOperationR
@Override
public long getLong(final int columnIndex) {
- return 0;
+ return 0L;
}
@Override
public long getLong(final String columnLabel) {
- return 0;
+ return 0L;
}
@Override
public float getFloat(final int columnIndex) {
- return 0;
+ return 0F;
}
@Override
public float getFloat(final String columnLabel) {
- return 0;
+ return 0F;
}
@Override
public double getDouble(final int columnIndex) {
- return 0;
+ return 0D;
}
@Override
public double getDouble(final String columnLabel) {
- return 0;
+ return 0D;
}
@Override
diff --git a/jdbc/src/test/java/org/apache/shardingsphere/driver/api/ShardingSphereDataSourceFactoryTest.java b/jdbc/src/test/java/org/apache/shardingsphere/driver/api/ShardingSphereDataSourceFactoryTest.java
index c1f3e2aeb95..64b1d82f54b 100644
--- a/jdbc/src/test/java/org/apache/shardingsphere/driver/api/ShardingSphereDataSourceFactoryTest.java
+++ b/jdbc/src/test/java/org/apache/shardingsphere/driver/api/ShardingSphereDataSourceFactoryTest.java
@@ -27,7 +27,7 @@ import org.mockito.internal.configuration.plugins.Plugins;
import javax.sql.DataSource;
import java.sql.SQLException;
-import java.util.HashMap;
+import java.util.Collections;
import java.util.LinkedList;
import java.util.Properties;
@@ -43,19 +43,19 @@ class ShardingSphereDataSourceFactoryTest {
@Test
void assertCreateDataSourceWithDatabaseNameAndModeConfiguration() throws SQLException {
- assertDataSource(ShardingSphereDataSourceFactory.createDataSource("test_db", new ModeConfiguration("Standalone", null), new HashMap<>(), null, null), "test_db");
+ assertDataSource(ShardingSphereDataSourceFactory.createDataSource("test_db", new ModeConfiguration("Standalone", null), Collections.emptyMap(), null, null), "test_db");
}
@Test
void assertCreateDataSourceWithAllParametersForMultipleDataSourcesWithDefaultDatabaseName() throws SQLException {
assertDataSource(ShardingSphereDataSourceFactory.createDataSource(
- new ModeConfiguration("Standalone", null), new HashMap<>(), new LinkedList<>(), new Properties()), DefaultDatabase.LOGIC_NAME);
+ new ModeConfiguration("Standalone", null), Collections.emptyMap(), new LinkedList<>(), new Properties()), DefaultDatabase.LOGIC_NAME);
}
@Test
void assertCreateDataSourceWithAllParametersForMultipleDataSources() throws SQLException {
assertDataSource(ShardingSphereDataSourceFactory.createDataSource(
- "test_db", new ModeConfiguration("Standalone", null), new HashMap<>(), new LinkedList<>(), new Properties()), "test_db");
+ "test_db", new ModeConfiguration("Standalone", null), Collections.emptyMap(), new LinkedList<>(), new Properties()), "test_db");
}
@Test
diff --git a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/ResultSetGetterAdapterTest.java b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/ResultSetGetterAdapterTest.java
index 094887aa125..1a71303be3a 100644
--- a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/ResultSetGetterAdapterTest.java
+++ b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/ResultSetGetterAdapterTest.java
@@ -191,15 +191,15 @@ class ResultSetGetterAdapterTest {
@Test
void assertGetBytesForColumnIndex() throws SQLException {
MergedResult mergedResult = mock(MergedResult.class);
- when(mergedResult.getValue(1, byte[].class)).thenReturn(new byte[]{1});
- assertThat(mockShardingSphereResultSet(mergedResult).getBytes(1), is(new byte[]{1}));
+ when(mergedResult.getValue(1, byte[].class)).thenReturn(new byte[]{(byte) 1});
+ assertThat(mockShardingSphereResultSet(mergedResult).getBytes(1), is(new byte[]{(byte) 1}));
}
@Test
void assertGetBytesForColumnLabel() throws SQLException {
MergedResult mergedResult = mock(MergedResult.class);
- when(mergedResult.getValue(1, byte[].class)).thenReturn(new byte[]{1});
- assertThat(mockShardingSphereResultSet(mergedResult).getBytes("col"), is(new byte[]{1}));
+ when(mergedResult.getValue(1, byte[].class)).thenReturn(new byte[]{(byte) 1});
+ assertThat(mockShardingSphereResultSet(mergedResult).getBytes("col"), is(new byte[]{(byte) 1}));
}
@Test
diff --git a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSourceTest.java b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSourceTest.java
index cecee9d459c..1567a6c8233 100644
--- a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSourceTest.java
+++ b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSourceTest.java
@@ -81,7 +81,7 @@ class ShardingSphereDataSourceTest {
void assertRemoveGlobalRuleConfiguration() throws Exception {
Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS);
when(connection.getMetaData().getURL()).thenReturn("jdbc:mock://127.0.0.1/foo_ds");
- CacheOption cacheOption = new CacheOption(1024, 1024);
+ CacheOption cacheOption = new CacheOption(1024, 1024L);
SQLParserRuleConfiguration sqlParserRuleConfig = new SQLParserRuleConfiguration(cacheOption, cacheOption);
try (
ShardingSphereDataSource actual = new ShardingSphereDataSource(DefaultDatabase.LOGIC_NAME,
@@ -150,8 +150,8 @@ class ShardingSphereDataSourceTest {
result.setPassword("root");
result.setMaximumPoolSize(10);
result.setMinimumIdle(2);
- result.setConnectionTimeout(15 * 1000L);
- result.setIdleTimeout(40 * 1000L);
+ result.setConnectionTimeout(15L * 1000L);
+ result.setIdleTimeout(40L * 1000L);
return result;
}
}
diff --git a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationPreparedStatementTest.java b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationPreparedStatementTest.java
index 7cfcd73fa2d..942585e6359 100644
--- a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationPreparedStatementTest.java
+++ b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationPreparedStatementTest.java
@@ -86,7 +86,7 @@ class UnsupportedOperationPreparedStatementTest {
@Test
void assertSetNClobForReaderAndLength() {
- assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSpherePreparedStatement.setNClob(1, new StringReader(""), 1));
+ assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSpherePreparedStatement.setNClob(1, new StringReader(""), 1L));
}
@Test
@@ -96,7 +96,7 @@ class UnsupportedOperationPreparedStatementTest {
@Test
void assertSetNCharacterStreamWithLength() {
- assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSpherePreparedStatement.setNCharacterStream(1, new StringReader(""), 1));
+ assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSpherePreparedStatement.setNCharacterStream(1, new StringReader(""), 1L));
}
@Test
diff --git a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedUpdateOperationResultSetTest.java b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedUpdateOperationResultSetTest.java
index 8c573fa2f0e..b82fa5318be 100644
--- a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedUpdateOperationResultSetTest.java
+++ b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedUpdateOperationResultSetTest.java
@@ -303,12 +303,12 @@ class UnsupportedUpdateOperationResultSetTest {
@Test
void assertUpdateNCharacterStreamForColumnIndexWithLength() {
- assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSphereResultSet.updateNCharacterStream(1, new StringReader(""), 1));
+ assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSphereResultSet.updateNCharacterStream(1, new StringReader(""), 1L));
}
@Test
void assertUpdateNCharacterStreamForColumnLabelWithLength() {
- assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSphereResultSet.updateNCharacterStream("label", new StringReader(""), 1));
+ assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSphereResultSet.updateNCharacterStream("label", new StringReader(""), 1L));
}
@Test
@@ -363,12 +363,12 @@ class UnsupportedUpdateOperationResultSetTest {
@Test
void assertUpdateBlobForColumnIndexWithInputStreamAndLength() {
- assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSphereResultSet.updateBlob(1, System.in, 100));
+ assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSphereResultSet.updateBlob(1, System.in, 100L));
}
@Test
void assertUpdateBlobForColumnLabelWithInputStreamAndLength() {
- assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSphereResultSet.updateBlob("label", System.in, 100));
+ assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSphereResultSet.updateBlob("label", System.in, 100L));
}
@Test
@@ -393,12 +393,12 @@ class UnsupportedUpdateOperationResultSetTest {
@Test
void assertUpdateClobForColumnIndexWithInputStreamAndLength() {
- assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSphereResultSet.updateClob(1, new StringReader(""), 100));
+ assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSphereResultSet.updateClob(1, new StringReader(""), 100L));
}
@Test
void assertUpdateClobForColumnLabelWithInputStreamAndLength() {
- assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSphereResultSet.updateClob("label", new StringReader(""), 100));
+ assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSphereResultSet.updateClob("label", new StringReader(""), 100L));
}
@Test
@@ -423,12 +423,12 @@ class UnsupportedUpdateOperationResultSetTest {
@Test
void assertUpdateNClobForColumnIndexWithInputStreamAndLength() {
- assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSphereResultSet.updateNClob(1, new StringReader(""), 100));
+ assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSphereResultSet.updateNClob(1, new StringReader(""), 100L));
}
@Test
void assertUpdateNClobForColumnLabelWithInputStreamAndLength() {
- assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSphereResultSet.updateNClob("label", new StringReader(""), 100));
+ assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSphereResultSet.updateNClob("label", new StringReader(""), 100L));
}
@Test
diff --git a/jdbc/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java b/jdbc/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java
index 554873c3b6e..facf443929f 100644
--- a/jdbc/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java
+++ b/jdbc/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java
@@ -42,7 +42,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import java.sql.Connection;
import java.util.Arrays;
-import java.util.LinkedHashMap;
+import java.util.Collections;
import java.util.Map;
import java.util.Properties;
@@ -71,11 +71,9 @@ class DriverStateContextTest {
}
private Map<String, ShardingSphereDatabase> mockDatabases() {
- Map<String, ShardingSphereDatabase> result = new LinkedHashMap<>();
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, Answers.RETURNS_DEEP_STUBS);
when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE"));
- result.put(DefaultDatabase.LOGIC_NAME, database);
- return result;
+ return Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database);
}
@Test
diff --git a/kernel/authority/provider/database/src/main/java/org/apache/shardingsphere/authority/provider/database/DatabasePermittedPrivilegeProvider.java b/kernel/authority/provider/database/src/main/java/org/apache/shardingsphere/authority/provider/database/DatabasePermittedPrivilegeProvider.java
index c51f4b7a183..8128c4b04a5 100644
--- a/kernel/authority/provider/database/src/main/java/org/apache/shardingsphere/authority/provider/database/DatabasePermittedPrivilegeProvider.java
+++ b/kernel/authority/provider/database/src/main/java/org/apache/shardingsphere/authority/provider/database/DatabasePermittedPrivilegeProvider.java
@@ -45,7 +45,7 @@ public final class DatabasePermittedPrivilegeProvider implements PrivilegeProvid
@Override
public void init(final Properties props) {
- userDatabaseMappings = props.getProperty(DatabasePermittedPrivilegeProvider.USER_DATABASE_MAPPINGS_KEY, "");
+ userDatabaseMappings = props.getProperty(USER_DATABASE_MAPPINGS_KEY, "");
checkUserDatabaseMappings();
}
diff --git a/kernel/sql-translator/distsql/handler/src/test/java/org/apache/shardingsphere/sqltranslator/distsql/handler/update/AlterSQLTranslatorRuleExecutorTest.java b/kernel/sql-translator/distsql/handler/src/test/java/org/apache/shardingsphere/sqltranslator/distsql/handler/update/AlterSQLTranslatorRuleExecutorTest.java
index 91a947cdc36..58199c766ca 100644
--- a/kernel/sql-translator/distsql/handler/src/test/java/org/apache/shardingsphere/sqltranslator/distsql/handler/update/AlterSQLTranslatorRuleExecutorTest.java
+++ b/kernel/sql-translator/distsql/handler/src/test/java/org/apache/shardingsphere/sqltranslator/distsql/handler/update/AlterSQLTranslatorRuleExecutorTest.java
@@ -37,20 +37,18 @@ import static org.mockito.Mockito.when;
class AlterSQLTranslatorRuleExecutorTest {
- private DistSQLUpdateExecuteEngine engine;
-
@Test
void assertExecute() {
- AlterSQLTranslatorRuleStatement sqlStatement = new AlterSQLTranslatorRuleStatement(new AlgorithmSegment("Native", PropertiesBuilder.build(new Property("foo", "bar"))), Boolean.TRUE);
- engine = new DistSQLUpdateExecuteEngine(sqlStatement, null, mockContextManager());
- assertDoesNotThrow(() -> engine.executeUpdate());
+ AlterSQLTranslatorRuleStatement sqlStatement = new AlterSQLTranslatorRuleStatement(new AlgorithmSegment("Native", PropertiesBuilder.build(new Property("foo", "bar"))), true);
+ DistSQLUpdateExecuteEngine engine = new DistSQLUpdateExecuteEngine(sqlStatement, null, mockContextManager());
+ assertDoesNotThrow(engine::executeUpdate);
}
@Test
void assertExecuteWithNullOriginalSQLWhenTranslatingFailed() {
AlterSQLTranslatorRuleStatement sqlStatement = new AlterSQLTranslatorRuleStatement(new AlgorithmSegment("Native", PropertiesBuilder.build(new Property("foo", "bar"))), null);
- engine = new DistSQLUpdateExecuteEngine(sqlStatement, null, mockContextManager());
- assertDoesNotThrow(() -> engine.executeUpdate());
+ DistSQLUpdateExecuteEngine engine = new DistSQLUpdateExecuteEngine(sqlStatement, null, mockContextManager());
+ assertDoesNotThrow(engine::executeUpdate);
}
@SuppressWarnings({"rawtypes", "unchecked"})
@@ -59,12 +57,8 @@ class AlterSQLTranslatorRuleExecutorTest {
SQLTranslatorRule rule = mock(SQLTranslatorRule.class);
GlobalRuleDefinitionExecutor executor = mock(GlobalRuleDefinitionExecutor.class);
when(executor.getRuleClass()).thenReturn(SQLTranslatorRule.class);
- when(rule.getConfiguration()).thenReturn(createSQLTranslatorRuleConfiguration());
+ when(rule.getConfiguration()).thenReturn(new SQLTranslatorRuleConfiguration("NATIVE", new Properties(), true));
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(executor.getRuleClass())).thenReturn(rule);
return result;
}
-
- private SQLTranslatorRuleConfiguration createSQLTranslatorRuleConfiguration() {
- return new SQLTranslatorRuleConfiguration("NATIVE", new Properties(), true);
- }
}