Skip to content

Commit

Permalink
Fix sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Dec 3, 2024
1 parent 676abda commit 3d0d54f
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
Expand All @@ -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)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<YamlShardingRuleConfiguration> 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) {
Expand All @@ -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<YamlReadwriteSplittingRuleConfiguration> 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);
Expand All @@ -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<YamlEncryptRuleConfiguration> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private void assertAuthorityRuleConfiguration(final ProxyConfiguration proxyConf
}

private Optional<AuthorityRuleConfiguration> findAuthorityRuleConfiguration(final Collection<RuleConfiguration> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void start() {
}

private void startDependencies() {
Collection<DockerITContainer> dependencies = getDependencies().stream().filter(each -> each instanceof DockerITContainer).map(each -> (DockerITContainer) each).collect(Collectors.toList());
Collection<DockerITContainer> 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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void start() {

@Override
public Collection<ITContainer> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected Collection<ShardingSphereSchema> mockSchemas(final String schemaName)

@Override
protected void mockRules(final Collection<ShardingSphereRule> rules, final String schemaName, final SQLStatement sqlStatement) {
Optional<SingleRule> singleRule = rules.stream().filter(each -> each instanceof SingleRule).map(each -> (SingleRule) each).findFirst();
Optional<SingleRule> 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected YamlRootConfiguration createRootConfiguration(final SQLRewriteEngineTe

@Override
protected void mockRules(final Collection<ShardingSphereRule> rules, final String schemaName, final SQLStatement sqlStatement) {
Optional<SingleRule> singleRule = rules.stream().filter(each -> each instanceof SingleRule).map(each -> (SingleRule) each).findFirst();
Optional<SingleRule> 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
});
Expand Down

0 comments on commit 3d0d54f

Please sign in to comment.