Skip to content

Commit

Permalink
Code Improvements (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
darxriggs committed Aug 13, 2021
1 parent 0cb6356 commit 06c671c
Show file tree
Hide file tree
Showing 41 changed files with 127 additions and 155 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@
<scope>import</scope>
<type>pom</type>
</dependency>
<!-- mockito and hibernete are conflicting (both test dependencies) -->
<!-- mockito and hibernate are conflicting (both test dependencies) -->
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/hudson/tasks/junit/CaseResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ public float getDuration() {
if(!Character.isJavaIdentifierPart(ch))
buf.setCharAt(i,'_');
}
Collection<CaseResult> siblings = classResult ==null ? Collections.<CaseResult>emptyList(): classResult.getChildren();
Collection<CaseResult> siblings = classResult ==null ? Collections.emptyList(): classResult.getChildren();
return safeName = uniquifyName(siblings, buf.toString());
}

Expand Down Expand Up @@ -791,11 +791,7 @@ public boolean isRegression() {
/**
* For sorting errors by age.
*/
/*package*/ static final Comparator<CaseResult> BY_AGE = new Comparator<CaseResult>() {
public int compare(CaseResult lhs, CaseResult rhs) {
return lhs.getAge()-rhs.getAge();
}
};
/*package*/ static final Comparator<CaseResult> BY_AGE = Comparator.comparingInt(CaseResult::getAge);

private static final long serialVersionUID = 1L;

Expand Down
12 changes: 2 additions & 10 deletions src/main/java/hudson/tasks/junit/ClassResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,9 @@
package hudson.tasks.junit;

import hudson.model.Run;
import io.jenkins.plugins.junit.storage.FileJunitTestResultStorage;
import io.jenkins.plugins.junit.storage.TestResultImpl;
import io.jenkins.plugins.junit.storage.JunitTestResultStorage;
import hudson.tasks.test.TabulatedResult;
import hudson.tasks.test.TestResult;
import hudson.tasks.test.TestObject;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.export.Exported;
Expand All @@ -48,7 +44,7 @@ public final class ClassResult extends TabulatedResult implements Comparable<Cla
private final String className; // simple name
private transient String safeName;

private final List<CaseResult> cases = new ArrayList<CaseResult>();
private final List<CaseResult> cases = new ArrayList<>();

private int passCount,failCount,skipCount;

Expand Down Expand Up @@ -91,12 +87,8 @@ public hudson.tasks.test.TestResult findCorrespondingResult(String id) {
if (id.length() > caseNameStart) {
caseName = id.substring(caseNameStart);
}
}
CaseResult child = getCaseResult(caseName);
if (child != null) {
return child;
}
return null;
return getCaseResult(caseName);
}

public String getTitle() {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/hudson/tasks/junit/JUnitResultArchiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public JUnitResultArchiver(
double healthScaleFactor) {
this.testResults = testResults;
setKeepLongStdio(keepLongStdio);
setTestDataPublishers(testDataPublishers == null ? Collections.<TestDataPublisher>emptyList() : testDataPublishers);
setTestDataPublishers(testDataPublishers == null ? Collections.emptyList() : testDataPublishers);
setHealthScaleFactor(healthScaleFactor);
setAllowEmptyResults(false);
}
Expand Down Expand Up @@ -347,7 +347,7 @@ public double getHealthScaleFactor() {
}

public @Nonnull List<TestDataPublisher> getTestDataPublishers() {
return testDataPublishers == null ? Collections.<TestDataPublisher>emptyList() : testDataPublishers;
return testDataPublishers == null ? Collections.emptyList() : testDataPublishers;
}

/**
Expand All @@ -356,7 +356,7 @@ public double getHealthScaleFactor() {
* @since 1.2
*/
@DataBoundSetter public final void setTestDataPublishers(@Nonnull List<TestDataPublisher> testDataPublishers) {
this.testDataPublishers = new DescribableList<TestDataPublisher,Descriptor<TestDataPublisher>>(Saveable.NOOP);
this.testDataPublishers = new DescribableList<>(Saveable.NOOP);
this.testDataPublishers.addAll(testDataPublishers);
}

Expand Down
14 changes: 7 additions & 7 deletions src/main/java/hudson/tasks/junit/PackageResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public final class PackageResult extends MetaTabulatedResult implements Comparab
/**
* All {@link ClassResult}s keyed by their short name.
*/
private final Map<String,ClassResult> classes = new TreeMap<String,ClassResult>();
private final Map<String,ClassResult> classes = new TreeMap<>();
private int passCount,failCount,skipCount;
private final hudson.tasks.junit.TestResult parent;
private float duration;
Expand Down Expand Up @@ -181,7 +181,7 @@ public List<CaseResult> getFailedTests() {
return pluggableStorage.getFailedTestsByPackage(packageName);
}

List<CaseResult> r = new ArrayList<CaseResult>();
List<CaseResult> r = new ArrayList<>();
for (ClassResult clr : classes.values()) {
for (CaseResult cr : clr.getChildren()) {
if (cr.isFailed()) {
Expand All @@ -199,7 +199,7 @@ public List<CaseResult> getFailedTests() {
*/
public List<CaseResult> getFailedTestsSortedByAge() {
List<CaseResult> failedTests = getFailedTests();
Collections.sort(failedTests, CaseResult.BY_AGE);
failedTests.sort(CaseResult.BY_AGE);
return failedTests;
}

Expand All @@ -215,15 +215,15 @@ public List<CaseResult> getPassedTests() {
return pluggableStorage.getPassedTestsByPackage(packageName);
}

List<CaseResult> r = new ArrayList<CaseResult>();
List<CaseResult> r = new ArrayList<>();
for (ClassResult clr : classes.values()) {
for (CaseResult cr : clr.getChildren()) {
if (cr.isPassed()) {
r.add(cr);
}
}
}
Collections.sort(r,CaseResult.BY_AGE);
r.sort(CaseResult.BY_AGE);
return r;
}

Expand All @@ -239,15 +239,15 @@ public List<CaseResult> getSkippedTests() {
return pluggableStorage.getSkippedTestsByPackage(packageName);
}

List<CaseResult> r = new ArrayList<CaseResult>();
List<CaseResult> r = new ArrayList<>();
for (ClassResult clr : classes.values()) {
for (CaseResult cr : clr.getChildren()) {
if (cr.isSkipped()) {
r.add(cr);
}
}
}
Collections.sort(r, CaseResult.BY_AGE);
r.sort(CaseResult.BY_AGE);
return r;
}

Expand Down
17 changes: 6 additions & 11 deletions src/main/java/hudson/tasks/junit/SuiteResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public final class SuiteResult implements Serializable {
/**
* All test cases.
*/
private final List<CaseResult> cases = new ArrayList<CaseResult>();
private final List<CaseResult> cases = new ArrayList<>();
private transient Map<String, CaseResult> casesByName;
private transient hudson.tasks.junit.TestResult parent;

Expand Down Expand Up @@ -159,7 +159,7 @@ public static class SuiteResultParserConfigurationContext {
*/
static List<SuiteResult> parse(File xmlReport, boolean keepLongStdio, PipelineTestDetails pipelineTestDetails)
throws DocumentException, IOException, InterruptedException {
List<SuiteResult> r = new ArrayList<SuiteResult>();
List<SuiteResult> r = new ArrayList<>();

// parse into DOM
SAXReader saxReader = new SAXReader();
Expand All @@ -173,14 +173,11 @@ static List<SuiteResult> parse(File xmlReport, boolean keepLongStdio, PipelineTe

saxReader.setEntityResolver(new XMLEntityResolver());

FileInputStream xmlReportStream = new FileInputStream(xmlReport);
try {
try (FileInputStream xmlReportStream = new FileInputStream(xmlReport)) {
Document result = saxReader.read(xmlReportStream);
Element root = result.getRootElement();

parseSuite(xmlReport, keepLongStdio, r, root, pipelineTestDetails);
} finally {
xmlReportStream.close();
}

return r;
Expand All @@ -198,8 +195,7 @@ private static void setFeatureQuietly(SAXReader reader, String feature, boolean
private static void parseSuite(File xmlReport, boolean keepLongStdio, List<SuiteResult> r, Element root,
PipelineTestDetails pipelineTestDetails) throws DocumentException, IOException {
// nested test suites
@SuppressWarnings("unchecked")
List<Element> testSuites = (List<Element>) root.elements("testsuite");
List<Element> testSuites = root.elements("testsuite");
for (Element suite : testSuites)
parseSuite(xmlReport, keepLongStdio, r, suite, pipelineTestDetails);

Expand Down Expand Up @@ -245,8 +241,7 @@ private SuiteResult(File xmlReport, Element suite, boolean keepLongStdio, @Check
addCase(new CaseResult(this, suite, "<init>", keepLongStdio));
}

@SuppressWarnings("unchecked")
List<Element> testCases = (List<Element>) suite.elements("testcase");
List<Element> testCases = suite.elements("testcase");
for (Element e : testCases) {
// https://issues.jenkins-ci.org/browse/JENKINS-1233 indicates that
// when <testsuites> is present, we are better off using @classname on the
Expand Down Expand Up @@ -433,7 +428,7 @@ public CaseResult getCase(String caseResultFullDisplayName) {
}

public Set<String> getClassNames() {
Set<String> result = new HashSet<String>();
Set<String> result = new HashSet<>();
for (CaseResult c : cases) {
result.add(c.getClassName());
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/tasks/junit/TestDataPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public TestResultAction.Data getTestData(
}

public static DescriptorExtensionList<TestDataPublisher, Descriptor<TestDataPublisher>> all() {
return Jenkins.get().<TestDataPublisher, Descriptor<TestDataPublisher>>getDescriptorList(TestDataPublisher.class);
return Jenkins.get().getDescriptorList(TestDataPublisher.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
import java.util.Map;

import static hudson.tasks.test.TestDurationTrendSeriesBuilder.SECONDS;
import static hudson.tasks.test.TestResultTrendSeriesBuilder.FAILED_KEY;
import static hudson.tasks.test.TestResultTrendSeriesBuilder.PASSED_KEY;
import static hudson.tasks.test.TestResultTrendSeriesBuilder.SKIPPED_KEY;
import static hudson.tasks.test.TestResultTrendSeriesBuilder.TOTALS_KEY;

public class TestDurationResultSummary implements Serializable {

Expand Down
19 changes: 8 additions & 11 deletions src/main/java/hudson/tasks/junit/TestResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,7 @@ public void parse(File reportFile, PipelineTestDetails pipelineTestDetails) thro
try {
for (SuiteResult suiteResult : SuiteResult.parse(reportFile, keepLongStdio, pipelineTestDetails))
add(suiteResult);
} catch (InterruptedException e) {
throw new IOException("Failed to read "+reportFile,e);
} catch (RuntimeException e) {
} catch (InterruptedException | RuntimeException e) {
throw new IOException("Failed to read "+reportFile,e);
} catch (DocumentException e) {
if (!reportFile.getPath().endsWith(".xml")) {
Expand Down Expand Up @@ -547,7 +545,7 @@ public synchronized List<CaseResult> getPassedTests() {
}

if(passedTests == null){
passedTests = new ArrayList<CaseResult>();
passedTests = new ArrayList<>();
for(SuiteResult s : suites) {
for(CaseResult cr : s.getCases()) {
if (cr.isPassed()) {
Expand All @@ -572,7 +570,7 @@ public synchronized List<CaseResult> getSkippedTests() {
}

if(skippedTests == null){
skippedTests = new ArrayList<CaseResult>();
skippedTests = new ArrayList<>();
for(SuiteResult s : suites) {
for(CaseResult cr : s.getCases()) {
if (cr.isSkipped()) {
Expand Down Expand Up @@ -668,8 +666,7 @@ public boolean isPassed() {
@Override
public Collection<PackageResult> getChildren() {
if (impl != null) {
List<PackageResult> allPackageResults = impl.getAllPackageResults();
return allPackageResults;
return impl.getAllPackageResults();
}

return byPackages.values();
Expand Down Expand Up @@ -864,14 +861,14 @@ public void freeze(TestResultAction parent) {
}
}

Collections.sort(failedTests,CaseResult.BY_AGE);
failedTests.sort(CaseResult.BY_AGE);

if(passedTests != null) {
Collections.sort(passedTests,CaseResult.BY_AGE);
passedTests.sort(CaseResult.BY_AGE);
}

if(skippedTests != null) {
Collections.sort(skippedTests,CaseResult.BY_AGE);
skippedTests.sort(CaseResult.BY_AGE);
}

for (PackageResult pr : byPackages.values())
Expand All @@ -884,7 +881,7 @@ private void addSuiteByNode(SuiteResult s) {
if (nodeId != null) {
// If we don't already have an entry for this node, initialize a list for it.
if (suitesByNode.get(nodeId) == null) {
suitesByNode.put(nodeId, new ArrayList<SuiteResult>());
suitesByNode.put(nodeId, new ArrayList<>());
}
// Add the suite to the list for the node in the map. Phew.
suitesByNode.get(nodeId).add(s);
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/hudson/tasks/junit/TestResultAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public synchronized void setResult(TestResult result, TaskListener listener) {
}
}

this.result = new WeakReference<TestResult>(result);
this.result = new WeakReference<>(result);
}

@Deprecated
Expand All @@ -148,14 +148,14 @@ public synchronized TestResult getResult() {
TestResult r;
if(result==null) {
r = load();
result = new WeakReference<TestResult>(r);
result = new WeakReference<>(r);
} else {
r = result.get();
}

if(r==null) {
r = load();
result = new WeakReference<TestResult>(r);
result = new WeakReference<>(r);
}
if(totalCount==null) {
totalCount = r.getTotalCount();
Expand Down Expand Up @@ -244,7 +244,7 @@ public Object getTarget() {
}

public List<TestAction> getActions(TestObject object) {
List<TestAction> result = new ArrayList<TestAction>();
List<TestAction> result = new ArrayList<>();
// Added check for null testData to avoid NPE from issue 4257.
if (testData != null) {
synchronized (testData) {
Expand Down Expand Up @@ -316,7 +316,7 @@ public static abstract class Data {
public Object readResolve() {
super.readResolve(); // let it do the post-deserialization work
if (testData == null) {
testData = new ArrayList<Data>(0);
testData = new ArrayList<>(0);
}

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public final void setHealthScaleFactor(double healthScaleFactor) {

public @Nonnull
List<TestDataPublisher> getTestDataPublishers() {
return testDataPublishers == null ? Collections.<TestDataPublisher>emptyList() : testDataPublishers;
return testDataPublishers == null ? Collections.emptyList() : testDataPublishers;
}

/**
Expand All @@ -94,7 +94,7 @@ List<TestDataPublisher> getTestDataPublishers() {
* @since 1.2
*/
@DataBoundSetter public final void setTestDataPublishers(@Nonnull List<TestDataPublisher> testDataPublishers) {
this.testDataPublishers = new DescribableList<TestDataPublisher,Descriptor<TestDataPublisher>>(Saveable.NOOP);
this.testDataPublishers = new DescribableList<>(Saveable.NOOP);
this.testDataPublishers.addAll(testDataPublishers);
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/hudson/tasks/test/AbstractTestResultAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public abstract class AbstractTestResultAction<T extends AbstractTestResultActio
@Deprecated
public transient AbstractBuild<?,?> owner;

private Map<String,String> descriptions = new ConcurrentHashMap<String, String>();
private Map<String,String> descriptions = new ConcurrentHashMap<>();

/** @since 1.545 */
protected AbstractTestResultAction() {}
Expand Down Expand Up @@ -340,10 +340,10 @@ private Area calcDefaultSize() {
}

private CategoryDataset buildDataSet(StaplerRequest req) {
boolean failureOnly = Boolean.valueOf(req.getParameter("failureOnly"));
boolean failureOnly = Boolean.parseBoolean(req.getParameter("failureOnly"));

// TODO stop using ChartUtil.NumberOnlyBuildLabel as it forces loading of a Run; create a plainer Comparable
DataSetBuilder<String,NumberOnlyBuildLabel> dsb = new DataSetBuilder<String,NumberOnlyBuildLabel>();
DataSetBuilder<String,NumberOnlyBuildLabel> dsb = new DataSetBuilder<>();

int cap = Integer.getInteger(AbstractTestResultAction.class.getName() + ".test.trend.max", Integer.MAX_VALUE);
int count = 0;
Expand Down Expand Up @@ -465,7 +465,7 @@ protected void setDescription(TestObject object, String description) {

public Object readResolve() {
if (descriptions == null) {
descriptions = new ConcurrentHashMap<String, String>();
descriptions = new ConcurrentHashMap<>();
}

return this;
Expand Down

0 comments on commit 06c671c

Please sign in to comment.