Skip to content

Commit

Permalink
Apply OpenRewrite Java 17 migrations.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhafner committed Sep 23, 2024
1 parent dc5c058 commit 46b00d1
Show file tree
Hide file tree
Showing 19 changed files with 45 additions and 44 deletions.
3 changes: 1 addition & 2 deletions src/main/java/edu/hm/hafner/grading/AnalysisScore.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public boolean equals(final Object o) {
if (!super.equals(o)) {
return false;
}
AnalysisScore that = (AnalysisScore) o;
var that = (AnalysisScore) o;
return errorSize == that.errorSize
&& highSeveritySize == that.highSeveritySize
&& normalSeveritySize == that.normalSeveritySize
Expand Down Expand Up @@ -273,4 +273,3 @@ public AnalysisScore build() {
}
}
}

2 changes: 1 addition & 1 deletion src/main/java/edu/hm/hafner/grading/AutoGradingRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ protected String createErrorMessageMarkdown(final FilteredLog log) {

@VisibleForTesting
String getConfiguration(final FilteredLog log) {
String configuration = System.getenv("CONFIG");
var configuration = System.getenv("CONFIG");
if (StringUtils.isBlank(configuration)) {
log.logInfo("No configuration provided (environment variable CONFIG not set), using default configuration");

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/edu/hm/hafner/grading/CoverageScore.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public Node getReport() {

@Override
public int getImpact() {
CoverageConfiguration configuration = getConfiguration();
var configuration = getConfiguration();

int change = 0;

Expand Down Expand Up @@ -198,7 +198,7 @@ public boolean equals(final Object o) {
if (!super.equals(o)) {
return false;
}
CoverageScore that = (CoverageScore) o;
var that = (CoverageScore) o;
return coveredPercentage == that.coveredPercentage;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import edu.hm.hafner.analysis.FileReaderFactory;
import edu.hm.hafner.analysis.Report;
import edu.hm.hafner.analysis.registry.ParserDescriptor;
import edu.hm.hafner.analysis.registry.ParserRegistry;
import edu.hm.hafner.grading.AggregatedScore.AnalysisReportFactory;
import edu.hm.hafner.util.FilteredLog;
Expand All @@ -23,14 +22,14 @@ public final class FileSystemAnalysisReportFactory implements AnalysisReportFact

@Override
public Report create(final ToolConfiguration tool, final FilteredLog log) {
ParserDescriptor parser = new ParserRegistry().get(tool.getId());
var parser = new ParserRegistry().get(tool.getId());

var displayName = getDisplayName(tool, parser.getName());
var total = new Report(tool.getId(), displayName);

var analysisParser = parser.createParser();
for (Path file : REPORT_FINDER.find(log, displayName, tool.getPattern())) {
Report report = analysisParser.parseFile(new FileReaderFactory(file));
var report = analysisParser.parseFile(new FileReaderFactory(file));
report.setOrigin(tool.getId(), displayName);
log.logInfo("- %s: %d warnings", PATH_UTIL.getRelativePath(file), report.size());
total.addAll(report);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/edu/hm/hafner/grading/JacksonFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ public JsonNode readJson(final String json) {
*/
public String getStringValue(final String json, final String property, final String defaultValue) {
try {
ObjectNode node = mapper.readValue(json, ObjectNode.class);
JsonNode typeNode = node.get(property);
var node = mapper.readValue(json, ObjectNode.class);
var typeNode = node.get(property);
if (typeNode != null) {
return typeNode.asText(defaultValue);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/edu/hm/hafner/grading/LogHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class LogHandler {

private int infoPosition;
private int errorPosition;
private boolean quiet = false;
private boolean quiet;

/**
* Creates a new {@link LogHandler}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @author Ullrich Hafner
*/
public class MutationCoverageMarkdown extends CoverageMarkdown {
private static final String PIT = "pit";
static final String TYPE = "Mutation Coverage Score";

/**
Expand All @@ -25,7 +26,7 @@ protected List<CoverageScore> createScores(final AggregatedScore aggregation) {

@Override
protected String getIcon(final CoverageScore score) {
if (score.getId().equals("pit")) {
if (PIT.equals(score.getId())) {
return format("<img src=\"https://pitest.org/images/pit-black-150x152.png\" alt=\"PIT\" height=\"%d\" width=\"%d\">",
ICON_SIZE, ICON_SIZE);
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/edu/hm/hafner/grading/ReportFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
Expand Down Expand Up @@ -56,7 +55,7 @@ List<Path> find(final FilteredLog log, final String displayName, final String pa
List<Path> findGlob(final String pattern, final String directory, final FilteredLog log) {
try {
var visitor = new PathMatcherFileVisitor(pattern);
Files.walkFileTree(Paths.get(directory), visitor);
Files.walkFileTree(Path.of(directory), visitor);
return visitor.getMatches();
}
catch (IOException exception) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/edu/hm/hafner/grading/TestConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public boolean equals(final Object o) {
if (!super.equals(o)) {
return false;
}
TestConfiguration that = (TestConfiguration) o;
var that = (TestConfiguration) o;
return failureImpact == that.failureImpact
&& passedImpact == that.passedImpact
&& skippedImpact == that.skippedImpact;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/edu/hm/hafner/grading/TestMarkdown.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ protected String createSummary(final TestScore score) {
summary.append(SPACE).append(SPACE)
.append(getTitle(score, 0));
if (score.hasFailures() || score.hasPassedTests() || score.hasSkippedTests()) {
summary.append(": ").append(
format("%2d %% successful", Math.round(score.getPassedSize() * 100.0 / score.getTotalSize())));
summary.append(": ")
.append(format("%2d %% successful", Math.round(score.getPassedSize() * 100.0 / score.getTotalSize())));
var joiner = new StringJoiner(", ", " (", ")");
if (score.hasFailures()) {
joiner.add(format(":x: %d failed", score.getFailedSize()));
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/edu/hm/hafner/grading/TestScore.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public int getReportFiles() {

@Override
public int getImpact() {
TestConfiguration configuration = getConfiguration();
var configuration = getConfiguration();

int change = 0;

Expand Down Expand Up @@ -177,8 +177,7 @@ private List<TestCase> filterTests(final TestResult result) {
protected String createSummary() {
var summary = new StringBuilder(CAPACITY);
if (hasFailures()) {
summary.append(
format("%d tests failed, %d passed", getFailedSize(), getPassedSize()));
summary.append(format("%d tests failed, %d passed", getFailedSize(), getPassedSize()));
}
else {
summary.append(format("%d tests passed", getPassedSize()));
Expand All @@ -200,7 +199,7 @@ public boolean equals(final Object o) {
if (!super.equals(o)) {
return false;
}
TestScore testScore = (TestScore) o;
var testScore = (TestScore) o;
return passedSize == testScore.passedSize
&& failedSize == testScore.failedSize
&& skippedSize == testScore.skippedSize;
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/edu/hm/hafner/grading/TruncatedString.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*
* @author Bill Collins
*/
public class TruncatedString {
public final class TruncatedString {
private final List<String> chunks;
private final String truncationText;
private final boolean truncateStart;
Expand Down Expand Up @@ -112,8 +112,8 @@ private String build(final int maxSize, final boolean chunkOnChars) {
*/
public static class TruncatedStringBuilder {
private String truncationText = "Output truncated.";
private boolean truncateStart = false;
private boolean chunkOnNewlines = false;
private boolean truncateStart;
private boolean chunkOnNewlines;
private final List<String> chunks = new ArrayList<>();

/**
Expand Down Expand Up @@ -251,8 +251,8 @@ public Set<Characteristics> characteristics() {

private class Accumulator {
private final List<String> chunks = new ArrayList<>();
private int length = 0;
private boolean truncated = false;
private int length;
private boolean truncated;

@CanIgnoreReturnValue
Accumulator combine(final Accumulator other) {
Expand Down
8 changes: 3 additions & 5 deletions src/test/java/edu/hm/hafner/grading/AggregatedScoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -508,8 +506,8 @@ void shouldGradeAnalysisReport() {
static Node readCoverageReport(final String fileName, final ToolConfiguration tool,
final CoverageParserType type) {
var parser = new ParserRegistry().get(type, ProcessingMode.FAIL_FAST);
try (InputStream stream = createStream(fileName);
Reader reader = new InputStreamReader(Objects.requireNonNull(stream), StandardCharsets.UTF_8)) {
try (var stream = createStream(fileName);
var reader = new InputStreamReader(Objects.requireNonNull(stream), StandardCharsets.UTF_8)) {
var root = parser.parse(reader, fileName, new FilteredLog("Test"));
var containerNode = new ModuleNode(tool.getDisplayName());
containerNode.addChild(root);
Expand All @@ -533,7 +531,7 @@ private Report readAnalysisReport(final ToolConfiguration tool) {
}

private Path createPath(final String fileName) throws URISyntaxException {
return Paths.get(Objects.requireNonNull(AggregatedScoreTest.class.getResource(
return Path.of(Objects.requireNonNull(AggregatedScoreTest.class.getResource(
fileName), "File not found: " + fileName).toURI());
}

Expand Down
8 changes: 5 additions & 3 deletions src/test/java/edu/hm/hafner/grading/AnalysisMarkdownTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
class AnalysisMarkdownTest {
private static final String IMPACT_CONFIGURATION = ":moneybag:|:heavy_minus_sign:|*-1*|*-2*|*-3*|*-4*|:heavy_minus_sign:|:heavy_minus_sign:";
private static final FilteredLog LOG = new FilteredLog("Test");
private static final String CHECKSTYLE = "checkstyle";
private static final String SPOTBUGS = "spotbugs";

@Test
void shouldSkipWhenThereAreNoScores() {
Expand Down Expand Up @@ -48,7 +50,7 @@ void shouldShowMaximumScore() {
}]
}
""", LOG);
score.gradeAnalysis((tool, log) -> new Report("checkstyle", "CheckStyle"));
score.gradeAnalysis((tool, log) -> new Report(CHECKSTYLE, "CheckStyle"));

var analysisMarkdown = new AnalysisMarkdown();

Expand Down Expand Up @@ -190,10 +192,10 @@ private static Report createAnotherSampleReport() {
}

static Report createTwoReports(final ToolConfiguration tool) {
if (tool.getId().equals("checkstyle")) {
if (CHECKSTYLE.equals(tool.getId())) {
return createSampleReport();
}
else if (tool.getId().equals("spotbugs")) {
else if (SPOTBUGS.equals(tool.getId())) {
return createAnotherSampleReport();
}
throw new IllegalArgumentException("Unexpected tool ID: " + tool.getId());
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/edu/hm/hafner/grading/AnalysisScoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ static Report createReportWith(final String name, final Severity... severities)
report.setOriginReportFile(name + ".xml");
try (var builder = new IssueBuilder()) {
for (int i = 0; i < severities.length; i++) {
Severity severity = severities[i];
var severity = severities[i];
var text = severity.toString() + "-" + i;
report.add(builder.setMessage(text)
.setFileName(text)
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/edu/hm/hafner/grading/CoverageMarkdownTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
class CoverageMarkdownTest {
private static final FilteredLog LOG = new FilteredLog("Test");
private static final String IMPACT_CONFIGURATION = ":moneybag:|*1*|*-1*|:heavy_minus_sign:";
private static final String JACOCO = "jacoco";
private static final String PIT = "pit";

@Test
void shouldSkip() {
Expand Down Expand Up @@ -260,13 +262,13 @@ void shouldShowScoreWithTwoResults() {
}

static ModuleNode createTwoReports(final ToolConfiguration tool) {
if (tool.getId().equals("jacoco")) {
if (JACOCO.equals(tool.getId())) {
var root = new ModuleNode(tool.getDisplayName());
root.addValue(new CoverageBuilder().withMetric(Metric.LINE).withCovered(80).withMissed(20).build());
root.addValue(new CoverageBuilder().withMetric(Metric.BRANCH).withCovered(60).withMissed(40).build());
return root;
}
else if (tool.getId().equals("pit")) {
else if (PIT.equals(tool.getId())) {
var root = new ModuleNode(tool.getDisplayName());
root.addValue(new CoverageBuilder().withMetric(Metric.LINE).withCovered(90).withMissed(10).build());
root.addValue(new CoverageBuilder().withMetric(Metric.MUTATION).withCovered(60).withMissed(40).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void shouldCreateAggregation() {
"IssuesTest.java",
"RobocopyParser.java");
assertThat(score.getIssues().stream()
.filter(issue -> issue.getBaseName().equals("CsharpNamespaceDetector.java")))
.filter(issue -> "CsharpNamespaceDetector.java".equals(issue.getBaseName())))
.map(Issue::getOriginName)
.hasSize(6).containsOnly("CheckStyle");
assertThat(log.getInfoMessages()).contains(
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/edu/hm/hafner/grading/TestMarkdownTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class TestMarkdownTest {
private static final String IMPACT_CONFIGURATION = ":moneybag:|:heavy_minus_sign:|*10*|*-1*|*-5*|:heavy_minus_sign:|:heavy_minus_sign:";
private static final FilteredLog LOG = new FilteredLog("Test");
private static final int TOO_MANY_FAILURES = 400;
private static final String INTEGRATION_TEST = "itest";
private static final String MODULE_TEST = "mtest";

@Test
void shouldSkipWhenThereAreNoScores() {
Expand Down Expand Up @@ -177,13 +179,13 @@ void shouldShowNoImpactsWithTwoSubResults() {
}

static Node createTwoReports(final ToolConfiguration tool) {
if (tool.getId().equals("itest")) {
if (INTEGRATION_TEST.equals(tool.getId())) {
if (tool.getName().contains("2")) {
return TestScoreTest.createTestReport(5, 3, 4, "2nd-");
}
return TestScoreTest.createTestReport(5, 3, 4);
}
else if (tool.getId().equals("mtest")) {
else if (MODULE_TEST.equals(tool.getId())) {
if (tool.getName().contains("2")) {
return TestScoreTest.createTestReport(0, 0, 10, "2nd-");
}
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/edu/hm/hafner/grading/TruncatedStringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ public void shouldTruncateByBytesOrChars(final boolean chunkOnNewlines, final bo
var builder = createBuilder(chunkOnNewlines);

builder.addText("☃☃☃\n"); // 3 + 1
assertThat(getRawString(builder).length()).isEqualTo(4);
assertThat(getRawString(builder)).hasSize(4);
assertThat(getRawString(builder).getBytes(StandardCharsets.UTF_8).length).isEqualTo(10);
assertThat(build(builder, chunkOnChars, 20)).isEqualTo("☃☃☃\n");

builder.addText("🕴️🕴️\n"); // 2 + 1
assertThat(getRawString(builder).length()).isEqualTo(11);
assertThat(getRawString(builder)).hasSize(11);
assertThat(getRawString(builder).getBytes(StandardCharsets.UTF_8).length).isEqualTo(25);
assertThat(build(builder, chunkOnChars, 20)).isEqualTo(chunkOnChars ? "☃☃☃\n🕴️🕴️\n" : "☃☃☃\nTruncated");
}
Expand All @@ -126,8 +126,8 @@ public void shouldTruncateByBytesOrChars(final boolean chunkOnNewlines, final bo
public void shouldHandleLongCharsInTruncationText(final boolean chunkOnNewlines, final boolean chunkOnChars) {
var builder = createBuilder(chunkOnNewlines);

String truncationText = "E_TOO_MUCH_☃";
assertThat(truncationText.length()).isEqualTo(12);
var truncationText = "E_TOO_MUCH_☃";
assertThat(truncationText).hasSize(12);
assertThat(truncationText.getBytes(StandardCharsets.UTF_8).length).isEqualTo(14);

builder.withTruncationText(truncationText);
Expand Down

0 comments on commit 46b00d1

Please sign in to comment.