From 3d0d54f9f2ef2693471b72049b8db969663ff379 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Tue, 3 Dec 2024 15:29:55 +0800 Subject: [PATCH] Fix sonar issues --- .../time/MySQLTimestamp2BinlogProtocolValueTest.java | 6 +++--- .../time/MySQLTimestampBinlogProtocolValueTest.java | 2 +- .../mysql/payload/MySQLPacketPayloadTest.java | 12 ++++++------ .../SeataATShardingSphereTransactionManagerTest.java | 8 ++++---- .../backend/config/ProxyConfigurationLoaderTest.java | 12 ++++++------ .../swapper/YamlProxyConfigurationSwapperTest.java | 2 +- .../e2e/env/container/atomic/DockerITContainer.java | 2 +- .../ShardingSphereMultiProxyClusterContainer.java | 2 +- .../engine/scenario/EncryptSQLRewriterIT.java | 2 +- .../engine/scenario/ShardingSQLRewriterIT.java | 2 +- .../test/natived/jdbc/databases/MySQLTest.java | 2 +- .../test/natived/proxy/databases/MySQLTest.java | 4 ++-- 12 files changed, 28 insertions(+), 28 deletions(-) diff --git a/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/binlog/row/column/value/time/MySQLTimestamp2BinlogProtocolValueTest.java b/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/binlog/row/column/value/time/MySQLTimestamp2BinlogProtocolValueTest.java index 219849627de42..58f8457415a35 100644 --- a/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/binlog/row/column/value/time/MySQLTimestamp2BinlogProtocolValueTest.java +++ b/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/binlog/row/column/value/time/MySQLTimestamp2BinlogProtocolValueTest.java @@ -52,7 +52,7 @@ void setUp() { @Test void assertReadWithoutFraction() { - int currentSeconds = Long.valueOf(System.currentTimeMillis() / 1000L).intValue(); + int currentSeconds = (int) (System.currentTimeMillis() / 1000L); when(byteBuf.readInt()).thenReturn(currentSeconds); assertThat(new MySQLTimestamp2BinlogProtocolValue().read(columnDef, payload), is(new Timestamp(currentSeconds * 1000L))); } @@ -61,8 +61,8 @@ void assertReadWithoutFraction() { void assertReadWithFraction() { columnDef.setColumnMeta(1); long currentTimeMillis = 1678795614082L; - int currentSeconds = Long.valueOf(currentTimeMillis / 1000L).intValue(); - int currentMilliseconds = Long.valueOf(currentTimeMillis % 100L).intValue(); + int currentSeconds = (int) (System.currentTimeMillis() / 1000L); + int currentMilliseconds = (int) (currentTimeMillis % 100L); when(payload.readInt1()).thenReturn(currentMilliseconds); when(byteBuf.readInt()).thenReturn(currentSeconds); assertThat("currentTimeMillis:" + currentTimeMillis, new MySQLTimestamp2BinlogProtocolValue().read(columnDef, payload), is(new Timestamp(currentSeconds * 1000L + currentMilliseconds * 10L))); diff --git a/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/binlog/row/column/value/time/MySQLTimestampBinlogProtocolValueTest.java b/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/binlog/row/column/value/time/MySQLTimestampBinlogProtocolValueTest.java index 6f128c8d291ab..37b6737e2711a 100644 --- a/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/binlog/row/column/value/time/MySQLTimestampBinlogProtocolValueTest.java +++ b/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/binlog/row/column/value/time/MySQLTimestampBinlogProtocolValueTest.java @@ -42,7 +42,7 @@ class MySQLTimestampBinlogProtocolValueTest { @Test void assertRead() { - int currentSeconds = Long.valueOf(System.currentTimeMillis() / 1000L).intValue(); + int currentSeconds = (int) (System.currentTimeMillis() / 1000L); when(payload.readInt4()).thenReturn(currentSeconds); assertThat(new MySQLTimestampBinlogProtocolValue().read(columnDef, payload), is(DateTimeFormatterFactory.getStandardFormatter().format(new Timestamp(currentSeconds * 1000L).toLocalDateTime()))); diff --git a/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/payload/MySQLPacketPayloadTest.java b/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/payload/MySQLPacketPayloadTest.java index 753f0a531a4d9..346c67767b7c5 100644 --- a/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/payload/MySQLPacketPayloadTest.java +++ b/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/payload/MySQLPacketPayloadTest.java @@ -151,23 +151,23 @@ void assertWriteIntLenencWithOneByte() { @Test void assertWriteIntLenencWithTwoBytes() { - new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeIntLenenc(Double.valueOf(Math.pow(2D, 16D)).longValue() - 1L); + new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeIntLenenc((long) (Math.pow(2D, 16D)) - 1L); verify(byteBuf).writeByte(0xfc); - verify(byteBuf).writeShortLE(Double.valueOf(Math.pow(2D, 16D)).intValue() - 1); + verify(byteBuf).writeShortLE((int) (Math.pow(2D, 16D)) - 1); } @Test void assertWriteIntLenencWithThreeBytes() { - new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeIntLenenc(Double.valueOf(Math.pow(2D, 24D)).longValue() - 1L); + new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeIntLenenc((long) (Math.pow(2D, 24D)) - 1L); verify(byteBuf).writeByte(0xfd); - verify(byteBuf).writeMediumLE(Double.valueOf(Math.pow(2D, 24D)).intValue() - 1); + verify(byteBuf).writeMediumLE((int) (Math.pow(2D, 24D)) - 1); } @Test void assertWriteIntLenencWithFourBytes() { - new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeIntLenenc(Double.valueOf(Math.pow(2D, 25D)).longValue() - 1L); + new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeIntLenenc((long) (Math.pow(2D, 25D)) - 1L); verify(byteBuf).writeByte(0xfe); - verify(byteBuf).writeLongLE(Double.valueOf(Math.pow(2D, 25D)).intValue() - 1L); + verify(byteBuf).writeLongLE((int) (Math.pow(2D, 25D)) - 1L); } @Test diff --git a/kernel/transaction/type/base/seata-at/src/test/java/org/apache/shardingsphere/transaction/base/seata/at/SeataATShardingSphereTransactionManagerTest.java b/kernel/transaction/type/base/seata-at/src/test/java/org/apache/shardingsphere/transaction/base/seata/at/SeataATShardingSphereTransactionManagerTest.java index 0d3f9a921ff96..ae9f350790507 100644 --- a/kernel/transaction/type/base/seata-at/src/test/java/org/apache/shardingsphere/transaction/base/seata/at/SeataATShardingSphereTransactionManagerTest.java +++ b/kernel/transaction/type/base/seata-at/src/test/java/org/apache/shardingsphere/transaction/base/seata/at/SeataATShardingSphereTransactionManagerTest.java @@ -163,11 +163,11 @@ void assertRollbackWithoutBegin() { } private void assertResult(final Class requestClass, final Class responseClass) { - assertTrue(requestQueue.stream().anyMatch(each -> each instanceof RegisterTMRequest)); - assertTrue(requestQueue.stream().anyMatch(each -> each instanceof RegisterRMRequest)); + assertTrue(requestQueue.stream().anyMatch(RegisterTMRequest.class::isInstance)); + assertTrue(requestQueue.stream().anyMatch(RegisterRMRequest.class::isInstance)); assertTrue(requestQueue.stream().anyMatch(each -> requestClass.equals(each.getClass()))); - assertTrue(responseQueue.stream().anyMatch(each -> each instanceof RegisterTMResponse)); - assertTrue(responseQueue.stream().anyMatch(each -> each instanceof RegisterRMResponse)); + assertTrue(responseQueue.stream().anyMatch(RegisterTMResponse.class::isInstance)); + assertTrue(responseQueue.stream().anyMatch(RegisterRMResponse.class::isInstance)); assertTrue(responseQueue.stream().anyMatch(each -> responseClass.equals(each.getClass()))); while (!requestQueue.isEmpty()) { Object requestPackage = requestQueue.poll(); diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/ProxyConfigurationLoaderTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/ProxyConfigurationLoaderTest.java index 3f912b1fa8842..300b9fdca2d77 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/ProxyConfigurationLoaderTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/ProxyConfigurationLoaderTest.java @@ -73,11 +73,11 @@ private void assertShardingRuleConfiguration(final YamlProxyDatabaseConfiguratio assertDataSourceConfiguration(actual.getDataSources().get("ds_0"), "jdbc:mysql://127.0.0.1:3306/ds_0"); assertDataSourceConfiguration(actual.getDataSources().get("ds_1"), "jdbc:mysql://127.0.0.1:3306/ds_1"); Optional shardingRuleConfig = actual.getRules().stream() - .filter(each -> each instanceof YamlShardingRuleConfiguration).findFirst().map(each -> (YamlShardingRuleConfiguration) each); + .filter(YamlShardingRuleConfiguration.class::isInstance).findFirst().map(YamlShardingRuleConfiguration.class::cast); assertTrue(shardingRuleConfig.isPresent()); assertShardingRuleConfiguration(shardingRuleConfig.get()); assertFalse( - actual.getRules().stream().filter(each -> each instanceof YamlEncryptRuleConfiguration).findFirst().map(each -> (YamlEncryptRuleConfiguration) each).isPresent()); + actual.getRules().stream().filter(YamlEncryptRuleConfiguration.class::isInstance).findFirst().map(YamlEncryptRuleConfiguration.class::cast).isPresent()); } private void assertShardingRuleConfiguration(final YamlShardingRuleConfiguration actual) { @@ -96,11 +96,11 @@ private void assertReadwriteSplittingRuleConfiguration(final YamlProxyDatabaseCo assertDataSourceConfiguration(actual.getDataSources().get("write_ds"), "jdbc:mysql://127.0.0.1:3306/write_ds"); assertDataSourceConfiguration(actual.getDataSources().get("read_ds_0"), "jdbc:mysql://127.0.0.1:3306/read_ds_0"); assertDataSourceConfiguration(actual.getDataSources().get("read_ds_1"), "jdbc:mysql://127.0.0.1:3306/read_ds_1"); - assertFalse(actual.getRules().stream().filter(each -> each instanceof YamlShardingRuleConfiguration).findFirst().map(each -> (YamlShardingRuleConfiguration) each).isPresent()); + assertFalse(actual.getRules().stream().filter(YamlShardingRuleConfiguration.class::isInstance).findFirst().map(YamlShardingRuleConfiguration.class::cast).isPresent()); assertFalse( actual.getRules().stream().filter(each -> each instanceof YamlEncryptRuleConfiguration).findFirst().map(each -> (YamlEncryptRuleConfiguration) each).isPresent()); Optional ruleConfig = actual.getRules().stream() - .filter(each -> each instanceof YamlReadwriteSplittingRuleConfiguration).findFirst().map(each -> (YamlReadwriteSplittingRuleConfiguration) each); + .filter(YamlReadwriteSplittingRuleConfiguration.class::isInstance).findFirst().map(YamlReadwriteSplittingRuleConfiguration.class::cast); assertTrue(ruleConfig.isPresent()); for (YamlReadwriteSplittingDataSourceGroupRuleConfiguration each : ruleConfig.get().getDataSourceGroups().values()) { assertReadwriteSplittingRuleConfiguration(each); @@ -117,9 +117,9 @@ private void assertEncryptRuleConfiguration(final YamlProxyDatabaseConfiguration assertThat(actual.getDataSources().size(), is(1)); assertDataSourceConfiguration(actual.getDataSources().get("ds_0"), "jdbc:mysql://127.0.0.1:3306/encrypt_ds"); assertFalse(actual.getRules().stream() - .filter(each -> each instanceof YamlShardingRuleConfiguration).findFirst().map(each -> (YamlShardingRuleConfiguration) each).isPresent()); + .filter(YamlShardingRuleConfiguration.class::isInstance).findFirst().map(YamlShardingRuleConfiguration.class::cast).isPresent()); Optional encryptRuleConfig = actual.getRules().stream() - .filter(each -> each instanceof YamlEncryptRuleConfiguration).findFirst().map(each -> (YamlEncryptRuleConfiguration) each); + .filter(YamlEncryptRuleConfiguration.class::isInstance).findFirst().map(YamlEncryptRuleConfiguration.class::cast); assertTrue(encryptRuleConfig.isPresent()); assertEncryptRuleConfiguration(encryptRuleConfig.get()); } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/yaml/swapper/YamlProxyConfigurationSwapperTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/yaml/swapper/YamlProxyConfigurationSwapperTest.java index 678790a3eab64..d5f323838d8a3 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/yaml/swapper/YamlProxyConfigurationSwapperTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/yaml/swapper/YamlProxyConfigurationSwapperTest.java @@ -97,7 +97,7 @@ private void assertAuthorityRuleConfiguration(final ProxyConfiguration proxyConf } private Optional findAuthorityRuleConfiguration(final Collection globalRuleConfigs) { - return globalRuleConfigs.stream().filter(each -> each instanceof AuthorityRuleConfiguration).findFirst().map(each -> (AuthorityRuleConfiguration) each); + return globalRuleConfigs.stream().filter(AuthorityRuleConfiguration.class::isInstance).findFirst().map(AuthorityRuleConfiguration.class::cast); } private void assertProxyConfigurationProps(final ProxyConfiguration proxyConfig) { diff --git a/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/DockerITContainer.java b/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/DockerITContainer.java index caf74c366ae81..8f99a40294398 100644 --- a/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/DockerITContainer.java +++ b/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/DockerITContainer.java @@ -53,7 +53,7 @@ public void start() { } private void startDependencies() { - Collection dependencies = getDependencies().stream().filter(each -> each instanceof DockerITContainer).map(each -> (DockerITContainer) each).collect(Collectors.toList()); + Collection dependencies = getDependencies().stream().filter(DockerITContainer.class::isInstance).map(DockerITContainer.class::cast).collect(Collectors.toList()); dependencies.stream().filter(each -> !each.isCreated()).forEach(GenericContainer::start); dependencies.stream() .filter(each -> { diff --git a/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/adapter/impl/ShardingSphereMultiProxyClusterContainer.java b/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/adapter/impl/ShardingSphereMultiProxyClusterContainer.java index 3bd4be35ebeeb..bb41ba9e7571f 100644 --- a/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/adapter/impl/ShardingSphereMultiProxyClusterContainer.java +++ b/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/adapter/impl/ShardingSphereMultiProxyClusterContainer.java @@ -79,7 +79,7 @@ public void start() { @Override public Collection getContainers() { - return proxyClusterContainers.stream().map(each -> (ITContainer) each).collect(Collectors.toList()); + return proxyClusterContainers.stream().map(ITContainer.class::cast).collect(Collectors.toList()); } private static class RandomDataSourceAdapter implements DataSource { diff --git a/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/EncryptSQLRewriterIT.java b/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/EncryptSQLRewriterIT.java index b2d127dce06f9..1e314b7a83a70 100644 --- a/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/EncryptSQLRewriterIT.java +++ b/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/EncryptSQLRewriterIT.java @@ -100,7 +100,7 @@ protected Collection mockSchemas(final String schemaName) @Override protected void mockRules(final Collection rules, final String schemaName, final SQLStatement sqlStatement) { - Optional singleRule = rules.stream().filter(each -> each instanceof SingleRule).map(each -> (SingleRule) each).findFirst(); + Optional singleRule = rules.stream().filter(SingleRule.class::isInstance).map(SingleRule.class::cast).findFirst(); if (singleRule.isPresent() && !(sqlStatement instanceof CreateTableStatement)) { singleRule.get().getAttributes().getAttribute(MutableDataNodeRuleAttribute.class).put("encrypt_ds", schemaName, "t_account"); singleRule.get().getAttributes().getAttribute(MutableDataNodeRuleAttribute.class).put("encrypt_ds", schemaName, "t_account_bak"); diff --git a/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/ShardingSQLRewriterIT.java b/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/ShardingSQLRewriterIT.java index 95d2ba30b5409..0c939beff9665 100644 --- a/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/ShardingSQLRewriterIT.java +++ b/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/ShardingSQLRewriterIT.java @@ -56,7 +56,7 @@ protected YamlRootConfiguration createRootConfiguration(final SQLRewriteEngineTe @Override protected void mockRules(final Collection rules, final String schemaName, final SQLStatement sqlStatement) { - Optional singleRule = rules.stream().filter(each -> each instanceof SingleRule).map(each -> (SingleRule) each).findFirst(); + Optional singleRule = rules.stream().filter(SingleRule.class::isInstance).map(SingleRule.class::cast).findFirst(); if (singleRule.isPresent() && !(sqlStatement instanceof CreateTableStatement)) { singleRule.get().getAttributes().getAttribute(MutableDataNodeRuleAttribute.class).put("db", schemaName, "t_single"); singleRule.get().getAttributes().getAttribute(MutableDataNodeRuleAttribute.class).put("db", schemaName, "t_single_extend"); diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/MySQLTest.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/MySQLTest.java index 33c9912e81788..876c2659d2fd9 100644 --- a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/MySQLTest.java +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/MySQLTest.java @@ -111,7 +111,7 @@ private Connection openConnection() throws SQLException { @SuppressWarnings({"SqlDialectInspection", "SqlNoDataSourceInspection"}) private DataSource createDataSource() throws SQLException { - Awaitility.await().atMost(Duration.ofMinutes(1L)).ignoreExceptionsMatching(e -> e instanceof CommunicationsException).until(() -> { + Awaitility.await().atMost(Duration.ofMinutes(1L)).ignoreExceptionsMatching(CommunicationsException.class::isInstance).until(() -> { openConnection().close(); return true; }); diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/proxy/databases/MySQLTest.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/proxy/databases/MySQLTest.java index f5a767f76db62..ee9c694297296 100644 --- a/test/native/src/test/java/org/apache/shardingsphere/test/natived/proxy/databases/MySQLTest.java +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/proxy/databases/MySQLTest.java @@ -56,7 +56,7 @@ class MySQLTest { @BeforeAll static void beforeAll() throws SQLException { - Awaitility.await().atMost(Duration.ofSeconds(30L)).ignoreExceptionsMatching(e -> e instanceof CommunicationsException).until(() -> { + Awaitility.await().atMost(Duration.ofSeconds(30L)).ignoreExceptionsMatching(CommunicationsException.class::isInstance).until(() -> { openConnection("root", "yourStrongPassword123!", "jdbc:mysql://127.0.0.1:" + MYSQL_CONTAINER.getMappedPort(3306)) .close(); return true; @@ -70,7 +70,7 @@ static void beforeAll() throws SQLException { } String absolutePath = Paths.get("src/test/resources/test-native/yaml/proxy/databases/mysql").toAbsolutePath().toString(); proxyTestingServer = new ProxyTestingServer(absolutePath); - Awaitility.await().atMost(Duration.ofSeconds(30L)).ignoreExceptionsMatching(e -> e instanceof CommunicationsException).until(() -> { + Awaitility.await().atMost(Duration.ofSeconds(30L)).ignoreExceptionsMatching(CommunicationsException.class::isInstance).until(() -> { openConnection("root", "root", "jdbc:mysql://127.0.0.1:" + proxyTestingServer.getProxyPort()).close(); return true; });