From 2f51345f7f1ab922597eac6ddf047f1c3376d255 Mon Sep 17 00:00:00 2001 From: Maduranga Siriwardena Date: Fri, 13 Sep 2024 14:09:25 +0530 Subject: [PATCH] Improve generator code --- .../identity/jacoco/CodeCoverageUtils.java | 95 +++++++------------ .../identity/jacoco/ReportGenerator.java | 83 +++++++--------- 2 files changed, 70 insertions(+), 108 deletions(-) diff --git a/modules/integration/tests-common/jacoco-report-generator/src/main/java/org/wso2/carbon/identity/jacoco/CodeCoverageUtils.java b/modules/integration/tests-common/jacoco-report-generator/src/main/java/org/wso2/carbon/identity/jacoco/CodeCoverageUtils.java index b18a61ccfa..7031e0a80d 100644 --- a/modules/integration/tests-common/jacoco-report-generator/src/main/java/org/wso2/carbon/identity/jacoco/CodeCoverageUtils.java +++ b/modules/integration/tests-common/jacoco-report-generator/src/main/java/org/wso2/carbon/identity/jacoco/CodeCoverageUtils.java @@ -26,37 +26,36 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; +/** + * Utility class for code coverage related operations. + */ public class CodeCoverageUtils { + private static final int BUFFER_SIZE = 1024; + private static final String INVALID_EXTENSION_ERROR = "Invalid extension: %s is invalid"; + private static final String EXTRACTION_ERROR = "Error on archive extraction"; + /** * Extract jar files given at jar file path * - * @param jarFilePath - Jar file patch + * @param jarFilePath - Jar file path + * @param tempDir - Temporary directory to extract jar file * @return - Jar file extracted directory. * @throws IOException - Throws if jar extraction fails */ - public synchronized static String extractJarFile(String jarFilePath, File tempDir) - throws IOException { + public synchronized static String extractJarFile(String jarFilePath, File tempDir) throws IOException { if (!jarFilePath.endsWith(".war") && !jarFilePath.endsWith(".jar")) { - throw new IllegalArgumentException("Invalid extension" + jarFilePath + " is invalid"); + throw new IllegalArgumentException(String.format(INVALID_EXTENSION_ERROR, jarFilePath)); } - String fileSeparator = (File.separatorChar == '\\') ? "\\" : File.separator; - String jarFileName = jarFilePath; - - if (jarFilePath.lastIndexOf(fileSeparator) != -1) { - jarFileName = jarFilePath.substring(jarFilePath.lastIndexOf(fileSeparator) + 1); - } + String jarFileName = new File(jarFilePath).getName(); + String tempExtractedDir = new File(tempDir, jarFileName.substring(0, jarFileName.lastIndexOf('.'))).getPath(); - String tempExtractedDir = null; try { - tempExtractedDir = tempDir + File.separator + - jarFileName.substring(0, jarFileName.lastIndexOf('.')); - extractFile(jarFilePath, tempExtractedDir); } catch (IOException e) { - System.out.println("Could not extract the file " + jarFileName); + throw new IOException("Could not extract the file " + jarFileName, e); } return tempExtractedDir; } @@ -72,6 +71,7 @@ public synchronized static String extractJarFile(String jarFilePath, File tempDi */ public static String[] scanDirectory(String jarExtractedDir, String[] includes, String[] excludes) throws IOException { + DirectoryScanner ds = new DirectoryScanner(); ds.setIncludes(includes); @@ -83,58 +83,33 @@ public static String[] scanDirectory(String jarExtractedDir, String[] includes, return ds.getIncludedFiles(); } - public static void extractFile(String sourceFilePath, String extractedDir) throws IOException { - FileOutputStream fileoutputstream = null; - String fileDestination = extractedDir + File.separator; - byte[] buf = new byte[1024]; - ZipInputStream zipinputstream = null; - ZipEntry zipentry; - try { - zipinputstream = new ZipInputStream(new FileInputStream(sourceFilePath)); - zipentry = zipinputstream.getNextEntry(); - while (zipentry != null) { - //for each entry to be extracted - String entryName = fileDestination + zipentry.getName(); - entryName = entryName.replace('/', File.separatorChar); - entryName = entryName.replace('\\', File.separatorChar); - int n; - File newFile = new File(entryName); - if (zipentry.isDirectory()) { - if (!newFile.exists()) { - if (!newFile.mkdirs()) { - throw new IOException("Error occurred created new directory"); - } + private static void extractFile(String sourceFilePath, String extractedDir) throws IOException { + + byte[] buf = new byte[BUFFER_SIZE]; + try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(sourceFilePath))) { + ZipEntry zipEntry; + while ((zipEntry = zipInputStream.getNextEntry()) != null) { + File newFile = new File(extractedDir, zipEntry.getName()); + if (zipEntry.isDirectory()) { + if (!newFile.isDirectory() && !newFile.mkdirs()) { + throw new IOException("Failed to create directory " + newFile); } - zipentry = zipinputstream.getNextEntry(); - continue; } else { - File resourceFile = - new File(entryName.substring(0, entryName.lastIndexOf(File.separator))); - if (!resourceFile.exists()) { - if (!resourceFile.mkdirs()) { - break; + File parent = newFile.getParentFile(); + if (!parent.isDirectory() && !parent.mkdirs()) { + throw new IOException("Failed to create directory " + parent); + } + try (FileOutputStream fos = new FileOutputStream(newFile)) { + int len; + while ((len = zipInputStream.read(buf)) > 0) { + fos.write(buf, 0, len); } } } - fileoutputstream = new FileOutputStream(entryName); - while ((n = zipinputstream.read(buf, 0, 1024)) > -1) { - fileoutputstream.write(buf, 0, n); - } - fileoutputstream.close(); - zipinputstream.closeEntry(); - zipentry = zipinputstream.getNextEntry(); + zipInputStream.closeEntry(); } - zipinputstream.close(); } catch (IOException e) { - System.out.println("Error on archive extraction "); - throw new IOException("Error on archive extraction ", e); - } finally { - if (fileoutputstream != null) { - fileoutputstream.close(); - } - if (zipinputstream != null) { - zipinputstream.close(); - } + throw new IOException(EXTRACTION_ERROR, e); } } } diff --git a/modules/integration/tests-common/jacoco-report-generator/src/main/java/org/wso2/carbon/identity/jacoco/ReportGenerator.java b/modules/integration/tests-common/jacoco-report-generator/src/main/java/org/wso2/carbon/identity/jacoco/ReportGenerator.java index eab96a1498..fb52120a34 100644 --- a/modules/integration/tests-common/jacoco-report-generator/src/main/java/org/wso2/carbon/identity/jacoco/ReportGenerator.java +++ b/modules/integration/tests-common/jacoco-report-generator/src/main/java/org/wso2/carbon/identity/jacoco/ReportGenerator.java @@ -42,7 +42,6 @@ public class ReportGenerator { public static final String CLASS_FILE_PATTERN = "**/*.class"; private final String title; - private final File executionDataFile; private final Set classDirectories; private final File xmlReport; @@ -57,7 +56,8 @@ public class ReportGenerator { * @param classDirectories the set of class directories */ public ReportGenerator(File executionDataFile, Set classDirectories) { - this.title = "something"; + + this.title = "Jacoco Coverage Report"; this.executionDataFile = executionDataFile; this.classDirectories = classDirectories; this.xmlReport = new File("./report/jacoco.xml"); @@ -65,10 +65,8 @@ public ReportGenerator(File executionDataFile, Set classDirectories) { // Create report directory if it does not exist File reportDir = this.xmlReport.getParentFile(); - if (!reportDir.exists()) { - if (!reportDir.mkdirs()) { - throw new RuntimeException("Failed to create report directory: " + reportDir.getAbsolutePath()); - } + if (!reportDir.exists() && !reportDir.mkdirs()) { + throw new RuntimeException("Failed to create report directory: " + reportDir.getAbsolutePath()); } } @@ -92,77 +90,65 @@ public void create() throws IOException { final IBundleCoverage bundleCoverage = analyzeStructure(); createReport(bundleCoverage); - } - private void createReport(final IBundleCoverage bundleCoverage) - throws IOException { + private void createReport(final IBundleCoverage bundleCoverage) throws IOException { // Create a concrete report visitor based on some supplied // configuration. In this case we use the defaults - final XMLFormatter xmlFormatter = new XMLFormatter(); - final IReportVisitor visitor = xmlFormatter.createVisitor(new FileOutputStream(xmlReport)); - - // Initialize the report with all the execution and session - // information. At this point the report doesn't know about the - // structure of the report being created - visitor.visitInfo(execFileLoader.getSessionInfoStore().getInfos(), - execFileLoader.getExecutionDataStore().getContents()); - - // Populate the report structure with the bundle coverage information. - // Call visitGroup if you need groups in your report. - visitor.visitBundle(bundleCoverage, - new DirectorySourceFileLocator(null, "utf-8", 4)); - - // Signal end of structure information to allow report to write all - // information out - visitor.visitEnd(); + try (FileOutputStream fos = new FileOutputStream(xmlReport)) { + final XMLFormatter xmlFormatter = new XMLFormatter(); + final IReportVisitor visitor = xmlFormatter.createVisitor(fos); + + // Initialize the report with all the execution and session + // information. At this point the report doesn't know about the + // structure of the report being created + visitor.visitInfo(execFileLoader.getSessionInfoStore().getInfos(), + execFileLoader.getExecutionDataStore().getContents()); + + // Populate the report structure with the bundle coverage information. + // Call visitGroup if you need groups in your report. + visitor.visitBundle(bundleCoverage, + new DirectorySourceFileLocator(null, "utf-8", 4)); + + // Signal end of structure information to allow report to write all + // information out + visitor.visitEnd(); + } } private void loadExecutionData() throws IOException { + execFileLoader = new ExecFileLoader(); execFileLoader.load(executionDataFile); } private IBundleCoverage analyzeStructure() throws IOException { + final CoverageBuilder coverageBuilder = new CoverageBuilder(); - final Analyzer analyzer = new Analyzer( - execFileLoader.getExecutionDataStore(), coverageBuilder); + final Analyzer analyzer = new Analyzer(execFileLoader.getExecutionDataStore(), coverageBuilder); List jarFilesToAnalyze = new ArrayList<>(); List classFilesToAnalyze = new ArrayList<>(); for (File classDirectory : classDirectories) { // Jar files to analyze - File [] files = classDirectory.listFiles(new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - return name.startsWith("org.wso2.carbon") && !name.contains(".stub_"); - } - }); - - if (files != null) { - jarFilesToAnalyze.addAll(Arrays.asList(files)); - } + File[] files = classDirectory.listFiles((dir, name) -> name.startsWith("org.wso2.carbon") && !name.contains(".stub_")); + if (files != null) { + jarFilesToAnalyze.addAll(Arrays.asList(files)); + } // Class files to analyze - files = classDirectory.listFiles(new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - return name.endsWith(".class"); - } - }); - + files = classDirectory.listFiles((dir, name) -> name.endsWith(".class")); if (files != null) { classFilesToAnalyze.addAll(Arrays.asList(files)); } - } + } String[] includes = {CLASS_FILE_PATTERN}; String[] excludes = {"-*.stub*", "-*.stub_", "-*.stub_4.0.0", "-*.stub-"}; for (final File jarFile : jarFilesToAnalyze) { - String extractedDir = CodeCoverageUtils.extractJarFile(jarFile.getAbsolutePath(), tempDirectory); String[] classFiles = CodeCoverageUtils.scanDirectory(extractedDir, includes, excludes); @@ -187,13 +173,14 @@ public boolean accept(File dir, String name) { * @throws IOException */ public static void main(final String[] args) throws IOException { + if (args.length < 2) { System.err.println("Usage: java -jar ReportGenerator.jar [ ...]"); System.exit(1); } File executionDataFile = new File(args[0]); - Set classDirectories = new HashSet(); + Set classDirectories = new HashSet<>(); for (int i = 1; i < args.length; i++) { classDirectories.add(new File(args[i])); }