Skip to content

Commit

Permalink
Refactor Fix code inspection for jdbc module (#31208)
Browse files Browse the repository at this point in the history
* Refactor Fix code inspection for jdbc module

* Refactor Fix code inspection for authority module
  • Loading branch information
terrymanu authored May 11, 2024
1 parent ed211e7 commit 78454cc
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public final int getUpdateCount() throws SQLException {
}

private int accumulate() throws SQLException {
long result = 0;
long result = 0L;
boolean hasResult = false;
for (Statement each : getRoutedStatements()) {
int updateCount = each.getUpdateCount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private Map<String, DataSource> getTrafficDataSourceMap(final String databaseNam

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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static Map<String, Integer> createColumnLabelAndIndexMap(final SQLStateme
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,32 +117,32 @@ public int getInt(final String columnLabel) {

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Properties;

Expand All @@ -43,19 +43,19 @@ void assertCreateDataSourceWithModeConfiguration() throws SQLException {

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,15 @@ void assertGetBigDecimalColumnLabelWithScale() throws SQLException {
@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void assertNewConstructorWithAllArguments() throws Exception {
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,
Expand Down Expand Up @@ -150,8 +150,8 @@ private HikariDataSource createHikariDataSource() {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void assertSetNClobForReader() {

@Test
void assertSetNClobForReaderAndLength() {
assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSpherePreparedStatement.setNClob(1, new StringReader(""), 1));
assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSpherePreparedStatement.setNClob(1, new StringReader(""), 1L));
}

@Test
Expand All @@ -96,7 +96,7 @@ void assertSetNCharacterStream() {

@Test
void assertSetNCharacterStreamWithLength() {
assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSpherePreparedStatement.setNCharacterStream(1, new StringReader(""), 1));
assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSpherePreparedStatement.setNCharacterStream(1, new StringReader(""), 1L));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,12 @@ void assertUpdateNCharacterStreamForColumnLabel() {

@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
Expand Down Expand Up @@ -363,12 +363,12 @@ void assertUpdateBlobForColumnLabelWithInputStream() {

@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
Expand All @@ -393,12 +393,12 @@ void assertUpdateClobForColumnLabelWithInputStream() {

@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
Expand All @@ -423,12 +423,12 @@ void assertUpdateNClobForColumnLabelWithInputStream() {

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

import java.sql.Connection;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;

Expand Down Expand Up @@ -71,11 +71,9 @@ void setUp() {
}

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,18 @@

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"})
Expand All @@ -59,12 +57,8 @@ private ContextManager mockContextManager() {
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);
}
}

0 comments on commit 78454cc

Please sign in to comment.