Skip to content

Commit

Permalink
Use Java 17 language features
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkEWaite committed Nov 22, 2024
1 parent 4098941 commit fc79c4a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 31 deletions.
49 changes: 23 additions & 26 deletions src/main/java/hudson/plugins/testng/Publisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,10 @@ public void perform(Run<?, ?> build, FilePath workspace, Launcher launcher, fina
}

String pathsPattern;
if (build instanceof AbstractBuild) {
if (build instanceof AbstractBuild<?, ?> abstractBuild) {
// replace any variables in the user specified pattern
EnvVars env = build.getEnvironment(listener);
env.overrideAll(((AbstractBuild) build).getBuildVariables());
env.overrideAll(abstractBuild.getBuildVariables());
pathsPattern = env.expand(reportFilenamePattern);
} else {
pathsPattern = reportFilenamePattern;
Expand Down Expand Up @@ -268,48 +268,45 @@ public void perform(Run<?, ?> build, FilePath workspace, Launcher launcher, fina
} else {
if (thresholdMode == 1) { // number of tests
if (results.getFailCount() > failedFails) {
logger.println(String.format(
"%d tests failed, which exceeded threshold of %d. Marking build as FAILURE",
results.getFailCount(), failedFails));
logger.println("%d tests failed, which exceeded threshold of %d. Marking build as FAILURE"
.formatted(results.getFailCount(), failedFails));
build.setResult(Result.FAILURE);
} else if (results.getSkipCount() > failedSkips) {
logger.println(String.format(
"%d tests were skipped, which exceeded threshold of %d. Marking build as FAILURE",
results.getSkipCount(), failedSkips));
logger.println("%d tests were skipped, which exceeded threshold of %d. Marking build as FAILURE"
.formatted(results.getSkipCount(), failedSkips));
build.setResult(Result.FAILURE);
} else if (results.getFailCount() > unstableFails) {
logger.println(String.format(
"%d tests failed, which exceeded threshold of %d. Marking build as UNSTABLE",
results.getFailCount(), unstableFails));
logger.println("%d tests failed, which exceeded threshold of %d. Marking build as UNSTABLE"
.formatted(results.getFailCount(), unstableFails));
build.setResult(Result.UNSTABLE);
} else if (results.getSkipCount() > unstableSkips) {
logger.println(String.format(
"%d tests were skipped, which exceeded threshold of %d. Marking build as UNSTABLE",
results.getSkipCount(), unstableSkips));
logger.println(
"%d tests were skipped, which exceeded threshold of %d. Marking build as UNSTABLE"
.formatted(results.getSkipCount(), unstableSkips));
build.setResult(Result.UNSTABLE);
}
} else if (thresholdMode == 2) { // percentage of tests
float failedPercent = 100 * results.getFailCount() / (float) results.getTotalCount();
float skipPercent = 100 * results.getSkipCount() / (float) results.getTotalCount();
if (failedPercent > failedFails) {
logger.println(String.format(
"%f%% of tests failed, which exceeded threshold of %d%%. Marking build as FAILURE",
failedPercent, failedFails));
logger.println(
"%f%% of tests failed, which exceeded threshold of %d%%. Marking build as FAILURE"
.formatted(failedPercent, failedFails));
build.setResult(Result.FAILURE);
} else if (skipPercent > failedSkips) {
logger.println(String.format(
"%f%% of tests were skipped, which exceeded threshold of %d%%. Marking build as FAILURE",
skipPercent, failedSkips));
logger.println(
"%f%% of tests were skipped, which exceeded threshold of %d%%. Marking build as FAILURE"
.formatted(skipPercent, failedSkips));

Check warning on line 299 in src/main/java/hudson/plugins/testng/Publisher.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 271-299 are not covered by tests
build.setResult(Result.FAILURE);
} else if (failedPercent > unstableFails) {
logger.println(String.format(
"%f%% of tests failed, which exceeded threshold of %d%%. Marking build as UNSTABLE",
failedPercent, unstableFails));
logger.println(
"%f%% of tests failed, which exceeded threshold of %d%%. Marking build as UNSTABLE"
.formatted(failedPercent, unstableFails));
build.setResult(Result.UNSTABLE);
} else if (skipPercent > unstableSkips) {
logger.println(String.format(
"%f%% of tests were skipped, which exceeded threshold of %d%%. Marking build as UNSTABLE",
skipPercent, unstableSkips));
logger.println(
"%f%% of tests were skipped, which exceeded threshold of %d%%. Marking build as UNSTABLE"
.formatted(skipPercent, unstableSkips));
build.setResult(Result.UNSTABLE);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import hudson.tasks.test.AbstractTestResultAction;
import java.io.IOException;
import java.io.PrintStream;
import java.io.Serial;
import java.io.Serializable;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
Expand All @@ -36,6 +37,7 @@ public class TestNGTestResultBuildAction extends AbstractTestResultAction
private static final Logger LOGGER = Logger.getLogger(TestNGTestResultBuildAction.class.getName());

/** Unique identifier for this class. */
@Serial
private static final long serialVersionUID = 31415926L;

/**
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/hudson/plugins/testng/results/TestNGResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.model.Run;
import hudson.plugins.testng.PluginImpl;
import java.io.Serial;
import java.io.Serializable;
import java.util.*;
import org.kohsuke.stapler.export.Exported;
Expand All @@ -17,7 +18,9 @@
@SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "ArrayList is Serializable")
public class TestNGResult extends BaseResult implements Serializable {

@Serial
private static final long serialVersionUID = -3491974223665601995L;

private List<TestNGTestResult> testList = new ArrayList<TestNGTestResult>();
private List<MethodResult> passedTests = new ArrayList<MethodResult>();
private List<MethodResult> failedTests = new ArrayList<MethodResult>();
Expand Down Expand Up @@ -169,12 +172,12 @@ public int hashCode() {

@Override
public String toString() {
return String.format(
"TestNGResult {"
return ("TestNGResult {"
+ "totalTests=%d, "
+ "failedTests=%d, skippedTests=%d, failedConfigs=%d, "
+ "skippedConfigs=%d}", // name,
passCount + failCount + skipCount, failCount, skipCount, failedConfigCount, skippedConfigCount);
+ "skippedConfigs=%d}")
.formatted( // name,
passCount + failCount + skipCount, failCount, skipCount, failedConfigCount, skippedConfigCount);
}

/** Updates the calculated fields */
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/testng/util/FormatUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static String formatTime(float duration) {
byte seconds = (byte) duration;
duration -= seconds;
int milliseconds = Math.round(duration * 1000f);
return String.format("%02d:%02d:%02d.%03d", hours, minutes, seconds, milliseconds);
return "%02d:%02d:%02d.%03d".formatted(hours, minutes, seconds, milliseconds);
} catch (Exception e) {
e.printStackTrace();
return "-1";
Expand Down

0 comments on commit fc79c4a

Please sign in to comment.