Skip to content

Commit

Permalink
Sonar: Local-Variable Type Inference should be used
Browse files Browse the repository at this point in the history
  • Loading branch information
qmonmert committed Feb 27, 2025
1 parent 6c097ba commit 4b97ef5
Show file tree
Hide file tree
Showing 25 changed files with 44 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class GitInfoConfiguration {

@Bean
public PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
PropertySourcesPlaceholderConfigurer propsConfig = new PropertySourcesPlaceholderConfigurer();
var propsConfig = new PropertySourcesPlaceholderConfigurer();
propsConfig.setLocation(new ClassPathResource("git.properties"));
propsConfig.setIgnoreResourceNotFound(true);
propsConfig.setIgnoreUnresolvablePlaceholders(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class HexagonalArchTest {

private static Function<Path, String> toPackageName() {
return path -> {
String stringPath = path.toString();
var stringPath = path.toString();
return stringPath.substring(0, stringPath.lastIndexOf("."));
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MemoizersTest {

@Test
void shouldMemoizeFunctionResult() {
AtomicInteger result = new AtomicInteger();
var result = new AtomicInteger();
Function<Object, Integer> memoizer = Memoizers.of(d -> result.incrementAndGet());
Expand All @@ -30,7 +30,7 @@ class MemoizersTest {

@Test
void shouldMemoizeNullResult() {
NullFactory factory = new NullFactory();
var factory = new NullFactory();
Function<Object, String> memoizer = Memoizers.of(factory);
memoizer.apply(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ProtobufDatesReaderTest {

@Test
void shouldGetInstantFromTimestampFromDate() {
Date date = new Date();
var date = new Date();
assertThat(ProtobufDatesReader.readInstant(Timestamps.fromDate(date))).isEqualTo(date.toInstant());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ final class SpringLiquibaseUtil {
DataSourceProperties dataSourceProperties
) {
SpringLiquibase liquibase;
DataSource liquibaseDataSource = getDataSource(liquibaseDatasource, liquibaseProperties, dataSource);
var liquibaseDataSource = getDataSource(liquibaseDatasource, liquibaseProperties, dataSource);
if (liquibaseDataSource != null) {
liquibase = new SpringLiquibase();
liquibase.setDataSource(liquibaseDataSource);
Expand Down Expand Up @@ -69,7 +69,7 @@ final class SpringLiquibaseUtil {
DataSourceProperties dataSourceProperties
) {
AsyncSpringLiquibase liquibase = new AsyncSpringLiquibase(executor, env, liquibaseProperties);
DataSource liquibaseDataSource = getDataSource(liquibaseDatasource, liquibaseProperties, dataSource);
var liquibaseDataSource = getDataSource(liquibaseDatasource, liquibaseProperties, dataSource);
if (liquibaseDataSource != null) {
liquibase.setCloseDataSourceOnceMigrated(false);
liquibase.setDataSource(liquibaseDataSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class AsyncSpringLiquibaseTest {
private final ConfigurableEnvironment environment = new MockEnvironment();
private final Executor executor = spy(new DirectExecutor());
private final LiquibaseProperties liquibaseProperties = new LiquibaseProperties();
private final var liquibaseProperties = new LiquibaseProperties();
@Logs
private LogsSpy logs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class SpringLiquibaseUtilTest {
@Test
void createSpringLiquibaseFromNormalDataSource() {
DataSource liquibaseDatasource = null;
LiquibaseProperties liquibaseProperties = new LiquibaseProperties();
var liquibaseProperties = new LiquibaseProperties();
DataSource normalDataSource = DataSourceBuilder.create().url(datasourceUrl).username("sa").build();
DataSourceProperties dataSourceProperties = null;
Expand All @@ -84,11 +84,11 @@ class SpringLiquibaseUtilTest {
@Test
void createSpringLiquibaseFromLiquibaseProperties() {
DataSource liquibaseDatasource = null;
LiquibaseProperties liquibaseProperties = new LiquibaseProperties();
var liquibaseProperties = new LiquibaseProperties();
liquibaseProperties.setUrl(datasourceUrl);
liquibaseProperties.setUser("sa");
DataSource normalDataSource = null;
DataSourceProperties dataSourceProperties = new DataSourceProperties();
var dataSourceProperties = new DataSourceProperties();
dataSourceProperties.setPassword("password");
SpringLiquibase liquibase = SpringLiquibaseUtil.createSpringLiquibase(
Expand Down Expand Up @@ -132,7 +132,7 @@ class SpringLiquibaseUtilTest {
@Test
void createAsyncSpringLiquibaseFromNormalDataSource() {
DataSource liquibaseDatasource = null;
LiquibaseProperties liquibaseProperties = new LiquibaseProperties();
var liquibaseProperties = new LiquibaseProperties();
DataSource normalDataSource = DataSourceBuilder.create().url(datasourceUrl).username("sa").build();
DataSourceProperties dataSourceProperties = null;
Expand All @@ -155,11 +155,11 @@ class SpringLiquibaseUtilTest {
@Test
void createAsyncSpringLiquibaseFromLiquibaseProperties() {
DataSource liquibaseDatasource = null;
LiquibaseProperties liquibaseProperties = new LiquibaseProperties();
var liquibaseProperties = new LiquibaseProperties();
liquibaseProperties.setUrl(datasourceUrl);
liquibaseProperties.setUser("sa");
DataSource normalDataSource = null;
DataSourceProperties dataSourceProperties = new DataSourceProperties();
var dataSourceProperties = new DataSourceProperties();
dataSourceProperties.setPassword("password");
AsyncSpringLiquibase liquibase = SpringLiquibaseUtil.createAsyncSpringLiquibase(
Expand All @@ -180,11 +180,11 @@ class SpringLiquibaseUtilTest {
@Test
void shouldNotCreateAsyncSpringLiquibaseFromLiquibasePropertiesWithNullUser() {
DataSource liquibaseDatasource = null;
LiquibaseProperties liquibaseProperties = new LiquibaseProperties();
var liquibaseProperties = new LiquibaseProperties();
liquibaseProperties.setUrl(datasourceUrl);
liquibaseProperties.setUser(null);
DataSource normalDataSource = null;
DataSourceProperties dataSourceProperties = new DataSourceProperties();
var dataSourceProperties = new DataSourceProperties();
dataSourceProperties.setPassword("password");
assertThatThrownBy(() ->
Expand All @@ -202,11 +202,11 @@ class SpringLiquibaseUtilTest {
@Test
void shouldNotCreateAsyncSpringLiquibaseFromLiquibasePropertiesWithNullUrl() {
DataSource liquibaseDatasource = null;
LiquibaseProperties liquibaseProperties = new LiquibaseProperties();
var liquibaseProperties = new LiquibaseProperties();
liquibaseProperties.setUrl(null);
liquibaseProperties.setUser("sa");
DataSource normalDataSource = null;
DataSourceProperties dataSourceProperties = new DataSourceProperties();
var dataSourceProperties = new DataSourceProperties();
dataSourceProperties.setPassword("password");
assertThatThrownBy(() ->
Expand All @@ -224,7 +224,7 @@ class SpringLiquibaseUtilTest {
private static Properties testProperties() {
try {
{{#yamlSpringConfigurationFormat}}
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
var yaml = new YamlPropertiesFactoryBean();
yaml.setResources(
new PathResource(SpringLiquibaseUtilTest.class.getClassLoader().getResource("config/application-test.yml").toURI())
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class LogstashTcpLifeCycle implements SmartLifecycle {
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
LogstashTcpSocketAppender logstashTcpSocketAppender = logstashTcpSocketAppender(context);
var logstashTcpSocketAppender = logstashTcpSocketAppender(context);
logstashTcpSocketAppender.start();
context.getLogger(Logger.ROOT_LOGGER_NAME).addAppender(logstashTcpSocketAppender);
running = true;
Expand All @@ -62,7 +62,7 @@ class LogstashTcpLifeCycle implements SmartLifecycle {

protected LogstashTcpSocketAppender logstashTcpSocketAppender(LoggerContext context) {
// Documentation is available at: https://github.com/logstash/logstash-logback-encoder
LogstashTcpSocketAppender logstashAppender = new LogstashTcpSocketAppender();
var logstashAppender = new LogstashTcpSocketAppender();
logstashAppender.addDestinations(new InetSocketAddress(properties.getHost(), properties.getPort()));
logstashAppender.setContext(context);
logstashAppender.setEncoder(logstashEncoder());
Expand All @@ -77,14 +77,14 @@ class LogstashTcpLifeCycle implements SmartLifecycle {
}

protected LogstashEncoder logstashEncoder() {
final LogstashEncoder logstashEncoder = new LogstashEncoder();
final var logstashEncoder = new LogstashEncoder();
logstashEncoder.setThrowableConverter(throwableConverter());
logstashEncoder.setCustomFields(serializedFields());
return logstashEncoder;
}

protected ThrowableHandlingConverter throwableConverter() {
final ShortenedThrowableConverter throwableConverter = new ShortenedThrowableConverter();
final var throwableConverter = new ShortenedThrowableConverter();
throwableConverter.setRootCauseFirst(true);
return throwableConverter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import net.logstash.logback.appender.LogstashTcpSocketAppender;
import net.logstash.logback.stacktrace.ShortenedThrowableConverter;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -66,7 +65,7 @@ class LogstashTcpLifeCycleTest {
LogstashTcpLifeCycle logstashTcpLifeCycle = new LogstashTcpLifeCycle(null, properties, new ObjectMapper());
LogstashTcpSocketAppender logstashTcpSocketAppender = logstashTcpLifeCycle.logstashTcpSocketAppender(context);
var logstashTcpSocketAppender = logstashTcpLifeCycle.logstashTcpSocketAppender(context);
assertThat(logstashTcpSocketAppender.getDestinations()).contains(new InetSocketAddress("127.0.0.1", 50000));
assertThat(logstashTcpSocketAppender.getContext()).isEqualTo(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class {{ baseName }}ExceptionTest {

@Test
void shouldGetFull{{ baseName }}Exception() {
RuntimeException cause = new RuntimeException();
var cause = new RuntimeException();
{{ baseName }}Exception exception = {{ baseName }}Exception.builder(StandardErrorKey.BAD_REQUEST)
.message("This is an error")
.cause(cause)
Expand All @@ -61,7 +61,7 @@ class {{ baseName }}ExceptionTest {

@Test
void shouldGetTechnicalErrorException() {
RuntimeException cause = new RuntimeException();
var cause = new RuntimeException();
{{ baseName }}Exception exception = {{ baseName }}Exception.technicalError("This is a problem", cause);

assertThat(exception.getMessage()).isEqualTo("This is a problem");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import {{packageName}}.UnitTest;
Expand Down Expand Up @@ -116,7 +115,7 @@ class AuthenticatedUserTest {
}

private void authenticate(Authentication token) {
SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
var securityContext = SecurityContextHolder.createEmptyContext();
securityContext.setAuthentication(token);
SecurityContextHolder.setContext(securityContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class JWTFilterTest {

@Test
void shouldAuthenticateUserWithValidToken() throws IOException, ServletException {
TestingAuthenticationToken authentication = new TestingAuthenticationToken("user", "password");
var authentication = new TestingAuthenticationToken("user", "password");
when(tokens.read("valid")).thenReturn(Optional.of(authentication));
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader(HttpHeaders.AUTHORIZATION, "Bearer valid");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class {{ baseName }}AuthorizationsTest {
}

private static Authentication createDummyAuthenticationWithPrincipalDetails(String username) {
UserDetails principalDetails = createUserDetailsWithUsername(username);
var principalDetails = createUserDetailsWithUsername(username);
Authentication authentication = mock(Authentication.class);
when(authentication.getPrincipal()).thenReturn(principalDetails);
when(authentication.toString()).thenReturn("Authentication with userDetails, username=" + username);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
Expand Down Expand Up @@ -195,7 +194,7 @@ class AuthenticatedUserTest {
}

private void authenticate(Authentication token) {
SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
var securityContext = SecurityContextHolder.createEmptyContext();
securityContext.setAuthentication(token);
SecurityContextHolder.setContext(securityContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class GitInfoConfiguration {
@Bean
public PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
PropertySourcesPlaceholderConfigurer propsConfig = new PropertySourcesPlaceholderConfigurer();
var propsConfig = new PropertySourcesPlaceholderConfigurer();
propsConfig.setLocation(new ClassPathResource("git.properties"));
propsConfig.setIgnoreResourceNotFound(true);
propsConfig.setIgnoreUnresolvablePlaceholders(true);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/tech/jhipster/lite/HexagonalArchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private static Path rootPackagePath() {

private static Function<Path, String> toPackageName() {
return path -> {
String stringPath = path.toString();
var stringPath = path.toString();
return stringPath.substring(0, stringPath.lastIndexOf("."));
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void shouldBuildModuleWithYamlSpringConfigurationFormat() {

assertThatModuleWithFiles(module, pomFile(), logbackFile(), testLogbackFile())
.hasFile("src/test/java/tech/jhipster/jhlitest/wire/liquibase/infrastructure/secondary/SpringLiquibaseUtilTest.java")
.containing("YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();")
.containing("var yaml = new YamlPropertiesFactoryBean();")
.notContaining("var properties = new Properties();");
}

Expand All @@ -123,7 +123,7 @@ void shouldBuildModuleWithPropertiesSpringConfigurationFormat() {
assertThatModuleWithFiles(module, pomFile(), logbackFile(), testLogbackFile())
.hasFile("src/test/java/tech/jhipster/jhlitest/wire/liquibase/infrastructure/secondary/SpringLiquibaseUtilTest.java")
.containing("var properties = new Properties();")
.notContaining("YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();");
.notContaining("var yaml = new YamlPropertiesFactoryBean();");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class MandatoryReplacementExceptionTest {

@Test
void shouldGetExceptionInformation() {
RuntimeException cause = new RuntimeException();
var cause = new RuntimeException();
MandatoryReplacementException exception = new MandatoryReplacementException(cause);

assertThat(exception.getMessage()).isEqualTo("Error applying mandatory replacement");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class GitCommitExceptionTest {

@Test
void shouldGetExceptionInformation() {
RuntimeException cause = new RuntimeException();
var cause = new RuntimeException();
GitCommitException exception = new GitCommitException("This is an error", cause);

assertThat(exception.getMessage()).isEqualTo("This is an error");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class GitInitExceptionTest {

@Test
void shouldGetExceptionInformation() {
RuntimeException cause = new RuntimeException();
var cause = new RuntimeException();
GitInitException exception = new GitInitException("This is an error", cause);

assertThat(exception.getMessage()).isEqualTo("This is an error");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class MalformedAdditionalInformationExceptionTest {

@Test
void shouldGetExceptionInformation() {
RuntimeException cause = new RuntimeException();
var cause = new RuntimeException();
MalformedAdditionalInformationException exception = new MalformedAdditionalInformationException(cause);

assertThat(exception.getMessage()).isEqualTo("Malformed XML additional elements for plugin");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void shouldGetExceptionWithoutCauseInformation() {

@Test
void shouldGetExceptionWithCauseInformation() {
RuntimeException cause = new RuntimeException();
var cause = new RuntimeException();
ProjectFormattingException exception = new ProjectFormattingException("This is an error", cause);

assertThat(exception.getMessage()).isEqualTo("This is an error");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void shouldGetExceptionWithMessageInformation() {

@Test
void shouldGetExceptionWithCauseInformation() {
RuntimeException cause = new RuntimeException();
var cause = new RuntimeException();
ProjectZippingException exception = new ProjectZippingException(cause);

assertThat(exception.getMessage()).isEqualTo("Error creating zip file");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void shouldGetMinimalGeneratorExceptionFromPrimary() {

@Test
void shouldGetFullGeneratorException() {
RuntimeException cause = new RuntimeException();
var cause = new RuntimeException();
GeneratorException exception = GeneratorException.builder(StandardErrorKey.BAD_REQUEST)
.message("This is an error")
.cause(cause)
Expand All @@ -52,7 +52,7 @@ void shouldGetFullGeneratorException() {

@Test
void shouldGetTechnicalErrorException() {
RuntimeException cause = new RuntimeException();
var cause = new RuntimeException();
GeneratorException exception = GeneratorException.technicalError("This is a problem", cause);

assertThat(exception.getMessage()).isEqualTo("This is a problem");
Expand Down
Loading

0 comments on commit 4b97ef5

Please sign in to comment.