Skip to content

Commit

Permalink
Merge pull request #11645 from pascalgrimaud/fix-warnings-related-to-…
Browse files Browse the repository at this point in the history
…errorprone

Fix some warnings related to errorprone
  • Loading branch information
pascalgrimaud authored Dec 27, 2024
2 parents 057bb79 + 922a846 commit 49462d0
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/test/java/tech/jhipster/lite/HexagonalArchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ private static Collection<String> buildPackagesPatterns(Collection<String> packa
}

private static Collection<String> packagesWithAnnotation(Class<? extends Annotation> annotationClass) throws AssertionError {
try {
return Files.walk(rootPackagePath())
try (Stream<Path> files = Files.walk(rootPackagePath())) {
return files
.filter(path -> path.toString().endsWith("package-info.java"))
.map(toPackageName())
.map(path -> path.replaceAll("[/]", "."))
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/tech/jhipster/lite/ReplaceCamelCase.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tech.jhipster.lite;

import java.lang.reflect.Method;
import java.util.Locale;
import org.junit.jupiter.api.DisplayNameGenerator;

public class ReplaceCamelCase extends DisplayNameGenerator.Standard {
Expand All @@ -13,7 +14,7 @@ public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod
private String replaceCapitals(String name) {
name = name.replaceAll("([A-Z])", " $1");
name = name.replaceAll("(\\d+)", " $1");
name = name.toLowerCase();
name = name.toLowerCase(Locale.ROOT);
return name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ private static RestQuery lastQuery() {

private static class RestQuery {

@SuppressWarnings("InlineFormatString")
private static final String URI_MATCHER = ".*\\/%s(\\/[\\w-]*\\/?)?";

private final HttpRequest request;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.jhipster.lite.cucumber.rest;

import static java.nio.charset.StandardCharsets.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -98,7 +99,7 @@ void shouldNotReadStatusCodeForUnreadableStatusCode() {
}

assertThatThrownBy(() ->
CucumberRestTestContext.addResponse(mockedRequest("/"), httpResponse, mock(ClientHttpRequestExecution.class), "body".getBytes())
CucumberRestTestContext.addResponse(mockedRequest("/"), httpResponse, mock(ClientHttpRequestExecution.class), "body".getBytes(UTF_8))
).isExactlyInstanceOf(AssertionError.class);
}

Expand All @@ -112,14 +113,14 @@ void shouldNotReadResponseCodeForUnreadableResponse() {
fail(e.getMessage());
}

CucumberRestTestContext.addResponse(mockedRequest("/"), httpResponse, mock(ClientHttpRequestExecution.class), "body".getBytes());
CucumberRestTestContext.addResponse(mockedRequest("/"), httpResponse, mock(ClientHttpRequestExecution.class), "body".getBytes(UTF_8));

assertThat(CucumberRestTestContext.getResponse()).isEmpty();
}

@Test
void shouldGracefullyHandleRetryErrors() throws IOException {
byte[] body = "body".getBytes();
byte[] body = "body".getBytes(UTF_8);

ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class);
HttpRequest request = mockedRequest("/");
Expand All @@ -137,15 +138,15 @@ private void addQuery(String response) {
private void addQuery(String path, String response) {
ClientHttpResponse httpResponse = mockedResponse(response);

CucumberRestTestContext.addResponse(mockedRequest(path), httpResponse, mock(ClientHttpRequestExecution.class), "body".getBytes());
CucumberRestTestContext.addResponse(mockedRequest(path), httpResponse, mock(ClientHttpRequestExecution.class), "body".getBytes(UTF_8));
}

private ClientHttpResponse mockedResponse(String response) {
ClientHttpResponse httpResponse = mock(ClientHttpResponse.class);

try {
when(httpResponse.getStatusCode()).thenReturn(HttpStatus.OK);
when(httpResponse.getBody()).thenReturn(new ByteArrayInputStream(response.getBytes()));
when(httpResponse.getBody()).thenReturn(new ByteArrayInputStream(response.getBytes(UTF_8)));
} catch (IOException e) {
fail(e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public SyncElementAsserter withValues(Collection<String> values) {
return this;
}

@Override
public SyncResponseAsserter and() {
return responseAsserter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.assertThatModuleWithFiles;

import java.time.Year;
import java.time.ZoneId;
import org.junit.jupiter.api.Test;
import tech.jhipster.lite.TestFileUtils;
import tech.jhipster.lite.UnitTest;
Expand All @@ -18,7 +19,7 @@ class LicenseModuleFactoryTest {
@Test
void shouldBuildMitModule() {
JHipsterModuleProperties properties = JHipsterModulesFixture.propertiesBuilder(TestFileUtils.tmpDirForTest()).build();
int year = Year.now().getValue();
int year = Year.now(ZoneId.systemDefault()).getValue();

JHipsterModule module = factory.buildMitModule(properties);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
class JHipsterModuleContextTest {

@Logs
@SuppressWarnings("unused")
private LogsSpy logs;

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import net.minidev.json.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.assertj.core.api.SoftAssertions;
Expand Down Expand Up @@ -276,8 +277,8 @@ private Supplier<String> fileFoundMessage(Path path) {
}

private String projectFiles() {
try {
return Files.walk(Paths.get(lastProjectFolder())).filter(Files::isRegularFile).map(Path::toString).collect(Collectors.joining(", "));
try (Stream<Path> files = Files.walk(Paths.get(lastProjectFolder()))) {
return files.filter(Files::isRegularFile).map(Path::toString).collect(Collectors.joining(", "));
} catch (IOException e) {
return "unreadable folder";
}
Expand Down Expand Up @@ -306,7 +307,9 @@ public void shouldHaveLandscapeLevelElements(int level, List<Map<String, String>
public void shouldHaveFilesCountInDirectory(int filesCount, String directory) throws IOException {
assertThatLastResponse().hasOkStatus();

assertThat(Files.list(Paths.get(lastProjectFolder(), directory)).count()).isEqualTo(filesCount);
try (Stream<Path> files = Files.list(Paths.get(lastProjectFolder(), directory))) {
assertThat(files.count()).isEqualTo(filesCount);
}
}

@Then("I should have unknown slug {string} error message")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.jhipster.lite.module.infrastructure.secondary;

import static java.nio.charset.StandardCharsets.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;

Expand Down Expand Up @@ -87,7 +88,7 @@ private static ProjectFiles mockProjectFilesWithValidPresetJson() {
]
}
""";
lenient().when(projectFiles.readBytes("/preset.json")).thenReturn(validPresetJson.getBytes());
lenient().when(projectFiles.readBytes("/preset.json")).thenReturn(validPresetJson.getBytes(UTF_8));

return projectFiles;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tech.jhipster.lite.module.infrastructure.secondary;

import static org.assertj.core.api.Assertions.assertThat;
import static tech.jhipster.lite.TestFileUtils.contentNormalizingNewLines;
import static org.assertj.core.api.Assertions.*;
import static tech.jhipster.lite.TestFileUtils.*;

import java.io.IOException;
import java.nio.file.*;
Expand Down Expand Up @@ -235,11 +235,8 @@ private static Supplier<String> fileNotFoundMessage(Path path, JHipsterProjectFo
}

private static String projectFiles(JHipsterProjectFolder projectFolder) {
try {
return Files.walk(Paths.get(projectFolder.folder()))
.filter(Files::isRegularFile)
.map(Path::toString)
.collect(Collectors.joining(", "));
try (Stream<Path> files = Files.walk(Paths.get(projectFolder.folder()))) {
return files.filter(Files::isRegularFile).map(Path::toString).collect(Collectors.joining(", "));
} catch (IOException e) {
return "unreadable folder";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tech.jhipster.lite.module.infrastructure.secondary.javadependency.maven;

import static org.assertj.core.api.Assertions.assertThat;
import static java.nio.charset.StandardCharsets.*;
import static org.assertj.core.api.Assertions.*;

import io.fabric8.maven.Maven;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -47,7 +48,8 @@ void allDeclaredDependenciesShouldUseAVersion(Dependency dependency) {

private static Model readMavenModel() {
var reader = new InputStreamReader(
Objects.requireNonNull(ConsistentMavenDependenciesDeclarationTest.class.getResourceAsStream(CURRENT_VERSIONS_FILE))
Objects.requireNonNull(ConsistentMavenDependenciesDeclarationTest.class.getResourceAsStream(CURRENT_VERSIONS_FILE)),
UTF_8
);
return Maven.readModel(reader);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ class AssertionErrorMessagesTest {
private static final Map<String, Properties> ALL_ASSERTION_MESSAGES = loadMessages();

private static Map<String, Properties> loadMessages() {
try {
return Files.list(Paths.get("src/main/resources/messages/assertions-errors")).collect(
Collectors.toUnmodifiableMap(Path::toString, toProperties())
);
try (Stream<Path> files = Files.list(Paths.get("src/main/resources/messages/assertions-errors"))) {
return files.collect(Collectors.toUnmodifiableMap(Path::toString, toProperties()));
} catch (IOException e) {
throw new AssertionError();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import ch.qos.logback.classic.Level;
import jakarta.validation.ConstraintViolationException;
import jakarta.validation.Validation;
import jakarta.validation.ValidatorFactory;
import jakarta.validation.constraints.NotNull;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -43,16 +44,19 @@ public void failingMethod() {

@Test
void shouldLogConstraintViolationInInfo() {
handler.handleConstraintViolationException(
new ConstraintViolationException(Validation.buildDefaultValidatorFactory().getValidator().validate(new ValidatedBean()))
);
try (ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory()) {
handler.handleConstraintViolationException(
new ConstraintViolationException(validatorFactory.getValidator().validate(new ValidatedBean()))
);

logs.shouldHave(Level.INFO, "parameter");
logs.shouldHave(Level.INFO, "parameter");
}
}

static class ValidatedBean {

@NotNull
@SuppressWarnings("unused")
private String parameter;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package tech.jhipster.lite.shared.error.infrastructure.primary;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.*;

import java.io.IOException;
import java.nio.file.Files;
Expand Down Expand Up @@ -37,10 +37,8 @@ class GeneratorErrorsMessagesTest {
private static final Map<String, Properties> ALL_ASSERTION_MESSAGES = loadMessages();

private static Map<String, Properties> loadMessages() {
try {
return Files.list(Paths.get("src/main/resources/messages/errors")).collect(
Collectors.toUnmodifiableMap(Path::toString, toProperties())
);
try (Stream<Path> files = Files.list(Paths.get("src/main/resources/messages/errors"))) {
return files.collect(Collectors.toUnmodifiableMap(Path::toString, toProperties()));
} catch (IOException e) {
throw new AssertionError();
}
Expand Down

0 comments on commit 49462d0

Please sign in to comment.