Converted List to Collection whenever applicable

This commit is contained in:
Erik C. Thauvin 2024-06-22 20:25:01 -07:00
parent fe4770148d
commit a6747fdfe5
Signed by: erik
GPG key ID: 776702A6A2DA330E
2 changed files with 175 additions and 82 deletions

View file

@ -1,9 +0,0 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Run Tests" type="Application" factoryName="Application" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="rife.bld.extension.JacocoReportOperationTest" />
<module name="app" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>

View file

@ -35,6 +35,7 @@ import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
@ -52,61 +53,61 @@ public class JacocoReportOperation extends AbstractOperation<JacocoReportOperati
/** /**
* The location of the java class files. * The location of the java class files.
*/ */
final List<File> classFiles = new ArrayList<>(); final private Collection<File> classFiles_ = new ArrayList<>();
/** /**
* The location of the exec files. * The location of the exec files.
*/ */
final List<File> execFiles = new ArrayList<>(); final private Collection<File> execFiles_ = new ArrayList<>();
/** /**
* The location of the source files. * The location of the source files.
*/ */
final List<File> sourceFiles = new ArrayList<>(); final private Collection<File> sourceFiles_ = new ArrayList<>();
/** /**
* The location of the CSV report. * The location of the CSV report.
*/ */
File csv; private File csv_;
/** /**
* The file to write execution data to. * The file to write execution data to.
*/ */
File destFile; private File destFile_;
/** /**
* The source file encoding. * The source file encoding.
*/ */
String encoding; private String encoding_;
/** /**
* The location of the HTML report. * The location of the HTML report.
*/ */
File html; private File html_;
/**
* The report name.
*/
String name = "JaCoCo Coverage Report";
/** /**
* The project reference. * The project reference.
*/ */
BaseProject project; private BaseProject project_;
/** /**
* The quiet flag. * The quiet flag.
*/ */
boolean quiet; private boolean quiet_;
/**
* The report name.
*/
private String reportName_ = "JaCoCo Coverage Report";
/** /**
* THe tab width. * THe tab width.
*/ */
int tabWidth = 4; private int tabWidth_ = 4;
/** /**
* THe location of the XML report * THe location of the XML report
*/ */
File xml; private File xml_;
private IBundleCoverage analyze(ExecutionDataStore data) throws IOException { private IBundleCoverage analyze(ExecutionDataStore data) throws IOException {
var builder = new CoverageBuilder(); var builder = new CoverageBuilder();
var analyzer = new Analyzer(data, builder); var analyzer = new Analyzer(data, builder);
for (var f : classFiles) { for (var f : classFiles_) {
LOGGER.info(f.getAbsolutePath()); LOGGER.info(f.getAbsolutePath());
analyzer.analyzeAll(f); analyzer.analyzeAll(f);
} }
return builder.getBundle(name); return builder.getBundle(reportName_);
} }
/** /**
@ -116,10 +117,30 @@ public class JacocoReportOperation extends AbstractOperation<JacocoReportOperati
* @return this operation instance * @return this operation instance
*/ */
public JacocoReportOperation classFiles(File... classFiles) { public JacocoReportOperation classFiles(File... classFiles) {
this.classFiles.addAll(List.of(classFiles)); classFiles_.addAll(List.of(classFiles));
return this; return this;
} }
/**
* Sets the locations of Java class files.
*
* @param classFiles the class files
* @return this operation instance
*/
public JacocoReportOperation classFiles(String... classFiles) {
classFiles_.addAll(Arrays.stream(classFiles).map(File::new).toList());
return this;
}
/**
* Returns the locations of Java class files.
*
* @return the class files
*/
public Collection<File> classFiles() {
return classFiles_;
}
/** /**
* Sets the locations of Java class files. * Sets the locations of Java class files.
* *
@ -127,7 +148,7 @@ public class JacocoReportOperation extends AbstractOperation<JacocoReportOperati
* @return this operation instance * @return this operation instance
*/ */
public JacocoReportOperation classFiles(Collection<File> classFiles) { public JacocoReportOperation classFiles(Collection<File> classFiles) {
this.classFiles.addAll(classFiles); classFiles_.addAll(classFiles);
return this; return this;
} }
@ -138,7 +159,29 @@ public class JacocoReportOperation extends AbstractOperation<JacocoReportOperati
* @return this operation instance * @return this operation instance
*/ */
public JacocoReportOperation csv(File cvs) { public JacocoReportOperation csv(File cvs) {
this.csv = cvs; csv_ = cvs;
return this;
}
/**
* Sets the location of the CSV report.
*
* @param cvs the report location
* @return this operation instance
*/
public JacocoReportOperation csv(String cvs) {
return csv(new File(cvs));
}
/**
* Sets the file to write execution data to.
*
* @param destFile the file
* @return this operation instance
*/
public JacocoReportOperation destFile(File destFile) {
destFile_ = destFile;
return this; return this;
} }
@ -148,9 +191,8 @@ public class JacocoReportOperation extends AbstractOperation<JacocoReportOperati
* @param destFile the file * @param destFile the file
* @return this operation instance * @return this operation instance
*/ */
public JacocoReportOperation destFile(File destFile) { public JacocoReportOperation destFile(String destFile) {
this.destFile = destFile; return destFile(new File(destFile));
return this;
} }
/** /**
@ -160,7 +202,7 @@ public class JacocoReportOperation extends AbstractOperation<JacocoReportOperati
* @return this operation instance * @return this operation instance
*/ */
public JacocoReportOperation encoding(String encoding) { public JacocoReportOperation encoding(String encoding) {
this.encoding = encoding; encoding_ = encoding;
return this; return this;
} }
@ -171,7 +213,18 @@ public class JacocoReportOperation extends AbstractOperation<JacocoReportOperati
* @return this operation instance * @return this operation instance
*/ */
public JacocoReportOperation execFiles(File... execFiles) { public JacocoReportOperation execFiles(File... execFiles) {
this.execFiles.addAll(List.of(execFiles)); execFiles_.addAll(List.of(execFiles));
return this;
}
/**
* Sets the locations of the JaCoCo *.exec files to read.
*
* @param execFiles the exec files
* @return this operation instance
*/
public JacocoReportOperation execFiles(String... execFiles) {
execFiles_.addAll(Arrays.stream(execFiles).map(File::new).toList());
return this; return this;
} }
@ -182,64 +235,73 @@ public class JacocoReportOperation extends AbstractOperation<JacocoReportOperati
* @return this operation instance * @return this operation instance
*/ */
public JacocoReportOperation execFiles(Collection<File> execFiles) { public JacocoReportOperation execFiles(Collection<File> execFiles) {
this.execFiles.addAll(execFiles); execFiles_.addAll(execFiles);
return this; return this;
} }
/**
* Returns the locations of the JaCoCo *.exec files to read.
*
* @return the exec files
*/
public Collection<File> execFiles() {
return execFiles_;
}
/** /**
* Performs the operation execution that can be wrapped by the {@code #executeOnce} call. * Performs the operation execution that can be wrapped by the {@code #executeOnce} call.
*/ */
@Override @Override
public void execute() throws IOException { public void execute() throws IOException {
if ((project == null) && LOGGER.isLoggable(Level.SEVERE)) { if ((project_ == null) && LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.severe("A project must be specified."); LOGGER.severe("A project must be specified.");
} else { } else {
var buildJacocoReportsDir = Path.of(project.buildDirectory().getPath(), "reports", "jacoco", "test").toFile(); var buildJacocoReportsDir = Path.of(project_.buildDirectory().getPath(), "reports", "jacoco", "test").toFile();
var buildJacocoExecDir = Path.of(project.buildDirectory().getPath(), "jacoco").toFile(); var buildJacocoExecDir = Path.of(project_.buildDirectory().getPath(), "jacoco").toFile();
var buildJacocoExec = Path.of(buildJacocoExecDir.getPath(), "jacoco.exec").toFile(); var buildJacocoExec = Path.of(buildJacocoExecDir.getPath(), "jacoco.exec").toFile();
if (destFile == null) { if (destFile_ == null) {
destFile = Path.of(buildJacocoExecDir.getPath(), "jacoco.exec").toFile(); destFile_ = Path.of(buildJacocoExecDir.getPath(), "jacoco.exec").toFile();
} }
if (execFiles.isEmpty()) { if (execFiles_.isEmpty()) {
var testOperation = project.testOperation().fromProject(project); var testOperation = project_.testOperation().fromProject(project_);
testOperation.javaOptions().javaAgent(Path.of(project.libBldDirectory().getPath(), testOperation.javaOptions().javaAgent(Path.of(project_.libBldDirectory().getPath(),
"org.jacoco.agent-" + JaCoCo.VERSION.substring(0, JaCoCo.VERSION.lastIndexOf('.')) "org.jacoco.agent-" + JaCoCo.VERSION.substring(0, JaCoCo.VERSION.lastIndexOf('.'))
+ "-runtime.jar").toFile(), "destfile=" + destFile.getPath()); + "-runtime.jar").toFile(), "destfile=" + destFile_.getPath());
try { try {
testOperation.execute(); testOperation.execute();
} catch (InterruptedException | ExitStatusException e) { } catch (InterruptedException | ExitStatusException e) {
throw new IOException(e); throw new IOException(e);
} }
if (LOGGER.isLoggable(Level.INFO) && !quiet) { if (LOGGER.isLoggable(Level.INFO) && !quiet_) {
LOGGER.log(Level.INFO, "Execution Data: {0}", destFile); LOGGER.log(Level.INFO, "Execution Data: {0}", destFile_);
} }
if (buildJacocoExec.exists()) { if (buildJacocoExec.exists()) {
execFiles.add(buildJacocoExec); execFiles_.add(buildJacocoExec);
} }
} }
if (sourceFiles.isEmpty()) { if (sourceFiles_.isEmpty()) {
sourceFiles.add(project.srcMainJavaDirectory()); sourceFiles_.add(project_.srcMainJavaDirectory());
} }
if (classFiles.isEmpty()) { if (classFiles_.isEmpty()) {
classFiles.add(project.buildMainDirectory()); classFiles_.add(project_.buildMainDirectory());
} }
if (html == null) { if (html_ == null) {
html = new File(buildJacocoReportsDir, "html"); html_ = new File(buildJacocoReportsDir, "html");
} }
if (xml == null) { if (xml_ == null) {
xml = new File(buildJacocoReportsDir, "jacocoTestReport.xml"); xml_ = new File(buildJacocoReportsDir, "jacocoTestReport.xml");
} }
if (csv == null) { if (csv_ == null) {
csv = new File(buildJacocoReportsDir, "jacocoTestReport.csv"); csv_ = new File(buildJacocoReportsDir, "jacocoTestReport.csv");
} }
//noinspection ResultOfMethodCallIgnored //noinspection ResultOfMethodCallIgnored
@ -260,7 +322,7 @@ public class JacocoReportOperation extends AbstractOperation<JacocoReportOperati
* @return this operation instance * @return this operation instance
*/ */
public JacocoReportOperation fromProject(BaseProject project) { public JacocoReportOperation fromProject(BaseProject project) {
this.project = project; project_ = project;
return this; return this;
} }
@ -271,17 +333,27 @@ public class JacocoReportOperation extends AbstractOperation<JacocoReportOperati
* @return this operation instance * @return this operation instance
*/ */
public JacocoReportOperation html(File html) { public JacocoReportOperation html(File html) {
this.html = html; html_ = html;
return this; return this;
} }
/**
* Sets the location of the HTML report.
*
* @param html the html
* @return this operation instance
*/
public JacocoReportOperation html(String html) {
return html(new File(html));
}
private ExecFileLoader loadExecFiles() throws IOException { private ExecFileLoader loadExecFiles() throws IOException {
var loader = new ExecFileLoader(); var loader = new ExecFileLoader();
if (execFiles.isEmpty() && LOGGER.isLoggable(Level.WARNING) && !quiet) { if (execFiles_.isEmpty() && LOGGER.isLoggable(Level.WARNING) && !quiet_) {
LOGGER.warning("No execution data files provided."); LOGGER.warning("No execution data files provided.");
} else { } else {
for (var f : execFiles) { for (var f : execFiles_) {
if (LOGGER.isLoggable(Level.INFO) && !quiet) { if (LOGGER.isLoggable(Level.INFO) && !quiet_) {
LOGGER.log(Level.INFO, "Loading execution data: {0}", LOGGER.log(Level.INFO, "Loading execution data: {0}",
f.getAbsolutePath()); f.getAbsolutePath());
} }
@ -298,7 +370,7 @@ public class JacocoReportOperation extends AbstractOperation<JacocoReportOperati
* @return this operation instance * @return this operation instance
*/ */
public JacocoReportOperation name(String name) { public JacocoReportOperation name(String name) {
this.name = name; reportName_ = name;
return this; return this;
} }
@ -309,26 +381,26 @@ public class JacocoReportOperation extends AbstractOperation<JacocoReportOperati
* @return this operation instance * @return this operation instance
*/ */
public JacocoReportOperation quiet(boolean quiet) { public JacocoReportOperation quiet(boolean quiet) {
this.quiet = quiet; quiet_ = quiet;
return this; return this;
} }
private IReportVisitor reportVisitor() throws IOException { private IReportVisitor reportVisitor() throws IOException {
List<IReportVisitor> visitors = new ArrayList<>(); List<IReportVisitor> visitors = new ArrayList<>();
if (xml != null) { if (xml_ != null) {
var formatter = new XMLFormatter(); var formatter = new XMLFormatter();
visitors.add(formatter.createVisitor(Files.newOutputStream(xml.toPath()))); visitors.add(formatter.createVisitor(Files.newOutputStream(xml_.toPath())));
} }
if (csv != null) { if (csv_ != null) {
var formatter = new CSVFormatter(); var formatter = new CSVFormatter();
visitors.add(formatter.createVisitor(Files.newOutputStream(csv.toPath()))); visitors.add(formatter.createVisitor(Files.newOutputStream(csv_.toPath())));
} }
if (html != null) { if (html_ != null) {
var formatter = new HTMLFormatter(); var formatter = new HTMLFormatter();
visitors.add(formatter.createVisitor(new FileMultiReportOutput(html))); visitors.add(formatter.createVisitor(new FileMultiReportOutput(html_)));
} }
return new MultiReportVisitor(visitors); return new MultiReportVisitor(visitors);
@ -341,7 +413,18 @@ public class JacocoReportOperation extends AbstractOperation<JacocoReportOperati
* @return this operation instance * @return this operation instance
*/ */
public JacocoReportOperation sourceFiles(File... sourceFiles) { public JacocoReportOperation sourceFiles(File... sourceFiles) {
this.sourceFiles.addAll(List.of(sourceFiles)); sourceFiles_.addAll(List.of(sourceFiles));
return this;
}
/**
* Sets the locations of the source files. (e.g., {@code src/main/java})
*
* @param sourceFiles the source files
* @return this operation instance
*/
public JacocoReportOperation sourceFiles(String... sourceFiles) {
sourceFiles_.addAll(Arrays.stream(sourceFiles).map(File::new).toList());
return this; return this;
} }
@ -352,15 +435,24 @@ public class JacocoReportOperation extends AbstractOperation<JacocoReportOperati
* @return this operation instance * @return this operation instance
*/ */
public JacocoReportOperation sourceFiles(Collection<File> sourceFiles) { public JacocoReportOperation sourceFiles(Collection<File> sourceFiles) {
this.sourceFiles.addAll(sourceFiles); sourceFiles_.addAll(sourceFiles);
return this; return this;
} }
/**
* Returns the locations of the source files.
*
* @return the source files
*/
public Collection<File> sourceFiles() {
return sourceFiles_;
}
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private ISourceFileLocator sourceLocator() { private ISourceFileLocator sourceLocator() {
var multi = new MultiSourceFileLocator(tabWidth); var multi = new MultiSourceFileLocator(tabWidth_);
for (var f : sourceFiles) { for (var f : sourceFiles_) {
multi.add(new DirectorySourceFileLocator(f, encoding, tabWidth)); multi.add(new DirectorySourceFileLocator(f, encoding_, tabWidth_));
} }
return multi; return multi;
} }
@ -372,13 +464,13 @@ public class JacocoReportOperation extends AbstractOperation<JacocoReportOperati
* @return this operation instance * @return this operation instance
*/ */
public JacocoReportOperation tabWidth(int tabWidth) { public JacocoReportOperation tabWidth(int tabWidth) {
this.tabWidth = tabWidth; tabWidth_ = tabWidth;
return this; return this;
} }
private void writeReports(IBundleCoverage bundle, ExecFileLoader loader) private void writeReports(IBundleCoverage bundle, ExecFileLoader loader)
throws IOException { throws IOException {
if (LOGGER.isLoggable(Level.INFO) && !quiet) { if (LOGGER.isLoggable(Level.INFO) && !quiet_) {
LOGGER.log(Level.INFO, "Analyzing {0} classes.", LOGGER.log(Level.INFO, "Analyzing {0} classes.",
bundle.getClassCounter().getTotalCount()); bundle.getClassCounter().getTotalCount());
} }
@ -387,10 +479,10 @@ public class JacocoReportOperation extends AbstractOperation<JacocoReportOperati
loader.getExecutionDataStore().getContents()); loader.getExecutionDataStore().getContents());
visitor.visitBundle(bundle, sourceLocator()); visitor.visitBundle(bundle, sourceLocator());
visitor.visitEnd(); visitor.visitEnd();
if (LOGGER.isLoggable(Level.INFO) && !quiet) { if (LOGGER.isLoggable(Level.INFO) && !quiet_) {
LOGGER.log(Level.INFO, "XML Report: file://{0}", xml.toURI().getPath()); LOGGER.log(Level.INFO, "XML Report: file://{0}", xml_.toURI().getPath());
LOGGER.log(Level.INFO, "CSV Report: file://{0}", csv.toURI().getPath()); LOGGER.log(Level.INFO, "CSV Report: file://{0}", csv_.toURI().getPath());
LOGGER.log(Level.INFO, "HTML Report: file://{0}index.html", html.toURI().getPath()); LOGGER.log(Level.INFO, "HTML Report: file://{0}index.html", html_.toURI().getPath());
} }
} }
@ -401,7 +493,17 @@ public class JacocoReportOperation extends AbstractOperation<JacocoReportOperati
* @return this operation instance * @return this operation instance
*/ */
public JacocoReportOperation xml(File xml) { public JacocoReportOperation xml(File xml) {
this.xml = xml; xml_ = xml;
return this; return this;
} }
/**
* Sets the location of the XML report.
*
* @param xml the report location
* @return this operation instance
*/
public JacocoReportOperation xml(String xml) {
return xml(new File(xml));
}
} }