com.teamscale.report.ReportUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of report-generator Show documentation
Show all versions of report-generator Show documentation
Utilities for generating JaCoCo and Testwise Coverage reports
package com.teamscale.report;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import com.squareup.moshi.Types;
import com.teamscale.client.FileSystemUtils;
import com.teamscale.client.TestDetails;
import com.teamscale.report.testwise.ETestArtifactFormat;
import com.teamscale.report.testwise.model.TestExecution;
import com.teamscale.report.testwise.model.TestwiseCoverageReport;
import okio.BufferedSink;
import okio.BufferedSource;
import okio.Okio;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/** Utilities for generating reports. */
public class ReportUtils {
private static Moshi moshi = new Moshi.Builder().build();
private static JsonAdapter> testDetailsAdapter = moshi.>adapter(
Types.newParameterizedType(List.class, TestDetails.class)).indent("\t");
private static JsonAdapter> testExecutionAdapter = moshi.>adapter(
Types.newParameterizedType(List.class, TestExecution.class)).indent("\t");
private static JsonAdapter testwiseCoverageReportAdapter = moshi
.adapter(TestwiseCoverageReport.class).indent("\t");
/** Converts to given test list to a json report and writes it to the given file. */
public static void writeTestListReport(File reportFile, List report) throws IOException {
writeReportToFile(reportFile, report, testDetailsAdapter);
}
/** Converts to given test execution report to a json report and writes it to the given file. */
public static void writeTestExecutionReport(File reportFile, List report) throws IOException {
writeReportToFile(reportFile, report, testExecutionAdapter);
}
/** Converts to given testwise coverage report to a json report and writes it to the given file. */
public static void writeTestwiseCoverageReport(File reportFile, TestwiseCoverageReport report) throws IOException {
writeReportToFile(reportFile, report, testwiseCoverageReportAdapter);
}
/** Converts to given report to a json string. For testing only. */
public static String getTestwiseCoverageReportAsString(TestwiseCoverageReport report) {
return testwiseCoverageReportAdapter.toJson(report);
}
/** Writes the report object to the given file as json. */
private static void writeReportToFile(File reportFile, T report, JsonAdapter adapter) throws IOException {
File directory = reportFile.getParentFile();
if (!directory.isDirectory() && !directory.mkdirs()) {
throw new IOException("Failed to create directory " + directory.getAbsolutePath());
}
try (BufferedSink sink = Okio.buffer(Okio.sink(reportFile))) {
adapter.toJson(sink, report);
}
}
/** Recursively lists all files in the given directory that match the specified extension. */
public static List readObjects(ETestArtifactFormat format, Class clazz,
List directoriesOrFiles) throws IOException {
List files = listFiles(format, directoriesOrFiles);
ArrayList result = new ArrayList<>();
for (File file : files) {
try (BufferedSource source = Okio.buffer(Okio.source(file))) {
T[] t = moshi.adapter(clazz).fromJson(source);
if (t != null) {
result.addAll(Arrays.asList(t));
}
}
}
return result;
}
/** Recursively lists all files of the given artifact type. */
public static List listFiles(ETestArtifactFormat format, List directoriesOrFiles) {
List filesWithSpecifiedArtifactType = new ArrayList<>();
for (File directoryOrFile : directoriesOrFiles) {
if (directoryOrFile.isDirectory()) {
filesWithSpecifiedArtifactType.addAll(FileSystemUtils
.listFilesRecursively(directoryOrFile, file -> fileIsOfArtifactFormat(file, format)));
} else if (fileIsOfArtifactFormat(directoryOrFile, format)) {
filesWithSpecifiedArtifactType.add(directoryOrFile);
}
}
return filesWithSpecifiedArtifactType;
}
private static boolean fileIsOfArtifactFormat(File file, ETestArtifactFormat format) {
return file.isFile() &&
file.getName().startsWith(format.filePrefix) &&
FileSystemUtils.getFileExtension(file).equalsIgnoreCase(format.extension);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy